Skip to content

Commit e03e26a

Browse files
committed
WIP consolidation task - take budget authorisation into account
1 parent 6f924e8 commit e03e26a

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

app/grandchallenge/challenges/tasks.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ def update_challenge_compute_costs():
113113
invoices = list(
114114
Invoice.objects.filter(challenge_id=challenge_id).order_by(
115115
"expires_on", "created"
116-
) # Todo: add queryset-level filter to only return those invoices with authorized budget
116+
)
117117
)
118118

119+
# TODO: maybe push remainder to challenge-level compute costs to cover if there are NO valid invoices? I.e. a deficit?
119120
overall_covered_compute_costs = 0
120121
last_index = len(invoices) - 1
121122
for idx, invoice in enumerate(invoices):
@@ -133,9 +134,13 @@ def update_challenge_compute_costs():
133134

134135
if not is_last:
135136
# Limit to authorized budget for all but the last invoice
137+
authorized_budget_compute_costs = (
138+
invoice.compute_costs_euros * 1000 * 100
139+
if invoice.is_budget_authorized
140+
else 0
141+
)
136142
covered_compute_costs = min(
137-
covered_compute_costs,
138-
invoice.compute_costs_euros * 1000 * 100,
143+
covered_compute_costs, authorized_budget_compute_costs
139144
)
140145

141146
invoice.compute_cost_euro_millicents = covered_compute_costs

app/tests/challenges_tests/test_tasks.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,5 +586,20 @@ def test_update_challenge_compute_costs_expired_utilization():
586586
invoice_later_this_year.compute_cost_euro_millicents == 70 * 1000 * 100
587587
)
588588

589+
# Cancel invoice_last_year and check costs are reallocated to invoice_later_this_year
590+
invoice_last_year.payment_status = PaymentStatusChoices.CANCELLED
591+
assert not invoice_last_year.is_budget_authorized
592+
invoice_last_year.save()
589593

590-
# TODO: add test that checks if we filter on authorized budget when calculating compute costs for invoices
594+
update_challenge_compute_costs()
595+
invoice_last_year.refresh_from_db()
596+
invoice_this_year.refresh_from_db()
597+
invoice_later_this_year.refresh_from_db()
598+
599+
assert invoice_last_year.compute_cost_euro_millicents == 0
600+
assert ( # Fully utilized
601+
invoice_this_year.compute_cost_euro_millicents == 20 * 1000 * 100
602+
)
603+
assert ( # Utilization from earlier bubble up to this one
604+
invoice_later_this_year.compute_cost_euro_millicents == 80 * 1000 * 100
605+
)

0 commit comments

Comments
 (0)