From 8010bbd750fc8c7f9a326a1715d921bf494189f1 Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Wed, 1 Jun 2022 07:58:06 -0400 Subject: [PATCH] stats --- src/bot/database.py | 21 ++++++++++++++++----- src/web/templates/base.html | 1 + src/web/templates/stats.html | 9 +++++++++ src/web/webserver_thread.py | 6 ++++++ 4 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/web/templates/stats.html diff --git a/src/bot/database.py b/src/bot/database.py index ff42668..65ebbe0 100644 --- a/src/bot/database.py +++ b/src/bot/database.py @@ -6,7 +6,7 @@ import sqlalchemy from alembic import command from alembic.config import Config from dateutil import tz -from sqlalchemy import func +from sqlalchemy import func, select from sqlalchemy.orm import Session, joinedload from sqlalchemy_utils import database_exists @@ -25,7 +25,7 @@ def run_db_migrations(script_location: str, db_url: str) -> None: alembic_cfg.set_main_option('sqlalchemy.url', db_url) if not database_exists(db_url): - logger.debug(f"'{db_url}' does not exist. Running normal migration to create db and tables." ) + logger.debug(f"'{db_url}' does not exist. Running normal migration to create db and tables.") command.upgrade(alembic_cfg, 'head') elif database_exists(db_url): logger.debug(f"'{db_url}' exists.") @@ -94,7 +94,7 @@ class GiveawayHelper: @classmethod def get(cls): with Session(engine) as session: - return session.query(TableGiveaway).options(joinedload('steam_item'))\ + return session.query(TableGiveaway).options(joinedload('steam_item')) \ .order_by(TableGiveaway.giveaway_ended_at.desc()).all() @classmethod @@ -102,9 +102,20 @@ class GiveawayHelper: with Session(engine) as session: paginated_giveaways = paginate_sqlalchemy.SqlalchemyOrmPage(session.query(TableGiveaway) .options(joinedload('steam_item')) - .order_by(TableGiveaway.giveaway_ended_at.desc()), page=page) + .order_by( + TableGiveaway.giveaway_ended_at.desc()), page=page) return paginated_giveaways + @classmethod + def total_giveaways(cls): + with Session(engine) as session: + return session.execute(select(func.count(TableGiveaway.giveaway_id))).scalar_one() + + @classmethod + def total_entered(cls): + with Session(engine) as session: + return session.query(TableGiveaway).filter_by(entered=True).count() + @classmethod def unix_timestamp_to_utc_datetime(cls, timestamp): return datetime.utcfromtimestamp(timestamp) @@ -164,4 +175,4 @@ class GiveawayHelper: entered=entered, game_entries=giveaway.game_entries) session.merge(g) - session.commit() \ No newline at end of file + session.commit() diff --git a/src/web/templates/base.html b/src/web/templates/base.html index 0992170..6922545 100644 --- a/src/web/templates/base.html +++ b/src/web/templates/base.html @@ -11,6 +11,7 @@