WIP: Started to add more stats to the product stats page.

This commit is contained in:
Luke Else 2024-02-09 18:45:33 +00:00
parent c55fdde3fc
commit 7c27aa31b6
4 changed files with 58 additions and 6 deletions

View File

@ -44,7 +44,10 @@ class DatabaseController(ABC):
""" """
obj = of.__new__(of) obj = of.__new__(of)
for attr, value in with_fields.items(): for attr, value in with_fields.items():
setattr(obj, attr, value) try:
setattr(obj, attr, value)
except KeyError:
return of(value)
return obj return obj
def do(self, query: str, params: list[str]): def do(self, query: str, params: list[str]):

View File

@ -76,6 +76,19 @@ class StatsController(DatabaseController):
return day_views return day_views
def read_product_views(self, id: int):
""" Returns the total number of views for a product """
params = [
id
]
# Total Views
query = """
SELECT count(id) FROM Views WHERE productID = ?
"""
return self.get_one(query, params, int)
def update(self): def update(self):
print("Doing work") print("Doing work")

View File

@ -5,6 +5,10 @@ from flask import Blueprint
from flask import render_template, request from flask import render_template, request
from controllers.database.stats import StatsController 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 to append user endpoints to
blueprint = Blueprint("stats", __name__, url_prefix='/stats') blueprint = Blueprint("stats", __name__, url_prefix='/stats')
@ -27,18 +31,49 @@ def view_product_stats(id: int):
prev_days: int = request.args.get('prev_days', 7, int) prev_days: int = request.args.get('prev_days', 7, int)
data = db.read_days(id, prev_days) data = db.read_days(id, prev_days)
product_frequency_data = dict(map( product_view_frequency_data = dict(map(
# lambda k, v: (k, random.randint(0, 100)), lambda k, v: (k, random.randint(0, 100)),
lambda k, v: (k, len(v)), # lambda k, v: (k, len(v)),
data.keys(), data.keys(),
data.values() 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( return render_template(
"index.html", "index.html",
content="stats.html", content="stats.html",
headings=list(reversed(product_frequency_data.keys())), headings=list(reversed(product_view_frequency_data.keys())),
data=list(reversed(product_frequency_data.values())) data=list(reversed(product_view_frequency_data.values()))
) )

1
venv.bat Normal file
View File

@ -0,0 +1 @@
./.venv/Scripts/activate