Compare commits
	
		
			No commits in common. "ab53b31ec0610f47c3a5292098a60d76a466f1fd" and "630a0435a28c6bf287d8e7bdf043936162f5859f" have entirely different histories.
		
	
	
		
			ab53b31ec0
			...
			630a0435a2
		
	
		
| @ -68,4 +68,3 @@ ssh -nNTvL 5432:fsmi-db.fsmi.org:5432 fsmi-login.fsmi.uni-karlsruhe.de | ||||
|   - [x] Figure out/Add documentation about building `entry.js` | ||||
|   - [ ] Clean up the code a little and add some comments | ||||
|   - [ ] Needs good documentation for maintainability | ||||
| - [ ] Use cool new function for deactivating items | ||||
|  | ||||
| @ -74,40 +74,30 @@ LEFT JOIN garfield.inventory_items AS b | ||||
|       AND b.bought > a.bought | ||||
| GROUP BY a.item_id; | ||||
| 
 | ||||
| -- We need to create this table so that we can put an index on it | ||||
| -- Otherwise the join in the consumption graph query becomes much slower | ||||
| -- Perhaps it would be nicer to use a materialized view instead | ||||
| DROP TABLE IF EXISTS last_n_days; | ||||
| CREATE TEMPORARY TABLE last_n_days AS ( | ||||
|   SELECT | ||||
|     generate_series(now() - interval '14 days', now(), interval '1 day')::date AS sale_date | ||||
| ); | ||||
| CREATE UNIQUE INDEX last_n_days_sale_date ON last_n_days (sale_date); | ||||
| 
 | ||||
| -- Get an array of how often items were sold over the last 14 days | ||||
| CREATE TEMPORARY VIEW inventory_last_n_days_sales AS | ||||
| WITH | ||||
| sales_by_date AS ( | ||||
|   SELECT | ||||
|     inventory_line AS item_id, | ||||
|     date_trunc('day', snack_sales_log_timestamp AT TIME ZONE 'UTC+1') AS sale_date, | ||||
|     count(*)::int AS sales | ||||
|   FROM garfield.snack_sales_log | ||||
|   GROUP BY item_id, sale_date | ||||
| last_n_days AS ( | ||||
|   SELECT generate_series(now() - interval '14 days', now(), interval '1 day')::date AS sale_date | ||||
| ), | ||||
| beeg AS ( | ||||
|   SELECT item_id, sale_date, count(snack_sales_log_timestamp)::int AS sales | ||||
| 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_line = item_id | ||||
| -- snack_sales_log has an index on snack_sales_log_timestamp to speed up this query | ||||
| -- If that index doesn't exist the query takes much longer. | ||||
|         AND sale_date = date_trunc('day', snack_sales_log_timestamp AT TIME ZONE 'UTC+1') | ||||
|   WHERE available | ||||
|          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(sales) AS last_n_days_sales | ||||
| FROM beeg | ||||
| SELECT | ||||
|   item_id, | ||||
|   array_agg(sale_date_sales) AS last_n_days_sales | ||||
| FROM last_n_days_sales | ||||
| GROUP BY item_id | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user