diff --git a/app/apps/net_worth/views.py b/app/apps/net_worth/views.py index ed92d8e..66598d2 100644 --- a/app/apps/net_worth/views.py +++ b/app/apps/net_worth/views.py @@ -1,63 +1,84 @@ +import json from datetime import datetime from dateutil.relativedelta import relativedelta +from django.core.serializers.json import DjangoJSONEncoder from django.http import JsonResponse from django.shortcuts import render from django.utils import timezone from apps.net_worth.utils.calculate_net_worth import ( - calculate_net_worth, - calculate_historical_net_worth, + calculate_currency_net_worth, + calculate_historical_currency_net_worth, calculate_account_net_worth, + calculate_historical_account_balance, ) from apps.currencies.models import Currency # Create your views here. def net_worth_main(request): - net_worth = calculate_net_worth() - detailed_net_worth = calculate_account_net_worth() - # historical = calculate_historical_net_worth( - # start_date=datetime(day=1, month=1, year=2021).date(), - # end_date=datetime(day=1, month=1, year=2025).date(), - # ) - # print(historical) + currency_net_worth = calculate_currency_net_worth() + account_net_worth = calculate_account_net_worth() - print(detailed_net_worth) - # Format the net worth with currency details - formatted_net_worth = [] - for currency_code, amount in net_worth.items(): - currency = Currency.objects.get(code=currency_code) - formatted_net_worth.append( + historical_net_worth = calculate_historical_currency_net_worth() + + labels = list(historical_net_worth.keys()) + currencies = list(historical_net_worth[labels[0]].keys()) + + datasets = [] + for i, currency in enumerate(currencies): + data = [ + float(month_data[currency]) for month_data in historical_net_worth.values() + ] + datasets.append( { - "amount": amount, - "code": currency.code, - "name": currency.name, - "prefix": currency.prefix, - "suffix": currency.suffix, - "decimal_places": currency.decimal_places, + "label": currency, + "data": data, + "yAxisID": f"y{i}", + "fill": False, + "tension": 0.1, } ) - end_date = timezone.now() - start_date = end_date - relativedelta(years=5) # Last year + chart_data_currency = {"labels": labels, "datasets": datasets} - # Calculate historical net worth - historical_data = calculate_historical_net_worth(start_date, end_date) + chart_data_currency_json = json.dumps(chart_data_currency, cls=DjangoJSONEncoder) - # Prepare data for the template - currencies = Currency.objects.all() - print(historical_data) + historical_account_balance = calculate_historical_account_balance() + + labels = list(historical_account_balance.keys()) + accounts = list(historical_account_balance[labels[0]].keys()) + + datasets = [] + for i, account in enumerate(accounts): + data = [ + float(month_data[account]) + for month_data in historical_account_balance.values() + ] + datasets.append( + { + "label": account, + "data": data, + "fill": False, + "tension": 0.1, + "yAxisID": f"y-axis-{i}", # Assign each dataset to its own Y-axis + } + ) + + chart_data_accounts = {"labels": labels, "datasets": datasets} + + chart_data_accounts_json = json.dumps(chart_data_accounts, cls=DjangoJSONEncoder) return render( request, "net_worth/net_worth.html", { - "currency_net_worth": formatted_net_worth, - "account_net_worth": detailed_net_worth, + "currency_net_worth": currency_net_worth.values(), + "account_net_worth": account_net_worth, + "chart_data_currency_json": chart_data_currency_json, "currencies": currencies, - "historical_data_json": JsonResponse(historical_data).content.decode( - "utf-8" - ), + "chart_data_accounts_json": chart_data_accounts_json, + "accounts": accounts, }, )