WMGZON/controllers/web/stats.py

86 lines
2.3 KiB
Python

""" 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/<int:id>')
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_view_frequency_data = dict(map(
lambda k, v: (k, random.randint(0, 100)),
# lambda k, v: (k, len(v)),
data.keys(),
data.values()
))
print(db.read_product_views(id))
db = ProductController()
product = db.read_id(id)
# Ranking in category
query = """
SELECT id, count(id) FROM stats WHERE id = (
SELECT id FROM Products WEHRE id = (
SELECT categoryID FROM Products WHERE id = ?
)
)
"""
# Stock Level
product.quantityAvailable
# Age
time_since_posted = datetime.now() - product.postedDate
data = {
"age": time_since_posted,
"ranking": 0,
"views": {
"total": 0,
"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",
headings=list(reversed(product_view_frequency_data.keys())),
data=list(reversed(product_view_frequency_data.values()))
)
@blueprint.route('/users/<int:id>')
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)