@@ -35,6 +35,8 @@ import (
35
35
const (
36
36
RuleLevelCluster RuleLevel = "cluster"
37
37
RuleLevelNamespace RuleLevel = "namespace"
38
+
39
+ AnnotationKeyRuleUpdateTime = "rule_update_time"
38
40
)
39
41
40
42
var (
@@ -217,10 +219,22 @@ func AlertingRuleIdCompare(leftId, rightId string) bool {
217
219
}
218
220
219
221
func (q * AlertingRuleQueryParams ) Sort (rules []* GettableAlertingRule ) {
220
- idCompare := func (left , right * GettableAlertingRule ) bool {
222
+ baseCompare := func (left , right * GettableAlertingRule ) bool {
223
+ var leftUpdateTime , rightUpdateTime string
224
+ if len (left .Annotations ) > 0 {
225
+ leftUpdateTime = left .Annotations [AnnotationKeyRuleUpdateTime ]
226
+ }
227
+ if len (right .Annotations ) > 0 {
228
+ rightUpdateTime = right .Annotations [AnnotationKeyRuleUpdateTime ]
229
+ }
230
+
231
+ if leftUpdateTime != rightUpdateTime {
232
+ return leftUpdateTime > rightUpdateTime
233
+ }
234
+
221
235
return AlertingRuleIdCompare (left .Id , right .Id )
222
236
}
223
- var compare = idCompare
237
+ var compare = baseCompare
224
238
if q != nil {
225
239
reverse := q .SortType == "desc"
226
240
switch q .SortField {
@@ -232,7 +246,7 @@ func (q *AlertingRuleQueryParams) Sort(rules []*GettableAlertingRule) {
232
246
}
233
247
return c < 0
234
248
}
235
- return idCompare (left , right )
249
+ return baseCompare (left , right )
236
250
}
237
251
case "lastEvaluation" :
238
252
compare = func (left , right * GettableAlertingRule ) bool {
@@ -250,7 +264,7 @@ func (q *AlertingRuleQueryParams) Sort(rules []*GettableAlertingRule) {
250
264
return left .LastEvaluation .Before (* right .LastEvaluation )
251
265
}
252
266
}
253
- return idCompare (left , right )
267
+ return baseCompare (left , right )
254
268
}
255
269
case "evaluationTime" :
256
270
compare = func (left , right * GettableAlertingRule ) bool {
@@ -260,7 +274,7 @@ func (q *AlertingRuleQueryParams) Sort(rules []*GettableAlertingRule) {
260
274
}
261
275
return left .EvaluationDurationSeconds < right .EvaluationDurationSeconds
262
276
}
263
- return idCompare (left , right )
277
+ return baseCompare (left , right )
264
278
}
265
279
}
266
280
}
0 commit comments