@@ -28,10 +28,13 @@ const (
2828)
2929
3030type ListSLOsRequest struct {
31- FilterServiceID string // Used for filtering SLOs by service ID.
32- FilterSearchInput string // Used for searching SLOs by name.
33- SortMode SLOListSortMode
34- Cursor string
31+ FilterServiceID string // Used for filtering SLOs by service ID.
32+ FilterSearchInput string // Used for searching SLOs by name.
33+ FilterAlertFiring bool // Used for filtering SLOs that have firing alerts.
34+ FilterPeriodBudgetConsumed bool // Used for filtering SLOs that have burned budget above threshold.
35+ FilterCurrentBurningBudgetOver100 bool // Used for filtering SLOs that are currently burning budget over 100%.
36+ SortMode SLOListSortMode
37+ Cursor string
3538}
3639
3740func (r * ListSLOsRequest ) defaults () error {
@@ -80,72 +83,99 @@ func (a *App) ListSLOs(ctx context.Context, req ListSLOsRequest) (*ListSLOsRespo
8083 }
8184 }
8285
86+ // Filter SLOs if required.
87+ filters := []sloFilter {}
88+ if req .FilterAlertFiring {
89+ filters = append (filters , filterIncludeSLOWithAlerts (true , true ))
90+ }
91+ if req .FilterPeriodBudgetConsumed {
92+ filters = append (filters , filterIncludeSLOsWithWindowBudgetConsumed (100 ))
93+ }
94+ if req .FilterCurrentBurningBudgetOver100 {
95+ filters = append (filters , filterIncludeSLOsCurrentBurningBudgetOverThreshold (100 ))
96+ }
97+
98+ filteredSLOs := slos
99+ if len (filters ) > 0 {
100+ filterChain := newSLOFilterChain (filters ... )
101+ filteredSLOs = []storage.SLOInstantDetails {}
102+ for _ , slo := range slos {
103+ include , err := filterChain .IncludeSLO (ctx , & slo )
104+ if err != nil {
105+ return nil , fmt .Errorf ("could not filter SLOs: %w" , err )
106+ }
107+ if include {
108+ filteredSLOs = append (filteredSLOs , slo )
109+ }
110+ }
111+ }
112+
83113 // Sort results based on request.
84114
85115 // Always sort by SLO ID first to have a stable sort.
86- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
116+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
87117 return strings .Compare (x .SLO .ID , y .SLO .ID )
88118 })
89119
90120 switch req .SortMode {
91121 case SLOListSortModeSLOIDDesc :
92- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
122+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
93123 return strings .Compare (y .SLO .ID , x .SLO .ID )
94124 })
95125 case SLOListSortModeServiceNameAsc :
96- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
126+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
97127 return strings .Compare (x .SLO .ServiceID , y .SLO .ServiceID )
98128 })
99129 case SLOListSortModeServiceNameDesc :
100- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
130+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
101131 return strings .Compare (y .SLO .ServiceID , x .SLO .ServiceID )
102132 })
103133 case SLOListSortModeCurrentBurningBudgetAsc :
104- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
134+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
105135 return cmp .Compare (
106136 x .BudgetDetails .BurningBudgetPercent ,
107137 y .BudgetDetails .BurningBudgetPercent ,
108138 )
109139 })
110140 case SLOListSortModeCurrentBurningBudgetDesc :
111- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
141+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
112142 return cmp .Compare (
113143 y .BudgetDetails .BurningBudgetPercent ,
114144 x .BudgetDetails .BurningBudgetPercent ,
115145 )
116146 })
117147 case SLOListSortModeBudgetBurnedWindowPeriodAsc :
118- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
148+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
119149 return cmp .Compare (
120150 x .BudgetDetails .BurnedBudgetWindowPercent ,
121151 y .BudgetDetails .BurnedBudgetWindowPercent ,
122152 )
123153 })
124154 case SLOListSortModeBudgetBurnedWindowPeriodDesc :
125- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
155+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
126156 return cmp .Compare (
127157 y .BudgetDetails .BurnedBudgetWindowPercent ,
128158 x .BudgetDetails .BurnedBudgetWindowPercent ,
129159 )
130160 })
131161 case SLOListSortModeAlertSeverityAsc :
132- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
162+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
133163 return cmp .Compare (
134164 getAlertSeverityScore ([]model.SLOAlerts {x .Alerts }),
135165 getAlertSeverityScore ([]model.SLOAlerts {y .Alerts }),
136166 )
137167 })
138168 case SLOListSortModeAlertSeverityDesc :
139- slices .SortStableFunc (slos , func (x , y storage.SLOInstantDetails ) int {
169+ slices .SortStableFunc (filteredSLOs , func (x , y storage.SLOInstantDetails ) int {
140170 return cmp .Compare (
141171 getAlertSeverityScore ([]model.SLOAlerts {y .Alerts }),
142172 getAlertSeverityScore ([]model.SLOAlerts {x .Alerts }),
143173 )
144174 })
145175 }
146176
147- rtSLOs := make ([]RealTimeSLODetails , 0 , len (slos ))
148- for _ , s := range slos {
177+ rtSLOs := make ([]RealTimeSLODetails , 0 , len (filteredSLOs ))
178+ for _ , s := range filteredSLOs {
149179 rtSLOs = append (rtSLOs , RealTimeSLODetails {
150180 SLO : s .SLO ,
151181 Alerts : s .Alerts ,
0 commit comments