feat: add metrics endpoint and improve month handling
Added a new /_metrics/ endpoint to expose financial data in Prometheus format, facilitating better monitoring and analytics. Enhanced the get_latest_month function to optionally exclude the current month, preventing potential issues in incomplete data scenarios, with this behavior being toggled by the application's development mode status.
This commit is contained in:
parent
ac5b16d2f7
commit
2357427d96
2 changed files with 35 additions and 2 deletions
23
main.py
23
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"):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue