firefly-exporter/firefly/budgets.py

96 lines
3.2 KiB
Python
Raw Normal View History

2020-10-25 16:49:59 +00:00
"""Expose budget 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_budgets():
budgets_json = requests.get(
host + "/api/v1/budgets", headers=header
).json()
budgets = budgets_json.get('data')
while "next" in budgets_json.get('links'):
budgets_json = requests.get(
budgets_json.get('links').get('next'), headers=header
).json()
budgets.extend(budgets_json.get('data'))
return budgets
def _get_current_limit(budget_id):
budgets_json = requests.get(
host + "/api/v1/budgets/{}/limits".format(budget_id), headers=header
).json()
budgets = budgets_json.get('data')
while "next" in budgets_json.get('links'):
budget_json = requests.get(
budgets_json.get('links').get('next'), headers=header
).json()
budgets.extend(budget_json.get('data'))
2020-10-31 10:33:31 +00:00
today = datetime.datetime.combine(datetime.date.today(), datetime.datetime.min.time())
2020-10-25 20:13:36 +00:00
return [
budget for budget in budgets
2020-11-01 15:53:43 +00:00
if today >= dateutil.parser.parse(budget.get('attributes').get('start')) and today <= dateutil.parser.parse(budget.get('attributes').get('end'))
2020-10-25 20:13:36 +00:00
][0]
2020-10-25 16:49:59 +00:00
def _get_current_transactions(budget_id):
limit_id = _get_current_limit(budget_id).get('id')
transactions_json = requests.get(
host + "/api/v1/budgets/limits/{}/transactions".format(
limit_id
), headers=header
).json()
transactions = transactions_json.get('data')
while 'next' in transactions_json:
transactions_json = requests.get(
transactions_json.get('links').get('next'), headers=header
).json()
transactions.extend(transactions_json.get('data'))
return transactions
def _get_current_spent_amount(budget_id):
transactions = _get_current_transactions(budget_id)
spent_amount = 0.0
for transaction in transactions:
for detail in transaction.get('attributes').get('transactions'):
if detail.get('type') == "withdrawal":
spent_amount += float(detail.get('amount'))
elif detail.get('type') == "deposit":
spent_amount -= float(detail.get('amount'))
2020-10-25 16:49:59 +00:00
return spent_amount
def get_budget_metrics():
out = ""
for budget in _get_budgets():
budget_id = budget.get('id')
budget_name = budget.get('attributes').get('name')
budget_limit = _get_current_limit(budget_id).get('attributes').get('amount')
budget_spent = str(_get_current_spent_amount(budget.get('id')))
out += 'firefly_budget_limit{{budget_id="{}",budget_name="{}",year="{}",month="{}"}} {}\n'.format(
budget_id,
budget_name,
datetime.datetime.today().year,
datetime.datetime.today().month,
budget_limit,
)
out += 'firefly_budget_spent{{budget_id="{}",budget_name="{}",year="{}",month="{}"}} {}\n'.format(
budget_id,
budget_name,
datetime.datetime.today().year,
datetime.datetime.today().month,
budget_spent,
)
return out