File tree Expand file tree Collapse file tree
grandchallenge/challenges Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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+ )
You can’t perform that action at this time.
0 commit comments