firefly-exporter/firefly/accounts.py
2020-10-25 21:13:36 +01:00

49 lines
1.5 KiB
Python

"""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'))
return (
asset_account for asset_account in accounts
if asset_account.get('attributes').get('type') == 'asset'
)
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