#5 Latest views are now displayed on the stats page
This commit is contained in:
		| @@ -49,30 +49,32 @@ class StatsController(DatabaseController): | |||||||
|             WHERE userID = ? |             WHERE userID = ? | ||||||
|         """ |         """ | ||||||
|  |  | ||||||
|         self.get_many(query, params) |         return self.get_many(query, params) | ||||||
|  |  | ||||||
|     def read_days(self, product_id: int, prev_days: int = 7 |     def read_days(self, product_id: int, prev_days: int = 7 | ||||||
|                   ) -> list[Stats] | None: |                   ) -> dict[int, list[Stats]] | None: | ||||||
|         """ Returns data from within the given number of days """ |         """ Returns data from within the given number of days """ | ||||||
|         data = self.read_product(product_id) |         data = self.read_product(product_id) | ||||||
|  |  | ||||||
|         filtered_data = list(filter( |         # Filter the values to only be within X prev days | ||||||
|             lambda d: is_within_x_days(d.viewDate, prev_days), |         filtered_data = list() | ||||||
|             data |         if data is not None: | ||||||
|         )) |             filtered_data = list(filter( | ||||||
|  |                 lambda d: is_within_x_days(d.viewDate, prev_days), | ||||||
|  |                 data | ||||||
|  |             )) | ||||||
|  |  | ||||||
|         day_views: dict[int, list[Stats]] = dict() |         day_views: dict[int, list[Stats]] = dict() | ||||||
|  |  | ||||||
|  |         for i in range(0, prev_days): | ||||||
|  |             day_views[i] = list() | ||||||
|  |  | ||||||
|  |         # Organise data into distinct | ||||||
|         for view in filtered_data: |         for view in filtered_data: | ||||||
|             diff = datetime.now()-view.viewDate |             diff = datetime.now()-view.viewDate | ||||||
|  |  | ||||||
|             if diff.days not in day_views: |  | ||||||
|                 day_views[diff.days] = [] |  | ||||||
|  |  | ||||||
|             day_views[diff.days].append(view) |             day_views[diff.days].append(view) | ||||||
|  |  | ||||||
|         for days in day_views.values(): |         return day_views | ||||||
|             print(len(days)) |  | ||||||
|  |  | ||||||
|     def update(self): |     def update(self): | ||||||
|         print("Doing work") |         print("Doing work") | ||||||
|   | |||||||
| @@ -2,6 +2,7 @@ | |||||||
|     in the web app |     in the web app | ||||||
| """ | """ | ||||||
| import json | import json | ||||||
|  | import random | ||||||
| from flask import Blueprint | from flask import Blueprint | ||||||
|  |  | ||||||
| from flask import render_template, redirect, request, session, flash | from flask import render_template, redirect, request, session, flash | ||||||
| @@ -19,8 +20,6 @@ def stats_index(): | |||||||
|     db = StatsController() |     db = StatsController() | ||||||
|     data = db.read() |     data = db.read() | ||||||
|     test = list(map(lambda d: d.productID, data)) |     test = list(map(lambda d: d.productID, data)) | ||||||
|     for i in test: |  | ||||||
|         print(i) |  | ||||||
|     return render_template("index.html", content="stats.html", data=test) |     return render_template("index.html", content="stats.html", data=test) | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -28,8 +27,23 @@ def stats_index(): | |||||||
| def view_product_stats(id: int): | def view_product_stats(id: int): | ||||||
|     """ Page to view statistics for a given product """ |     """ Page to view statistics for a given product """ | ||||||
|     db = StatsController() |     db = StatsController() | ||||||
|     data = db.read_days(id, 7) |  | ||||||
|     return render_template("index.html", content="stats.html", data=data) |     prev_days: int = request.args.get('prev_days', 7, int) | ||||||
|  |     data = db.read_days(id, prev_days) | ||||||
|  |  | ||||||
|  |     product_frequency_data = dict(map( | ||||||
|  |         # lambda k, v: (k, random.randint(0, 100)), | ||||||
|  |         lambda k, v: (k, len(v)), | ||||||
|  |         data.keys(), | ||||||
|  |         data.values() | ||||||
|  |     )) | ||||||
|  |  | ||||||
|  |     return render_template( | ||||||
|  |         "index.html", | ||||||
|  |         content="stats.html", | ||||||
|  |         headings=list(reversed(product_frequency_data.keys())), | ||||||
|  |         data=list(reversed(product_frequency_data.values())) | ||||||
|  |     ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @blueprint.route('/user/<int:id>') | @blueprint.route('/user/<int:id>') | ||||||
|   | |||||||
| @@ -46,6 +46,7 @@ | |||||||
|                     {% if product.quantityAvailable > 0 %} |                     {% if product.quantityAvailable > 0 %} | ||||||
|                         <div class="product-instock">In Stock</div> |                         <div class="product-instock">In Stock</div> | ||||||
|                         <div class="product-quantity">{{product.quantityAvailable}} Available</div> |                         <div class="product-quantity">{{product.quantityAvailable}} Available</div> | ||||||
|  |                         <a href="{{url_for('main.stats.view_product_stats', id=product.id)}}"><input type="button" class="product-add-to-cart" value="View Product Stats"/></a> | ||||||
|                     {% else %} |                     {% else %} | ||||||
|                         <div class="product-nostock">Out of Stock</div> |                         <div class="product-nostock">Out of Stock</div> | ||||||
|                     {% endif %} |                     {% endif %} | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ | |||||||
|   |   | ||||||
|  |  | ||||||
| <div> | <div> | ||||||
|     <canvas id="myChart"></canvas> |     <canvas id="myChart" width="800px" height="500px"></canvas> | ||||||
| </div> | </div> | ||||||
|  |  | ||||||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||||||
| @@ -13,9 +13,9 @@ | |||||||
|     new Chart(ctx, { |     new Chart(ctx, { | ||||||
|         type: 'line', |         type: 'line', | ||||||
|         data: { |         data: { | ||||||
|             labels: {{data}}, |             labels: {{headings}}, | ||||||
|             datasets: [{ |             datasets: [{ | ||||||
|                 label: '# of Votes', |                 label: '# of Views', | ||||||
|                 data: {{data}}, |                 data: {{data}}, | ||||||
|                 borderWidth: 1 |                 borderWidth: 1 | ||||||
|             }] |             }] | ||||||
| @@ -23,7 +23,17 @@ | |||||||
|         options: { |         options: { | ||||||
|             scales: { |             scales: { | ||||||
|                 y: { |                 y: { | ||||||
|  |                     title: { | ||||||
|  |                         display: true, | ||||||
|  |                         text: "Num Views" | ||||||
|  |                     }, | ||||||
|                     beginAtZero: true |                     beginAtZero: true | ||||||
|  |                 }, | ||||||
|  |                 x: { | ||||||
|  |                     title: { | ||||||
|  |                         display: true, | ||||||
|  |                         text: "Prev Days" | ||||||
|  |                     }, | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user