Skip to content

Commit a44e0c4

Browse files
committed
use decimal and simplejson
1 parent c062e22 commit a44e0c4

2 files changed

Lines changed: 22 additions & 18 deletions

File tree

surftimer/ck_bonus.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
from fastapi import APIRouter, Request, Response, status
22
from fastapi.responses import JSONResponse
33
from pydantic import BaseModel
4+
from decimal import Decimal
5+
import simplejson as json
46
from sql import selectQuery, insertQuery
57
from globals import (
68
set_cache,
79
get_cache,
810
)
9-
import time, json, surftimer.queries
11+
import time, surftimer.queries
1012

1113

1214
class NewBonus(BaseModel):
1315
steamid32: str
1416
name: str
1517
mapname: str
16-
runtime: int
18+
runtime: Decimal
1719
zonegroup: int
1820
velStartXY: int
1921
velStartXYZ: int
@@ -42,8 +44,8 @@ def insertBonus(
4244
response: Response,
4345
data: NewBonus,
4446
):
45-
"""Inserts a new record to the table\n
46-
```char sql_insertLatestRecords[] = ....```"""
47+
"""Inserts a new `Bonus` record to the table\n
48+
```char sql_insertBonus[] = ....```"""
4749
tic = time.perf_counter()
4850

4951
sql = surftimer.queries.sql_insertBonus.format(
@@ -127,7 +129,7 @@ def selectBonusCount(request: Request, response: Response, mapname: str):
127129
if cached_data:
128130
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
129131
return JSONResponse(
130-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
132+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
131133
)
132134

133135
xquery = selectQuery(surftimer.queries.sql_selectBonusCount.format(mapname))
@@ -166,7 +168,7 @@ def selectPersonalBonusRecords(
166168
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
167169

168170
return JSONResponse(
169-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
171+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
170172
)
171173

172174
xquery = selectQuery(
@@ -210,7 +212,7 @@ def selectPlayerRankBonus(
210212
if cached_data is not None:
211213
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
212214
return JSONResponse(
213-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
215+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
214216
)
215217

216218
xquery = selectQuery(
@@ -257,7 +259,7 @@ def selectFastestBonus(
257259
if cached_data is not None:
258260
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
259261
return JSONResponse(
260-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
262+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
261263
)
262264

263265
xquery = selectQuery(surftimer.queries.sql_selectFastestBonus.format(mapname))
@@ -296,7 +298,7 @@ def selectAllBonusTimesinMap(
296298
if cached_data is not None:
297299
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
298300
return JSONResponse(
299-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
301+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
300302
)
301303

302304
xquery = selectQuery(surftimer.queries.sql_selectAllBonusTimesinMap.format(mapname))
@@ -337,7 +339,7 @@ def selectTopBonusSurfers(
337339
if cached_data is not None:
338340
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
339341
return JSONResponse(
340-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
342+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
341343
)
342344

343345
xquery = selectQuery(

surftimer/ck_checkpoints.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
from sql import selectQuery, insertQuery
44
from globals import get_cache, set_cache
55
from pydantic import BaseModel
6-
import time, json, surftimer.queries
6+
from decimal import Decimal
7+
import simplejson as json
8+
import time, surftimer.queries
79

810

911
class PlayerCheckpoints(BaseModel):
1012
steamid: str
1113
mapname: str
1214
cp: int
13-
time: str
15+
time: Decimal
1416
stage_time: str
1517
stage_attempts: int
1618
zonegroup: int
@@ -76,7 +78,7 @@ async def selectCheckpoints(
7678
if cached_data is not None:
7779
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
7880
return JSONResponse(
79-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
81+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
8082
)
8183

8284
xquery = selectQuery(
@@ -121,7 +123,7 @@ async def selectCheckpointsinZoneGroup(
121123
if cached_data is not None:
122124
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
123125
return JSONResponse(
124-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
126+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
125127
)
126128

127129
xquery = selectQuery(
@@ -163,7 +165,7 @@ async def selectRecordCheckpoints(
163165
if cached_data is not None:
164166
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
165167
return JSONResponse(
166-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
168+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
167169
)
168170

169171
xquery = selectQuery(
@@ -212,7 +214,7 @@ async def deleteCheckpoints(
212214
toc = time.perf_counter()
213215
print(f"Execution time {toc - tic:0.4f}")
214216

215-
return {"inserted": xquery, "xtime": time.perf_counter() - tic}
217+
return {"deleted": xquery, "xtime": time.perf_counter() - tic}
216218

217219

218220
@router.get(
@@ -232,7 +234,7 @@ async def selectStageTimes(
232234
if cached_data is not None:
233235
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
234236
return JSONResponse(
235-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
237+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
236238
)
237239

238240
xquery = selectQuery(
@@ -272,7 +274,7 @@ async def selectStageAttempts(
272274
if cached_data is not None:
273275
print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)")
274276
return JSONResponse(
275-
status_code=status.HTTP_200_OK, content=json.loads(cached_data)
277+
status_code=status.HTTP_200_OK, content=json.loads(cached_data, use_decimal=True, parse_nan=True)
276278
)
277279

278280
xquery = selectQuery(

0 commit comments

Comments
 (0)