Skip to content

Commit

Permalink
remove unused components
Browse files Browse the repository at this point in the history
  • Loading branch information
thearyadev committed Dec 23, 2023
1 parent 3920339 commit 11046e6
Showing 1 changed file with 30 additions and 164 deletions.
194 changes: 30 additions & 164 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import json
import os
from functools import lru_cache
from typing import Annotated, Any, Dict, List
from typing import Any

from dotenv import load_dotenv
from fastapi import Depends, FastAPI, Request
from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse, Response
from fastapi import FastAPI
from fastapi.responses import Response
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

import database
import heroes
import leaderboards
from statistic import (
get_hero_occurrence_trend,
get_hero_trends_all_heroes_by_region,
get_mean,
get_number_of_ohp,
get_number_of_thp,
get_occurrences,
get_occurrences_most_played,
get_stdev,
get_variance,
)
from utils.raise_for_missing_env import raise_for_missing_env_vars
import csv
import io
import zipfile
import xml.etree.ElementTree as ET

load_dotenv()
templates = Jinja2Templates(directory="templates")
Expand All @@ -44,20 +36,6 @@
)


@lru_cache
def get_sitemap() -> str:
urls: list[str] = [
f"https://t500-aggregator.aryankothari.dev/season/{season}" for season in
db.get_seasons()]
urls.append("https://t500-aggregator.aryankothari.dev/trends/seasonal")
urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
for url in urls:
url_element = ET.SubElement(urlset, "url")
loc = ET.SubElement(url_element, "loc")
loc.text = url
return ET.tostring(urlset, encoding="utf8", method="xml").decode()


@lru_cache
def seasons_list() -> list[str]:
"""
Expand All @@ -68,24 +46,16 @@ def seasons_list() -> list[str]:
return db.get_seasons()


@lru_cache
def get_seasons_data_as_csv() -> io.BytesIO:
files: dict[str, io.StringIO] = dict()
for season_ in db.get_seasons():
season_csv_string_io: io.StringIO = io.StringIO()
data = db.get_all_records(season_)
writer = csv.writer(season_csv_string_io)
writer.writerow(("region", "role", "firstMostPlayed", "secondMostPlayed", "thirdMostPlayed"))
for entry in data:
writer.writerow((entry.region.name, entry.role.name, entry.heroes[0], entry.heroes[1], entry.heroes[2]))
files[f"season{season_}.csv"] = season_csv_string_io
in_memory_zip = io.BytesIO()
with zipfile.ZipFile(in_memory_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
for filename, content in files.items():
content.seek(0)
zipf.writestr(filename, content.read())
content.close()
return in_memory_zip
def map_to_label_count_array(data: dict):
result = dict()
for season in data.keys():
result[season] = dict()
for chart, values in data[season].items():
result[season][chart] = {"graph": {"labels": list(), "values": list()}, "statistic": values["statistic"]}
for hero in values['graph']:
result[season][chart]["graph"]["labels"].append(hero['hero'])
result[season][chart]["graph"]['values'].append(hero['count'])
return result


@lru_cache
Expand Down Expand Up @@ -411,23 +381,17 @@ def season_data() -> dict[str, Any]:
"O_ALL_ALL": {
"graph": get_occurrences(data=dataset, region=leaderboards.Region.ALL),
},
"MISC": {
"OHP": get_number_of_ohp(dataset),
"THP": get_number_of_thp(dataset),
},
}

# conducts calculations for mean variance and standard dev
for key, val in data[s].items():
if key != "MISC":
graphData = data[s][key]["graph"] # type: ignore
data[s][key]["statistic"] = { # type: ignore
"mean": round(get_mean(graphData), 3),
"variance": round(get_variance(graphData), 3),
"standard_deviation": round(get_stdev(graphData), 3),
}
data[s][key] = json.dumps(val)
return data
graphData = data[s][key]["graph"] # type: ignore
data[s][key]["statistic"] = { # type: ignore
"mean": round(get_mean(graphData), 3),
"variance": round(get_variance(graphData), 3),
"standard_deviation": round(get_stdev(graphData), 3),
}
return map_to_label_count_array(data)


@lru_cache
Expand All @@ -440,113 +404,15 @@ def trends_data() -> list[dict[str, list[int]]]:
"""
return get_hero_occurrence_trend(db=db)

@app.get('/d/seasons')
async def seasons_list_d():
return Response(json.dumps(seasons_list()), media_type="application/json")

@app.get("/{_}")
@app.get("/")
async def index_redirect(
request: Request,
seasons_list: Annotated[list[str], Depends(seasons_list)],
seasons_data: Annotated[dict, Depends(season_data)],
):
if "favicon.ico" in str(request.url):
return FileResponse("static/favicon.ico")

if "robots.txt" in str(request.url):
return FileResponse("static/robots.txt")

if "apple-touch-icon.png" in str(request.url):
return FileResponse("static/apple-touch-icon.png")

if "favicon-32x32.png" in str(request.url):
return FileResponse("static/favicon-32x32.png")

if "sitemap.xml" in str(request.url):
return Response(content=get_sitemap(), media_type="application/xml")

if "favicon-16x16.png" in str(request.url):
return FileResponse("static/favicon-16x16.png")

if "site.webmanifest" in str(request.url):
return FileResponse("static/site.webmanifest")

if "safari-pinned-tab.svg" in str(request.url):
return FileResponse("static/safari-pinned-tab.svg")

return await season(
request,
season_number=seasons_list[-1],
seasons_data=seasons_data,
seasons_list=seasons_list,
)
@app.get("/chart/{season}")
async def chart_data(season: str):
print("gnome!")
return Response(content=json.dumps(season_data()[season]), media_type="application/json")


@app.get("/season/{season_number}")
async def season(
request: Request,
season_number: str,
seasons_data: Annotated[dict, Depends(season_data)],
seasons_list: Annotated[list[str], Depends(seasons_list)],
):
request.app.state.templates.env.filters["group_subseasons"] = group_subseasons

if season_number in seasons_list:
return templates.TemplateResponse(
"season.html",
{
"request": request,
"seasons": seasons_list,
"currentSeason": season_number,
"hero_colors": json.dumps(heroes.Heroes().hero_colors),
**seasons_data[season_number], # type: ignore
**seasons_data[season_number]["MISC"], # type: ignore
# this does work. Im not sure why mypy is complaining.
# It unpacks all of the chart datas into the global scope of the template
"disclaimer": db.get_season_disclaimer(season_number),
},
)
return RedirectResponse(f"/season{seasons_list[-1]}")


@app.get("/trends/seasonal")
async def trendsEndpoint(
request: Request,
seasons_list: Annotated[list[str], Depends(seasons_list)],
trends_data: Annotated[dict, Depends(trends_data)],
):
request.app.state.templates.env.filters["group_subseasons"] = group_subseasons

return templates.TemplateResponse(
"trends.html",
{
"request": request,
"seasons": seasons_list,
"trends": json.dumps(trends_data),
"hero_colors": json.dumps(heroes.Heroes().hero_colors),
},
)


@app.get("/data/csv")
async def get_data_csv(request: Request) -> StreamingResponse:
zipfile_data = get_seasons_data_as_csv()
zipfile_data.seek(0)
return StreamingResponse(zipfile_data, media_type="application/zip",
headers={
"Content-Disposition": "attachment; filename=t500-aggregator-all-seasons-archive.zip"})


def group_subseasons(seasons: list[str]) -> dict[str, list[str]]:
"""
Groups sub seasons together, to shrink the menu size.
Args:
seasons: list of seasons
Returns:
dict[str, list[str]]: dict of subseasons and their seasons
"""
subseasons: dict[str, list[str]] = {}
for season in seasons:
subseason = season.split("_")[0]
if subseason not in subseasons:
subseasons[subseason] = []
subseasons[subseason].append(season)
return subseasons
@app.get("/chart/trend/d")
async def trend_chart_data():
return Response(content=json.dumps(trends_data()), media_type="application/json")

0 comments on commit 11046e6

Please sign in to comment.