|
1 | 1 | from fastapi import APIRouter, Request, Response, status
|
2 | 2 | from fastapi.responses import JSONResponse
|
3 | 3 | from sql import selectQuery, insertQuery, insert_escaped_query
|
4 |
| -from globals import get_cache, set_cache |
| 4 | +from globals import get_cache, set_cache, all_styles |
5 | 5 | from pydantic import BaseModel
|
6 | 6 | import time, json
|
7 | 7 | import surftimer.queries
|
@@ -406,6 +406,49 @@ async def selectRankedPlayersRank(
|
406 | 406 | return xquery
|
407 | 407 |
|
408 | 408 |
|
| 409 | +@router.get( |
| 410 | + "/surftimer/selectRankedPlayerRankAllStyles", |
| 411 | + name="Player rank for all styles", |
| 412 | + tags=["ck_playerrank", "Refactored"], |
| 413 | +) |
| 414 | +async def selectRankedPlayerRankAllStyles( |
| 415 | + request: Request, |
| 416 | + response: Response, |
| 417 | + steamid32: str, |
| 418 | +): |
| 419 | + """`char[] sql_selectRankedPlayersRank = ....`\n |
| 420 | + Get all styles player rank in 1 go""" |
| 421 | + tic = time.perf_counter() |
| 422 | + |
| 423 | + # Check if data is cached in Redis |
| 424 | + cache_key = f"selectRankedPlayerRankAllStyles:{steamid32}" |
| 425 | + cached_data = get_cache(cache_key) |
| 426 | + if cached_data is not None: |
| 427 | + print(f"[Redis] Loaded '{cache_key}' ({time.perf_counter() - tic:0.4f}s)") |
| 428 | + response.headers["content-type"] = "application/json" |
| 429 | + response.status_code = status.HTTP_200_OK |
| 430 | + response.body = json.loads(cached_data, use_decimal=True, parse_nan=True) |
| 431 | + return response |
| 432 | + |
| 433 | + output = [] |
| 434 | + |
| 435 | + i = 0 |
| 436 | + for style in all_styles: |
| 437 | + xquery = selectQuery( |
| 438 | + surftimer.queries.sql_selectPlayersStylesRank.format(i, steamid32, i) |
| 439 | + ) |
| 440 | + output.append({"style": style,"rank": xquery.pop()["COUNT(steamid)"]}) |
| 441 | + i = i + 1 |
| 442 | + |
| 443 | + toc = time.perf_counter() |
| 444 | + |
| 445 | + print(f"Execution time {toc - tic:0.4f}") |
| 446 | + # Cache the data in Redis |
| 447 | + set_cache(cache_key, output) |
| 448 | + |
| 449 | + return output |
| 450 | + |
| 451 | + |
409 | 452 | @router.get(
|
410 | 453 | "/surftimer/selectRankedPlayers",
|
411 | 454 | name="Select Ranked Players",
|
@@ -1535,9 +1578,7 @@ async def rankCommandSelf(
|
1535 | 1578 | response.body = json.loads(cached_data, use_decimal=True, parse_nan=True)
|
1536 | 1579 | return response
|
1537 | 1580 |
|
1538 |
| - xquery = selectQuery( |
1539 |
| - surftimer.queries.sql_stray_rankCommandSelf.format(steamid32) |
1540 |
| - ) |
| 1581 | + xquery = selectQuery(surftimer.queries.sql_stray_rankCommandSelf.format(steamid32)) |
1541 | 1582 |
|
1542 | 1583 | if xquery:
|
1543 | 1584 | xquery = xquery.pop()
|
|
0 commit comments