""" The user controller to manage all of the stats related endpoints in the web app """ from flask import Blueprint from flask import render_template, request from controllers.database.stats import StatsController # Blueprint to append user endpoints to blueprint = Blueprint("stats", __name__, url_prefix='/stats') @blueprint.route('/') def index(): """ Main page to view all of the statistics for the site """ db = StatsController() data = db.read() test = list(map(lambda d: d.productID, data)) return render_template("index.html", content="stats.html", data=test) @blueprint.route('/products/') def view_product_stats(id: int): """ Page to view statistics for a given product """ db = StatsController() 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())) ) @blueprint.route('/users/') def view_user_stats(id: int): """ Page to view statistics for a given user """ db = StatsController() data = db.read_user(id) return render_template("index.html", content="stats.html", data=data)