#5 Created ability to generate views for a given product from the past X days
This commit is contained in:
@ -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")
|
||||
|
||||
|
Reference in New Issue
Block a user