Browse Source

simplify table filter query

Daniel Sheffield 1 year ago
parent
commit
e63ccd6250
2 changed files with 21 additions and 37 deletions
  1. 1 1
      app/rest/form.py
  2. 20 36
      app/rest/pyapi.py

+ 1 - 1
app/rest/form.py

@@ -15,7 +15,7 @@ def get_option_groups(
     k: str, g: str, _type: str
 ):
     in_chart = data['$/unit'].apply(lambda x: (x or False) and True)
-    if k in data:
+    if k in data or (k == 'tag' and 'tags' in data):
         k_data_in_chart = chain(*data[in_chart]["tags"]) if k == "tag" else data[in_chart][k]  
     else:
         k_data_in_chart = []

+ 20 - 36
app/rest/pyapi.py

@@ -47,7 +47,7 @@ conn = connect(f"{host} {db} {user} {password}")
 
 CACHE = Cache(10)
 
-def get_product_rollup_statement(filters, having=None):
+def get_product_rollup_statement(filters):
     where = [ get_where_include_exclude(
         k[0], "name", list(include), list(exclude)
     ) for k, (include, exclude) in filters.items() ]
@@ -57,9 +57,9 @@ SELECT
   count(DISTINCT p.id) AS "Products",
   count(DISTINCT c.id) AS "Categories",
   count(DISTINCT g.id) AS "Groups",
-  p.name AS "Product",
-  c.name AS "Category",
-  g.name AS "Group"
+  p.name AS "product",
+  c.name AS "category",
+  g.name AS "group"
 FROM products p
 JOIN categories c ON p.category_id = c.id
 JOIN groups g ON c.group_id = g.id
@@ -70,10 +70,6 @@ WHERE {where}
         SQL("""
 GROUP BY ROLLUP (g.name, c.name, p.name)
 """),
-        SQL("""
-HAVING {having}
-ORDER BY "Product", "Category", "Group"
-""").format(having=having) if having else SQL('')
     ])
 
 def normalize_query(query: FormsDict, allow: Iterable[str] = None) -> str:
@@ -130,22 +126,19 @@ def groups():
     _filter = get_filter(request.query, allow=PARAMS)
     try:
         with conn.cursor() as cur:
-            inner = get_product_rollup_statement(
-                filters,
-                having=SQL("c.name IS NULL")
-            )
+            inner = get_product_rollup_statement(filters)
             query = SQL("""
 SELECT
     "Products",
     "Categories",
-    COALESCE("Group", "Groups"||'') "Group"
+    COALESCE("group", "Groups"||'') "Group"
 FROM (
 {inner}
 ) q
+WHERE q.category IS NULL
 """).format(inner=inner)
-            data = DataFrame(list(get_data(cur, query))[:-1])
+            data = DataFrame(get_data(cur, inner)).dropna()
             data['$/unit'] = 1
-            data['group'] = data['Group']
             form = get_form(request.path.split('/')[-1], 'get', _filter, data)
             xml = cur.execute(SQL("""
 SELECT query_to_xml_and_xmlschema({q}, false, false, ''::text)
@@ -165,23 +158,19 @@ def categories():
     ])
     try:
         with conn.cursor() as cur:
-            inner = get_product_rollup_statement(
-                filters,
-                having=SQL("p.name IS NULL AND (c.name IS NOT NULL OR g.name IS NULL)")
-            )
+            inner = get_product_rollup_statement(filters)
             query = SQL("""
 SELECT
-    "Products",
-    COALESCE("Category", "Categories"||'') "Category",
-    COALESCE("Group", "Groups"||'') "Group"
+    "Products" "Product",
+    COALESCE("category", "Categories"||'') "Category",
+    COALESCE("group", "Groups"||'') "Group"
 FROM (
 {inner}
 ) q
+WHERE q.product IS NULL AND (q.category IS NOT NULL OR q.group IS NULL)
 """).format(inner=inner)
-            data = DataFrame(list(get_data(cur, query))[:-1])
+            data = DataFrame(get_data(cur, inner)).dropna()
             data['$/unit'] = 1
-            data['group'] = data['Group']
-            data['category'] = data['Category']
             form = get_form(request.path.split('/')[-1], 'get', _filter, data)
             xml = cur.execute(SQL("""
 SELECT query_to_xml_and_xmlschema({q}, false, false, ''::text)
@@ -201,25 +190,20 @@ def products():
     ])
     try:
         with conn.cursor() as cur:
-            inner = get_product_rollup_statement(
-                filters,
-                having=SQL("p.name IS NOT NULL OR g.name IS NULL")
-            )
+            inner = get_product_rollup_statement(filters)
             query = SQL("""
 SELECT
     --"Transactions",
-    COALESCE("Product", "Products"||'') "Product",
-    COALESCE("Category", "Categories"||'') "Category",
-    COALESCE("Group", "Groups"||'') "Group"
+    COALESCE("product", "Products"||'') "Product",
+    COALESCE("category", "Categories"||'') "Category",
+    COALESCE("group", "Groups"||'') "Group"
 FROM (
 {inner}
 ) q
+WHERE q.product IS NOT NULL OR q.group IS NULL
 """).format(inner=inner)
-            data = DataFrame(list(get_data(cur, query))[:-1])
+            data = DataFrame(get_data(cur, inner)).dropna()
             data['$/unit'] = 1
-            data['product'] = data['Product']
-            data['group'] = data['Group']
-            data['category'] = data['Category']
             form = get_form(request.path.split('/')[-1], 'get', _filter, data)
             xml = cur.execute(SQL("""
 SELECT query_to_xml_and_xmlschema({q}, false, false, ''::text)