Explorar o código

add volume and trend charts

Daniel Sheffield hai 8 meses
pai
achega
3dfaf23b17

+ 2 - 0
sqlpage/grocery/volume.sql

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

+ 0 - 1
sqlpage/sqlpage/data/transactions.sql

@@ -12,7 +12,6 @@ SELECT
   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(quantity, '9.'||repeat('9',3-1)||'EEEE') AS Quantity,
   to_char(CASE
     WHEN ut.name = 'Count' THEN t.quantity
     ELSE t.quantity * convert_unit(u.name, CASE

+ 5 - 0
sqlpage/sqlpage/data/units.sql

@@ -0,0 +1,5 @@
+DROP TABLE IF EXISTS sqlpage_units;
+CREATE TABLE IF NOT EXISTS sqlpage_units AS
+SELECT u.name, ut.name AS type FROM units u
+JOIN unit_types ut ON ut.id = u.unit_type_id
+;

+ 7 - 6
sqlpage/sqlpage/internal/entry.sql

@@ -32,6 +32,11 @@ SET end = CASE
   THEN to_char(now(), 'YYYY-MM-DD')
   ELSE $end
 END;
+SET log_scale = CASE
+  WHEN $log_scale = '' OR $log_scale IS NULL
+  THEN TRUE
+  ELSE $log_scale::bool
+END;
 
 SET page = CASE
   WHEN $title IN (
@@ -54,7 +59,8 @@ SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/transactions.sql')
 WHERE $apply::bool OR $clear::bool;
 SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/products.sql') AS properties
 WHERE $apply::bool OR $clear::bool;
-
+SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/units.sql') AS properties
+WHERE $apply::bool OR $clear::bool;
 -- SELECT 'dynamic' AS component, sqlpage.run_sql('sqlpage/data/'||sql||'.sql')
 -- FROM (SELECT UNNEST(ARRAY[
 --   'tags',
@@ -130,11 +136,6 @@ FROM (
 WHERE value IS NOT NULL
 );
 
-DROP TABLE IF EXISTS sqlpage_units;
-CREATE TEMPORARY TABLE IF NOT EXISTS sqlpage_units AS
-SELECT u.name, ut.name AS type FROM units u
-JOIN unit_types ut ON ut.id = u.unit_type_id
-;
 SET unit_volume_options = (
 SELECT json_agg(json_build_object(
    'label', o.name,

+ 47 - 2
sqlpage/sqlpage/pages/Trend.sql

@@ -1,6 +1,51 @@
+SET height = '750';
+SET type = 'line';
+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;
+
 SELECT 'chart' AS component
 , 'Cost by Weight' AS title
-, 'line' AS type
-, $logarithmic AS logarithmic
+, $height AS height
+, '$/'||$unit_mass AS ytitle
+, $type AS type
+, $log_scale::bool AS logarithmic
 , TRUE AS time
 ;
+SELECT "Product" AS series
+, "Time" AS x
+, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+FROM sqlpage_txn
+WHERE "Unit" = $unit_mass
+;
+
+SELECT 'chart' AS component
+, 'Cost by Volume' AS title
+, $height AS height
+, '$/'||$unit_volume AS ytitle
+, $type AS type
+, $log_scale::bool AS logarithmic
+, TRUE AS time
+;
+SELECT "Product" AS series
+, "Time" AS x
+, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+FROM sqlpage_txn
+WHERE "Unit" = $unit_volume
+;
+
+SELECT 'chart' AS component
+, 'Cost by Count' AS title
+, $height AS height
+, '$/'||$unit_count_chart AS ytitle
+, $type AS type
+, $log_scale::bool AS logarithmic
+, TRUE AS time
+WHERE $display_by_count::bool
+;
+SELECT "Product" AS series
+, "Time" AS x
+, to_char("Price"::numeric / "Quantity"::numeric, 'FM999999999.000') AS y
+FROM sqlpage_txn
+WHERE $display_by_count::bool
+AND "Unit" = $unit_count_chart
+;

+ 106 - 0
sqlpage/sqlpage/pages/Volume.sql

@@ -0,0 +1,106 @@
+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 group_by = (CASE
+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
+THEN 'Category'
+ELSE 'Product'
+END);
+SET total = (SELECT sum("Quantity"::numeric) 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
+;
+SELECT "Group" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Group' AND "Unit" = $unit_mass
+GROUP BY "Group"
+;
+SELECT "Category" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Category' AND "Unit" = $unit_mass
+GROUP BY "Category"
+;
+SELECT "Product" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $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
+THEN 'Group'
+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);
+SELECT 'chart' AS component
+, 'Share by Volume - Total: '||$total||' '||$unit_volume AS title
+, $height AS height
+, $type AS type
+, TRUE AS labels
+;
+SELECT "Group" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Group' AND "Unit" = $unit_volume
+GROUP BY "Group"
+;
+SELECT "Category" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Category' AND "Unit" = $unit_volume
+GROUP BY "Category"
+;
+SELECT "Product" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $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
+THEN 'Group'
+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);
+SELECT 'chart' AS component
+, 'Share by Volume - Total: '||$total||' '||$unit_count_chart AS title
+, $height AS height
+, $type AS type
+, TRUE AS labels
+;
+SELECT "Group" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Group' AND "Unit" = $unit_count_chart
+GROUP BY "Group"
+;
+SELECT "Category" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Category' AND "Unit" = $unit_count_chart
+GROUP BY "Category"
+;
+SELECT "Product" AS series
+, sum("Quantity"::numeric) AS value
+FROM sqlpage_txn
+WHERE $group_by = 'Product' AND "Unit" = $unit_count_chart
+GROUP BY "Product"
+;