add get user id endpoint (/users),

fix issue in tiered cache,
update basic test with new user id set
This commit is contained in:
Arthur Lu 2025-02-21 19:39:09 +00:00 committed by root
parent dd5fc9f83c
commit dee97f09c2
4 changed files with 14 additions and 3 deletions

@ -36,7 +36,7 @@ class TieredCache(BaselineCache):
path = f"tiered_cache/{k}"
self.l2_map[k] = path
f = open(path, "w+")
f.write(v)
f.write(str(v))
f.close()
self.cache[key] = val

@ -8,6 +8,9 @@ DB_LOCATION = "database/datastore/" + DB_FILE
db = TinyDB(DB_LOCATION)
User = Query()
def get_user_ids():
return [x["user_id"] for x in db.all()]
def get_user_profile(user_id):
"""Fetch user profile from TinyDB"""
result = db.search(User.user_id == user_id)

@ -1,5 +1,5 @@
from fastapi import FastAPI, HTTPException
from database import get_user_profile, update_user_profile
from database import get_user_ids, get_user_profile, update_user_profile
from cache.cache import BaselineCache
from cache.prefetch_cache import PrefetchCache
from cache.tiered_cache import TieredCache
@ -21,6 +21,10 @@ elif CACHE_STRATEGY == "Seive":
else:
raise ValueError(f"Invalid CACHE_STRATEGY: {CACHE_STRATEGY}")
@app.get("/users")
def fetch_user_ids():
return {"ids": get_user_ids()}
@app.get("/user/{user_id}")
def fetch_user_profile(user_id: str):
"""Fetch user profile with caching"""

@ -10,10 +10,14 @@ endpoints = {
"/user/{user_id}": 1
}
user_ids = json.loads(requests.get(baseurl + "/users").content)["ids"]
random.seed(0)
def generate_random():
x = random.choices(list(endpoints.keys()), list(endpoints.values()))[0] # select randomly from endpoint (keys) with weight (values)
random_user = str(random.randint(1, 3))
random_user = str(random.choice(user_ids))
x = x.replace("{user_id}", random_user)
return baseurl + x