Coverage for dj/config.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.2.3, created at 2023-04-17 20:05 -0700

1""" 

2Configuration for the metric repository. 

3""" 

4import urllib.parse 

5from datetime import timedelta 

6from pathlib import Path 

7from typing import List, Optional 

8 

9from cachelib.base import BaseCache 

10from cachelib.file import FileSystemCache 

11from cachelib.redis import RedisCache 

12from celery import Celery 

13from pydantic import BaseSettings 

14 

15 

16class Settings( 

17 BaseSettings, 

18): # pylint: disable=too-few-public-methods #pragma: no cover 

19 """ 

20 DataJunction configuration. 

21 """ 

22 

23 name: str = "DJ server" 

24 description: str = "A DataJunction metrics layer" 

25 url: str = "http://localhost:8000/" 

26 

27 # A list of hostnames that are allowed to make cross-site HTTP requests 

28 cors_origin_whitelist: List[str] = ["http://localhost:3000"] 

29 

30 # SQLAlchemy URI for the metadata database. 

31 index: str = "sqlite:///dj.db?check_same_thread=False" 

32 

33 # Directory where the repository lives. This should have 2 subdirectories, "nodes" and 

34 # "databases". 

35 repository: Path = Path(".") 

36 

37 # Where to store the results from queries. 

38 results_backend: BaseCache = FileSystemCache("/tmp/dj", default_timeout=0) 

39 

40 # Cache for paginating results and potentially other things. 

41 redis_cache: Optional[str] = None 

42 paginating_timeout: timedelta = timedelta(minutes=5) 

43 

44 # Configure Celery for async requests. If not configured async queries will be 

45 # executed using FastAPI's ``BackgroundTasks``. 

46 celery_broker: Optional[str] = None 

47 

48 # How long to wait when pinging databases to find out the fastest online database. 

49 do_ping_timeout: timedelta = timedelta(seconds=5) 

50 

51 # Query service 

52 query_service: Optional[str] = None 

53 

54 @property 

55 def celery(self) -> Celery: 

56 """ 

57 Return Celery app. 

58 """ 

59 return Celery(__name__, broker=self.celery_broker) 

60 

61 @property 

62 def cache(self) -> Optional[BaseCache]: 

63 """ 

64 Configure the Redis cache. 

65 """ 

66 if self.redis_cache is None: 

67 return None 

68 

69 parsed = urllib.parse.urlparse(self.redis_cache) 

70 return RedisCache( 

71 host=parsed.hostname, 

72 port=parsed.port, 

73 password=parsed.password, 

74 db=parsed.path.strip("/"), 

75 )