rewrite frontend and logic in core app
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
from datetime import timedelta
|
||||
from decimal import Decimal
|
||||
from typing import Iterable
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.utils import timezone
|
||||
|
||||
from core.models import Subject, Transaction
|
||||
|
||||
@@ -85,3 +88,28 @@ def predict_transactions(subject: Subject):
|
||||
"day_of_month": day_of_month,
|
||||
}
|
||||
return transactions, prediction_info
|
||||
|
||||
|
||||
def predict_all(subjects: Iterable[Subject], past_days=30, future_days=60):
|
||||
today = timezone.now().date()
|
||||
past_lookup_bound = today - timedelta(days=past_days)
|
||||
future_lookup_bound = today + timedelta(days=future_days)
|
||||
lookup_bounds = (past_lookup_bound, future_lookup_bound)
|
||||
|
||||
prediction_list = []
|
||||
for subject in subjects:
|
||||
transactions = list(
|
||||
subject.transactions.filter(booking_date__range=lookup_bounds).order_by("booking_date"))
|
||||
|
||||
predicted_transactions, prediction_info = predict_transactions(subject)
|
||||
if predicted_transactions:
|
||||
first_predicted_date = predicted_transactions[0].booking_date
|
||||
if first_predicted_date >= past_lookup_bound:
|
||||
# if two weeks after the first predicted transaction have passed, the subject is considered done
|
||||
for transaction in predicted_transactions:
|
||||
if past_lookup_bound <= transaction.booking_date <= future_lookup_bound:
|
||||
transactions.append(transaction)
|
||||
|
||||
prediction_list.append((subject, transactions, prediction_info))
|
||||
|
||||
return prediction_list
|
||||
|
||||
Reference in New Issue
Block a user