mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-07-10 14:52:42 +02:00
feat: improve networth calculations
This commit is contained in:
+54
-33
@@ -1,63 +1,84 @@
|
|||||||
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from apps.net_worth.utils.calculate_net_worth import (
|
from apps.net_worth.utils.calculate_net_worth import (
|
||||||
calculate_net_worth,
|
calculate_currency_net_worth,
|
||||||
calculate_historical_net_worth,
|
calculate_historical_currency_net_worth,
|
||||||
calculate_account_net_worth,
|
calculate_account_net_worth,
|
||||||
|
calculate_historical_account_balance,
|
||||||
)
|
)
|
||||||
from apps.currencies.models import Currency
|
from apps.currencies.models import Currency
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
def net_worth_main(request):
|
def net_worth_main(request):
|
||||||
net_worth = calculate_net_worth()
|
currency_net_worth = calculate_currency_net_worth()
|
||||||
detailed_net_worth = calculate_account_net_worth()
|
account_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)
|
|
||||||
|
|
||||||
print(detailed_net_worth)
|
historical_net_worth = calculate_historical_currency_net_worth()
|
||||||
# Format the net worth with currency details
|
|
||||||
formatted_net_worth = []
|
labels = list(historical_net_worth.keys())
|
||||||
for currency_code, amount in net_worth.items():
|
currencies = list(historical_net_worth[labels[0]].keys())
|
||||||
currency = Currency.objects.get(code=currency_code)
|
|
||||||
formatted_net_worth.append(
|
datasets = []
|
||||||
|
for i, currency in enumerate(currencies):
|
||||||
|
data = [
|
||||||
|
float(month_data[currency]) for month_data in historical_net_worth.values()
|
||||||
|
]
|
||||||
|
datasets.append(
|
||||||
{
|
{
|
||||||
"amount": amount,
|
"label": currency,
|
||||||
"code": currency.code,
|
"data": data,
|
||||||
"name": currency.name,
|
"yAxisID": f"y{i}",
|
||||||
"prefix": currency.prefix,
|
"fill": False,
|
||||||
"suffix": currency.suffix,
|
"tension": 0.1,
|
||||||
"decimal_places": currency.decimal_places,
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
end_date = timezone.now()
|
chart_data_currency = {"labels": labels, "datasets": datasets}
|
||||||
start_date = end_date - relativedelta(years=5) # Last year
|
|
||||||
|
|
||||||
# Calculate historical net worth
|
chart_data_currency_json = json.dumps(chart_data_currency, cls=DjangoJSONEncoder)
|
||||||
historical_data = calculate_historical_net_worth(start_date, end_date)
|
|
||||||
|
|
||||||
# Prepare data for the template
|
historical_account_balance = calculate_historical_account_balance()
|
||||||
currencies = Currency.objects.all()
|
|
||||||
print(historical_data)
|
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(
|
return render(
|
||||||
request,
|
request,
|
||||||
"net_worth/net_worth.html",
|
"net_worth/net_worth.html",
|
||||||
{
|
{
|
||||||
"currency_net_worth": formatted_net_worth,
|
"currency_net_worth": currency_net_worth.values(),
|
||||||
"account_net_worth": detailed_net_worth,
|
"account_net_worth": account_net_worth,
|
||||||
|
"chart_data_currency_json": chart_data_currency_json,
|
||||||
"currencies": currencies,
|
"currencies": currencies,
|
||||||
"historical_data_json": JsonResponse(historical_data).content.decode(
|
"chart_data_accounts_json": chart_data_accounts_json,
|
||||||
"utf-8"
|
"accounts": accounts,
|
||||||
),
|
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user