Эх сурвалжийг харах

don't stringify underlying table and fix charts

Daniel Sheffield 8 сар өмнө
parent
commit
c362065ee9

+ 6 - 0
sqlpage/grocery/categories.sql

@@ -1,2 +1,8 @@
 SET title = 'Categories';
+SET input_log_scale = 'hidden';
+SET input_start = 'disbled';
+SET input_end = 'disbled';
+SET input_unit_mass = 'hidden';
+SET input_unit_volume = 'hidden';
+SET input_unit_count = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 6 - 0
sqlpage/grocery/groups.sql

@@ -1,2 +1,8 @@
 SET title = 'Groups';
+SET input_log_scale = 'hidden';
+SET input_start = 'disbled';
+SET input_end = 'disbled';
+SET input_unit_mass = 'hidden';
+SET input_unit_volume = 'hidden';
+SET input_unit_count = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 6 - 0
sqlpage/grocery/products.sql

@@ -1,2 +1,8 @@
 SET title = 'Products';
+SET input_log_scale = 'hidden';
+SET input_start = 'disbled';
+SET input_end = 'disbled';
+SET input_unit_mass = 'hidden';
+SET input_unit_volume = 'hidden';
+SET input_unit_count = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 4 - 0
sqlpage/grocery/tags.sql

@@ -1,2 +1,6 @@
 SET title = 'Tags';
+SET input_log_scale = 'hidden';
+SET input_unit_mass = 'hidden';
+SET input_unit_volume = 'hidden';
+SET input_unit_count = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 1 - 0
sqlpage/grocery/transactions.sql

@@ -1,2 +1,3 @@
 SET title = 'Transactions';
+SET input_log_scale = 'hidden';
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/internal/entry.sql') AS properties;

+ 3 - 3
sqlpage/sqlpage/data/products.sql

@@ -20,10 +20,10 @@ FROM products p
 JOIN categories c ON p.category_id = c.id
 JOIN groups g ON c.group_id = g.id
 WHERE
-  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR ($products IS NULL OR $products = ''))
+  (p.name IN (SELECT v#>>'{}' FROM json_array_elements($products::json) j(v)) OR $products IS NULL)
 AND
-  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR ($groups IS NULL OR $groups = ''))
+  (c.name IN (SELECT v#>>'{}' FROM json_array_elements($categories::json) j(v)) OR $categories IS NULL)
 AND
-  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR ($groups IS NULL OR $groups = ''))
+  (g.name IN (SELECT v#>>'{}' FROM json_array_elements($groups::json) j(v)) OR $groups IS NULL)
 GROUP BY ROLLUP ("group", category, product)
 ;

+ 11 - 14
sqlpage/sqlpage/data/transactions.sql

@@ -1,29 +1,26 @@
 DROP TABLE IF EXISTS sqlpage_txn;
 CREATE TABLE IF NOT EXISTS sqlpage_txn AS (
 SELECT
-  to_char(
-    (ts AT TIME ZONE 'UTC') AT TIME ZONE 'Pacific/Auckland', --COALESCE('$ParameterTZ', 'Pacific/Auckland'),
-    'Mon DD HH24:MI:SS'
-  ) AS "Time",
-  t.description AS Description,
-  s.code AS "Store",
-  p.name AS "Product",
-  c.name AS "Category",
-  g.name AS "Group",
-  CASE WHEN t.organic THEN 'yes' ELSE 'no' END AS "Organic",
-  to_char(t.price, 'FM999999999.00') AS "Price",
-  to_char(CASE
+  t.ts AT TIME ZONE 'UTC' AS ts,
+  t.description,
+  s.code,
+  p.name AS product,
+  c.name AS category,
+  g.name AS "group",
+  t.organic,
+  t.price,
+  CASE
     WHEN ut.name = 'Count' THEN t.quantity
     ELSE t.quantity * convert_unit(u.name, CASE
       WHEN ut.name = 'Volume' THEN $unit_volume
       WHEN ut.name = 'Mass' THEN $unit_mass
     END, p.name)
-    END, 'FM999999999.000') AS "Quantity",
+  END AS quantity,
   CASE
     WHEN ut.name = 'Count' THEN u.name
     WHEN ut.name = 'Volume' THEN $unit_volume
     WHEN ut.name = 'Mass' THEN $unit_mass
-  END AS "Unit"
+  END AS unit
 FROM transactions t
 JOIN products p ON (product_id = p.id)
 JOIN categories c ON (category_id = c.id)

+ 3 - 3
sqlpage/sqlpage/internal/entry.sql

@@ -33,9 +33,9 @@ SET end = CASE
   ELSE $end
 END;
 SET log_scale = CASE
-  WHEN $log_scale = '' OR $log_scale IS NULL
-  THEN TRUE
-  ELSE $log_scale::bool
+  WHEN lower($log_scale) = 'true'
+  THEN 'true'
+  ELSE 'false'
 END;
 
 SET page = CASE

+ 46 - 29
sqlpage/sqlpage/internal/nav.sql

@@ -1,3 +1,10 @@
+SET input_log_scale = COALESCE($input_log_scale, 'checkbox');
+--SET input_log_scale = 'hidden'; -- log scale now working yet
+SET input_start = COALESCE($input_start, 'date');
+SET input_end = COALESCE($input_end, 'date');
+SET input_unit_mass = COALESCE($input_unit_mass, 'select');
+SET input_unit_volume = COALESCE($input_unit_volume, 'select');
+SET input_unit_count = COALESCE($input_unit_count, 'select');
 SELECT 'tab' AS component, TRUE AS center;
 
 SELECT title
@@ -43,60 +50,70 @@ SELECT 'Groups' AS label
 , TRUE AS multiple
 , $group_options AS options
 ;
-SELECT 'From' AS label
-, 'start' AS name
-, 2 AS width
-, $start AS value
-, 'date' AS type
-;
-SELECT 'To' AS label
-, 'end' AS name
-, 2 AS width
-, $end AS value
-, 'date' AS type
-;
-SELECT 'Tags' AS label
-, 'tags[]' AS name
-, 4 AS width
-, 'select' AS "type"
-, TRUE AS dropdown
-, TRUE AS multiple
-, $tag_options AS options
-;
 SELECT 'Unit (vol.)' AS label
 , 'unit_volume' AS name
-, 1 AS width
-, 'select' AS type
+, 2 AS width
+, $input_unit_volume AS type
 , $unit_volume_options AS options
 ;
 SELECT 'Unit (mass)' AS label
 , 'unit_mass' AS name
-, 1 AS width
-, 'select' AS type
+, 2 AS width
+, $input_unit_mass AS type
 , $unit_mass AS value
 , $unit_mass_options AS options
 ;
 SELECT 'Unit (count)' AS label
 , 'unit_count[]' AS name
-, 2 AS width
-, 'select' AS type
+, 4 AS width
+, $input_unit_count AS type
 , TRUE AS multiple
 , TRUE AS dropdown
 , '' AS value
-, $unit_count_options AS options;
+, $unit_count_options AS options
+;
+SELECT 'Tags' AS label
+, 'tags[]' AS name
+, 4 AS width
+, 'select' AS "type"
+, TRUE AS dropdown
+, TRUE AS multiple
+, $tag_options AS options
+;
+SELECT 'From' AS label
+, 'start' AS name
+, 2 AS width
+, $start AS value
+, $input_start AS type
+;
+SELECT 'To' AS label
+, 'end' AS name
+, 2 AS width
+, $end AS value
+, $input_end AS type
+;
 
 SELECT 'Apply' AS value
 , 'apply' AS name
 , '' AS label
 , 'blue' AS color
-, 1 AS width
+, 2 AS width
 , 'submit' AS type
 , '/grocery/internal/apply.sql' AS formaction
 ;
 SELECT 'Clear' AS value
 , 'clear' AS name
 , '' AS label
-, 1 AS width
+, 2 AS width
 , 'submit' AS type
 , '/grocery/internal/clear.sql' AS formaction
 ;
+
+SELECT 'Log Scale' AS label
+, 'log_scale' AS name
+, $input_log_scale AS type
+, TRUE AS disabled
+, 2 AS width
+, lower($log_scale) = 'true' AS checked
+, TRUE AS value
+;

+ 15 - 1
sqlpage/sqlpage/pages/Transactions.sql

@@ -9,7 +9,21 @@ SELECT
   'Unit' AS align_right,
   'Notice' AS markdown
 ;
-SELECT * FROM sqlpage_txn;
+SELECT
+  to_char(ts
+     AT TIME ZONE 'Pacific/Auckland', --COALESCE('$ParameterTZ', 'Pacific/Auckland'),
+    'Mon DD HH24:MI:SS'
+  ) AS "Time",
+  description AS "Description",
+  code AS "Store",
+  product AS "Product",
+  category AS "Category",
+  "group" AS "Group",
+  CASE WHEN organic THEN 'yes' ELSE 'no' END AS "Organic",
+  to_char(price, 'FM999999999.00') AS "Price",
+  to_char(quantity, 'FM999999999.000') AS "Quantity",
+  unit AS "Unit"
+FROM sqlpage_txn;
 
 SELECT '# No Data' AS "Notice"
 WHERE NOT EXISTS(SELECT * FROM sqlpage_txn LIMIT 1);

+ 19 - 13
sqlpage/sqlpage/pages/Trend.sql

@@ -8,14 +8,16 @@ SELECT 'chart' AS component
 , $height AS height
 , '$/'||$unit_mass AS ytitle
 , $type AS type
-, $log_scale::bool AS logarithmic
+, $log_scale = 'true' AS logarithmic
 , TRUE AS time
+, 0 AS ymin
 ;
-SELECT "Product" AS series
-, "Time" AS x
-, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+SELECT product AS series
+, ts AS x
+, to_char(price/quantity, 'FM999999999.000') AS y
 FROM sqlpage_txn
-WHERE "Unit" = $unit_mass
+WHERE unit = $unit_mass
+ORDER BY ts
 ;
 
 SELECT 'chart' AS component
@@ -25,12 +27,14 @@ SELECT 'chart' AS component
 , $type AS type
 , $log_scale::bool AS logarithmic
 , TRUE AS time
+, 0 AS ymin
 ;
-SELECT "Product" AS series
-, "Time" AS x
-, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+SELECT product AS series
+, ts AS x
+, to_char(price/quantity, 'FM999999999.000') AS y
 FROM sqlpage_txn
-WHERE "Unit" = $unit_volume
+WHERE unit = $unit_volume
+ORDER BY ts
 ;
 
 SELECT 'chart' AS component
@@ -40,12 +44,14 @@ SELECT 'chart' AS component
 , $type AS type
 , $log_scale::bool AS logarithmic
 , TRUE AS time
+, 0 AS ymin
 WHERE $display_by_count::bool
 ;
-SELECT "Product" AS series
-, "Time" AS x
-, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+SELECT product AS series
+, ts AS x
+, to_char(price/quantity, 'FM999999999.000') AS y
 FROM sqlpage_txn
 WHERE $display_by_count::bool
-AND "Unit" = $unit_count_chart
+AND unit = $unit_count_chart
+ORDER BY ts
 ;

+ 62 - 49
sqlpage/sqlpage/pages/Volume.sql

@@ -1,106 +1,119 @@
 SET height = '500';
 SET type = 'pie';
+
 SET unit_count_chart = (SELECT v#>>'{}' FROM json_array_elements($unit_count::json) j(v) LIMIT 1);
-SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1;
+SET display_by_count = (SELECT count(*) FROM json_array_elements($unit_count::json) j(v)) == 1
+AND (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_count_chart LIMIT 1));
+SET display_by_mass = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_mass LIMIT 1));
+SET display_by_volume = (EXISTS (SELECT 1 FROM sqlpage_txn WHERE unit = $unit_volume LIMIT 1));
 
 SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_mass) > 2
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_mass) > 2
 THEN 'Group'
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_mass) > 2
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_mass) > 2
 THEN 'Category'
 ELSE 'Product'
 END);
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
-WHERE "Unit" = $unit_mass);
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_mass);
 SELECT 'chart' AS component
 , 'Share by Weight - Total: '||$total||' '||$unit_mass AS title
 , $height AS height
 , $type AS type
 , TRUE AS labels
+WHERE $display_by_mass::bool
 ;
-SELECT "Group" AS series
-, sum("Quantity"::numeric) AS value
+SELECT "group" AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Group' AND "Unit" = $unit_mass
-GROUP BY "Group"
+WHERE $display_by_mass::bool
+AND $group_by = 'Group' AND unit = $unit_mass
+GROUP BY "group"
 ;
-SELECT "Category" AS series
-, sum("Quantity"::numeric) AS value
+SELECT category AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Category' AND "Unit" = $unit_mass
-GROUP BY "Category"
+WHERE $display_by_mass::bool
+AND $group_by = 'Category' AND unit = $unit_mass
+GROUP BY category
 ;
-SELECT "Product" AS series
-, sum("Quantity"::numeric) AS value
+SELECT product AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Product' AND "Unit" = $unit_mass
-GROUP BY "Product"
+WHERE $display_by_mass::bool
+AND $group_by = 'Product' AND unit = $unit_mass
+GROUP BY product
 ;
 
 SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_volume) > 2
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_volume) > 2
 THEN 'Group'
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_volume) > 2
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_volume) > 2
 THEN 'Category'
 ELSE 'Product'
 END);
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
-WHERE "Unit" = $unit_volume);
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_volume);
 SELECT 'chart' AS component
 , 'Share by Volume - Total: '||$total||' '||$unit_volume AS title
 , $height AS height
 , $type AS type
 , TRUE AS labels
+WHERE $display_by_volume::bool
 ;
-SELECT "Group" AS series
-, sum("Quantity"::numeric) AS value
+SELECT "group" AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Group' AND "Unit" = $unit_volume
-GROUP BY "Group"
+WHERE $display_by_volume::bool
+AND $group_by = 'Group' AND unit = $unit_volume
+GROUP BY "group"
 ;
-SELECT "Category" AS series
-, sum("Quantity"::numeric) AS value
+SELECT category AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Category' AND "Unit" = $unit_volume
-GROUP BY "Category"
+WHERE $display_by_volume::bool
+AND $group_by = 'Category' AND unit = $unit_volume
+GROUP BY category
 ;
-SELECT "Product" AS series
-, sum("Quantity"::numeric) AS value
+SELECT product AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Product' AND "Unit" = $unit_volume
-GROUP BY "Product"
+WHERE $display_by_volume::bool
+AND $group_by = 'Product' AND unit = $unit_volume
+GROUP BY product
 ;
 
 SET group_by = (CASE
-WHEN (SELECT count(DISTINCT "Group") FROM sqlpage_txn WHERE "Unit" = $unit_count_chart) > 2
+WHEN (SELECT count(DISTINCT "group") FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
 THEN 'Group'
-WHEN (SELECT count(DISTINCT "Category") FROM sqlpage_txn WHERE "Unit" = $unit_count_chart) > 2
+WHEN (SELECT count(DISTINCT category) FROM sqlpage_txn WHERE unit = $unit_count_chart) > 2
 THEN 'Category'
 ELSE 'Product'
 END);
-SET total = (SELECT sum("Quantity"::numeric) FROM sqlpage_txn
-WHERE "Unit" = $unit_count_chart);
+SET total = (SELECT sum(quantity) FROM sqlpage_txn WHERE unit = $unit_count_chart);
 SELECT 'chart' AS component
 , 'Share by Volume - Total: '||$total||' '||$unit_count_chart AS title
 , $height AS height
 , $type AS type
 , TRUE AS labels
+WHERE $display_by_count::bool
 ;
-SELECT "Group" AS series
-, sum("Quantity"::numeric) AS value
+SELECT "group" AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Group' AND "Unit" = $unit_count_chart
-GROUP BY "Group"
+WHERE $display_by_count::bool
+AND $group_by = 'Group' AND unit = $unit_count_chart
+GROUP BY "group"
 ;
-SELECT "Category" AS series
-, sum("Quantity"::numeric) AS value
+SELECT category AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Category' AND "Unit" = $unit_count_chart
-GROUP BY "Category"
+WHERE $display_by_count::bool
+AND $group_by = 'Category' AND unit = $unit_count_chart
+GROUP BY category
 ;
-SELECT "Product" AS series
-, sum("Quantity"::numeric) AS value
+SELECT product AS series
+, sum(quantity) AS value
 FROM sqlpage_txn
-WHERE $group_by = 'Product' AND "Unit" = $unit_count_chart
-GROUP BY "Product"
+WHERE $display_by_count::bool
+AND $group_by = 'Product' AND unit = $unit_count_chart
+GROUP BY product
 ;