|
@@ -20,7 +20,9 @@ from bottle import (
|
|
template,
|
|
template,
|
|
HTTPError,
|
|
HTTPError,
|
|
static_file,
|
|
static_file,
|
|
|
|
+ TEMPLATE_PATH,
|
|
)
|
|
)
|
|
|
|
+TEMPLATE_PATH.append("app/rest/templates")
|
|
from psycopg import connect
|
|
from psycopg import connect
|
|
from psycopg.sql import SQL, Literal
|
|
from psycopg.sql import SQL, Literal
|
|
import matplotlib.pyplot as plt
|
|
import matplotlib.pyplot as plt
|
|
@@ -158,10 +160,7 @@ def get_form(action, method, filter_data, data):
|
|
'group': 2,
|
|
'group': 2,
|
|
}[x])
|
|
}[x])
|
|
in_chart = data['$/unit'].apply(lambda x: (x or False) and True)
|
|
in_chart = data['$/unit'].apply(lambda x: (x or False) and True)
|
|
- return template(
|
|
|
|
- 'app/rest/templates/form',
|
|
|
|
- action=action,
|
|
|
|
- method=method,
|
|
|
|
|
|
+ return template('form', action=action, method=method,
|
|
**{
|
|
**{
|
|
k: {
|
|
k: {
|
|
"name": k,
|
|
"name": k,
|
|
@@ -224,7 +223,7 @@ def trend():
|
|
if request.query_string != normalized:
|
|
if request.query_string != normalized:
|
|
return redirect(f'{path}?{normalized}')
|
|
return redirect(f'{path}?{normalized}')
|
|
|
|
|
|
- loading = template("app/rest/templates/loading", progress=[])
|
|
|
|
|
|
+ loading = template("loading", progress=[])
|
|
if request.query_string in CACHE:
|
|
if request.query_string in CACHE:
|
|
if LOCK.acquire(block=False):
|
|
if LOCK.acquire(block=False):
|
|
try:
|
|
try:
|
|
@@ -263,37 +262,37 @@ def trend_internal(path, query):
|
|
raise abort(400, f"Unsupported unit {unit}")
|
|
raise abort(400, f"Unsupported unit {unit}")
|
|
|
|
|
|
progress.append({ "name": "Loading data", "status": ""})
|
|
progress.append({ "name": "Loading data", "status": ""})
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
data = get_data(query_manager, **fields)
|
|
data = get_data(query_manager, **fields)
|
|
|
|
|
|
progress[-1]["status"] = "done"
|
|
progress[-1]["status"] = "done"
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
|
|
|
|
if data.empty:
|
|
if data.empty:
|
|
raise abort(404, f"No data for {fields}")
|
|
raise abort(404, f"No data for {fields}")
|
|
|
|
|
|
progress.append({ "name": "Loading chart", "status": ""})
|
|
progress.append({ "name": "Loading chart", "status": ""})
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
pivot = data.pivot_table(index=['ts_raw',], columns=['product',], values=['$/unit'], aggfunc='mean')
|
|
pivot = data.pivot_table(index=['ts_raw',], columns=['product',], values=['$/unit'], aggfunc='mean')
|
|
pivot.columns = pivot.columns.droplevel()
|
|
pivot.columns = pivot.columns.droplevel()
|
|
plt.figure(facecolor='black', figsize=[16, 9])
|
|
plt.figure(facecolor='black', figsize=[16, 9])
|
|
line(pivot, xlabel='Time', ylabel=f'$ / {unit}')
|
|
line(pivot, xlabel='Time', ylabel=f'$ / {unit}')
|
|
|
|
|
|
progress[-1]["status"] = "done"
|
|
progress[-1]["status"] = "done"
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
|
|
|
|
progress.append({ "name": "Rendering chart", "status": ""})
|
|
progress.append({ "name": "Rendering chart", "status": ""})
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
|
|
|
|
f = StringIO()
|
|
f = StringIO()
|
|
plt.savefig(f, format='svg')
|
|
plt.savefig(f, format='svg')
|
|
form = get_form(path, 'get', get_filter(request.query, allow=PARAMS), data)
|
|
form = get_form(path, 'get', get_filter(request.query, allow=PARAMS), data)
|
|
|
|
|
|
progress[-1]["status"] = "done"
|
|
progress[-1]["status"] = "done"
|
|
- yield template("app/rest/templates/loading", progress=progress)
|
|
|
|
|
|
+ yield template("loading", progress=progress)
|
|
|
|
|
|
- final = template("app/rest/templates/trend", form=form, svg=f.getvalue())
|
|
|
|
- resp = lambda: template("app/rest/templates/trend", form=form, svg=f.getvalue())
|
|
|
|
|
|
+ final = template("trend", form=form, svg=f.getvalue())
|
|
|
|
+ resp = lambda: template("trend", form=form, svg=f.getvalue())
|
|
|
|
|
|
except HTTPError as e:
|
|
except HTTPError as e:
|
|
resp = lambda exception=e: exception
|
|
resp = lambda exception=e: exception
|
|
@@ -308,7 +307,7 @@ def table():
|
|
title = request.query['title'] if 'title' in request.query.keys() else None
|
|
title = request.query['title'] if 'title' in request.query.keys() else None
|
|
foot = request.query['foot'] == 'True' and "last()" if 'foot' in request.query.keys() else "-1"
|
|
foot = request.query['foot'] == 'True' and "last()" if 'foot' in request.query.keys() else "-1"
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
- return template("app/rest/templates/query-to-xml-xslt", title=title, foot=foot)
|
|
|
|
|
|
+ return template("query-to-xml-xslt", title=title, foot=foot)
|
|
|
|
|
|
@route('/grocery/groups')
|
|
@route('/grocery/groups')
|
|
def groups():
|
|
def groups():
|
|
@@ -331,12 +330,7 @@ FROM (
|
|
finally:
|
|
finally:
|
|
conn.commit()
|
|
conn.commit()
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
- return template(
|
|
|
|
- "app/rest/query-to-xml",
|
|
|
|
- title="Groups",
|
|
|
|
- foot=True,
|
|
|
|
- xml=xml
|
|
|
|
- )
|
|
|
|
|
|
+ return template("query-to-xml", title="Groups", foot=True, xml=xml)
|
|
|
|
|
|
@route('/grocery/categories')
|
|
@route('/grocery/categories')
|
|
def categories():
|
|
def categories():
|
|
@@ -359,12 +353,7 @@ FROM (
|
|
finally:
|
|
finally:
|
|
conn.commit()
|
|
conn.commit()
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
- return template(
|
|
|
|
- "app/rest/templates/query-to-xml",
|
|
|
|
- title="Categories",
|
|
|
|
- foot=True,
|
|
|
|
- xml=xml
|
|
|
|
- )
|
|
|
|
|
|
+ return template("query-to-xml", title="Categories", foot=True, xml=xml)
|
|
|
|
|
|
@route('/grocery/products')
|
|
@route('/grocery/products')
|
|
def products():
|
|
def products():
|
|
@@ -388,12 +377,7 @@ FROM (
|
|
finally:
|
|
finally:
|
|
conn.commit()
|
|
conn.commit()
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
- return template(
|
|
|
|
- "app/rest/templates/query-to-xml",
|
|
|
|
- title="Products",
|
|
|
|
- foot=True,
|
|
|
|
- xml=xml
|
|
|
|
- )
|
|
|
|
|
|
+ return template("query-to-xml", title="Products", foot=True, xml=xml)
|
|
|
|
|
|
@route('/grocery/tags')
|
|
@route('/grocery/tags')
|
|
def tags():
|
|
def tags():
|
|
@@ -413,11 +397,6 @@ SELECT query_to_xml_and_xmlschema({inner}, false, false, ''::text)
|
|
finally:
|
|
finally:
|
|
conn.commit()
|
|
conn.commit()
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
response.content_type = 'application/xhtml+xml; charset=utf-8'
|
|
- return template(
|
|
|
|
- "app/rest/templates/query-to-xml",
|
|
|
|
- title="Tags",
|
|
|
|
- foot="false",
|
|
|
|
- xml=xml
|
|
|
|
- )
|
|
|
|
|
|
+ return template("query-to-xml", title="Tags", foot="false", xml=xml)
|
|
|
|
|
|
run(host='0.0.0.0', port=6772, server='gunicorn')
|
|
run(host='0.0.0.0', port=6772, server='gunicorn')
|