calcUsageMonthlyCost computes the monthly cost for a usage-based resource given a monthly quantity. It uses the OnDemand entry's tiers when available, otherwise falls back to the flat rate.
(entries []resources.PriceEntry, monthlyQty float64)
| 694 | // given a monthly quantity. It uses the OnDemand entry's tiers when available, |
| 695 | // otherwise falls back to the flat rate. |
| 696 | func calcUsageMonthlyCost(entries []resources.PriceEntry, monthlyQty float64) decimal.Decimal { |
| 697 | // Find the OnDemand entry. |
| 698 | var target *resources.PriceEntry |
| 699 | for i := range entries { |
| 700 | if entries[i].IsCurrent { |
| 701 | target = &entries[i] |
| 702 | break |
| 703 | } |
| 704 | } |
| 705 | if target == nil && len(entries) > 0 { |
| 706 | target = &entries[0] |
| 707 | } |
| 708 | if target == nil { |
| 709 | return decimal.Zero |
| 710 | } |
| 711 | |
| 712 | qty := decimal.NewFromFloat(monthlyQty) |
| 713 | |
| 714 | if len(target.Tiers) == 0 { |
| 715 | // Flat rate: price per unit × quantity. |
| 716 | return target.RatePerHr.Mul(qty) |
| 717 | } |
| 718 | |
| 719 | // Tiered pricing: walk through tiers and accumulate cost. |
| 720 | return calcTieredCost(target.Tiers, qty) |
| 721 | } |
| 722 | |
| 723 | // calcTieredCost applies volume-tiered pricing to a total quantity. |
| 724 | // Each tier covers [startRange, endRange) units at its price. |
no test coverage detected