-
Notifications
You must be signed in to change notification settings - Fork 129
/
tables.go
164 lines (147 loc) · 5.54 KB
/
tables.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package repository
import (
"github.com/pkg/errors"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/internal/lookoutv2/model"
)
const (
stateField = "state"
submittedField = "submitted"
lastTransitionTimeField = "lastTransitionTime"
jobTable = "job"
jobRunTable = "job_run"
jobTableAbbrev = "j"
jobRunTableAbbrev = "jr"
jobIdCol = "job_id"
queueCol = "queue"
namespaceCol = "namespace"
jobSetCol = "jobset"
stateCol = "state"
ownerCol = "owner"
cpuCol = "cpu"
memoryCol = "memory"
ephemeralStorageCol = "ephemeral_storage"
gpuCol = "gpu"
priorityCol = "priority"
submittedCol = "submitted"
lastTransitionTimeCol = "last_transition_time_seconds"
priorityClassCol = "priority_class"
)
type AggregateType int
const (
Unknown AggregateType = -1
Max = 0
Average = 1
StateCounts = 2
Min = 3
)
type LookoutTables struct {
// field name -> column name
fieldColumnMap map[string]string
// set of column names that can be ordered
orderableColumns map[string]bool
// column name -> set of supported matches for column
filterableColumns map[string]map[string]bool
// table name -> abbreviated table name
tableAbbrevs map[string]string
// columns that can be grouped by
groupableColumns map[string]bool
// map from column to aggregate that can be performed on it
groupAggregates map[string]AggregateType
}
func NewTables() *LookoutTables {
return &LookoutTables{
fieldColumnMap: map[string]string{
"jobId": jobIdCol,
"queue": queueCol,
"jobSet": jobSetCol,
"owner": ownerCol,
"namespace": namespaceCol,
"state": stateCol,
"cpu": cpuCol,
"memory": memoryCol,
"ephemeralStorage": ephemeralStorageCol,
"gpu": gpuCol,
"priority": priorityCol,
"submitted": submittedCol,
"lastTransitionTime": lastTransitionTimeCol,
"priorityClass": priorityClassCol,
},
orderableColumns: util.StringListToSet([]string{
jobIdCol,
jobSetCol,
submittedCol,
lastTransitionTimeCol,
}),
filterableColumns: map[string]map[string]bool{
jobIdCol: util.StringListToSet([]string{model.MatchExact}),
queueCol: util.StringListToSet([]string{model.MatchExact, model.MatchStartsWith, model.MatchContains}),
jobSetCol: util.StringListToSet([]string{model.MatchExact, model.MatchStartsWith, model.MatchContains}),
ownerCol: util.StringListToSet([]string{model.MatchExact, model.MatchStartsWith, model.MatchContains}),
namespaceCol: util.StringListToSet([]string{model.MatchExact, model.MatchStartsWith, model.MatchContains}),
stateCol: util.StringListToSet([]string{model.MatchExact, model.MatchAnyOf}),
cpuCol: util.StringListToSet([]string{model.MatchExact, model.MatchGreaterThan, model.MatchLessThan, model.MatchGreaterThanOrEqualTo, model.MatchLessThanOrEqualTo}),
memoryCol: util.StringListToSet([]string{model.MatchExact, model.MatchGreaterThan, model.MatchLessThan, model.MatchGreaterThanOrEqualTo, model.MatchLessThanOrEqualTo}),
ephemeralStorageCol: util.StringListToSet([]string{model.MatchExact, model.MatchGreaterThan, model.MatchLessThan, model.MatchGreaterThanOrEqualTo, model.MatchLessThanOrEqualTo}),
gpuCol: util.StringListToSet([]string{model.MatchExact, model.MatchGreaterThan, model.MatchLessThan, model.MatchGreaterThanOrEqualTo, model.MatchLessThanOrEqualTo}),
priorityCol: util.StringListToSet([]string{model.MatchExact, model.MatchGreaterThan, model.MatchLessThan, model.MatchGreaterThanOrEqualTo, model.MatchLessThanOrEqualTo}),
priorityClassCol: util.StringListToSet([]string{model.MatchExact, model.MatchStartsWith, model.MatchContains}),
},
tableAbbrevs: map[string]string{
jobTable: jobTableAbbrev,
jobRunTable: jobRunTableAbbrev,
},
groupableColumns: util.StringListToSet([]string{
queueCol,
namespaceCol,
jobSetCol,
stateCol,
}),
groupAggregates: map[string]AggregateType{
submittedCol: Min,
lastTransitionTimeCol: Average,
stateCol: StateCounts,
},
}
}
func (c *LookoutTables) ColumnFromField(field string) (string, error) {
col, ok := c.fieldColumnMap[field]
if !ok {
return "", errors.Errorf("column for field %s not found", field)
}
return col, nil
}
func (c *LookoutTables) IsOrderable(col string) bool {
_, ok := c.orderableColumns[col]
return ok
}
func (c *LookoutTables) IsFilterable(col string) bool {
_, ok := c.filterableColumns[col]
return ok
}
func (c *LookoutTables) SupportsMatch(col, match string) bool {
supportedMatches, ok := c.filterableColumns[col]
if !ok {
return false
}
_, isSupported := supportedMatches[match]
return isSupported
}
func (c *LookoutTables) TableAbbrev(table string) (string, error) {
abbrev, ok := c.tableAbbrevs[table]
if !ok {
return "", errors.Errorf("abbreviation for table %s not found", table)
}
return abbrev, nil
}
func (c *LookoutTables) IsGroupable(col string) bool {
_, ok := c.groupableColumns[col]
return ok
}
func (c *LookoutTables) GroupAggregateForCol(col string) (AggregateType, error) {
aggregate, ok := c.groupAggregates[col]
if !ok {
return Unknown, errors.Errorf("no aggregate found for column %s", col)
}
return aggregate, nil
}