-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathaliasing.go
130 lines (120 loc) · 4.29 KB
/
aliasing.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package common
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/m3db/m3/src/query/graphite/errors"
"github.com/m3db/m3/src/query/graphite/ts"
)
var (
backReferenceRe = regexp.MustCompile(`\\\d+`)
)
// Alias takes one metric or a wildcard seriesList and a string in quotes.
// Prints the string instead of the metric name in the legend.
func Alias(_ *Context, series ts.SeriesList, a string) (ts.SeriesList, error) {
renamed := make([]*ts.Series, series.Len())
for i := range series.Values {
renamed[i] = series.Values[i].RenamedTo(a)
}
series.Values = renamed
return series, nil
}
// AliasByMetric takes a seriesList and applies an alias derived from the base
// metric name.
func AliasByMetric(ctx *Context, series ts.SeriesList) (ts.SeriesList, error) {
renamed := make([]*ts.Series, series.Len())
for i, s := range series.Values {
firstPart := strings.Split(s.Name(), ",")[0]
terms := strings.Split(firstPart, ".")
renamed[i] = s.RenamedTo(terms[len(terms)-1])
}
series.Values = renamed
return series, nil
}
// AliasByNode renames a time series result according to a subset of the nodes
// in its hierarchy.
func AliasByNode(_ *Context, seriesList ts.SeriesList, nodes ...int) (ts.SeriesList, error) {
renamed := make([]*ts.Series, 0, seriesList.Len())
for _, series := range seriesList.Values {
name := series.Name()
left := strings.LastIndex(name, "(") + 1
name = name[left:]
right := strings.IndexAny(name, ",)")
if right == -1 {
right = len(name)
}
nameParts := strings.Split(name[0:right], ".")
newNameParts := make([]string, 0, len(nodes))
for _, node := range nodes {
// NB(jayp): graphite supports negative indexing, so we need to also!
if node < 0 {
node += len(nameParts)
}
if node < 0 || node >= len(nameParts) {
continue
}
newNameParts = append(newNameParts, nameParts[node])
}
newName := strings.Join(newNameParts, ".")
newSeries := series.RenamedTo(newName)
renamed = append(renamed, newSeries)
}
seriesList.Values = renamed
return seriesList, nil
}
// AliasSub runs series names through a regex search/replace.
func AliasSub(_ *Context, input ts.SeriesList, search, replace string) (ts.SeriesList, error) {
regex, err := regexp.Compile(search)
if err != nil {
return ts.SeriesList{}, err
}
output := make([]*ts.Series, input.Len())
for idx, series := range input.Values {
name := series.Name()
if submatches := regex.FindStringSubmatch(name); submatches == nil {
// if the pattern doesn't match, we don't change the series name.
output[idx] = series
} else {
// go regexp package doesn't support back-references, so we need to work around it.
newName := regex.ReplaceAllString(name, replace)
newName = backReferenceRe.ReplaceAllStringFunc(newName, func(matched string) string {
index, retErr := strconv.Atoi(matched[1:])
if retErr != nil {
err = retErr
return ""
}
if index >= len(submatches) {
err = errors.NewInvalidParamsError(fmt.Errorf("invalid group reference in %s", replace))
return ""
}
return submatches[index]
})
if err != nil {
return ts.SeriesList{}, err
}
output[idx] = series.RenamedTo(newName)
}
}
input.Values = output
return input, nil
}