#5 Created ability to generate views for a given product from the past X days

This commit is contained in:
Luke Else 2024-02-05 23:19:39 +00:00
parent ee33965baf
commit 3a08686fc3
3 changed files with 34 additions and 1 deletions

View File

@ -1,5 +1,7 @@
from .database import DatabaseController
from models.stats import Stats
from datetime import datetime
from utils.general_utils import is_within_x_days
class StatsController(DatabaseController):
@ -49,6 +51,29 @@ class StatsController(DatabaseController):
self.get_many(query, params)
def read_days(self, product_id: int, prev_days: int = 7
) -> 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
))
day_views: dict[int, list[Stats]] = dict()
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))
def update(self):
print("Doing work")

View File

@ -28,7 +28,7 @@ def stats_index():
def view_product_stats(id: int):
""" Page to view statistics for a given product """
db = StatsController()
data = db.read_product(id)
data = db.read_days(id, 7)
return render_template("index.html", content="stats.html", data=data)

8
utils/general_utils.py Normal file
View File

@ -0,0 +1,8 @@
from datetime import datetime
def is_within_x_days(date: datetime, x_days: float = 7):
""" Returns true if the date is within X days """
current_date = datetime.now()
difference = current_date - date
return abs(difference.days) <= x_days