|
@@ -13,6 +13,7 @@ from bottle import (
|
|
)
|
|
)
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib
|
|
import matplotlib
|
|
|
|
+from pandas import DataFrame
|
|
import seaborn as sns
|
|
import seaborn as sns
|
|
from psycopg import Connection
|
|
from psycopg import Connection
|
|
from psycopg.connection import TupleRow
|
|
from psycopg.connection import TupleRow
|
|
@@ -36,7 +37,7 @@ from .form import(
|
|
matplotlib.use('agg')
|
|
matplotlib.use('agg')
|
|
|
|
|
|
def abort(code, text):
|
|
def abort(code, text):
|
|
- return HTTPError(code, text)
|
|
+ raise HTTPError(code, text)
|
|
|
|
|
|
def trend(queue: Queue, conn: Connection[TupleRow], path: str, query: FormsDict):
|
|
def trend(queue: Queue, conn: Connection[TupleRow], path: str, query: FormsDict):
|
|
for item in trend_internal(conn, path, query):
|
|
for item in trend_internal(conn, path, query):
|
|
@@ -48,10 +49,13 @@ def trend_internal(conn: Connection[TupleRow], path: str, query: FormsDict):
|
|
'stage': None,
|
|
'stage': None,
|
|
'percent': None,
|
|
'percent': None,
|
|
}
|
|
}
|
|
|
|
+ action = path.split('/')[-1]
|
|
|
|
+ organic = BOOLEAN.get(query.organic, None)
|
|
|
|
+ _filter = get_filter(query, allow=PARAMS)
|
|
|
|
+ yield template("trend", start=True)
|
|
try:
|
|
try:
|
|
with conn.cursor() as cur:
|
|
with conn.cursor() as cur:
|
|
query_manager = QueryManager(cur, display_mapper)
|
|
query_manager = QueryManager(cur, display_mapper)
|
|
- _filter = get_filter(query, allow=PARAMS)
|
|
|
|
fields = {
|
|
fields = {
|
|
k: get_query_param(*_filter[k])
|
|
k: get_query_param(*_filter[k])
|
|
for k in sorted(_filter) if k not in ('organic', 'unit') and _filter[k]
|
|
for k in sorted(_filter) if k not in ('organic', 'unit') and _filter[k]
|
|
@@ -59,16 +63,14 @@ def trend_internal(conn: Connection[TupleRow], path: str, query: FormsDict):
|
|
unit = fields['unit'] = query.unit or 'kg'
|
|
unit = fields['unit'] = query.unit or 'kg'
|
|
fields['organic'] = BOOLEAN.get(query.organic, None)
|
|
fields['organic'] = BOOLEAN.get(query.organic, None)
|
|
if unit and unit not in ALL_UNITS:
|
|
if unit and unit not in ALL_UNITS:
|
|
- yield abort(400, f"Unsupported unit {unit}")
|
|
+ abort(400, f"Unsupported unit: {unit}")
|
|
- return
|
|
|
|
|
|
|
|
progress.update({ "stage": "Querying database", "percent": "10"})
|
|
progress.update({ "stage": "Querying database", "percent": "10"})
|
|
- yield template("trend", start=True) + template("progress", **progress)
|
|
+ yield template("done") + template("progress", **progress)
|
|
data = get_data(query_manager, **fields)
|
|
data = get_data(query_manager, **fields)
|
|
|
|
|
|
if data.empty:
|
|
if data.empty:
|
|
- yield abort(404, f"No data for {fields}")
|
|
+ abort(404, f"No data.")
|
|
- return
|
|
|
|
|
|
|
|
progress.update({ "stage": "Preparing data", "percent": "30"})
|
|
progress.update({ "stage": "Preparing data", "percent": "30"})
|
|
yield template("done") + template("progress", **progress)
|
|
yield template("done") + template("progress", **progress)
|
|
@@ -124,14 +126,16 @@ def trend_internal(conn: Connection[TupleRow], path: str, query: FormsDict):
|
|
progress.update({ "stage": "Done", "percent": "100" })
|
|
progress.update({ "stage": "Done", "percent": "100" })
|
|
yield template("done") + template("progress", **progress)
|
|
yield template("done") + template("progress", **progress)
|
|
|
|
|
|
- organic = BOOLEAN.get(query.organic, None)
|
|
|
|
- action = path.split('/')[-1]
|
|
|
|
form = get_form(action, 'post', _filter, organic, data)
|
|
form = get_form(action, 'post', _filter, organic, data)
|
|
|
|
|
|
yield template("trend", end=True, form=form, svg=f.getvalue())
|
|
yield template("trend", end=True, form=form, svg=f.getvalue())
|
|
|
|
|
|
- except:
|
|
+ except HTTPError as e:
|
|
- yield abort(500, f"Failed to render page")
|
|
+ if 'data' not in locals():
|
|
|
|
+ data = DataFrame()
|
|
|
|
+ if 'form' not in locals():
|
|
|
|
+ form = get_form(action, 'post', _filter, organic, data)
|
|
|
|
+ yield template("done") + template("trend", end=True, form=form, error=e.body)
|
|
|
|
|
|
finally:
|
|
finally:
|
|
conn.commit()
|
|
conn.commit()
|