This repository was archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmodels.go
231 lines (201 loc) · 5.59 KB
/
models.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
// pmm-managed
// Copyright (C) 2017 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Package models contains generated Reform records and helpers.
//
// Common order of helpers:
// * unexported validators (checkXXX);
// * FindAllXXX;
// * FindXXXByID;
// * other finder (e.g. FindNodesForAgent);
// * CreateXXX;
// * ChangeXXX;
// * RemoveXXX.
package models
import (
"database/sql/driver"
"encoding/json"
"regexp"
"sort"
"strings"
"time"
"github.com/percona-platform/saas/pkg/common"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Now returns current time with database precision.
var Now = func() time.Time {
return time.Now().Truncate(time.Microsecond).UTC()
}
// RemoveMode defines how Remove functions deal with dependend objects.
type RemoveMode int
const (
// RemoveRestrict returns error if there dependend objects.
RemoveRestrict RemoveMode = iota
// RemoveCascade removes dependend objects recursively.
RemoveCascade
)
// MergeLabels merges unified labels of Node, Service, and Agent (each can be nil).
func MergeLabels(node *Node, service *Service, agent *Agent) (map[string]string, error) {
res := make(map[string]string, 16)
if node != nil {
labels, err := node.UnifiedLabels()
if err != nil {
return nil, err
}
for name, value := range labels {
res[name] = value
}
}
if service != nil {
labels, err := service.UnifiedLabels()
if err != nil {
return nil, err
}
for name, value := range labels {
res[name] = value
}
}
if agent != nil {
labels, err := agent.UnifiedLabels()
if err != nil {
return nil, err
}
for name, value := range labels {
res[name] = value
}
}
return res, nil
}
// deduplicateStrings deduplicates elements in string slice.
func deduplicateStrings(strings []string) []string {
set := make(map[string]struct{})
for _, p := range strings {
set[p] = struct{}{}
}
slice := make([]string, 0, len(set))
for s := range set {
slice = append(slice, s)
}
sort.Strings(slice)
return slice
}
var labelNameRE = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
// prepareLabels checks that label names are valid, and trims or removes empty values.
func prepareLabels(m map[string]string, removeEmptyValues bool) error {
for name, value := range m {
if !labelNameRE.MatchString(name) {
return status.Errorf(codes.InvalidArgument, "Invalid label name %q.", name)
}
if strings.HasPrefix(name, "__") {
return status.Errorf(codes.InvalidArgument, "Invalid label name %q.", name)
}
value = strings.TrimSpace(value)
if value == "" {
if removeEmptyValues {
delete(m, name)
} else {
m[name] = value
}
}
}
return nil
}
// getLabels deserializes model's Prometheus labels.
func getLabels(b []byte) (map[string]string, error) {
if len(b) == 0 {
return nil, nil
}
m := make(map[string]string)
if err := json.Unmarshal(b, &m); err != nil {
return nil, errors.Wrap(err, "failed to decode custom labels")
}
return m, nil
}
// getLabels serializes model's Prometheus labels.
func setLabels(m map[string]string, res *[]byte) error {
if err := prepareLabels(m, false); err != nil {
return err
}
if len(m) == 0 {
*res = nil
return nil
}
b, err := json.Marshal(m)
if err != nil {
return errors.Wrap(err, "failed to encode custom labels")
}
*res = b
return nil
}
// jsonValue implements database/sql/driver.Valuer interface for v that should be a value.
func jsonValue(v interface{}) (driver.Value, error) {
b, err := json.Marshal(v)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal JSON column")
}
return b, nil
}
// jsonScan implements database/sql.Scanner interface for v that should be a pointer.
func jsonScan(v, src interface{}) error {
var b []byte
switch v := src.(type) {
case []byte:
b = v
case string:
b = []byte(v)
default:
return errors.Errorf("expected []byte or string, got %T (%q)", src, src)
}
if err := json.Unmarshal(b, v); err != nil {
return errors.Wrap(err, "failed to unmarshal JSON column")
}
return nil
}
// Severity represents alert severity.
// Integer values is the same as common.Severity. Common constants can be used.
// Database representation is a string and is handled by Value and Scan methods below.
type Severity common.Severity
// Value implements database/sql/driver Valuer interface.
func (s Severity) Value() (driver.Value, error) {
cs := common.Severity(s)
if err := cs.Validate(); err != nil {
return nil, err
}
return cs.String(), nil
}
// Scan implements database/sql Scanner interface.
func (s *Severity) Scan(src interface{}) error {
switch src := src.(type) {
case string:
cs := common.ParseSeverity(src)
if err := cs.Validate(); err != nil {
return err
}
*s = Severity(cs)
return nil
default:
return errors.Errorf("expected string, got %T (%q)", src, src)
}
}
// ParamType represents parameter type.
type ParamType string
// Available parameter types.
const (
Float = ParamType("float")
Bool = ParamType("bool")
String = ParamType("string")
)