2024-01-30 21:49:17 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.stats import Stats
|
2024-02-05 23:19:39 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from utils.general_utils import is_within_x_days
|
2024-01-30 21:49:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StatsController(DatabaseController):
|
|
|
|
FIELDS = ['id', 'userID', 'productID', 'viewDate']
|
2024-02-04 23:47:00 +00:00
|
|
|
TYPE = Stats
|
2024-01-30 21:49:17 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def create(self, view: Stats):
|
|
|
|
params = [
|
|
|
|
view.userID,
|
|
|
|
view.productID,
|
|
|
|
view.viewDate
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
2024-01-30 21:49:17 +00:00
|
|
|
INSERT INTO Views
|
|
|
|
(userID, productID, viewDate)
|
2024-02-04 23:47:00 +00:00
|
|
|
VALUES (?, ?, ?)
|
|
|
|
"""
|
2024-01-30 21:49:17 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
self.do(query, params)
|
2024-01-30 21:49:17 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
def read(self) -> list[Stats] | None:
|
|
|
|
query = "SELECT * FROM Views"
|
|
|
|
self.get_many(query, [])
|
2024-01-30 21:49:17 +00:00
|
|
|
|
|
|
|
def read_product(self, product_id: int = 0) -> list[Stats] | None:
|
|
|
|
params = [
|
|
|
|
product_id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
SELECT * FROM Views
|
2024-01-30 21:49:17 +00:00
|
|
|
WHERE productID = ?
|
2024-02-04 23:47:00 +00:00
|
|
|
"""
|
2024-01-30 21:49:17 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.get_many(query, params)
|
2024-01-30 21:49:17 +00:00
|
|
|
|
|
|
|
def read_user(self, user_id: int) -> list[Stats] | None:
|
|
|
|
params = [
|
|
|
|
user_id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
SELECT * FROM Views
|
2024-01-30 21:49:17 +00:00
|
|
|
WHERE userID = ?
|
2024-02-04 23:47:00 +00:00
|
|
|
"""
|
2024-01-30 21:49:17 +00:00
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
return self.get_many(query, params)
|
2024-01-30 21:49:17 +00:00
|
|
|
|
2024-02-05 23:19:39 +00:00
|
|
|
def read_days(self, product_id: int, prev_days: int = 7
|
2024-02-06 19:15:44 +00:00
|
|
|
) -> dict[int, list[Stats]] | None:
|
2024-02-05 23:19:39 +00:00
|
|
|
""" Returns data from within the given number of days """
|
|
|
|
data = self.read_product(product_id)
|
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
# 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
|
|
|
|
))
|
2024-02-05 23:19:39 +00:00
|
|
|
|
|
|
|
day_views: dict[int, list[Stats]] = dict()
|
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
for i in range(0, prev_days):
|
|
|
|
day_views[i] = list()
|
|
|
|
|
|
|
|
# Organise data into distinct
|
2024-02-05 23:19:39 +00:00
|
|
|
for view in filtered_data:
|
|
|
|
diff = datetime.now()-view.viewDate
|
|
|
|
day_views[diff.days].append(view)
|
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
return day_views
|
2024-02-05 23:19:39 +00:00
|
|
|
|
2024-01-30 21:49:17 +00:00
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
print("Doing work")
|