diff --git a/web/finances.py b/web/finances.py index a0d5958..b8998b5 100755 --- a/web/finances.py +++ b/web/finances.py @@ -9,6 +9,8 @@ from dotenv import load_dotenv load_dotenv() +# cache get_finances() for 5 minutes + def get_finances(): return requests.get( url=f'{os.environ["CORE_API_URL"]}/finances', @@ -18,7 +20,6 @@ def get_finances(): timeout=5 ).json() - def register(app): @app.route('/finances') def finances_view(): @@ -28,6 +29,7 @@ def register(app): return flask.Response('Error fetching finances from database. This might be cause the data is still being processed. Sorry.', status=500) finances = { + 'last_update': data['timestamp'], 'donations_total': round(sum([i['amount_usd'] for i in data['donations']])), 'donations_num': len(data['donations']), 'donations_avg': round(sum([i['amount_usd'] for i in data['donations']]) / len(data['donations']), 2), @@ -43,8 +45,13 @@ def register(app): @app.route('/finances/json') def finances_download(): + try: + data = get_finances() + except Exception: + return flask.Response('Error fetching finances from database. This might be cause the data is still being processed. Sorry.', status=500) + virtual_file = io.StringIO() - json.dump(get_finances(), virtual_file, indent=4) + json.dump(data, virtual_file, indent=4) name = f'finances-{time.strftime("%Y-%m-%d")}.json' virtual_file.seek(0) diff --git a/web/templates/finances.html b/web/templates/finances.html index 76e9a1a..7436be9 100644 --- a/web/templates/finances.html +++ b/web/templates/finances.html @@ -3,7 +3,14 @@

Our finances - transparency is key!

- Warning - no guarantees can be made that this information is 100% accurate. Crypto currency prices are updated once every hour. + Warning - no guarantees can be made that this information is 100% accurate. + Crypto currency prices are updated once every hour. +
+ As of: Loading... +

@@ -15,8 +22,8 @@

Donations

- - + +