simplier report storing

This commit is contained in:
Aaron Kimbre 2022-01-16 18:49:28 -06:00
parent 4a3fe08fe7
commit 1a5531eb19
3 changed files with 29 additions and 35 deletions

View File

@ -7,6 +7,7 @@ import logging
from flask_sqlalchemy import BaseQuery from flask_sqlalchemy import BaseQuery
from sqlalchemy.dialects import mysql from sqlalchemy.dialects import mysql
from sqlalchemy.exc import OperationalError, StatementError from sqlalchemy.exc import OperationalError, StatementError
from sqlalchemy.types import JSON
from time import sleep from time import sleep
import random import random
import string import string
@ -983,20 +984,15 @@ class Server(db.Model):
class ItemReports(db.Model): class ItemReports(db.Model):
__tablename__ = 'item_reports' __tablename__ = 'item_reports'
item = db.Column( data = db.Column(
db.Integer(), JSON(),
primary_key=True,
nullable=False
)
count = db.Column(
db.Integer(),
nullable=False nullable=False
) )
date = db.Column( date = db.Column(
db.Date(), db.Date(),
primary_key=False, primary_key=True,
autoincrement=False
) )
def save(self): def save(self):
@ -1007,4 +1003,3 @@ class ItemReports(db.Model):
def delete(self): def delete(self):
db.session.delete(self) db.session.delete(self)
db.session.commit() db.session.commit()

View File

@ -22,8 +22,15 @@ def items_by_date(date):
return render_template('reports/items/by_date.html.j2', items=items, 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(): 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( char_xmls = CharacterXML.query.join(
CharacterInfo, CharacterInfo,
CharacterInfo.id==CharacterXML.id CharacterInfo.id==CharacterXML.id
@ -31,7 +38,9 @@ def gen_item_report():
Account, Account,
CharacterInfo.account_id==Account.id CharacterInfo.account_id==Account.id
).filter(Account.gm_level < 3).all() ).filter(Account.gm_level < 3).all()
date = datetime.date.today().strftime('%Y-%m-%d')
report_data={}
for char_xml in char_xmls: for char_xml in char_xmls:
character_json = xmltodict.parse( character_json = xmltodict.parse(
char_xml.xml_data, char_xml.xml_data,
@ -40,19 +49,10 @@ def gen_item_report():
for inv in character_json["obj"]["inv"]["items"]["in"]: 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): 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"]: for item in inv["i"]:
entry = ItemReports.query.filter( if item["attr_l"] in report_data:
ItemReports.item == int(item["attr_l"]) and \ report_data[item["attr_l"]] = report_data[item["attr_l"]] + int(item["attr_c"])
ItemReports.date == date
).first()
if entry:
entry.count = entry.count + int(item["attr_c"])
entry.save()
else: else:
new_entry = ItemReports( report_data[item["attr_l"]] = int(item["attr_c"])
item=int(item["attr_l"]),
count=int(item["attr_c"]),
date=date
)
new_entry.save()
return "Done"
return f"Generated Report for {date}"

View File

@ -1,16 +1,16 @@
"""itemreport table """items-reports
Revision ID: b89c21b5112f Revision ID: 3da561e7f7c0
Revises: 8a2966b9f7ee Revises: 8a2966b9f7ee
Create Date: 2022-01-15 19:21:44.544653 Create Date: 2022-01-16 18:27:11.067205
""" """
from alembic import op from alembic import op
import sqlalchemy as sa import sqlalchemy as sa
from sqlalchemy.dialects import mysql
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = 'b89c21b5112f' revision = '3da561e7f7c0'
down_revision = '8a2966b9f7ee' down_revision = '8a2966b9f7ee'
branch_labels = None branch_labels = None
depends_on = None depends_on = None
@ -19,10 +19,9 @@ depends_on = None
def upgrade(): def upgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###
op.create_table('item_reports', op.create_table('item_reports',
sa.Column('item', sa.Integer(), nullable=False), sa.Column('data', sa.JSON(), nullable=False),
sa.Column('count', sa.Integer(), nullable=False), sa.Column('date', sa.Date(), autoincrement=False, nullable=False),
sa.Column('date', sa.Date(), nullable=False), sa.PrimaryKeyConstraint('date')
sa.PrimaryKeyConstraint('item', 'date')
) )
# ### end Alembic commands ### # ### end Alembic commands ###