Skip to content

Commit

Permalink
feat(api/settings): add proposal cache path to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertRosca committed Nov 5, 2024
1 parent 63488e0 commit d74408a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
19 changes: 10 additions & 9 deletions api/src/damnit_api/metadata/proposals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions api/src/damnit_api/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import UTC, datetime
from pathlib import Path
from typing import Annotated

from pydantic import (
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d74408a

Please sign in to comment.