Add inline SVG consumption graphs
This commit is contained in:
		
							parent
							
								
									1b6d0c40f3
								
							
						
					
					
						commit
						6baee544c9
					
				| @ -58,3 +58,29 @@ FROM garfield.inventory_items | |||||||
|   ) m USING (item_id) |   ) m USING (item_id) | ||||||
| ORDER BY inventory_items.name; | 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 | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| SELECT | SELECT | ||||||
|   * |   * | ||||||
| FROM all_inventory_item_overview | 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) | WHERE (%(location_id)s IS NULL OR location = %(location_id)s) | ||||||
|   AND available |   AND available | ||||||
| ORDER BY | ORDER BY | ||||||
|  | |||||||
| @ -74,3 +74,7 @@ th { | |||||||
| details { | details { | ||||||
|   font-size: 0.8em; |   font-size: 0.8em; | ||||||
| } | } | ||||||
|  | .consumption-graph { | ||||||
|  |   display: block; | ||||||
|  |   height: 1em; | ||||||
|  | } | ||||||
|  | |||||||
| @ -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) %} | ||||||
|  | <svg viewBox="0 0 {{ width }} {{ height }}" role="img" class="consumption-graph"> | ||||||
|  |   <polyline | ||||||
|  |     points=" | ||||||
|  |     {% for value in values %} | ||||||
|  |       {{ padding + loop.index * dx }}, {{ height - padding - value * dy }} | ||||||
|  |     {% endfor %} | ||||||
|  |     " | ||||||
|  |     stroke="green" | ||||||
|  |     stroke-width="{{ stroke_width }}" | ||||||
|  |     stroke-linecap="round" | ||||||
|  |     stroke-linejoin="round" | ||||||
|  |     fill="none" | ||||||
|  |   > | ||||||
|  | </svg> | ||||||
|  | {% endmacro -%} | ||||||
|  | 
 | ||||||
| {% extends "base.html" %} | {% extends "base.html" %} | ||||||
| 
 | 
 | ||||||
| {% block content %} | {% block content %} | ||||||
| <table> | <table> | ||||||
|   <tr> |   <tr> | ||||||
|     <th>ID</th> |     <th>ID</th> | ||||||
|  |     <th>Graph</th> | ||||||
|     <th>Barcode</th> |     <th>Barcode</th> | ||||||
|     <th>Name</th> |     <th>Name</th> | ||||||
|     <th>Preis (Netto)</th> |     <th>Preis (Netto)</th> | ||||||
| @ -18,6 +42,7 @@ | |||||||
| {% for item in items %} | {% for item in items %} | ||||||
|   <tr> |   <tr> | ||||||
|     <td><a href="/inventory/item/{{ item.item_id }}">{{ item.item_id }}</a></td> |     <td><a href="/inventory/item/{{ item.item_id }}">{{ item.item_id }}</a></td> | ||||||
|  |     <td>{{ consumption_graph_svg(item.last_n_days_sales) }}</td> | ||||||
|     <td><code>{{ item.item_barcode }}</code></td> |     <td><code>{{ item.item_barcode }}</code></td> | ||||||
|     <td>{{ item.name }}</td> |     <td>{{ item.name }}</td> | ||||||
|     <td class="--align-right">{{ format_currency(item.unit_price) }}</td> |     <td class="--align-right">{{ format_currency(item.unit_price) }}</td> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user