diff --git a/controllers/database/stats.py b/controllers/database/stats.py index fca7046..59127da 100644 --- a/controllers/database/stats.py +++ b/controllers/database/stats.py @@ -90,6 +90,34 @@ class StatsController(DatabaseController): return self.get_one(query, params, int) + 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) + def update(self): print("Doing work") diff --git a/controllers/web/stats.py b/controllers/web/stats.py index ad9663b..91ab33d 100644 --- a/controllers/web/stats.py +++ b/controllers/web/stats.py @@ -42,18 +42,12 @@ def view_product_stats(id: int): # Total Views total_views = db.read_product_views(id) + # Ranking in category + ranking = db.read_ranking(id) + db = ProductController() product = db.read_id(id) - # Ranking in category - query = """ - SELECT id, count(id) FROM stats WHERE id = ( - SELECT id FROM Products WEHRE id = ( - SELECT categoryID FROM Products WHERE id = ? - ) - ) - """ - # Stock Level product.quantityAvailable @@ -62,7 +56,7 @@ def view_product_stats(id: int): data = { "age": time_since_posted, - "ranking": 0, + "ranking": ranking, "views": { "total": total_views, "headings": list(reversed(product_view_frequency_data.keys())), diff --git a/static/css/stats.css b/static/css/stats.css index 9609f35..74de60c 100644 --- a/static/css/stats.css +++ b/static/css/stats.css @@ -1,5 +1,6 @@ .stats { display: flex; + flex-direction: row; justify-content: center; align-items: center; gap: 1rem; diff --git a/static/css/style.css b/static/css/style.css index c3087eb..26b01ab 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -182,3 +182,10 @@ a:active { flex-grow: 1; height: 1%; } + +.hidden { + display: none; + visibility: hidden; + width: 0; + height: 0; +} \ No newline at end of file diff --git a/templates/stats.html b/templates/stats.html index a7752a2..91206e8 100644 --- a/templates/stats.html +++ b/templates/stats.html @@ -1,25 +1,27 @@ +

-
- -
- -
- - - + +
+ +
+ +
+ + +
-

Age: {{data['age']}}

-

Ranking: {{data['ranking']}}

-

Stock Level: {{data['stocklevel']}}

-

Total Views: {{data['views']['total']}}

+

Age: {{data['age']}}

+

Ranking: {{data['ranking']}}

+

Stock Level: {{data['stocklevel']}}

+

Total Views: {{data['views']['total']}}

@@ -58,4 +60,16 @@ } } }); + + + // Remove the need for a submit button on the graph buttons + const form = document.getElementById("timeframe_form"); + const rbs = form.querySelectorAll("input[type='radio']"); + console.log(rbs) + console.log(form) + rbs.forEach((input) => { + input.addEventListener("change", () => { + form.submit(); + }); + }); \ No newline at end of file