|
| 1 | +from fastapi import APIRouter, Request, Response, status |
| 2 | +from fastapi.responses import JSONResponse |
| 3 | +from sql import selectQuery, insertQuery |
| 4 | +from globals import get_cache, set_cache |
| 5 | +from pydantic import BaseModel |
| 6 | +from decimal import Decimal |
| 7 | +import simplejson as json |
| 8 | +import time, surftimer.queries |
| 9 | + |
| 10 | + |
| 11 | +class PersonalRecordModel(BaseModel): |
| 12 | + """Body for adding or updating **prinfo** entry""" |
| 13 | + |
| 14 | + steamid32: str |
| 15 | + name: str = None |
| 16 | + mapname: str |
| 17 | + runtime: Decimal = None |
| 18 | + zonegroup: int |
| 19 | + PRtimeinzone: int |
| 20 | + PRcomplete: int |
| 21 | + PRattempts: int |
| 22 | + PRstcomplete: int |
| 23 | + |
| 24 | + |
| 25 | +router = APIRouter() |
| 26 | + |
| 27 | + |
| 28 | +@router.get( |
| 29 | + "/surftimer/selectPR", |
| 30 | + name="Get Personal Record Info", |
| 31 | + tags=["ck_prinfo"], |
| 32 | +) |
| 33 | +async def selectPR( |
| 34 | + request: Request, |
| 35 | + response: Response, |
| 36 | + steamid32: str, |
| 37 | + mapname: str, |
| 38 | + zonegroup: int, |
| 39 | +): |
| 40 | + """`char[] sql_selectPR = ....`""" |
| 41 | + tic = time.perf_counter() |
| 42 | + |
| 43 | + # Check if data is cached in Redis |
| 44 | + cache_key = f"selectPR:{steamid32}-{mapname}-{zonegroup}" |
| 45 | + cached_data = get_cache(cache_key) |
| 46 | + if cached_data is not None: |
| 47 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 48 | + response.headers["content-type"] = "application/json" |
| 49 | + response.status_code = status.HTTP_200_OK |
| 50 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 51 | + return response |
| 52 | + |
| 53 | + xquery = selectQuery( |
| 54 | + surftimer.queries.sql_selectPR.format(steamid32, mapname, zonegroup) |
| 55 | + ) |
| 56 | + |
| 57 | + if len(xquery) <= 0: |
| 58 | + response.headers["content-type"] = "application/json" |
| 59 | + response.status_code = status.HTTP_204_NO_CONTENT |
| 60 | + return response |
| 61 | + |
| 62 | + # Cache the data in Redis |
| 63 | + set_cache(cache_key, xquery) |
| 64 | + |
| 65 | + toc = time.perf_counter() |
| 66 | + |
| 67 | + print(f"Execution time {toc - tic:0.4f}") |
| 68 | + |
| 69 | + return xquery |
| 70 | + |
| 71 | + |
| 72 | +@router.get( |
| 73 | + "/surftimer/PRinfoByName", |
| 74 | + name="Get Personal Record Info by Name", |
| 75 | + tags=["ck_prinfo", "strays"], |
| 76 | +) |
| 77 | +async def PRinfoByName( |
| 78 | + request: Request, |
| 79 | + response: Response, |
| 80 | + mapname: str, |
| 81 | + zonegroup: int, |
| 82 | + steamid32: str, |
| 83 | +): |
| 84 | + """`char[] sql_stray_PRinfoByName = ....`""" |
| 85 | + tic = time.perf_counter() |
| 86 | + |
| 87 | + # Check if data is cached in Redis |
| 88 | + cache_key = f"PRinfoByName:{mapname}-{zonegroup}-{steamid32}" |
| 89 | + cached_data = get_cache(cache_key) |
| 90 | + if cached_data is not None: |
| 91 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 92 | + response.headers["content-type"] = "application/json" |
| 93 | + response.status_code = status.HTTP_200_OK |
| 94 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 95 | + return response |
| 96 | + |
| 97 | + xquery = selectQuery( |
| 98 | + surftimer.queries.sql_stray_PRinfoByName.format(mapname, zonegroup, steamid32) |
| 99 | + ) |
| 100 | + |
| 101 | + if len(xquery) <= 0: |
| 102 | + response.headers["content-type"] = "application/json" |
| 103 | + response.status_code = status.HTTP_204_NO_CONTENT |
| 104 | + return response |
| 105 | + |
| 106 | + # Cache the data in Redis |
| 107 | + set_cache(cache_key, xquery) |
| 108 | + |
| 109 | + toc = time.perf_counter() |
| 110 | + |
| 111 | + print(f"Execution time {toc - tic:0.4f}") |
| 112 | + |
| 113 | + return xquery |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +@router.post( |
| 118 | + "/surftimer/insertPR", |
| 119 | + name="Add Personal Record Info", |
| 120 | + tags=["ck_prinfo"], |
| 121 | +) |
| 122 | +async def insertPR( |
| 123 | + request: Request, |
| 124 | + response: Response, |
| 125 | + data: PersonalRecordModel, |
| 126 | +): |
| 127 | + """```c |
| 128 | + char[] sql_insertReplayCPTicks = .... |
| 129 | + ```""" |
| 130 | + tic = time.perf_counter() |
| 131 | + |
| 132 | + xquery = insertQuery( |
| 133 | + surftimer.queries.sql_insertPR.format( |
| 134 | + data.steamid32, |
| 135 | + data.name, |
| 136 | + data.mapname, |
| 137 | + data.runtime, |
| 138 | + data.zonegroup, |
| 139 | + data.PRtimeinzone, |
| 140 | + data.PRcomplete, |
| 141 | + data.PRattempts, |
| 142 | + data.PRstcomplete, |
| 143 | + ) |
| 144 | + ) |
| 145 | + |
| 146 | + content_data = {"inserted": xquery, "xtime": time.perf_counter() - tic} |
| 147 | + if xquery < 1: |
| 148 | + # response.body = json.dumps(content_data).encode('utf-8') |
| 149 | + response.headers["content-type"] = "application/json" |
| 150 | + response.status_code = status.HTTP_304_NOT_MODIFIED |
| 151 | + return response |
| 152 | + |
| 153 | + # Prepare the response |
| 154 | + toc = time.perf_counter() |
| 155 | + print(f"Execution time {toc - tic:0.4f}") |
| 156 | + |
| 157 | + response.body = json.dumps(content_data).encode("utf-8") |
| 158 | + response.status_code = status.HTTP_201_CREATED |
| 159 | + return response |
| 160 | + |
| 161 | + |
| 162 | +@router.put( |
| 163 | + "/surftimer/updatePrInfo", |
| 164 | + name="Update Personal Record Info", |
| 165 | + tags=["ck_prinfo"], |
| 166 | +) |
| 167 | +async def updatePrInfo( |
| 168 | + request: Request, |
| 169 | + response: Response, |
| 170 | + data: PersonalRecordModel, |
| 171 | +): |
| 172 | + """```c |
| 173 | + char[] sql_updatePrinfo = .... |
| 174 | + ```\n |
| 175 | + and\n |
| 176 | + ```c |
| 177 | + char[] sql_updatePrinfo_withruntime = .... |
| 178 | + ```\n""" |
| 179 | + tic = time.perf_counter() |
| 180 | + |
| 181 | + if data.runtime is None: |
| 182 | + xquery = insertQuery( |
| 183 | + surftimer.queries.sql_updatePrinfo.format( |
| 184 | + data.PRtimeinzone, |
| 185 | + data.PRcomplete, |
| 186 | + data.PRattempts, |
| 187 | + data.PRstcomplete, |
| 188 | + data.steamid32, |
| 189 | + data.mapname, |
| 190 | + data.zonegroup, |
| 191 | + ) |
| 192 | + ) |
| 193 | + else: |
| 194 | + xquery = insertQuery( |
| 195 | + surftimer.queries.sql_updatePrinfo_withruntime.format( |
| 196 | + data.PRtimeinzone, |
| 197 | + data.PRcomplete, |
| 198 | + data.PRattempts, |
| 199 | + data.PRstcomplete, |
| 200 | + data.runtime, |
| 201 | + data.steamid32, |
| 202 | + data.mapname, |
| 203 | + data.zonegroup, |
| 204 | + ) |
| 205 | + ) |
| 206 | + |
| 207 | + content_data = {"updated": xquery, "xtime": time.perf_counter() - tic} |
| 208 | + if xquery < 1: |
| 209 | + # response.body = json.dumps(content_data).encode('utf-8') |
| 210 | + response.headers["content-type"] = "application/json" |
| 211 | + response.status_code = status.HTTP_304_NOT_MODIFIED |
| 212 | + return response |
| 213 | + |
| 214 | + # Prepare the response |
| 215 | + toc = time.perf_counter() |
| 216 | + print(f"Execution time {toc - tic:0.4f}") |
| 217 | + |
| 218 | + response.body = json.dumps(content_data).encode("utf-8") |
| 219 | + response.headers["content-type"] = "application/json" |
| 220 | + response.status_code = status.HTTP_200_OK |
| 221 | + return response |
| 222 | + |
| 223 | + |
| 224 | +@router.put( |
| 225 | + "/surftimer/clearPRruntime", |
| 226 | + name="Clear Personal Record Time", |
| 227 | + tags=["ck_prinfo"], |
| 228 | +) |
| 229 | +async def clearPRruntime( |
| 230 | + request: Request, |
| 231 | + response: Response, |
| 232 | + steamid32: str, |
| 233 | + mapname: str, |
| 234 | + zonegroup: int, |
| 235 | +): |
| 236 | + """```c |
| 237 | + char[] sql_clearPRruntime = .... |
| 238 | + ```\n""" |
| 239 | + tic = time.perf_counter() |
| 240 | + |
| 241 | + xquery = insertQuery( |
| 242 | + surftimer.queries.sql_clearPRruntime.format(steamid32, mapname, zonegroup) |
| 243 | + ) |
| 244 | + |
| 245 | + content_data = {"updated": xquery, "xtime": time.perf_counter() - tic} |
| 246 | + if xquery < 1: |
| 247 | + # response.body = json.dumps(content_data).encode('utf-8') |
| 248 | + response.headers["content-type"] = "application/json" |
| 249 | + response.status_code = status.HTTP_304_NOT_MODIFIED |
| 250 | + return response |
| 251 | + |
| 252 | + # Prepare the response |
| 253 | + toc = time.perf_counter() |
| 254 | + print(f"Execution time {toc - tic:0.4f}") |
| 255 | + |
| 256 | + response.body = json.dumps(content_data).encode("utf-8") |
| 257 | + response.headers["content-type"] = "application/json" |
| 258 | + response.status_code = status.HTTP_200_OK |
| 259 | + return response |
0 commit comments