#5 Latest views are now displayed on the stats page

This commit is contained in:
Luke Else 2024-02-06 19:15:44 +00:00
parent 3a08686fc3
commit f254c011b6
4 changed files with 46 additions and 19 deletions

View File

@ -49,30 +49,32 @@ class StatsController(DatabaseController):
WHERE userID = ?
"""
self.get_many(query, params)
return self.get_many(query, params)
def read_days(self, product_id: int, prev_days: int = 7
) -> list[Stats] | None:
) -> dict[int, list[Stats]] | None:
""" Returns data from within the given number of days """
data = self.read_product(product_id)
filtered_data = list(filter(
lambda d: is_within_x_days(d.viewDate, prev_days),
data
))
# Filter the values to only be within X prev days
filtered_data = list()
if data is not None:
filtered_data = list(filter(
lambda d: is_within_x_days(d.viewDate, prev_days),
data
))
day_views: dict[int, list[Stats]] = dict()
for i in range(0, prev_days):
day_views[i] = list()
# Organise data into distinct
for view in filtered_data:
diff = datetime.now()-view.viewDate
if diff.days not in day_views:
day_views[diff.days] = []
day_views[diff.days].append(view)
for days in day_views.values():
print(len(days))
return day_views
def update(self):
print("Doing work")

View File

@ -2,6 +2,7 @@
in the web app
"""
import json
import random
from flask import Blueprint
from flask import render_template, redirect, request, session, flash
@ -19,8 +20,6 @@ def stats_index():
db = StatsController()
data = db.read()
test = list(map(lambda d: d.productID, data))
for i in test:
print(i)
return render_template("index.html", content="stats.html", data=test)
@ -28,8 +27,23 @@ def stats_index():
def view_product_stats(id: int):
""" Page to view statistics for a given product """
db = StatsController()
data = db.read_days(id, 7)
return render_template("index.html", content="stats.html", data=data)
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('/user/<int:id>')

View File

@ -46,6 +46,7 @@
{% if product.quantityAvailable > 0 %}
<div class="product-instock">In Stock</div>
<div class="product-quantity">{{product.quantityAvailable}} Available</div>
<a href="{{url_for('main.stats.view_product_stats', id=product.id)}}"><input type="button" class="product-add-to-cart" value="View Product Stats"/></a>
{% else %}
<div class="product-nostock">Out of Stock</div>
{% endif %}

View File

@ -2,7 +2,7 @@
<div>
<canvas id="myChart"></canvas>
<canvas id="myChart" width="800px" height="500px"></canvas>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
@ -13,9 +13,9 @@
new Chart(ctx, {
type: 'line',
data: {
labels: {{data}},
labels: {{headings}},
datasets: [{
label: '# of Votes',
label: '# of Views',
data: {{data}},
borderWidth: 1
}]
@ -23,7 +23,17 @@
options: {
scales: {
y: {
title: {
display: true,
text: "Num Views"
},
beginAtZero: true
},
x: {
title: {
display: true,
text: "Prev Days"
},
}
}
}