EvaluateGuardrail compares the computed totals against the configured thresholds and returns a verdict. The monthly total is parsed from totals.MonthlyTotal (the same string that is printed), so the judged number always matches the displayed number.
(totals JSONTotals, cfg GuardrailConfig)
| 46 | // totals.MonthlyTotal (the same string that is printed), so the judged number |
| 47 | // always matches the displayed number. |
| 48 | func EvaluateGuardrail(totals JSONTotals, cfg GuardrailConfig) GuardrailResult { |
| 49 | monthly, _ := strconv.ParseFloat(totals.MonthlyTotal, 64) |
| 50 | |
| 51 | res := GuardrailResult{ |
| 52 | Passed: true, |
| 53 | MonthlyTotal: monthly, |
| 54 | Budget: cfg.Budget, |
| 55 | Baseline: cfg.Baseline, |
| 56 | } |
| 57 | |
| 58 | // Absolute budget cap. |
| 59 | if cfg.Budget != nil && monthly > *cfg.Budget { |
| 60 | res.Passed = false |
| 61 | res.Breaches = append(res.Breaches, |
| 62 | fmt.Sprintf("monthly cost $%.2f exceeds budget $%.2f", monthly, *cfg.Budget)) |
| 63 | } |
| 64 | |
| 65 | // Relative checks against the baseline. |
| 66 | if cfg.Baseline != nil { |
| 67 | baseline := *cfg.Baseline |
| 68 | delta := monthly - baseline |
| 69 | res.Delta = &delta |
| 70 | |
| 71 | var pct float64 |
| 72 | if baseline != 0 { |
| 73 | pct = delta / baseline * 100 |
| 74 | res.DeltaPct = &pct |
| 75 | } |
| 76 | |
| 77 | if cfg.MaxIncrease != nil && delta > *cfg.MaxIncrease { |
| 78 | res.Passed = false |
| 79 | res.Breaches = append(res.Breaches, |
| 80 | fmt.Sprintf("cost increase $%.2f exceeds max increase $%.2f (baseline $%.2f → $%.2f)", |
| 81 | delta, *cfg.MaxIncrease, baseline, monthly)) |
| 82 | } |
| 83 | |
| 84 | if cfg.MaxIncreasePct != nil { |
| 85 | switch { |
| 86 | case baseline == 0 && delta > 0: |
| 87 | // Any positive increase from a zero baseline is an infinite percentage. |
| 88 | res.Passed = false |
| 89 | res.Breaches = append(res.Breaches, |
| 90 | fmt.Sprintf("cost increased from $0.00 to $%.2f, exceeding max increase of %.1f%%", |
| 91 | monthly, *cfg.MaxIncreasePct)) |
| 92 | case baseline != 0 && pct > *cfg.MaxIncreasePct: |
| 93 | res.Passed = false |
| 94 | res.Breaches = append(res.Breaches, |
| 95 | fmt.Sprintf("cost increase %.1f%% exceeds max increase %.1f%% (baseline $%.2f → $%.2f)", |
| 96 | pct, *cfg.MaxIncreasePct, baseline, monthly)) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | return res |
| 102 | } |