2024-01-30 21:49:17 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.stats import Stats
|
2024-02-09 22:01:25 +00:00
|
|
|
from datetime import datetime, timedelta
|
2024-02-05 23:19:39 +00:00
|
|
|
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-09 22:01:25 +00:00
|
|
|
) -> dict[datetime, 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
|
|
|
|
2024-02-09 22:01:25 +00:00
|
|
|
day_views: dict[str, list[Stats]] = dict()
|
2024-02-05 23:19:39 +00:00
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
for i in range(0, prev_days):
|
2024-02-09 22:01:25 +00:00
|
|
|
key = datetime.today() - timedelta(days=i)
|
|
|
|
day_views[key.strftime('%b-%d')] = list()
|
2024-02-06 19:15:44 +00:00
|
|
|
|
|
|
|
# Organise data into distinct
|
2024-02-05 23:19:39 +00:00
|
|
|
for view in filtered_data:
|
2024-02-09 22:01:25 +00:00
|
|
|
key = datetime.today() - (datetime.today() - view.viewDate)
|
|
|
|
day_views[key.strftime('%b-%d')].append(view)
|
2024-02-05 23:19:39 +00:00
|
|
|
|
2024-02-06 19:15:44 +00:00
|
|
|
return day_views
|
2024-02-05 23:19:39 +00:00
|
|
|
|
2024-02-09 18:45:33 +00:00
|
|
|
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)
|
|
|
|
|
2024-02-11 15:52:12 +00:00
|
|
|
def read_ranking(self, id: int):
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
|
|
|
|
query = """
|
|
|
|
SELECT
|
|
|
|
ranking
|
|
|
|
FROM
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
p.id as id,
|
|
|
|
RANK() OVER (
|
|
|
|
PARTITION BY p.categoryID
|
|
|
|
ORDER BY COUNT(v.productID) DESC
|
|
|
|
)AS ranking
|
|
|
|
FROM
|
|
|
|
Products p
|
|
|
|
LEFT JOIN
|
|
|
|
Views v ON p.id = v.productID
|
|
|
|
GROUP BY
|
|
|
|
p.id, p.categoryID
|
|
|
|
)
|
|
|
|
WHERE id = ?
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.get_one(query, params, int)
|
|
|
|
|
2024-01-30 21:49:17 +00:00
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
print("Doing work")
|