From ded0e55501d5927b4d0e32fad4c8c7a0aa7998ef Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Sat, 14 May 2022 08:53:23 -0500 Subject: [PATCH] Resolves #33 --- app/reports.py | 94 ++++++++++++++++++----------- app/templates/reports/graph.html.j2 | 63 +++++++++++++++++-- app/templates/reports/index.html.j2 | 6 +- 3 files changed, 118 insertions(+), 45 deletions(-) diff --git a/app/reports.py b/app/reports.py index d3a09ec..5b2b2d7 100644 --- a/app/reports.py +++ b/app/reports.py @@ -49,14 +49,15 @@ def items_by_date(date): return render_template('reports/items/by_date.html.j2', data=data, date=date) -@reports_blueprint.route('/items/graph', methods=['GET', 'POST']) +@reports_blueprint.route('/items/graph//', methods=['GET', 'POST']) @login_required @gm_level(3) -def items_graph(): - thirty_days_ago = datetime.date.today() - datetime.timedelta(days=30) +def items_graph(start, end): + start_date = datetime.date.today() - datetime.timedelta(weeks=int(start)) + end_date = datetime.date.today() - datetime.timedelta(weeks=int(end)) entries = Reports.query.filter( Reports.report_type == "items" - ).filter(Reports.date >= thirty_days_ago).all() + ).filter(Reports.date.between(start_date, end_date)).all() # transform data for chartjs labels = [] items = dict() @@ -74,21 +75,28 @@ def items_graph(): data.append(entry.data[key]) else: data.append(0) - color = "#" + ''.join([random.choice('ABCDEF0123456789') for i in range(6)]) - datasets.append({ - "label": value, - "data": data, - "backgroundColor": color, - "borderColor": color - }) + color = "#" + value.encode("utf-8").hex()[1:7] + if max(data) > 10: + datasets.append({ + "label": value, + "data": data, + "backgroundColor": color, + "borderColor": color + }) return render_template( 'reports/graph.html.j2', labels=labels, datasets=datasets, - name="Item" + name="Item", + start=start, + end=end, + data_type="items", + start_date=start_date, + end_date=end_date ) + @reports_blueprint.route('/currency/by_date/', methods=['GET', 'POST']) @login_required @gm_level(3) @@ -97,14 +105,15 @@ def currency_by_date(date): return render_template('reports/currency/by_date.html.j2', data=data, date=date) -@reports_blueprint.route('/currency/graph', methods=['GET', 'POST']) +@reports_blueprint.route('/currency/graph//', methods=['GET', 'POST']) @login_required @gm_level(3) -def currency_graph(): - thirty_days_ago = datetime.date.today() - datetime.timedelta(days=30) +def currency_graph(start, end): + start_date = datetime.date.today() - datetime.timedelta(weeks=int(start)) + end_date = datetime.date.today() - datetime.timedelta(weeks=int(end)) entries = Reports.query.filter( Reports.report_type == "currency" - ).filter(Reports.date >= thirty_days_ago).all() + ).filter(Reports.date.between(start_date, end_date)).all() characters = CharacterInfo.query.options(load_only(CharacterInfo.name)).all() labels = [] datasets = [] @@ -118,18 +127,24 @@ def currency_graph(): data.append(entry.data[character.name]) else: data.append(0) - color = "#" + ''.join([random.choice('ABCDEF0123456789') for i in range(6)]) - datasets.append({ - "label": character.name, - "data": data, - "backgroundColor": color, - "borderColor": color - }) + color = "#" + character.name.encode("utf-8").hex()[1:7] + if max(data) > 10000: + datasets.append({ + "label": character.name, + "data": data, + "backgroundColor": color, + "borderColor": color + }) return render_template( 'reports/graph.html.j2', labels=labels, datasets=datasets, - name="Currency" + name="Currency", + start=start, + end=end, + data_type="currency", + start_date=start_date, + end_date=end_date ) @@ -141,14 +156,15 @@ def uscore_by_date(date): return render_template('reports/uscore/by_date.html.j2', data=data, date=date) -@reports_blueprint.route('/uscore/graph', methods=['GET', 'POST']) +@reports_blueprint.route('/uscore/graph//', methods=['GET', 'POST']) @login_required @gm_level(3) -def uscore_graph(): - thirty_days_ago = datetime.date.today() - datetime.timedelta(days=30) +def uscore_graph(start, end): + start_date = datetime.date.today() - datetime.timedelta(weeks=int(start)) + end_date = datetime.date.today() - datetime.timedelta(weeks=int(end)) entries = Reports.query.filter( Reports.report_type == "uscore" - ).filter(Reports.date >= thirty_days_ago).all() + ).filter(Reports.date.between(start_date, end_date)).all() characters = CharacterInfo.query.options(load_only(CharacterInfo.name)).all() labels = [] datasets = [] @@ -162,18 +178,24 @@ def uscore_graph(): data.append(entry.data[character.name]) else: data.append(0) - color = "#" + ''.join([random.choice('ABCDEF0123456789') for i in range(6)]) - datasets.append({ - "label": character.name, - "data": data, - "backgroundColor": color, - "borderColor": color - }) + color = "#" + character.name.encode("utf-8").hex()[1:7] + if max(data) > 1000: + datasets.append({ + "label": character.name, + "data": data, + "backgroundColor": color, + "borderColor": color + }) return render_template( 'reports/graph.html.j2', labels=labels, datasets=datasets, - name="U-Score" + name="U-Score", + start=start, + end=end, + data_type="uscore", + start_date=start_date, + end_date=end_date ) diff --git a/app/templates/reports/graph.html.j2 b/app/templates/reports/graph.html.j2 index b8c20a2..e1fcbba 100644 --- a/app/templates/reports/graph.html.j2 +++ b/app/templates/reports/graph.html.j2 @@ -1,14 +1,65 @@ {% extends 'base.html.j2' %} {% block title %} - {{ name }} History for the Last 30 Days + {{ name }} History for {{start_date}} to {{end_date}} {% endblock title %} {% block content_before %} - {{ name }} History for the Last 30 Days + {{ name }} History for {{start_date}} to {{end_date}} {% endblock content_before %} {% block content %} +
+ {% if data_type == "items" %} + + {% if end|int > 0 %} + + {% endif %} + {% elif data_type == "currency" %} + + {% if end|int > 0 %} + + {% endif %} + {% elif data_type == "uscore" %} + + {% if end|int > 0 %} + + {% endif %} + {% else %} +

INVALID DATA TYPE

+ {% endif %} +
+
{% endblock %} @@ -27,18 +78,18 @@ plugins: { legend: { display: false }, }, scales: { x: { + display: true, title: { display: true, text: 'Date' - }, - display: true + } }, y: { display: true, - title: { + title: { display: true, text: '{{ name }}' - }, + } } } } diff --git a/app/templates/reports/index.html.j2 b/app/templates/reports/index.html.j2 index 0412d90..22e6a7c 100644 --- a/app/templates/reports/index.html.j2 +++ b/app/templates/reports/index.html.j2 @@ -13,7 +13,7 @@
Items: + href='{{url_for('reports.items_graph', start=1, end=0)}}'> Graph
{% for report in reports_items|reverse %} @@ -26,7 +26,7 @@
Currency: + href='{{url_for('reports.currency_graph', start=1, end=0)}}'> Graph
{% for report in reports_currency|reverse %} @@ -39,7 +39,7 @@
U-Score: + href='{{url_for('reports.uscore_graph', start=1, end=0)}}'> Graph
{% for report in reports_uscore|reverse %}