-
Notifications
You must be signed in to change notification settings - Fork 3
/
sort.go
237 lines (203 loc) · 5.42 KB
/
sort.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package sort
import (
"fmt"
"github.com/flant/glaball/pkg/client"
"github.com/ahmetb/go-linq"
)
var (
// We use negative values because these fields don't exist in any struct
byHost = FieldIndex{-1, 3}
byLen = FieldIndex{-1, 2}
empty = FieldIndex{-1, 1}
notFound = FieldIndex{-1, 0}
)
type Options struct {
SortBy, GroupBy string
OrderBy []string
StructType interface{}
}
type Result struct {
Count int
Key string
Elements Elements
Cached Cached
}
type Element struct {
Host *client.Host
Struct interface{}
Cached Cached
}
type Cached bool
func (c Cached) String() string {
if c {
return "yes"
}
return "no"
}
type Elements []interface{}
func (e Elements) Hosts() client.Hosts {
s := make(client.Hosts, 0, len(e))
for _, v := range e {
s = append(s, v.(Element).Host)
}
return s
}
func (e Elements) Cached() (cached Cached) {
for _, v := range e {
if !v.(Element).Cached {
return false
}
}
return true
}
func (e Elements) Typed() []Element {
s := make([]Element, 0, len(e))
for _, v := range e {
s = append(s, v.(Element))
}
return s
}
func FromChannel(ch chan interface{}, opt *Options) (results []Result) {
FromChannelQuery(ch, opt).ToSlice(&results)
return results
}
func FromChannelQuery(ch chan interface{}, opt *Options) linq.Query {
var orderedQuery linq.OrderedQuery
// Initialize GroupBy key with `empty` value
groupBy := empty
// Initialize _first_ OrderBy key with `empty` value
first := empty
// Create `json` tag names tree of current struct fields
// to get their values fast (by index)
t := JsonFieldIndexTree(opt.StructType)
// Add some additional fields
t.Insert("host", byHost)
t.Insert("count", byLen)
query := linq.FromChannel(ch)
if opt.GroupBy != "" {
groupBy = notFound
// Set GroupBy key to some field name if struct has it
if v, ok := t.Get(opt.GroupBy); ok {
groupBy = v.(FieldIndex)
}
query = query.GroupBy(GroupBy(groupBy))
}
if opt.OrderBy != nil {
first = notFound
// Set _first_ OrderBy key to some field name if struct has it
if v, ok := t.Get(opt.OrderBy[0]); ok {
first = v.(FieldIndex)
}
}
switch o := opt; {
case o.GroupBy != "" && o.OrderBy[0] == "count":
orderedQuery = query.OrderBy(ByLen()) // Order by count first if it is declared
orderedQuery = orderedQuery.ThenByDescending(ByKey()) // and then by GroupBy key
default:
orderedQuery = query.OrderByDescending(OrderBy(groupBy, first)) // Otherwise use GroupBy key and _first_ OrderBy key
}
// Add additional order if we have other OrderBy keys
for _, key := range opt.OrderBy[1:] {
idx := notFound
if v, ok := t.Get(key); ok {
idx = v.(FieldIndex)
}
orderedQuery = orderedQuery.ThenByDescending(OrderBy(groupBy, idx))
}
// Set ascending or descending order. Default is descending.
switch opt.SortBy {
case "asc":
query = orderedQuery.Reverse()
default:
query = orderedQuery.Query
}
query = query.Select(func(i interface{}) interface{} {
// Check if we have a group or single element
switch v := i.(type) {
case Element:
var key interface{}
// Use specific key if we want to order results by host project name
switch opt.OrderBy[0] {
case "host":
key = v.Host.Project
default:
key = ValidFieldValue(t, opt.OrderBy, v.Struct)
}
return Result{
Count: 1, // Count of single element is always 1
Key: fmt.Sprint(key),
Elements: Elements{v},
Cached: v.Cached,
}
case linq.Group:
return Result{
Count: len(v.Group),
Key: fmt.Sprint(v.Key),
Elements: Elements(v.Group),
Cached: Elements(v.Group).Cached(),
}
default:
return v
}
})
return query
}
func GroupBy(groupBy FieldIndex) (func(i interface{}) interface{}, func(i interface{}) interface{}) {
return func(i interface{}) interface{} {
return FieldIndexValue(groupBy, i.(Element).Struct)
},
func(i interface{}) interface{} { return i }
}
func OrderBy(groupBy, orderBy FieldIndex) func(i interface{}) interface{} {
switch v := orderBy; {
case v.Equal(byHost):
// Return Host.Project name
return ByHost()
case v.Equal(byLen):
// Return length of the group if we have GroupBy key
if !groupBy.Equal(empty) {
return ByLen()
}
// Return count == 1 if we don't have any groups
return func(i interface{}) interface{} { return 1 }
case orderBy.Equal(notFound):
// Return length of the group if we have GroupBy key
if !groupBy.Equal(empty) {
return ByLen()
}
// Return count == 1 if we don't have any groups
return func(i interface{}) interface{} { return 1 }
}
// Order by GroupBy key if it exists
if !groupBy.Equal(empty) {
return ByKey()
}
// Otherwise order by other keys
return ByFieldIndex(orderBy)
}
// Order by count
func ByLen() func(i interface{}) interface{} {
return func(i interface{}) interface{} { return len(i.(linq.Group).Group) }
}
// Order by GroupBy key
func ByKey() func(i interface{}) interface{} {
return func(i interface{}) interface{} { return i.(linq.Group).Key }
}
// Order by Host.Project key
func ByHost() func(i interface{}) interface{} {
return func(i interface{}) interface{} {
if v, ok := i.(Element); ok {
return v.Host
}
return Elements(i.(linq.Group).Group).Hosts().Projects(true)[0]
}
}
// Order by the field
func ByFieldIndex(n FieldIndex) func(i interface{}) interface{} {
return func(i interface{}) interface{} {
if v, ok := i.(Element); ok {
return FieldIndexValue(n, v.Struct)
}
return FieldIndexValue(n, i)
}
}