applyModelSelector finds the best matching PriceEntry for the given selector, marks it as IsCurrent (and clears all others), and returns the effective hourly rate (with upfront amortized). Returns the original OnDemand rate and isAmortized=false when no match is found.
(entries []resources.PriceEntry, sel ModelSelector)
| 150 | // hourly rate (with upfront amortized). Returns the original OnDemand rate |
| 151 | // and isAmortized=false when no match is found. |
| 152 | func applyModelSelector(entries []resources.PriceEntry, sel ModelSelector) (selected []resources.PriceEntry, effectiveRate decimal.Decimal, isAmortized bool) { |
| 153 | // Find all candidates that match the selector. |
| 154 | type candidate struct { |
| 155 | idx int |
| 156 | rate decimal.Decimal |
| 157 | amrt bool |
| 158 | } |
| 159 | var candidates []candidate |
| 160 | |
| 161 | for i, e := range entries { |
| 162 | if !strings.EqualFold(strings.TrimSpace(e.Model), strings.TrimSpace(sel.Model)) { |
| 163 | continue |
| 164 | } |
| 165 | if sel.PurchaseOption != "" && !strings.EqualFold(strings.TrimSpace(e.PurchaseOption), strings.TrimSpace(sel.PurchaseOption)) { |
| 166 | continue |
| 167 | } |
| 168 | if sel.Term != "" && !strings.EqualFold(strings.TrimSpace(e.Term), strings.TrimSpace(sel.Term)) { |
| 169 | continue |
| 170 | } |
| 171 | r, amrt := amortizedHourlyRate(e) |
| 172 | candidates = append(candidates, candidate{i, r, amrt}) |
| 173 | } |
| 174 | |
| 175 | if len(candidates) == 0 { |
| 176 | // No match — keep original IsCurrent flags, return OnDemand rate. |
| 177 | for _, e := range entries { |
| 178 | if e.IsCurrent { |
| 179 | r, amrt := amortizedHourlyRate(e) |
| 180 | return entries, r, amrt |
| 181 | } |
| 182 | } |
| 183 | return entries, decimal.Zero, false |
| 184 | } |
| 185 | |
| 186 | // Pick the candidate with the lowest effective rate. |
| 187 | best := candidates[0] |
| 188 | for _, c := range candidates[1:] { |
| 189 | if c.rate.LessThan(best.rate) { |
| 190 | best = c |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Update IsCurrent flags. |
| 195 | result := make([]resources.PriceEntry, len(entries)) |
| 196 | copy(result, entries) |
| 197 | for i := range result { |
| 198 | result[i].IsCurrent = (i == best.idx) |
| 199 | } |
| 200 | |
| 201 | return result, best.rate, best.amrt |
| 202 | } |
| 203 | |
| 204 | // EstimateAllResources estimates costs for all resources. |
| 205 | // usageMap maps resource name → monthly quantity for usage-based resources. |
no test coverage detected