support asset account balances

This commit is contained in:
chris 2020-10-25 21:02:18 +01:00
parent 970a508ad9
commit a9de27fdef
2 changed files with 52 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import os
import sys
import firefly.budgets
import firefly.accounts
from flask import Flask
@ -18,4 +19,4 @@ app = Flask(__name__)
@app.route('/metrics')
def metric():
return firefly.budgets.get_budget_metrics()
return firefly.budgets.get_budget_metrics() + firefly.accounts.get_account_metrics()

50
firefly/accounts.py Normal file
View file

@ -0,0 +1,50 @@
"""Expose account metrics."""
import os
import calendar
import datetime
import requests
import dateutil.parser
header = {"Authorization": "Bearer " + os.environ.get('FIREFLY_PERSONAL_ACCESS_TOKEN')}
host = os.environ.get('FIREFLY_API_HOST')
def _get_asset_accounts():
accounts_json = requests.get(
host + "/api/v1/accounts", headers=header
).json()
accounts = accounts_json.get('data')
while "next" in accounts_json.get('links'):
accounts_json = requests.get(
accounts_json.get('links').get('next'), headers=header
).json()
accounts.extend(accounts_json.get('data'))
asset_accounts = []
for account in accounts:
if account.get('attributes').get('type') == "asset":
asset_accounts.append(account)
return asset_accounts
def get_account_metrics():
out = ""
for asset_account in _get_asset_accounts():
account_id = asset_account.get('id')
account_name = asset_account.get('attributes').get('name')
account_balance = asset_account.get('attributes').get('current_balance')
account_balance_date = int(
dateutil.parser.parse(
asset_account.get('attributes').get('current_balance_date')
).timestamp()
)
out += 'firefly_asset_account_balance{{account_id="{}",account_name="{}",balance_date="{}"}} {}\n'.format(
account_id,
account_name,
account_balance_date,
account_balance,
)
return out