Browse Source

display errors

Daniel Sheffield 1 year ago
parent
commit
f836c9588b

+ 1 - 7
app/rest/PageCache.py

@@ -22,7 +22,6 @@ def delete_page(name: str, root: str = 'app/rest/static/files'):
 
 
 def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files') -> str:
 def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files') -> str:
     directory = f'{root}/{name}'
     directory = f'{root}/{name}'
-    cherrypy.log(f"saving file {directory}/{name}")
     try:
     try:
         os.mkdir(directory, mode=0o700, dir_fd=None)
         os.mkdir(directory, mode=0o700, dir_fd=None)
     except FileExistsError:
     except FileExistsError:
@@ -36,17 +35,14 @@ def save_page(name: str, content: bytes, tool: str, root='app/rest/static/files'
 def get_page(name: str, root: str = 'app/rest/static/files') -> str:
 def get_page(name: str, root: str = 'app/rest/static/files') -> str:
     directory = f'{root}/{name}'
     directory = f'{root}/{name}'
 
 
-    cherrypy.log(f"getting {name} from file")
     try:
     try:
         mtime = os.stat(f'{directory}/{name}.file').st_mtime
         mtime = os.stat(f'{directory}/{name}.file').st_mtime
     except:
     except:
         mtime = None
         mtime = None
     
     
     if mtime is None:
     if mtime is None:
-        cherrypy.log(f"no file")
         return None
         return None
-    cherrypy.log(f"mtime: {mtime}")
-    cherrypy.log(f"mtime stale: {mtime and time() - mtime > STALE}")
+    
     if mtime and time() - mtime > STALE:
     if mtime and time() - mtime > STALE:
         delete_page(name)
         delete_page(name)
         return None
         return None
@@ -105,9 +101,7 @@ class PageCache:
     def get(self, key: str) -> CachedLoadingPage:
     def get(self, key: str) -> CachedLoadingPage:
         key = key_to_hash(key)
         key = key_to_hash(key)
 
 
-        cherrypy.log(f"getting {key} from cache")
         if key not in self._cache:
         if key not in self._cache:
-            cherrypy.log(f"getting {key} from file")
             try:
             try:
                 existing = get_page(hash_to_base32(key))
                 existing = get_page(hash_to_base32(key))
             except:
             except:

+ 0 - 2
app/rest/QueryCache.py

@@ -8,7 +8,6 @@ from typing import Dict, Iterable, Tuple
 from urllib.parse import urlencode
 from urllib.parse import urlencode
 
 
 from bottle import FormsDict
 from bottle import FormsDict
-import cherrypy
 from ..data.filter import get_filter, get_query_param
 from ..data.filter import get_filter, get_query_param
 
 
 from . import BOOLEAN, PARAMS
 from . import BOOLEAN, PARAMS
@@ -114,7 +113,6 @@ class QueryCache:
 
 
     def get(self, key: str) -> str:
     def get(self, key: str) -> str:
         query, _hash = norm(key)
         query, _hash = norm(key)
-        cherrypy.log(f"{key}")
         if _hash not in self._cache:
         if _hash not in self._cache:
             if query:
             if query:
                 return self.add(_hash, query)
                 return self.add(_hash, query)

+ 2 - 5
app/rest/pyapi.py

@@ -4,15 +4,13 @@
 #
 #
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 # THIS SOFTWARE IS PROVIDED AS IS WITHOUT WARRANTY
 import os
 import os
-import cherrypy
 from threading import Thread
 from threading import Thread
-from typing import Tuple, Union
+from typing import Tuple
 from bottle import (
 from bottle import (
     route, request, response,
     route, request, response,
     static_file,
     static_file,
     FormsDict,
     FormsDict,
 )
 )
-import cherrypy
 from psycopg import Cursor, connect
 from psycopg import Cursor, connect
 from psycopg.rows import TupleRow
 from psycopg.rows import TupleRow
 from urllib.parse import parse_qs
 from urllib.parse import parse_qs
@@ -50,11 +48,10 @@ QUERY_CACHE = QueryCache(None)
 @cache(query_cache=QUERY_CACHE, page_cache=PAGE_CACHE)
 @cache(query_cache=QUERY_CACHE, page_cache=PAGE_CACHE)
 def trend(key: Tuple[str, int], cache: PageCache):
 def trend(key: Tuple[str, int], cache: PageCache):
     _, _, path, *_ = request.urlparts
     _, _, path, *_ = request.urlparts
-    cherrypy.log(f"key={key}")
+
     page = cache[key]
     page = cache[key]
     if page is None:
     if page is None:
         form = key_to_form(key)
         form = key_to_form(key)
-        cherrypy.log(f"form={form}")
         page = cache.add(key, CachedLoadingPage([], trend_thread(conn, path, form)))
         page = cache.add(key, CachedLoadingPage([], trend_thread(conn, path, form)))
     
     
     for i in iter_page(page):
     for i in iter_page(page):

+ 0 - 3
app/rest/route_decorators.py

@@ -6,7 +6,6 @@
 from typing import Callable, Iterable, Tuple
 from typing import Callable, Iterable, Tuple
 from urllib.parse import urlencode
 from urllib.parse import urlencode
 from bottle import request, FormsDict, redirect, HTTPError
 from bottle import request, FormsDict, redirect, HTTPError
-import cherrypy
 from psycopg import Connection
 from psycopg import Connection
 from psycopg.connection import TupleRow
 from psycopg.connection import TupleRow
 
 
@@ -56,8 +55,6 @@ def _cache_decorator(func: Callable, query_cache: QueryCache = None, page_cache:
                 cached = query_cache.add(key, None)
                 cached = query_cache.add(key, None)
             else:
             else:
                 return HTTPError(404, f"No query found for hash: {_hash}")
                 return HTTPError(404, f"No query found for hash: {_hash}")
-        
-        cherrypy.log(f"cached query={cached}")
 
 
         if not request.params.hash:
         if not request.params.hash:
             if cached and len(cached) > 2000:
             if cached and len(cached) > 2000:

+ 1 - 0
app/rest/templates/error-500.tpl

@@ -0,0 +1 @@
+<span style="font-size: 3em">{{ error }}</span>

+ 6 - 1
app/rest/templates/trend.tpl

@@ -1,5 +1,6 @@
 % setdefault("start", False)
 % setdefault("start", False)
 % setdefault("end", False)
 % setdefault("end", False)
+% setdefault("error", '')
 % if start:
 % if start:
 <html>
 <html>
   <head>
   <head>
@@ -58,7 +59,7 @@ div.progress:not(:has(+ .done)) {
 }
 }
     </style>
     </style>
   </head>
   </head>
-  <body align="center">
+  <body>
     <div class="loader-container">
     <div class="loader-container">
     <span class="loader"></span>
     <span class="loader"></span>
 % end
 % end
@@ -66,7 +67,11 @@ div.progress:not(:has(+ .done)) {
     </div>
     </div>
     <div class="done"></div>
     <div class="done"></div>
 {{!form}}
 {{!form}}
+    % if error:
+    % include('error-500', error=error)
+    % else:
 {{!svg}}
 {{!svg}}
+    % end
     </body>
     </body>
 </html>
 </html>
 % end
 % end

+ 15 - 11
app/rest/trend.py

@@ -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}")
-                return
+                abort(400, f"Unsupported unit: {unit}")
 
 
             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}")
-                return
+                abort(404, f"No data.")
             
             
             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:
-        yield abort(500, f"Failed to render page")
+    except HTTPError as e:
+        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()