diff --git a/app.py b/app.py index 3c6679b..c7b5757 100644 --- a/app.py +++ b/app.py @@ -20,8 +20,9 @@ app.ext.add_dependency(Quotas, get_quotas) from room import bp as room_bp from propic import bp as propic_bp from export import bp as export_bp +from stats import bp as stats_bp -app.blueprint([room_bp,propic_bp,export_bp]) +app.blueprint([room_bp,propic_bp,export_bp,stats_bp]) @app.exception(exceptions.SanicException) async def clear_session(request, exception): @@ -51,27 +52,6 @@ async def gen_barcode(request, code): return raw(img.getvalue(), content_type="image/png") -@app.route("/manage/cache") -async def cache_status(request): - return - - -@app.route("/manage/stats") -async def stats(request, order: Order): - - with open('res/stats.json') as f: - stats = json.load(f) - - tpl = app.ctx.tpl.get_template('stats.html') - return html(tpl.render(order=order, stats=stats)) - -@app.route("/manage/nosecount") -async def nose_count(request, order: Order): - orders = {key:value for key,value in sorted(app.ctx.om.cache.items(), key=lambda x: len(x[1].room_members), reverse=True) if value.status not in ['c', 'e']} - - tpl = app.ctx.tpl.get_template('nosecount.html') - return html(tpl.render(orders=orders, order=order)) - @app.route("/furizon/beyond/order///open/") async def redirect_explore(request, code, secret, order: Order, secret2=None): diff --git a/stats.py b/stats.py new file mode 100644 index 0000000..c90b3f5 --- /dev/null +++ b/stats.py @@ -0,0 +1,50 @@ +from sanic.response import html +from sanic import Blueprint, exceptions +from ext import * +from config import headers +from time import time +import asyncio + +bp = Blueprint("stats", url_prefix="/manage") + +def by_count(d): + return {k:v for k,v in sorted(d.items(), key=lambda x: x[1], reverse=True)} + +async def gen_stats(app): + orders = app.ctx.om.cache.values() + + countries = {} + sponsors = {} + + for o in orders: + countries[o.country] = countries.get(o.country, 0) +1 + sponsors[o.sponsorship or 'no'] = sponsors.get(o.sponsorship or 'no', 0) +1 + + app.ctx.stats = { + 'countries': by_count(countries), + 'sponsors': by_count(sponsors), + 'time': time() + } + + return app.ctx.stats + +@bp.route("/stats") +async def stats(request, order: Order): + + stats = getattr(request.app.ctx, 'stats', None) + '''if not stats: + await gen_stats(request.app) + elif time() - stats['time'] > 1800: + asyncio.create_task(gen_stats(request.app))''' + + request.app.ctx.stats = await gen_stats(request.app) + + tpl = request.app.ctx.tpl.get_template('stats.html') + return html(tpl.render(order=order, stats=request.app.ctx.stats)) + +@bp.route("/nosecount") +async def nose_count(request, order: Order): + orders = {key:value for key,value in sorted(request.app.ctx.om.cache.items(), key=lambda x: len(x[1].room_members), reverse=True) if value.status not in ['c', 'e']} + + tpl = request.app.ctx.tpl.get_template('nosecount.html') + return html(tpl.render(orders=orders, order=order)) diff --git a/tpl/stats.html b/tpl/stats.html new file mode 100644 index 0000000..1c79bec --- /dev/null +++ b/tpl/stats.html @@ -0,0 +1,54 @@ +{% extends "base.html" %} +{% block title %}Furizon 2023 Stats{% endblock %} +{% block main %} + +
+
+ + + + +
+

Countries

+
+ +
+ +

Sponsors

+
+ +
+ +
+{% endblock %}