diff --git a/app/models.py b/app/models.py index 6dc6af2..2cdaf6f 100644 --- a/app/models.py +++ b/app/models.py @@ -7,6 +7,7 @@ import logging from flask_sqlalchemy import BaseQuery from sqlalchemy.dialects import mysql from sqlalchemy.exc import OperationalError, StatementError +from sqlalchemy.types import JSON from time import sleep import random import string @@ -983,20 +984,15 @@ class Server(db.Model): class ItemReports(db.Model): __tablename__ = 'item_reports' - item = db.Column( - db.Integer(), - primary_key=True, - nullable=False - ) - - count = db.Column( - db.Integer(), + data = db.Column( + JSON(), nullable=False ) date = db.Column( db.Date(), - primary_key=False, + primary_key=True, + autoincrement=False ) def save(self): @@ -1007,4 +1003,3 @@ class ItemReports(db.Model): def delete(self): db.session.delete(self) db.session.commit() - diff --git a/app/reports.py b/app/reports.py index 3ec353f..16dfd0d 100644 --- a/app/reports.py +++ b/app/reports.py @@ -22,8 +22,15 @@ def items_by_date(date): return render_template('reports/items/by_date.html.j2', items=items, date=date) -@scheduler.task("cron", id="gen_item_report", hour=3) +@scheduler.task("cron", id="gen_item_report", hour=7) def gen_item_report(): + date = datetime.date.today().strftime('%Y-%m-%d') + report = ItemReports.query.filter(ItemReports.date==date).first() + + # Only one report per day + if report != None: + return f"Report Already Generated for {date}" + char_xmls = CharacterXML.query.join( CharacterInfo, CharacterInfo.id==CharacterXML.id @@ -31,7 +38,9 @@ def gen_item_report(): Account, CharacterInfo.account_id==Account.id ).filter(Account.gm_level < 3).all() - date = datetime.date.today().strftime('%Y-%m-%d') + + report_data={} + for char_xml in char_xmls: character_json = xmltodict.parse( char_xml.xml_data, @@ -40,19 +49,10 @@ def gen_item_report(): 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"]: - entry = ItemReports.query.filter( - ItemReports.item == int(item["attr_l"]) and \ - ItemReports.date == date - ).first() - if entry: - entry.count = entry.count + int(item["attr_c"]) - entry.save() + if item["attr_l"] in report_data: + report_data[item["attr_l"]] = report_data[item["attr_l"]] + int(item["attr_c"]) else: - new_entry = ItemReports( - item=int(item["attr_l"]), - count=int(item["attr_c"]), - date=date - ) - new_entry.save() + report_data[item["attr_l"]] = int(item["attr_c"]) - return "Done" + + return f"Generated Report for {date}" diff --git a/migrations/versions/b89c21b5112f_itemreport_table.py b/migrations/versions/3da561e7f7c0_items_reports.py similarity index 57% rename from migrations/versions/b89c21b5112f_itemreport_table.py rename to migrations/versions/3da561e7f7c0_items_reports.py index bc307c9..f562181 100644 --- a/migrations/versions/b89c21b5112f_itemreport_table.py +++ b/migrations/versions/3da561e7f7c0_items_reports.py @@ -1,16 +1,16 @@ -"""itemreport table +"""items-reports -Revision ID: b89c21b5112f +Revision ID: 3da561e7f7c0 Revises: 8a2966b9f7ee -Create Date: 2022-01-15 19:21:44.544653 +Create Date: 2022-01-16 18:27:11.067205 """ from alembic import op import sqlalchemy as sa -from sqlalchemy.dialects import mysql + # revision identifiers, used by Alembic. -revision = 'b89c21b5112f' +revision = '3da561e7f7c0' down_revision = '8a2966b9f7ee' branch_labels = None depends_on = None @@ -19,10 +19,9 @@ depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.create_table('item_reports', - sa.Column('item', sa.Integer(), nullable=False), - sa.Column('count', sa.Integer(), nullable=False), - sa.Column('date', sa.Date(), nullable=False), - sa.PrimaryKeyConstraint('item', 'date') + sa.Column('data', sa.JSON(), nullable=False), + sa.Column('date', sa.Date(), autoincrement=False, nullable=False), + sa.PrimaryKeyConstraint('date') ) # ### end Alembic commands ###