Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ab53b31ec0 | ||
|
|
a201ce00b6 | ||
|
|
630a0435a2 | ||
|
|
61cf7696a5 | ||
|
|
3d9ad49020 |
@@ -68,3 +68,4 @@ ssh -nNTvL 5432:fsmi-db.fsmi.org:5432 fsmi-login.fsmi.uni-karlsruhe.de
|
|||||||
- [x] Figure out/Add documentation about building `entry.js`
|
- [x] Figure out/Add documentation about building `entry.js`
|
||||||
- [ ] Clean up the code a little and add some comments
|
- [ ] Clean up the code a little and add some comments
|
||||||
- [ ] Needs good documentation for maintainability
|
- [ ] Needs good documentation for maintainability
|
||||||
|
- [ ] Use cool new function for deactivating items
|
||||||
|
|||||||
+43
-16
@@ -58,29 +58,56 @@ FROM garfield.inventory_items
|
|||||||
) m USING (item_id)
|
) m USING (item_id)
|
||||||
ORDER BY inventory_items.name;
|
ORDER BY inventory_items.name;
|
||||||
|
|
||||||
|
-- How many *other* active inventory lines exist with the same barcode in the same location that are newer?
|
||||||
|
CREATE TEMPORARY VIEW more_recent_inventory_lines_with_same_barcode AS
|
||||||
|
SELECT
|
||||||
|
a.item_id,
|
||||||
|
-- It's important not to count(*) here because item_id is NULL
|
||||||
|
-- when no other inventory lines exist.
|
||||||
|
count(b.item_id) AS other_lines_count
|
||||||
|
FROM garfield.inventory_items AS a
|
||||||
|
LEFT JOIN garfield.inventory_items AS b
|
||||||
|
ON a.item_barcode = b.item_barcode
|
||||||
|
AND a.item_id != b.item_id
|
||||||
|
AND a.location = b.location
|
||||||
|
AND b.available
|
||||||
|
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
|
CREATE TEMPORARY VIEW inventory_last_n_days_sales AS
|
||||||
WITH
|
WITH
|
||||||
last_n_days AS (
|
sales_by_date AS (
|
||||||
SELECT generate_series(now() - interval '14 days', now(), interval '1 day')::date AS sale_date
|
|
||||||
),
|
|
||||||
last_n_days_sales AS (
|
|
||||||
SELECT
|
SELECT
|
||||||
item_id,
|
inventory_line AS item_id,
|
||||||
sale_date,
|
date_trunc('day', snack_sales_log_timestamp AT TIME ZONE 'UTC+1') AS sale_date,
|
||||||
count(snack_sales_log_timestamp) AS sale_date_sales
|
count(*)::int AS sales
|
||||||
|
FROM garfield.snack_sales_log
|
||||||
|
GROUP BY item_id, sale_date
|
||||||
|
),
|
||||||
|
beeg AS (
|
||||||
|
SELECT item_id, sale_date, count(snack_sales_log_timestamp)::int AS sales
|
||||||
FROM garfield.inventory_items
|
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
|
CROSS JOIN last_n_days
|
||||||
LEFT JOIN garfield.snack_sales_log
|
LEFT JOIN garfield.snack_sales_log
|
||||||
ON inventory_map.snack_id = snack_sales_log.snack_id
|
ON inventory_line = item_id
|
||||||
AND DATE_TRUNC('day', snack_sales_log_timestamp) = sale_date
|
-- 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
|
||||||
GROUP BY item_id, sale_date
|
GROUP BY item_id, sale_date
|
||||||
ORDER BY item_id, sale_date
|
ORDER BY item_id, sale_date
|
||||||
)
|
)
|
||||||
SELECT
|
SELECT item_id, array_agg(sales) AS last_n_days_sales
|
||||||
item_id,
|
FROM beeg
|
||||||
array_agg(sale_date_sales) AS last_n_days_sales
|
|
||||||
FROM last_n_days_sales
|
|
||||||
GROUP BY item_id
|
GROUP BY item_id
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
SELECT
|
SELECT
|
||||||
*
|
*
|
||||||
FROM all_inventory_item_overview
|
FROM all_inventory_item_overview
|
||||||
|
LEFT JOIN more_recent_inventory_lines_with_same_barcode USING (item_id)
|
||||||
LEFT JOIN inventory_last_n_days_sales USING (item_id)
|
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
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
|
<th>Stat</th>
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Graph</th>
|
<th>Graph</th>
|
||||||
<th>Barcode</th>
|
<th>Barcode</th>
|
||||||
@@ -41,6 +42,10 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{% for item in items %}
|
{% for item in items %}
|
||||||
<tr>
|
<tr>
|
||||||
|
<td>
|
||||||
|
{% if item.units_left == 0 %}<span title="Leerer aktiver Inventareintrag">🅾️</span>{% endif %}
|
||||||
|
{% if item.other_lines_count != 0 %}<span title="{% if item.other_lines_count == 1 %}Neuerer Eintrag mit demselben Barcode ist aktiv{% else %}{{ item.other_lines_count }} Einträge mit demselben Barcode sind aktiv{% endif %}">🔄</span>{% endif %}
|
||||||
|
</td>
|
||||||
<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>{{ consumption_graph_svg(item.last_n_days_sales) }}</td>
|
||||||
<td><code>{{ item.item_barcode }}</code></td>
|
<td><code>{{ item.item_barcode }}</code></td>
|
||||||
|
|||||||
Reference in New Issue
Block a user