""" 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 from controllers.database.product import ProductController from datetime import datetime import random # 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) # Recent Views product_view_frequency_data = dict(map( lambda k, v: (k, random.randint(70, 100)), # lambda k, v: (k, len(v)), data.keys(), data.values() )) # Total Views total_views = db.read_product_views(id) # Ranking in category ranking = db.read_ranking(id) db = ProductController() product = db.read_id(id) # Stock Level product.quantityAvailable # Age time_since_posted = datetime.now() - product.postedDate data = { "age": time_since_posted, "ranking": ranking, "views": { "total": total_views, "headings": list(reversed(product_view_frequency_data.keys())), "data": list(reversed(product_view_frequency_data.values())) }, "stocklevel": product.quantityAvailable } return render_template( "index.html", content="stats.html", data=data ) @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)