Skip to content

Commit 6f924e8

Browse files
committed
WIP consolidation task - small fixes
1 parent a0565a0 commit 6f924e8

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

app/grandchallenge/challenges/costs.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ def get_compute_costs_for_invoice(*, invoice, limit_to_covered_period):
6161
item["compute_cost_euro_millicents__sum"] or 0 for item in items
6262
)
6363

64-
if limit_to_covered_period:
65-
total = min(total, invoice.compute_costs_euros * 1000 * 100)
66-
6764
return total
6865

6966

app/grandchallenge/challenges/tasks.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,32 @@ def update_challenge_compute_costs():
116116
) # Todo: add queryset-level filter to only return those invoices with authorized budget
117117
)
118118

119-
covered_compute_costs = 0
119+
overall_covered_compute_costs = 0
120120
last_index = len(invoices) - 1
121121
for idx, invoice in enumerate(invoices):
122122
is_last = idx == last_index
123+
123124
compute_costs = get_compute_costs_for_invoice(
124125
invoice=invoice,
125126
limit_to_covered_period=not is_last,
126127
)
127-
invoice.compute_cost_euro_millicents = (
128-
compute_costs - covered_compute_costs
128+
129+
# Take into account earlier covered compute costs
130+
covered_compute_costs = (
131+
compute_costs - overall_covered_compute_costs
129132
)
130-
covered_compute_costs = compute_costs
133+
134+
if not is_last:
135+
# Limit to authorized budget for all but the last invoice
136+
covered_compute_costs = min(
137+
covered_compute_costs,
138+
invoice.compute_costs_euros * 1000 * 100,
139+
)
140+
141+
invoice.compute_cost_euro_millicents = covered_compute_costs
142+
143+
# Add newly covered compute costs
144+
overall_covered_compute_costs += covered_compute_costs
131145

132146
@retry_with_backoff((LockNotAvailable,))
133147
def save_invoices():

app/tests/challenges_tests/test_tasks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def test_update_challenge_compute_costs_expired_utilization():
543543
compute_costs_euros=20,
544544
payment_status=PaymentStatusChoices.PAID,
545545
payment_type=PaymentTypeChoices.PREPAID,
546-
expires_on=now().date(),
546+
expires_on=now().date() - timedelta(days=1),
547547
)
548548

549549
invoice_later_this_year = InvoiceFactory(
@@ -560,7 +560,7 @@ def test_update_challenge_compute_costs_expired_utilization():
560560
)
561561
job_utilization = job_last_year.job_utilization
562562
job_utilization.challenge = challenge
563-
job_utilization.created = now() - timedelta(days=365)
563+
job_utilization.created = now() - timedelta(days=400)
564564
job_utilization.compute_cost_euro_millicents = (
565565
100 * 1000 * 100 # Note: Huge
566566
)
@@ -573,6 +573,9 @@ def test_update_challenge_compute_costs_expired_utilization():
573573
update_challenge_compute_costs()
574574

575575
invoice_last_year.refresh_from_db()
576+
invoice_this_year.refresh_from_db()
577+
invoice_later_this_year.refresh_from_db()
578+
576579
assert ( # Fully utilized
577580
invoice_last_year.compute_cost_euro_millicents == 10 * 1000 * 100
578581
)

0 commit comments

Comments
 (0)