diff --git a/jon/db/add_views.sql b/jon/db/add_views.sql index a6c0834..4002da5 100644 --- a/jon/db/add_views.sql +++ b/jon/db/add_views.sql @@ -58,3 +58,29 @@ FROM garfield.inventory_items ) m USING (item_id) ORDER BY inventory_items.name; +CREATE TEMPORARY VIEW inventory_last_n_days_sales AS +WITH +last_n_days AS ( + SELECT generate_series(now() - interval '14 days', now(), interval '1 day')::date AS sale_date +), +last_n_days_sales AS ( + SELECT + item_id, + sale_date, + count(snack_sales_log_timestamp) AS sale_date_sales + FROM garfield.inventory_items + LEFT JOIN garfield.inventory_map ON item_id = inventory_id + -- Is there a better way of writing this? + -- TODO: Try to speed this query up + CROSS JOIN last_n_days + LEFT JOIN garfield.snack_sales_log + ON inventory_map.snack_id = snack_sales_log.snack_id + AND DATE_TRUNC('day', snack_sales_log_timestamp) = sale_date + GROUP BY item_id, sale_date + ORDER BY item_id, sale_date +) +SELECT + item_id, + array_agg(sale_date_sales) AS last_n_days_sales +FROM last_n_days_sales +GROUP BY item_id diff --git a/jon/db/get_inventory_overview.sql b/jon/db/get_inventory_overview.sql index fe2a44f..4f4adf0 100644 --- a/jon/db/get_inventory_overview.sql +++ b/jon/db/get_inventory_overview.sql @@ -1,6 +1,7 @@ SELECT * FROM all_inventory_item_overview +LEFT JOIN inventory_last_n_days_sales USING (item_id) WHERE (%(location_id)s IS NULL OR location = %(location_id)s) AND available ORDER BY diff --git a/jon/static/jon.css b/jon/static/jon.css index 9f92248..66dde1c 100644 --- a/jon/static/jon.css +++ b/jon/static/jon.css @@ -74,3 +74,7 @@ th { details { font-size: 0.8em; } +.consumption-graph { + display: block; + height: 1em; +} diff --git a/jon/templates/inventory/index.html b/jon/templates/inventory/index.html index f87e979..3f1d993 100644 --- a/jon/templates/inventory/index.html +++ b/jon/templates/inventory/index.html @@ -1,9 +1,33 @@ +{% macro consumption_graph_svg(values) -%} +{% set stroke_width = 8 %} +{% set width = 300 %} +{% set height = 100 %} +{% set padding = 4 %} +{% set dx = (width - 2 * padding) / ((values | length) + 1) %} +{% set dy = (height - 2 * padding) / ((values + [1]) | max) %} + +{% endmacro -%} + {% extends "base.html" %} {% block content %}
ID | +Graph | Barcode | Name | Preis (Netto) | @@ -18,6 +42,7 @@ {% for item in items %}
---|---|---|---|---|
{{ item.item_id }} | +{{ consumption_graph_svg(item.last_n_days_sales) }} | {{ item.item_barcode }} |
{{ item.name }} | {{ format_currency(item.unit_price) }} |