|
| 1 | +from fastapi import APIRouter, Request, Response, status |
| 2 | +from sql import selectQuery |
| 3 | +from globals import get_cache, set_cache |
| 4 | +import time, json |
| 5 | +import surftimer.queries |
| 6 | + |
| 7 | +router = APIRouter() |
| 8 | + |
| 9 | +@router.get( |
| 10 | + "/surftimer/point_calc_finishedStages", |
| 11 | + name="Count Player Finished Stages", |
| 12 | + tags=["strays", "Point Calculation"], |
| 13 | +) |
| 14 | +def point_calc_finishedStages( |
| 15 | + request: Request, |
| 16 | + response: Response, |
| 17 | + steamid32: str, |
| 18 | + style: int, |
| 19 | +): |
| 20 | + """```char sql_stray_point_calc_finishedStages[] = ....```\n""" |
| 21 | + tic = time.perf_counter() |
| 22 | + |
| 23 | + # Check if data is cached in Redis |
| 24 | + cache_key = f"point_calc_finishedStages:{steamid32}-{style}" |
| 25 | + cached_data = get_cache(cache_key) |
| 26 | + if cached_data is not None: |
| 27 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 28 | + response.headers["content-type"] = "application/json" |
| 29 | + response.status_code = status.HTTP_200_OK |
| 30 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 31 | + return response |
| 32 | + |
| 33 | + xquery = selectQuery( |
| 34 | + surftimer.queries.sql_stray_point_calc_finishedStages.format(steamid32, style) |
| 35 | + ) |
| 36 | + |
| 37 | + if len(xquery) <= 0: |
| 38 | + response.status_code = status.HTTP_204_NO_CONTENT |
| 39 | + return response |
| 40 | + |
| 41 | + toc = time.perf_counter() |
| 42 | + print(f"Execution time {toc - tic:0.4f}") |
| 43 | + |
| 44 | + # Cache the data in Redis |
| 45 | + set_cache(cache_key, xquery) |
| 46 | + |
| 47 | + return xquery |
| 48 | + |
| 49 | + |
| 50 | +@router.get( |
| 51 | + "/surftimer/point_calc_finishedMaps", |
| 52 | + name="Count Player Finished Maps", |
| 53 | + tags=["strays", "Point Calculation"], |
| 54 | +) |
| 55 | +def point_calc_finishedMaps( |
| 56 | + request: Request, |
| 57 | + response: Response, |
| 58 | + steamid32: str, |
| 59 | + style: int, |
| 60 | +): |
| 61 | + """```char sql_stray_point_calc_finishedMaps[] = ....```\n""" |
| 62 | + tic = time.perf_counter() |
| 63 | + |
| 64 | + # Check if data is cached in Redis |
| 65 | + cache_key = f"point_calc_finishedStages:{steamid32}-{style}" |
| 66 | + cached_data = get_cache(cache_key) |
| 67 | + if cached_data is not None: |
| 68 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 69 | + response.headers["content-type"] = "application/json" |
| 70 | + response.status_code = status.HTTP_200_OK |
| 71 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 72 | + return response |
| 73 | + |
| 74 | + xquery = selectQuery( |
| 75 | + surftimer.queries.sql_stray_point_calc_finishedMaps.format( |
| 76 | + style, style, steamid32, style |
| 77 | + ) |
| 78 | + ) |
| 79 | + |
| 80 | + if len(xquery) <= 0: |
| 81 | + response.status_code = status.HTTP_204_NO_CONTENT |
| 82 | + return response |
| 83 | + |
| 84 | + toc = time.perf_counter() |
| 85 | + print(f"Execution time {toc - tic:0.4f}") |
| 86 | + |
| 87 | + # Cache the data in Redis |
| 88 | + set_cache(cache_key, xquery) |
| 89 | + |
| 90 | + return xquery |
0 commit comments