-
Notifications
You must be signed in to change notification settings - Fork 786
/
priority.go
51 lines (41 loc) · 1.4 KB
/
priority.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package tripperware
import (
"time"
"github.com/cortexproject/cortex/pkg/util/validation"
)
func GetPriority(query string, minTime, maxTime int64, now time.Time, queryPriority validation.QueryPriority) int64 {
if !queryPriority.Enabled || query == "" || len(queryPriority.Priorities) == 0 {
return queryPriority.DefaultPriority
}
for _, priority := range queryPriority.Priorities {
for _, attribute := range priority.QueryAttributes {
if attribute.Regex != "" && attribute.Regex != ".*" && attribute.Regex != ".+" {
if attribute.CompiledRegex != nil && !attribute.CompiledRegex.MatchString(query) {
continue
}
}
if isWithinTimeAttributes(attribute.TimeWindow, now, minTime, maxTime) {
return priority.Priority
}
}
}
return queryPriority.DefaultPriority
}
func isWithinTimeAttributes(timeWindow validation.TimeWindow, now time.Time, startTime, endTime int64) bool {
if timeWindow.Start == 0 && timeWindow.End == 0 {
return true
}
if timeWindow.Start != 0 {
startTimeThreshold := now.Add(-1 * time.Duration(timeWindow.Start).Abs()).Add(-1 * time.Minute).Truncate(time.Minute).UnixMilli()
if startTime < startTimeThreshold {
return false
}
}
if timeWindow.End != 0 {
endTimeThreshold := now.Add(-1 * time.Duration(timeWindow.End).Abs()).Add(1 * time.Minute).Truncate(time.Minute).UnixMilli()
if endTime > endTimeThreshold {
return false
}
}
return true
}