-
Notifications
You must be signed in to change notification settings - Fork 153
/
label_replace.go
230 lines (187 loc) · 6.03 KB
/
label_replace.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
package promql
import (
"fmt"
"regexp"
"github.com/influxdata/flux/runtime"
"github.com/influxdata/flux/values"
"github.com/influxdata/flux"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/plan"
)
const (
// LabelReplaceKind is the Kind for the LabelReplace Flux function
LabelReplaceKind = "labelReplace"
)
type LabelReplaceOpSpec struct {
Source string `json:"source"`
Destination string `json:"destination"`
Regex string `json:"regex"`
Replacement string `json:"replacement"`
}
func init() {
labelReplaceSignature := runtime.MustLookupBuiltinType("internal/promql", "labelReplace")
runtime.RegisterPackageValue("internal/promql", "labelReplace", flux.MustValue(flux.FunctionValue(LabelReplaceKind, createLabelReplaceOpSpec, labelReplaceSignature)))
flux.RegisterOpSpec(LabelReplaceKind, func() flux.OperationSpec { return &LabelReplaceOpSpec{} })
plan.RegisterProcedureSpec(LabelReplaceKind, newLabelReplaceProcedure, LabelReplaceKind)
execute.RegisterTransformation(LabelReplaceKind, createLabelReplaceTransformation)
}
func createLabelReplaceOpSpec(args flux.Arguments, a *flux.Administration) (flux.OperationSpec, error) {
if err := a.AddParentFromArgs(args); err != nil {
return nil, err
}
spec := new(LabelReplaceOpSpec)
if src, err := args.GetRequiredString("source"); err != nil {
return nil, err
} else {
spec.Source = src
}
if dst, err := args.GetRequiredString("destination"); err != nil {
return nil, err
} else {
spec.Destination = dst
}
if re, err := args.GetRequiredString("regex"); err != nil {
return nil, err
} else {
spec.Regex = re
}
if repl, err := args.GetRequiredString("replacement"); err != nil {
return nil, err
} else {
spec.Replacement = repl
}
return spec, nil
}
func (s *LabelReplaceOpSpec) Kind() flux.OperationKind {
return LabelReplaceKind
}
func newLabelReplaceProcedure(qs flux.OperationSpec, pa plan.Administration) (plan.ProcedureSpec, error) {
s, ok := qs.(*LabelReplaceOpSpec)
if !ok {
return nil, fmt.Errorf("invalid spec type %T", qs)
}
return &LabelReplaceProcedureSpec{
Source: s.Source,
Destination: s.Destination,
Regex: s.Regex,
Replacement: s.Replacement,
}, nil
}
type LabelReplaceProcedureSpec struct {
plan.DefaultCost
Source string
Destination string
Regex string
Replacement string
}
func (s *LabelReplaceProcedureSpec) Kind() plan.ProcedureKind {
return LabelReplaceKind
}
func (s *LabelReplaceProcedureSpec) Copy() plan.ProcedureSpec {
ns := new(LabelReplaceProcedureSpec)
*ns = *s
return ns
}
func createLabelReplaceTransformation(id execute.DatasetID, mode execute.AccumulationMode, spec plan.ProcedureSpec, a execute.Administration) (execute.Transformation, execute.Dataset, error) {
s, ok := spec.(*LabelReplaceProcedureSpec)
if !ok {
return nil, nil, fmt.Errorf("invalid spec type %T", spec)
}
cache := execute.NewTableBuilderCache(a.Allocator())
d := execute.NewDataset(id, mode, cache)
t := NewLabelReplaceTransformation(d, cache, s)
return t, d, nil
}
type labelReplaceTransformation struct {
execute.ExecutionNode
d execute.Dataset
cache execute.TableBuilderCache
source string
destination string
regex string
replacement string
}
func NewLabelReplaceTransformation(d execute.Dataset, cache execute.TableBuilderCache, spec *LabelReplaceProcedureSpec) *labelReplaceTransformation {
return &labelReplaceTransformation{
d: d,
cache: cache,
source: spec.Source,
destination: spec.Destination,
regex: spec.Regex,
replacement: spec.Replacement,
}
}
func (t *labelReplaceTransformation) RetractTable(id execute.DatasetID, key flux.GroupKey) error {
return t.d.RetractTable(key)
}
func (t *labelReplaceTransformation) Process(id execute.DatasetID, tbl flux.Table) (err error) {
re, err := regexp.Compile("^(?:" + t.regex + ")$")
if err != nil {
return fmt.Errorf("invalid regular expression in label_replace(): %s", t.regex)
}
key := tbl.Key()
var srcVal string
if key.HasCol(t.source) {
srcVal = key.LabelValue(t.source).Str()
}
indexes := re.FindStringSubmatchIndex(srcVal)
var outKey flux.GroupKey
if indexes == nil {
// If there is no match, no replacement should take place.
outKey = key
} else {
res := re.ExpandString([]byte{}, t.replacement, srcVal, indexes)
outCols := make([]flux.ColMeta, 0, len(key.Cols()))
outVals := make([]values.Value, 0, len(key.Cols()))
for i, col := range key.Cols() {
if col.Label == t.destination {
continue
}
outCols = append(outCols, col)
outVals = append(outVals, key.Values()[i])
}
if len(res) > 0 {
outCols = append(outCols, flux.ColMeta{Label: t.destination, Type: flux.TString})
outVals = append(outVals, values.NewString(string(res)))
}
outKey = execute.NewGroupKey(outCols, outVals)
}
builder, created := t.cache.TableBuilder(outKey)
if !created {
return fmt.Errorf("labelReplace found duplicate table with key: %v", tbl.Key())
}
if err := execute.AddTableKeyCols(outKey, builder); err != nil {
return err
}
cols := tbl.Cols()
valIdx := execute.ColIdx(execute.DefaultValueColLabel, cols)
if valIdx < 0 {
return fmt.Errorf("value column not found: %s", execute.DefaultValueColLabel)
}
outValIdx, err := builder.AddCol(flux.ColMeta{Label: execute.DefaultValueColLabel, Type: flux.TFloat})
if err != nil {
return fmt.Errorf("error appending value column: %s", err)
}
return tbl.Do(func(cr flux.ColReader) error {
for i := 0; i < cr.Len(); i++ {
err := execute.AppendKeyValues(outKey, builder)
if err != nil {
return err
}
v := execute.ValueForRow(cr, i, valIdx)
if err := builder.AppendValue(outValIdx, v); err != nil {
return err
}
}
return nil
})
}
func (t *labelReplaceTransformation) UpdateWatermark(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateWatermark(pt)
}
func (t *labelReplaceTransformation) UpdateProcessingTime(id execute.DatasetID, pt execute.Time) error {
return t.d.UpdateProcessingTime(pt)
}
func (t *labelReplaceTransformation) Finish(id execute.DatasetID, err error) {
t.d.Finish(err)
}