From b2af6d967a70d7627d61882031c598fd0a5f4679 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Sun, 16 Jan 2022 20:24:47 -0600 Subject: [PATCH] Fix reports Add currency report make reports use json storage Make tables nicer --- app/models.py | 11 +- app/reports.py | 120 +++++++++++++----- app/static/scss/site.scss | 2 +- app/templates/accounts/index.html.j2 | 2 +- app/templates/bug_reports/index.html.j2 | 2 +- app/templates/character/index.html.j2 | 2 +- app/templates/header.html.j2 | 2 +- app/templates/logs/activity.html.j2 | 2 +- app/templates/logs/command.html.j2 | 2 +- app/templates/main/about.html.j2 | 6 +- app/templates/moderation/index.html.j2 | 6 +- app/templates/play_keys/index.html.j2 | 2 +- app/templates/properties/index.html.j2 | 2 +- .../reports/currency/by_date.html.j2 | 51 ++++++++ app/templates/reports/index.html.j2 | 13 +- app/templates/reports/items/by_date.html.j2 | 24 +++- ...ems_reports.py => aee4c6c24811_reports.py} | 15 ++- 17 files changed, 197 insertions(+), 67 deletions(-) create mode 100644 app/templates/reports/currency/by_date.html.j2 rename migrations/versions/{3da561e7f7c0_items_reports.py => aee4c6c24811_reports.py} (64%) diff --git a/app/models.py b/app/models.py index 2cdaf6f..b003014 100644 --- a/app/models.py +++ b/app/models.py @@ -981,14 +981,21 @@ class Server(db.Model): db.session.commit() -class ItemReports(db.Model): - __tablename__ = 'item_reports' +class Reports(db.Model): + __tablename__ = 'reports' data = db.Column( JSON(), nullable=False ) + report_type = db.Column( + db.VARCHAR(35), + nullable=False, + primary_key=True, + autoincrement=False + ) + date = db.Column( db.Date(), primary_key=True, diff --git a/app/reports.py b/app/reports.py index 16dfd0d..cdade50 100644 --- a/app/reports.py +++ b/app/reports.py @@ -1,8 +1,8 @@ from flask import render_template, Blueprint, redirect, url_for, request, abort, flash, request from flask_user import login_required, current_user -from app.models import db, CharacterInfo, Account, CharacterXML, ItemReports +from app.models import db, CharacterInfo, Account, CharacterXML, Reports from app import gm_level, scheduler -import datetime, xmltodict +import datetime, xmltodict, json reports_blueprint = Blueprint('reports', __name__) @@ -10,49 +10,103 @@ reports_blueprint = Blueprint('reports', __name__) @login_required @gm_level(3) def index(): - items = ItemReports.query.distinct(ItemReports.date).group_by(ItemReports.date).all() - - return render_template('reports/index.html.j2', items=items) + reports = Reports.query.distinct(Reports.date).group_by(Reports.date).all() + print(gen_item_report()) + print(gen_currency_report()) + return render_template('reports/index.html.j2', reports=reports) @reports_blueprint.route('/items/by_date/', methods=['GET', 'POST']) @login_required @gm_level(3) def items_by_date(date): - items = ItemReports.query.filter(ItemReports.date==date).order_by(ItemReports.count.desc()).all() - return render_template('reports/items/by_date.html.j2', items=items, date=date) + data = Reports.query.filter(Reports.date==date).filter(Reports.report_type=="items").first().data + return render_template('reports/items/by_date.html.j2', data=data, date=date) + +@reports_blueprint.route('/currency/by_date/', methods=['GET', 'POST']) +@login_required +@gm_level(3) +def currency_by_date(date): + data = Reports.query.filter(Reports.date==date).filter(Reports.report_type=="currency").first().data + return render_template('reports/currency/by_date.html.j2', data=data, date=date) -@scheduler.task("cron", id="gen_item_report", hour=7) +@scheduler.task("cron", id="gen_item_report", hour=23) def gen_item_report(): - date = datetime.date.today().strftime('%Y-%m-%d') - report = ItemReports.query.filter(ItemReports.date==date).first() + with scheduler.app.app_context(): + date = datetime.date.today().strftime('%Y-%m-%d') + report = Reports.query.filter(Reports.date==date).filter(Reports.report_type=="items").first() - # Only one report per day - if report != None: - return f"Report Already Generated for {date}" + # Only one report per day + if report != None: + return f"Item Report Already Generated for {date}" - char_xmls = CharacterXML.query.join( - CharacterInfo, - CharacterInfo.id==CharacterXML.id - ).join( - Account, - CharacterInfo.account_id==Account.id - ).filter(Account.gm_level < 3).all() + char_xmls = CharacterXML.query.join( + CharacterInfo, + CharacterInfo.id==CharacterXML.id + ).join( + Account, + CharacterInfo.account_id==Account.id + ).filter(Account.gm_level < 3).all() - report_data={} + report_data={} - for char_xml in char_xmls: - character_json = xmltodict.parse( - char_xml.xml_data, - attr_prefix="attr_" + for char_xml in char_xmls: + character_json = xmltodict.parse( + char_xml.xml_data, + attr_prefix="attr_" + ) + for inv in character_json["obj"]["inv"]["items"]["in"]: + if "i" in inv.keys() and type(inv["i"]) == list and (int(inv["attr_t"])==0 or int(inv["attr_t"])==0): + for item in inv["i"]: + if item["attr_l"] in report_data: + report_data[item["attr_l"]] = report_data[item["attr_l"]] + int(item["attr_c"]) + else: + report_data[item["attr_l"]] = int(item["attr_c"]) + + new_report = Reports( + data=report_data, + report_type="items", + date=date ) - for inv in character_json["obj"]["inv"]["items"]["in"]: - if "i" in inv.keys() and type(inv["i"]) == list and (int(inv["attr_t"])==0 or int(inv["attr_t"])==0): - for item in inv["i"]: - if item["attr_l"] in report_data: - report_data[item["attr_l"]] = report_data[item["attr_l"]] + int(item["attr_c"]) - else: - report_data[item["attr_l"]] = int(item["attr_c"]) + + new_report.save() + + return f"Generated Item Report for {date}" - return f"Generated Report for {date}" +@scheduler.task("cron", id="gen_currency_report", hour=23) +def gen_currency_report(): + with scheduler.app.app_context(): + date = datetime.date.today().strftime('%Y-%m-%d') + report = Reports.query.filter(Reports.date==date).filter(Reports.report_type=="currency").first() + + # Only one report per day + if report != None: + return f"Currency Report Already Generated for {date}" + + characters = CharacterXML.query.join( + CharacterInfo, + CharacterInfo.id==CharacterXML.id + ).join( + Account, + CharacterInfo.account_id==Account.id + ).filter(Account.gm_level < 3).all() + + report_data={} + + for character in characters: + character_json = xmltodict.parse( + character.xml_data, + attr_prefix="attr_" + ) + report_data[CharacterInfo.query.filter(CharacterInfo.id==character.id).first().name] = int(character_json["obj"]["char"]["attr_cc"]) + + new_report = Reports( + data=report_data, + report_type="currency", + date=date + ) + + new_report.save() + + return f"Generated Currency Report for {date}" diff --git a/app/static/scss/site.scss b/app/static/scss/site.scss index 5e24a0e..d082fed 100644 --- a/app/static/scss/site.scss +++ b/app/static/scss/site.scss @@ -8,7 +8,7 @@ $theme-colors: ( $link-color: #005ac2; @import "../bootstrap-4.2.1/scss/bootstrap"; -@import url(http://fonts.googleapis.com/css?family=Nunito:700); +@import url(https://fonts.googleapis.com/css?family=Nunito:700); body { font-family:'Nunito', Helvetica, Arial, sans-serif; } diff --git a/app/templates/accounts/index.html.j2 b/app/templates/accounts/index.html.j2 index f8dec43..73d596f 100644 --- a/app/templates/accounts/index.html.j2 +++ b/app/templates/accounts/index.html.j2 @@ -9,7 +9,7 @@ {% endblock content_before %} {% block content %} - +
diff --git a/app/templates/bug_reports/index.html.j2 b/app/templates/bug_reports/index.html.j2 index 65b3f79..d79c975 100644 --- a/app/templates/bug_reports/index.html.j2 +++ b/app/templates/bug_reports/index.html.j2 @@ -9,7 +9,7 @@ {% endblock content_before %} {% block content %} -
Actions
+
diff --git a/app/templates/character/index.html.j2 b/app/templates/character/index.html.j2 index e5d03a5..b1b73d1 100644 --- a/app/templates/character/index.html.j2 +++ b/app/templates/character/index.html.j2 @@ -17,7 +17,7 @@
{% endif %} -
Actions
+
diff --git a/app/templates/header.html.j2 b/app/templates/header.html.j2 index b8ee1fc..fe1bb93 100644 --- a/app/templates/header.html.j2 +++ b/app/templates/header.html.j2 @@ -47,7 +47,7 @@ {% endif %} {% if current_user.is_authenticated and current_user.gm_level >= 2 %} - + Reports
Actions
+
diff --git a/app/templates/logs/command.html.j2 b/app/templates/logs/command.html.j2 index c67c42c..5c77d24 100644 --- a/app/templates/logs/command.html.j2 +++ b/app/templates/logs/command.html.j2 @@ -17,7 +17,7 @@
{% endif %} -
ID
+
diff --git a/app/templates/main/about.html.j2 b/app/templates/main/about.html.j2 index c8cc7b9..87382be 100644 --- a/app/templates/main/about.html.j2 +++ b/app/templates/main/about.html.j2 @@ -49,10 +49,10 @@
- TODO: add more Contributors + Developer:
- Add more + Jett.
@@ -62,7 +62,7 @@ Source diff --git a/app/templates/moderation/index.html.j2 b/app/templates/moderation/index.html.j2 index aca2f0a..596e5f9 100644 --- a/app/templates/moderation/index.html.j2 +++ b/app/templates/moderation/index.html.j2 @@ -11,7 +11,7 @@ {% block content %}

Characters


-
ID
+
@@ -28,7 +28,7 @@

Pets


-
Actions
+
@@ -41,7 +41,7 @@

Properties


-
Actions
+
diff --git a/app/templates/play_keys/index.html.j2 b/app/templates/play_keys/index.html.j2 index bd629b4..2e729e9 100644 --- a/app/templates/play_keys/index.html.j2 +++ b/app/templates/play_keys/index.html.j2 @@ -26,7 +26,7 @@ Bulk Create Play Keys
-
Actions
+
diff --git a/app/templates/properties/index.html.j2 b/app/templates/properties/index.html.j2 index 0ae3bd2..90f8d09 100644 --- a/app/templates/properties/index.html.j2 +++ b/app/templates/properties/index.html.j2 @@ -17,7 +17,7 @@
{% endif %} -
Actions
+
diff --git a/app/templates/reports/currency/by_date.html.j2 b/app/templates/reports/currency/by_date.html.j2 new file mode 100644 index 0000000..7f65f63 --- /dev/null +++ b/app/templates/reports/currency/by_date.html.j2 @@ -0,0 +1,51 @@ +{% extends 'base.html.j2' %} + +{% block title %} + Currency on {{ date }} +{% endblock title %} + +{% block content_before %} + Currency on {{ date }} +{% endblock content_before %} + +{% block content %} +
+
Actions
+ + + + + + {% for name, currency in data.items() %} + + + + + + {% endfor %} + +
+ Character + + currency +
+ {{ name }} + + {{ currency }} +
+ +{% endblock %} + +{% block js %} + {{ super () }} + +{% endblock %} diff --git a/app/templates/reports/index.html.j2 b/app/templates/reports/index.html.j2 index 7d29ef6..4ed59af 100644 --- a/app/templates/reports/index.html.j2 +++ b/app/templates/reports/index.html.j2 @@ -12,14 +12,21 @@
Items:
- {% for item in items %} + {% for report in reports %} - {{item.date}} + href='{{url_for('reports.items_by_date', date=report.date)}}'> + {{report.date}} {% endfor %}
+ Currency:
+ {% for report in reports %} + + {{report.date}} + + {% endfor %}
diff --git a/app/templates/reports/items/by_date.html.j2 b/app/templates/reports/items/by_date.html.j2 index d357c96..aeaca58 100644 --- a/app/templates/reports/items/by_date.html.j2 +++ b/app/templates/reports/items/by_date.html.j2 @@ -11,9 +11,8 @@ {% block content %}
+ id="items_by_date" + data-order='[[ 1, "desc" ]]'> - {% for item in items %} + {% for lot, count in data.items() %} {% endfor %} @@ -44,3 +43,14 @@ {% endblock %} +{% block js %} + {{ super () }} + +{% endblock %} diff --git a/migrations/versions/3da561e7f7c0_items_reports.py b/migrations/versions/aee4c6c24811_reports.py similarity index 64% rename from migrations/versions/3da561e7f7c0_items_reports.py rename to migrations/versions/aee4c6c24811_reports.py index f562181..9fcc07e 100644 --- a/migrations/versions/3da561e7f7c0_items_reports.py +++ b/migrations/versions/aee4c6c24811_reports.py @@ -1,8 +1,8 @@ -"""items-reports +"""reports -Revision ID: 3da561e7f7c0 +Revision ID: aee4c6c24811 Revises: 8a2966b9f7ee -Create Date: 2022-01-16 18:27:11.067205 +Create Date: 2022-01-16 20:12:39.816567 """ from alembic import op @@ -10,7 +10,7 @@ import sqlalchemy as sa # revision identifiers, used by Alembic. -revision = '3da561e7f7c0' +revision = 'aee4c6c24811' down_revision = '8a2966b9f7ee' branch_labels = None depends_on = None @@ -18,15 +18,16 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.create_table('item_reports', + op.create_table('reports', sa.Column('data', sa.JSON(), nullable=False), + sa.Column('report_type', sa.VARCHAR(length=35), autoincrement=False, nullable=False), sa.Column('date', sa.Date(), autoincrement=False, nullable=False), - sa.PrimaryKeyConstraint('date') + sa.PrimaryKeyConstraint('report_type', 'date') ) # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('item_reports') + op.drop_table('reports') # ### end Alembic commands ###
Item @@ -26,16 +25,16 @@
- {{ item.item|get_lot_name }} + {{ lot|get_lot_name }} - {{ item.count }} + {{ count }} - {{ item.item|get_lot_rarity }} + {{ lot|get_lot_rarity }}