2024-02-01 01:44:02 +00:00
|
|
|
""" The user controller to manage all of the stats related endpoints
|
|
|
|
in the web app
|
|
|
|
"""
|
|
|
|
from flask import Blueprint
|
|
|
|
|
2024-02-06 19:30:08 +00:00
|
|
|
from flask import render_template, request
|
2024-02-01 01:44:02 +00:00
|
|
|
from controllers.database.stats import StatsController
|
|
|
|
|
|
|
|
# Blueprint to append user endpoints to
|
|
|
|
blueprint = Blueprint("stats", __name__, url_prefix='/stats')
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route('/')
|
2024-02-06 19:30:08 +00:00
|
|
|
def index():
|
2024-02-02 16:42:19 +00:00
|
|
|
""" Main page to view all of the statistics for the site """
|
2024-02-01 01:44:02 +00:00
|
|
|
db = StatsController()
|
|
|
|
data = db.read()
|
2024-02-02 21:01:29 +00:00
|
|
|
test = list(map(lambda d: d.productID, data))
|
|
|
|
return render_template("index.html", content="stats.html", data=test)
|
2024-02-01 01:44:02 +00:00
|
|
|
|
|
|
|
|
2024-02-06 20:11:04 +00:00
|
|
|
@blueprint.route('/products/<int:id>')
|
2024-02-01 01:44:02 +00:00
|
|
|
def view_product_stats(id: int):
|
2024-02-02 16:42:19 +00:00
|
|
|
""" Page to view statistics for a given product """
|
2024-02-01 01:44:02 +00:00
|
|
|
db = StatsController()
|
2024-02-06 19:15:44 +00:00
|
|
|
|
|
|
|
prev_days: int = request.args.get('prev_days', 7, int)
|
|
|
|
data = db.read_days(id, prev_days)
|
|
|
|
|
|
|
|
product_frequency_data = dict(map(
|
|
|
|
# lambda k, v: (k, random.randint(0, 100)),
|
|
|
|
lambda k, v: (k, len(v)),
|
|
|
|
data.keys(),
|
|
|
|
data.values()
|
|
|
|
))
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"index.html",
|
|
|
|
content="stats.html",
|
|
|
|
headings=list(reversed(product_frequency_data.keys())),
|
|
|
|
data=list(reversed(product_frequency_data.values()))
|
|
|
|
)
|
2024-02-01 01:44:02 +00:00
|
|
|
|
2024-02-01 01:45:39 +00:00
|
|
|
|
2024-02-06 20:11:04 +00:00
|
|
|
@blueprint.route('/users/<int:id>')
|
2024-02-01 01:44:02 +00:00
|
|
|
def view_user_stats(id: int):
|
2024-02-02 16:42:19 +00:00
|
|
|
""" Page to view statistics for a given user """
|
2024-02-01 01:44:02 +00:00
|
|
|
db = StatsController()
|
|
|
|
data = db.read_user(id)
|
2024-02-02 16:42:19 +00:00
|
|
|
return render_template("index.html", content="stats.html", data=data)
|