firefly-exporter/firefly/accounts.py

50 lines
1.5 KiB
Python
Raw Normal View History

2020-10-25 20:02:18 +00:00
"""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'))
2020-10-25 20:13:36 +00:00
return (
asset_account for asset_account in accounts
if asset_account.get('attributes').get('type') == 'asset'
)
2020-10-25 20:02:18 +00:00
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