Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server improvements #39

Merged
merged 20 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .github/workflows/ghcr.yml

This file was deleted.

64 changes: 64 additions & 0 deletions .github/workflows/tests_and_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Tests and CI
on:
pull_request:
push:

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Start MySQL Server
run: docker run -d -p 3800:3306 --name mysql -e MYSQL_ROOT_PASSWORD=dbtestpassword -e MYSQL_DATABASE=ghworkflow_testdb mysql@sha256:566007208a3f1cc8f9df6b767665b5c9b800fc4fb5f863d17aa1df362880ed04

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install MySQL Client
run: sudo apt-get install -y mysql-client

- name: Wait for MySQL Server to Start
run: sleep 10

- name: Add Testing Database
run: mysql -h 127.0.0.1 -u root -pdbtestpassword -P 3800 -e "CREATE DATABASE testing_ghworkflow_testdb;"

- name: Copy data into database
run: mysql -h 127.0.0.1 -u root -pdbtestpassword -P 3800 ghworkflow_testdb < ./archive/latest.sql


- name: Install Poetry
uses: snok/install-poetry@v1
- name: Install Dependencies
run: |
poetry install --no-root --no-interaction
- name: Run Tests
run: |
poetry run pytest
env:
MYSQLDATABASE: 'ghworkflow_testdb'
MYSQLUSER: 'root'
MYSQLPASSWORD: 'dbtestpassword'
MYSQLHOST: 'localhost'
MYSQLPORT: '3800'

TESTING_MYSQLDATABASE: 'testing_ghworkflow_testdb'
TESTING_MYSQLUSER: 'root'
TESTING_MYSQLPASSWORD: 'dbtestpassword'
TESTING_MYSQLHOST: 'localhost'
TESTING_MYSQLPORT: '3800'

build_and_publish:
runs-on: ubuntu-latest
needs: tests
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Build and push the image
run: |
docker login --username thearyadev --password ${{ secrets.GH_PAT }} ghcr.io
docker build . --tag ghcr.io/thearyadev/top500-aggregator:latest
docker push ghcr.io/thearyadev/top500-aggregator:latest
313 changes: 313 additions & 0 deletions archive/latest.sql

Large diffs are not rendered by default.

284 changes: 0 additions & 284 deletions archive/season_5.sql

This file was deleted.

126 changes: 82 additions & 44 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import os

from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse
from fastapi import FastAPI, Request, Depends
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from jinja2 import Environment

import leaderboards
import database
Expand All @@ -21,11 +20,14 @@
get_variance,
)
from utils.raise_for_missing_env import raise_for_missing_env_vars
from typing import Annotated, Any
from functools import lru_cache

load_dotenv()

templates = Jinja2Templates(directory="templates")

app = FastAPI()
app.state.templates = templates # type: ignore
app.mount("/static", StaticFiles(directory="static"), name="static")

db = database.DatabaseAccess(
host=os.getenv("MYSQLHOST") or raise_for_missing_env_vars(),
Expand All @@ -34,17 +36,30 @@
database=os.getenv("MYSQLDATABASE") or raise_for_missing_env_vars(),
port=os.getenv("MYSQLPORT") or raise_for_missing_env_vars(),
)
seasons = db.get_seasons()

data = dict()
hits = 0

@lru_cache
def seasons_list() -> list[str]:
"""
Wrapper for db.get_seasons() to cache the result
Returns:
list[str]: list of seasons
"""
return db.get_seasons()

trends_data = json.dumps(get_hero_trends_all_heroes_by_region(db=db))

@lru_cache
def season_data() -> dict[str, Any]:
"""
Creates the data structure for use on the season page.
This function is cached.
Returns:
dict[str, Any]: data structure for use on the season page
see function implementation for exact data structure shape. (sorry)
def calculate():
for s in seasons:
"""
data: dict = dict()
for s in seasons_list():
dataset: list[leaderboards.LeaderboardEntry] = db.get_all_records(s)
data[s] = {
# occurrences first most played
Expand Down Expand Up @@ -372,69 +387,92 @@ def calculate():
"standard_deviation": round(get_stdev(graphData), 3),
}
data[s][key] = json.dumps(val)
return data


app = FastAPI()
app.state.templates = templates
app.mount("/static", StaticFiles(directory="static"), name="static")
@lru_cache
def trends_data() -> dict[str, dict[str, list[dict[str, int]]]]:
"""
Creates the data structure for use on the trends page.
This function is cached.
Returns:
dict[str, dict[str, list[dict[str, int]]]]: data structure for use on the trends page
"""
return get_hero_trends_all_heroes_by_region(db=db)

calculate()

@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")

@app.get("/season/{season_number}")
async def season(request: Request, season_number: str):
global hits
if "robots.txt" in str(request.url):
return FileResponse("static/robots.txt")
return await season(
request,
season_number=seasons_list[-1],
seasons_data=seasons_data,
seasons_list=seasons_list,
)


@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:
hits += 1
if season_number in seasons_list:
return templates.TemplateResponse(
"season.html",
{
"request": request,
"seasons": seasons,
"seasons": seasons_list,
"currentSeason": season_number,
**data[season_number], # type: ignore
**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
**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[-1]}")


@app.get("/{_}")
@app.get("/")
async def index_redirect(request: Request):
if "favicon.ico" in str(request.url):
return FileResponse("static/favicon.ico")

if "robots.txt" in str(request.url):
return FileResponse("static/robots.txt")
return await season(request, season_number=seasons[-1])


@app.get("/i/hits", response_class=JSONResponse)
async def hit_endpoint():
return {"hits": hits}
return RedirectResponse(f"/season{seasons_list[-1]}")


@app.get("/trends/seasonal")
async def trendsEndpoint(request: Request):
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,
"trends": trends_data,
"seasons": seasons_list,
"trends": json.dumps(trends_data),
},
)


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]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_info_table_create():
),
"disclaimer",
)
== None
is None
)


Expand Down Expand Up @@ -79,7 +79,7 @@ def test_season_table_add_entries():
],
),
)
== None
is None
)


Expand Down