Command stats

Stijn De Clercq 2021-09-07 17:16:40 +02:00
parent ce9ecc8c61
commit 41f75d7bbd
4 changed files with 47 additions and 45 deletions

View File

@ -0,0 +1,15 @@
from datetime import date
from quart.json import JSONEncoder
class CustomEncoder(JSONEncoder):
"""
Custom JSON encoder to handle things like date parsing
"""
def default(self, o):
# Return dates as ISO strings
if isinstance(o, date):
return o.isoformat()
return super().default(o)

View File

@ -1,6 +1,7 @@
from werkzeug.exceptions import abort from werkzeug.exceptions import abort
from .error_handlers import handler_404, handler_422 from .error_handlers import handler_404, handler_422
from .json_encoder import CustomEncoder
from .routes.dm import dm_blueprint from .routes.dm import dm_blueprint
from .routes.ping import ping_blueprint from .routes.ping import ping_blueprint
from .routes.stats import stats_blueprint from .routes.stats import stats_blueprint
@ -15,6 +16,7 @@ app = Quart(__name__)
# needs higher Python & Quart version # needs higher Python & Quart version
app = cors(app, allow_origin="*") app = cors(app, allow_origin="*")
app.url_map.strict_slashes = False app.url_map.strict_slashes = False
app.json_encoder = CustomEncoder
# Register blueprints # Register blueprints
app.register_blueprint(dm_blueprint) app.register_blueprint(dm_blueprint)

View File

@ -1,4 +1,5 @@
from database import all_models, engine from database import all_models, engine, Base
from typing import Dict
def create_all_tables(): def create_all_tables():
@ -9,3 +10,16 @@ def create_all_tables():
""" """
for model in all_models: for model in all_models:
model.__table__.create(engine, checkfirst=True) model.__table__.create(engine, checkfirst=True)
def row_to_dict(row: Base) -> Dict:
"""
Create a dictionary from a database entry
vars() or dict() can NOT be used as these add extra SQLAlchemy-related properties!
"""
d = {}
for column in row.__table__.columns:
d[column.name] = getattr(row, column.name)
return d

View File

@ -2,8 +2,8 @@ from enum import IntEnum
from typing import Dict, List from typing import Dict, List
from database.db import session from database.db import session
from database.utils import row_to_dict
from database.models import CommandStats from database.models import CommandStats
from functions.database import utils
from functions.stringFormatters import leading_zero as lz from functions.stringFormatters import leading_zero as lz
import time import time
@ -24,27 +24,16 @@ def _is_present(date: str) -> bool:
""" """
Check if a given date is present in the database Check if a given date is present in the database
""" """
connection = utils.connect() res = session.query(CommandStats).filter(CommandStats.day == date).scalar()
cursor = connection.cursor() return res is not None
cursor.execute("SELECT * FROM command_stats WHERE day = %s", (date,))
res = cursor.fetchone()
if res:
return True
return False
def _add_date(date: str): def _add_date(date: str):
""" """
Add a date into the db Add a date into the db
""" """
connection = utils.connect() entry = CommandStats(day=date, commands=0, slash_commands=0, context_menus=0)
cursor = connection.cursor() session.add(entry)
cursor.execute("INSERT INTO command_stats(day, commands, slash_commands, context_menus) VALUES (%s, 0, 0, 0)", (date,))
connection.commit()
def _update(date: str, inv: InvocationType): def _update(date: str, inv: InvocationType):
@ -55,40 +44,22 @@ def _update(date: str, inv: InvocationType):
if not _is_present(date): if not _is_present(date):
_add_date(date) _add_date(date)
connection = utils.connect() column_name: str = ["commands", "slash_commands", "context_menus"][inv.value]
cursor = connection.cursor() session.query(CommandStats).filter(CommandStats.day == date)\
.update({column_name: (getattr(CommandStats, column_name) + 1)})
column_name = ["commands", "slash_commands", "context_menus"][inv.value] # Commit changes, including adding the date if necessary
session.commit()
# String formatting is safe here because the input comes from above ^
cursor.execute(f"""
UPDATE command_stats
SET {column_name} = {column_name} + 1
WHERE day = %s
""", (date,))
connection.commit()
def _get_all():
"""
Get all rows
"""
connection = utils.connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM command_stats")
return cursor.fetchall()
def query_command_stats() -> List[Dict]: def query_command_stats() -> List[Dict]:
"""
Return all rows as dicts
"""
stats = [] stats = []
instance: CommandStats
for instance in session.query(CommandStats).order_by(CommandStats.day): for instance in session.query(CommandStats).order_by(CommandStats.day):
stats.append({ stats.append(row_to_dict(instance))
"day": instance.day,
"commands": instance.commands,
"slash_commands": instance.slash_commands,
"context_menus": instance.context_menus
})
return stats return stats