-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
165 lines (137 loc) · 3.95 KB
/
common.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
package api
// Copyright 2022 Severalnines AB
//
// This file is part of cmon-proxy.
//
// cmon-proxy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2 of the License.
//
// cmon-proxy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with cmon-proxy. If not, see <https://www.gnu.org/licenses/>.
import (
"strings"
)
type SimpleFilteredRequest struct {
Filters []*Filter `json:"filters"`
}
// WithControllerID is used in replies to extend the standard cmon replies
type WithControllerID struct {
Xid string `json:"xid"`
ControllerID string `json:"controller_id"`
ControllerURL string `json:"controller_url"`
}
type ForceUpdateRequest struct {
ForceUpdate bool `json:"force_update"`
}
type ListRequest struct {
Page uint64 `json:"page"`
PerPage uint64 `json:"perPage"`
Order string `json:"order"`
Filters []*Filter `json:"filters"`
}
type ListResponse struct {
Page uint64 `json:"page,omitempty"`
PerPage uint64 `json:"perPage,omitempty"`
Total uint64 `json:"total,omitempty"`
}
// Filter is a generic filter by a key and accepted values
type Filter struct {
Key string `json:"key"`
Value string `json:"value,omitempty"`
Values []string `json:"values,omitempty"` /* OR like filter */
MatchAll []string `json:"matchall,omitempty"` /* all values specified here must match, for tags */
}
func (f *Filter) AcceptsValue(value string) bool {
if f == nil || len(value) < 1 {
return true
}
for _, val := range f.Values {
if val == value {
return true
}
}
return f.Value == value
}
type LazyStringSlFn func() []string
// PassTagsFilterLazy checks if all the defined tags in filter.MatchAll are in the 'tags' list
func PassTagsFilterLazy(filters []*Filter, tagsFn LazyStringSlFn) bool {
for _, filter := range filters {
if filter == nil || filter.Key != "tags" || len(filter.MatchAll) < 1 {
continue
}
tags := tagsFn()
tagsMap := make(map[string]bool)
for _, tag := range tags {
tagsMap[tag] = true
}
for _, tag := range filter.MatchAll {
if _, found := tagsMap[tag]; !found {
// one of the required tags not found, fail
return false
}
}
// all tags found
return true
}
// no tags filter present
return true
}
func PassFilter(filters []*Filter, key, value string) bool {
// no filters at all... fine
if filters == nil || len(filters) < 1 {
return true
}
for _, filter := range filters {
if filter != nil && filter.Key == key {
return filter.AcceptsValue(value)
}
}
// the field isn't filtrated
return true
}
type LazyStringFn func() string
func PassFilterLazy(filters []*Filter, key string, fn LazyStringFn) bool {
// no filters at all... fine
if filters == nil || len(filters) < 1 {
return true
}
for _, filter := range filters {
if filter != nil && filter.Key == key {
return filter.AcceptsValue(fn())
}
}
// the field isn't filtrated
return true
}
func (listRequest ListRequest) GetOrder() (order string, descending bool) {
order = ""
descending = false
parts := strings.Split(listRequest.Order, " ")
if len(parts) >= 2 {
descending = strings.ToLower(parts[1]) == "desc"
}
if len(parts) > 0 {
order = parts[0]
}
return
}
func Paginate(listRequest ListRequest, length int) (int, int) {
if listRequest.PerPage <= 0 {
return 0, length
}
// UI starts counting from 1, but we need it from 0 here
page := listRequest.Page - 1
// hack but we don't want invalid data here, it would crash
if page < 0 {
page = 0
}
start := int(page * listRequest.PerPage)
if start > length {
start = length
}
end := start + int(listRequest.PerPage)
if end > length {
end = length
}
return start, end
}