From dee0c262607e8cbd4db8e09290b7f0b725645f2f Mon Sep 17 00:00:00 2001 From: Jack Jackson Date: Mon, 29 Jul 2024 11:04:13 -0700 Subject: [PATCH] Support filtering for graph (no frontend support yet) --- app/routers/stats.py | 8 ++++++-- app/static/js/stats/graph.js | 10 ---------- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/routers/stats.py b/app/routers/stats.py index af804f3..a06a42e 100644 --- a/app/routers/stats.py +++ b/app/routers/stats.py @@ -1,4 +1,5 @@ from collections import defaultdict +from typing import Optional from fastapi import APIRouter, Depends, Request from fastapi.responses import HTMLResponse @@ -16,7 +17,7 @@ html_router = APIRouter( @api_router.get("/graph") -def stats_graph_api(db=Depends(get_db)): +def stats_graph_api(deck_ids: Optional[str] = None, db=Depends(get_db)): # TODO - parallelize? (Probably not worth it :P ) # SO Answer on row_number: https://stackoverflow.com/a/38160409/1040915 @@ -34,8 +35,11 @@ def stats_graph_api(db=Depends(get_db)): .outerjoin(models.EloScore, models.Deck.id == models.EloScore.deck_id) .join(models.Game, models.EloScore.after_game_id == models.Game.id) .add_column(row_number_column) - .subquery() ) + if deck_ids is not None: + sub_query = sub_query.filter(models.Deck.id.in_(deck_ids.split(","))) + + sub_query = sub_query.subquery() query = db.query(sub_query).filter(sub_query.c.row_number == 1) results = query.all() diff --git a/app/static/js/stats/graph.js b/app/static/js/stats/graph.js index 230547e..9fe961b 100644 --- a/app/static/js/stats/graph.js +++ b/app/static/js/stats/graph.js @@ -1,14 +1,4 @@ $(document).ready(function() { - const data = [ - { year: 2010, count: 10 }, - { year: 2011, count: 20 }, - { year: 2012, count: 15 }, - { year: 2013, count: 25 }, - { year: 2014, count: 22 }, - { year: 2015, count: 30 }, - { year: 2016, count: 28 }, - ]; - fetch('/api/stats/graph') .then(response => response.json()) .then(response => {