From a7674e2a60625b5e7cecee0499cedf27ae392208 Mon Sep 17 00:00:00 2001 From: Aryan Kothari <87589047+thearyadev@users.noreply.github.com> Date: Mon, 25 Dec 2023 19:06:06 -0500 Subject: [PATCH] 51 add trends per role std dev and variance metric (#102) * add flatten func and endpoint for std dev trends * if color not found use default color logic * add fetch and line chart for new chart --- frontend/app/components/charts/lineChart.tsx | 2 +- frontend/pages/trends.tsx | 10 ++++++-- server.py | 26 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/frontend/app/components/charts/lineChart.tsx b/frontend/app/components/charts/lineChart.tsx index 48a39dd3..bca73cfc 100644 --- a/frontend/app/components/charts/lineChart.tsx +++ b/frontend/app/components/charts/lineChart.tsx @@ -28,7 +28,7 @@ const LineChart = (props: LineChartProps) => { max: 9 }, series: data.map(item => ({ - color: HeroColors[item.name], + color: HeroColors[item.name] ?? null, type: "line", ...item })), diff --git a/frontend/pages/trends.tsx b/frontend/pages/trends.tsx index bc50143b..9530f1ae 100644 --- a/frontend/pages/trends.tsx +++ b/frontend/pages/trends.tsx @@ -8,11 +8,13 @@ export type TrendLine = { } -const Trends = ({data, season_list}: {data: TrendLine[], season_list: string[]}) => { +const Trends = ({data, season_list, std_dev_data}: {data: TrendLine[], season_list: string[], std_dev_data: TrendLine[]}) => { return ( <> + + ) @@ -26,13 +28,17 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { const res2 = await fetch("http://server:8000/d/seasons") const season_list = await res2.json() + const res3 = await fetch("http://server:8000/d/all_seasons_std_by_role") + const std_dev_data = await res3.json() + return { props: { data, season_list, + std_dev_data, }, }; } -export default Trends; \ No newline at end of file +export default Trends; diff --git a/server.py b/server.py index ad6ca4e7..885ea8ff 100644 --- a/server.py +++ b/server.py @@ -444,7 +444,9 @@ def std_dev_data() -> dict[str, dict[str, float]]: def filter_fn(percentile): def filter_fn_inner(x): return x > percentile + return filter_fn_inner + exclude_percentile = 10 support = list(filter(filter_fn(np.percentile(support, exclude_percentile)), support)) damage = list(filter(filter_fn(np.percentile(damage, exclude_percentile)), damage)) @@ -454,11 +456,28 @@ def filter_fn_inner(x): return result +def std_dev_data_flatten(data: dict[str, dict[str, float]]): + result_pre = dict(SUPPORT=list(), DAMAGE=list(), TANK=list()) + for roles_and_std_dev in data.values(): + for role, std_dev in roles_and_std_dev.items(): + result_pre[role].append(std_dev) + result: list[dict[str, list[float] | str]] = list() + for key, val in result_pre.items(): + result.append( + {"name": key, "data": val} + ) + + return result + + @app.get("/d/seasons") async def seasons_list_d(): return Response(json.dumps(seasons_list()), media_type="application/json") +std_dev_data_flatten(std_dev_data()) + + @app.get("/d/single_season_std_by_role/{season}") async def single_season_std_by_role(season: str): return Response( @@ -466,6 +485,13 @@ async def single_season_std_by_role(season: str): ) +@app.get("/d/all_seasons_std_by_role") +async def all_seasons_std_by_role(): + return Response( + content=json.dumps(std_dev_data_flatten(std_dev_data())), media_type="application/json" + ) + + @app.get("/chart/{season}") async def chart_data(season: str): return Response(