Coverage for dj/config.py: 100%
9 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 20:05 -0700
« 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
9from cachelib.base import BaseCache
10from cachelib.file import FileSystemCache
11from cachelib.redis import RedisCache
12from celery import Celery
13from pydantic import BaseSettings
16class Settings(
17 BaseSettings,
18): # pylint: disable=too-few-public-methods #pragma: no cover
19 """
20 DataJunction configuration.
21 """
23 name: str = "DJ server"
24 description: str = "A DataJunction metrics layer"
25 url: str = "http://localhost:8000/"
27 # A list of hostnames that are allowed to make cross-site HTTP requests
28 cors_origin_whitelist: List[str] = ["http://localhost:3000"]
30 # SQLAlchemy URI for the metadata database.
31 index: str = "sqlite:///dj.db?check_same_thread=False"
33 # Directory where the repository lives. This should have 2 subdirectories, "nodes" and
34 # "databases".
35 repository: Path = Path(".")
37 # Where to store the results from queries.
38 results_backend: BaseCache = FileSystemCache("/tmp/dj", default_timeout=0)
40 # Cache for paginating results and potentially other things.
41 redis_cache: Optional[str] = None
42 paginating_timeout: timedelta = timedelta(minutes=5)
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
48 # How long to wait when pinging databases to find out the fastest online database.
49 do_ping_timeout: timedelta = timedelta(seconds=5)
51 # Query service
52 query_service: Optional[str] = None
54 @property
55 def celery(self) -> Celery:
56 """
57 Return Celery app.
58 """
59 return Celery(__name__, broker=self.celery_broker)
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
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 )