sla.go
1 package components 2 3 type SLA struct { 4 Target float64 5 Current float64 6 ErrorBudget int 7 RemainingErrors int 8 Total int 9 Caught int 10 Lost int 11 } 12 13 func NewSLA(target float64, errorBudget int) *SLA { 14 return &SLA{ 15 Target: target, 16 Current: 100.0, 17 ErrorBudget: errorBudget, 18 RemainingErrors: errorBudget, 19 } 20 } 21 22 // GetType implements Component interface 23 func (s *SLA) GetType() string { 24 return "SLA" 25 } 26 27 // GetCurrent implements SLAComponent interface 28 func (s *SLA) GetCurrent() float64 { 29 return s.Current 30 } 31 32 // GetTarget implements SLAComponent interface 33 func (s *SLA) GetTarget() float64 { 34 return s.Target 35 } 36 37 // GetErrorsRemaining implements SLAComponent interface 38 func (s *SLA) GetErrorsRemaining() int { 39 return s.RemainingErrors 40 } 41 42 // SetCurrent implements SLAComponent interface 43 func (s *SLA) SetCurrent(current float64) { 44 s.Current = current 45 } 46 47 // SetErrorsRemaining implements SLAComponent interface 48 func (s *SLA) SetErrorsRemaining(errors int) { 49 s.RemainingErrors = errors 50 } 51 52 // Added helpers for ECS-stateless systems to update counters on the component 53 54 // GetTotals returns total, caught, lost counters 55 func (s *SLA) GetTotals() (int, int, int) { 56 return s.Total, s.Caught, s.Lost 57 } 58 59 // SetTarget updates the SLA target percentage 60 func (s *SLA) SetTarget(target float64) { 61 s.Target = target 62 } 63 64 // SetErrorBudget updates the allowed error budget and resets remaining accordingly 65 func (s *SLA) SetErrorBudget(budget int) { 66 s.ErrorBudget = budget 67 if s.RemainingErrors > budget { 68 s.RemainingErrors = budget 69 } 70 } 71 72 // IncrementCaught increments caught and total counters 73 func (s *SLA) IncrementCaught() { 74 s.Caught++ 75 s.Total++ 76 } 77 78 // IncrementLost increments lost and total counters and decreases remaining 79 func (s *SLA) IncrementLost() { 80 s.Lost++ 81 s.Total++ 82 s.RemainingErrors = s.ErrorBudget - s.Lost 83 if s.RemainingErrors < 0 { 84 s.RemainingErrors = 0 85 } 86 } 87 88 // ResetCounters clears derived counters but preserves configuration 89 func (s *SLA) ResetCounters() { 90 s.Total = 0 91 s.Caught = 0 92 s.Lost = 0 93 s.Current = 100.0 94 s.RemainingErrors = s.ErrorBudget 95 }