api
This commit is contained in:
parent
480c1f4725
commit
7c21d724e4
|
|
@ -3,3 +3,5 @@ click
|
||||||
psycopg2-binary
|
psycopg2-binary
|
||||||
ratelimit
|
ratelimit
|
||||||
backoff
|
backoff
|
||||||
|
fastapi
|
||||||
|
python-multipart
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
from fastapi import FastAPI, status
|
||||||
|
from transform import get_db_cols
|
||||||
|
from datetime import datetime
|
||||||
|
import db
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/players")
|
||||||
|
async def get_players():
|
||||||
|
cols = get_db_cols("profile_meta")
|
||||||
|
profiles = db.get_all_profiles()
|
||||||
|
profiles = map(
|
||||||
|
lambda profile: {cols[i]: profile[i] for i, _ in enumerate(profile)},
|
||||||
|
profiles,
|
||||||
|
)
|
||||||
|
|
||||||
|
return list(profiles)
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/players/{player_id}/games")
|
||||||
|
async def get_player_games(
|
||||||
|
player_id,
|
||||||
|
begin: datetime | None = None,
|
||||||
|
end: datetime | None = None,
|
||||||
|
):
|
||||||
|
print(begin)
|
||||||
|
print(end)
|
||||||
|
|
||||||
|
game_ids = db.get_profile_game_ids(player_id, begin=begin, end=end)
|
||||||
|
print(len(game_ids))
|
||||||
|
if len(game_ids) == 0:
|
||||||
|
return status.HTTP_404_NOT_FOUND
|
||||||
|
|
||||||
|
return {"game_ids": game_ids}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/players/{player_id}/player_stats/")
|
||||||
|
async def get_player_stats(player_id: str, game_ids_param: str | None = None):
|
||||||
|
if not game_ids_param or len(game_ids_param) == 0:
|
||||||
|
game_ids = db.get_profile_game_ids(player_id)
|
||||||
|
else:
|
||||||
|
game_ids = [id.strip('" ') for id in game_ids_param.split(",")]
|
||||||
|
|
||||||
|
print(len(game_ids))
|
||||||
|
cols = get_db_cols("player_stats")
|
||||||
|
stats = db.get_player_stats(game_ids)
|
||||||
|
print(len(stats))
|
||||||
|
stats = map(
|
||||||
|
lambda stat: {cols[i]: stat[i] for i, _ in enumerate(stat)},
|
||||||
|
stats,
|
||||||
|
)
|
||||||
|
|
||||||
|
return {"player_id": player_id, "player_stats": list(stats)}
|
||||||
45
src/db.py
45
src/db.py
|
|
@ -32,6 +32,15 @@ def get_all_profile_names(conn=None):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
@connect_by_default
|
||||||
|
def get_all_profiles(conn=None):
|
||||||
|
with conn.cursor() as curs:
|
||||||
|
curs.execute("SELECT * from data.profile_meta;")
|
||||||
|
data = curs.fetchall()
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
@connect_by_default
|
@connect_by_default
|
||||||
def get_profile_game_count(user_id, conn=None):
|
def get_profile_game_count(user_id, conn=None):
|
||||||
with conn.cursor() as curs:
|
with conn.cursor() as curs:
|
||||||
|
|
@ -48,18 +57,42 @@ def get_profile_game_count(user_id, conn=None):
|
||||||
|
|
||||||
|
|
||||||
@connect_by_default
|
@connect_by_default
|
||||||
def get_profile_game_ids(user_id, conn=None):
|
def get_profile_game_ids(user_id, begin=None, end=None, conn=None):
|
||||||
|
sql = """
|
||||||
|
SELECT game_id FROM data.profile_game
|
||||||
|
WHERE leetify_user_id = %s"""
|
||||||
|
vals = [user_id]
|
||||||
|
if begin:
|
||||||
|
sql += """
|
||||||
|
AND game_finished_at >= (%s)
|
||||||
|
"""
|
||||||
|
vals.append(begin)
|
||||||
|
if end:
|
||||||
|
sql += """
|
||||||
|
AND game_finished_at < (%s)
|
||||||
|
"""
|
||||||
|
vals.append(end)
|
||||||
|
sql += ";"
|
||||||
with conn.cursor() as curs:
|
with conn.cursor() as curs:
|
||||||
curs.execute(
|
curs.execute(sql, (*vals,))
|
||||||
"""SELECT game_id FROM data.profile_game
|
|
||||||
WHERE leetify_user_id = %s;""",
|
|
||||||
(user_id,),
|
|
||||||
)
|
|
||||||
data = curs.fetchall()
|
data = curs.fetchall()
|
||||||
|
|
||||||
return [row[0] for row in data]
|
return [row[0] for row in data]
|
||||||
|
|
||||||
|
|
||||||
|
@connect_by_default
|
||||||
|
def get_player_stats(game_ids, conn=None):
|
||||||
|
sql = """
|
||||||
|
SELECT * FROM data.player_stats
|
||||||
|
WHERE game_id = ANY(%s);
|
||||||
|
"""
|
||||||
|
with conn.cursor() as curs:
|
||||||
|
curs.execute(sql, (game_ids,))
|
||||||
|
data = curs.fetchall()
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
@connect_by_default
|
@connect_by_default
|
||||||
def insert_player_stats(stats, conn=None):
|
def insert_player_stats(stats, conn=None):
|
||||||
cols_api, cols_db = get_cols("player_stats")
|
cols_api, cols_db = get_cols("player_stats")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue