oedb-backend/oedb/resources/demo/demo_stats.py
2025-09-22 11:44:25 +02:00

53 lines
1.7 KiB
Python

"""
Stats page for demo: list counts per what, and optional map per selected type.
"""
import os
import falcon
from collections import Counter
from oedb.utils.logging import logger
from oedb.utils.db import db_connect
import jinja2
class DemoStatsResource:
def __init__(self):
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
self.jinja_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_dir),
autoescape=jinja2.select_autoescape(['html', 'xml'])
)
def on_get(self, req, resp):
logger.info("Processing GET request to /demo/stats")
resp.content_type = 'text/html'
selected_what = req.get_param('what')
try:
db = db_connect()
cur = db.cursor()
# Compter par what
cur.execute("""
SELECT events_what, COUNT(*)
FROM events
GROUP BY events_what
ORDER BY COUNT(*) DESC
""")
rows = cur.fetchall()
counts = [(r[0], r[1]) for r in rows]
total_events = sum(c for _, c in counts)
template = self.jinja_env.get_template('stats.html')
html = template.render(counts=counts, total_events=total_events, selected_what=selected_what)
resp.text = html
resp.status = falcon.HTTP_200
logger.success("Successfully processed GET request to /demo/stats")
except Exception as e:
logger.error(f"Error processing GET request to /demo/stats: {e}")
resp.status = falcon.HTTP_500
resp.text = f"Error: {str(e)}"
demo_stats = DemoStatsResource()