From d74408a8b6b7206140dd033a757c18a07bd85d50 Mon Sep 17 00:00:00 2001 From: Robert Rosca <32569096+RobertRosca@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:47:14 +0100 Subject: [PATCH] feat(api/settings): add proposal cache path to settings --- api/src/damnit_api/metadata/proposals.py | 19 ++++++++++--------- api/src/damnit_api/settings.py | 3 +++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/api/src/damnit_api/metadata/proposals.py b/api/src/damnit_api/metadata/proposals.py index d0a136c..ad7bab9 100644 --- a/api/src/damnit_api/metadata/proposals.py +++ b/api/src/damnit_api/metadata/proposals.py @@ -7,19 +7,18 @@ from async_lru import alru_cache +from ..settings import settings from .mymdc import MyMDC -DAMNIT_PROPOSALS_CACHE = "/tmp/damnit-web/damnit_proposals.json" - @alru_cache(ttl=60) async def get_proposal_info(proposal_num: str, use_cache: bool = True) -> dict: - cache = Path(DAMNIT_PROPOSALS_CACHE) + cache = Path(settings.proposal_cache) proposals = None # Check from cache if existing if use_cache and cache.exists(): - with open(DAMNIT_PROPOSALS_CACHE) as file: + with open(settings.proposal_cache) as file: # TODO: Update cache for new proposals proposals = json.load(file) if info := proposals.get(proposal_num): @@ -44,7 +43,7 @@ async def get_proposal_info(proposal_num: str, use_cache: bool = True) -> dict: if proposals is None: if cache.exists(): - with open(DAMNIT_PROPOSALS_CACHE) as file: + with open(settings.proposal_cache) as file: # TODO: Update cache for new proposals proposals = json.load(file) else: @@ -55,7 +54,7 @@ async def get_proposal_info(proposal_num: str, use_cache: bool = True) -> dict: proposals = dict(sorted(proposals.items())) # Update the cache - with open(DAMNIT_PROPOSALS_CACHE, "w") as file: + with open(settings.proposal_cache, "w") as file: json.dump( proposals, file, @@ -131,9 +130,9 @@ def get_read_permissions(current_user_groups: list[str]) -> list[str]: def get_damnit_proposals(use_cache: bool) -> dict: - cache = Path(DAMNIT_PROPOSALS_CACHE) + cache = Path(settings.proposal_cache) if use_cache and cache.exists(): - with open(DAMNIT_PROPOSALS_CACHE) as file: + with open(settings.proposal_cache) as file: # TODO: Update cache for new proposals return json.load(file) @@ -154,7 +153,7 @@ def get_damnit_proposals(use_cache: bool) -> dict: proposals = dict(sorted(proposals.items())) # Write to file - with open(DAMNIT_PROPOSALS_CACHE, "w") as file: + with open(settings.proposal_cache, "w") as file: json.dump( proposals, file, @@ -189,6 +188,8 @@ def get_damnit_path(path: str | Path, suffix: str | None = None) -> str: if p.is_dir(): return str(p) + return None + def get_proposal_number_from_path(path: str) -> str: return path.split("/")[6].lstrip("p0") diff --git a/api/src/damnit_api/settings.py b/api/src/damnit_api/settings.py index b4d7cf9..541c418 100644 --- a/api/src/damnit_api/settings.py +++ b/api/src/damnit_api/settings.py @@ -1,4 +1,5 @@ from datetime import UTC, datetime +from pathlib import Path from typing import Annotated from pydantic import ( @@ -57,6 +58,8 @@ class MyMdCCredentials(BaseSettings): class Settings(BaseSettings): auth: AuthSettings + proposal_cache: Path = Path("/tmp/damnit-web/damnit_proposals.json") + debug: bool = True log_level: str = "DEBUG"