diff --git a/helpers/finances.py b/helpers/finances.py index b55111f..001ab35 100644 --- a/helpers/finances.py +++ b/helpers/finances.py @@ -1,12 +1,24 @@ from decimal import Decimal +from datetime import datetime -def get_latest_month(data): +def get_latest_month(data, allow_current=False): years = sorted(data.keys()) latest_year = years[-1] months = sorted(data[latest_year].keys()) latest_month = months[-1] + if ( + not allow_current + and latest_year == str(datetime.now().year) + and latest_month == str(datetime.now().month) + ): + try: + latest_month = months[-2] + except IndexError: + latest_year = years[-2] + latest_month = months[-1] + return int(latest_month), int(latest_year) diff --git a/main.py b/main.py index ae42d8f..3df6e1c 100644 --- a/main.py +++ b/main.py @@ -54,7 +54,9 @@ def catch_all(path): (pathlib.Path(__file__).parent / "data" / "finances.json").read_text() ) - finances_month, finances_year = get_latest_month(finances) + allow_current = app.development_mode + + finances_month, finances_year = get_latest_month(finances, allow_current) finances_period = datetime.date(finances_year, finances_month, 1) finances_period_str = finances_period.strftime("%B %Y") @@ -97,6 +99,25 @@ def catch_all(path): return "404 Not Found", 404 +@app.route("/_metrics/") +def metrics(): + finances = json.loads( + (pathlib.Path(__file__).parent / "data" / "finances.json").read_text() + ) + + balances = get_transparency_data(finances)["end_balance"] + + response = ( + "# HELP privatecoffee_balance The balance of the private.coffee account\n" + ) + response += "# TYPE privatecoffee_balance gauge\n" + + for currency, balance in balances.items(): + response += f'privatecoffee_balance{{currency="{currency}"}} {balance}\n' + + return response + + app.development_mode = False if os.environ.get("PRIVATECOFFEE_DEV"):