github.com/m3db/m3@v1.5.0/src/query/graphite/native/alias_functions.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package native 22 23 import ( 24 "strings" 25 26 "github.com/m3db/m3/src/query/graphite/common" 27 "github.com/m3db/m3/src/query/graphite/ts" 28 ) 29 30 // alias takes one metric or a wildcard seriesList and a string in quotes. 31 // Prints the string instead of the metric name in the legend. 32 func alias(ctx *common.Context, series singlePathSpec, a string) (ts.SeriesList, error) { 33 return common.Alias(ctx, ts.SeriesList(series), a) 34 } 35 36 // aliasByMetric takes a seriesList and applies an alias derived from the base 37 // metric name. 38 func aliasByMetric(ctx *common.Context, seriesList singlePathSpec) (ts.SeriesList, error) { 39 return aliasByNode(ctx, seriesList, -1) 40 } 41 42 // aliasByNode renames a time series result according to a subset of the nodes 43 // in its hierarchy. 44 func aliasByNode(ctx *common.Context, seriesList singlePathSpec, nodes ...int) (ts.SeriesList, error) { 45 renamed := make([]*ts.Series, 0, ts.SeriesList(seriesList).Len()) 46 for _, series := range seriesList.Values { 47 name, err := getFirstPathExpression(series.Name()) 48 if err != nil { 49 return ts.SeriesList{}, err 50 } 51 52 nameParts := strings.Split(name, ".") 53 newNameParts := make([]string, 0, len(nodes)) 54 for _, node := range nodes { 55 // NB(jayp): graphite supports negative indexing, so we need to also! 56 if node < 0 { 57 node += len(nameParts) 58 } 59 if node < 0 || node >= len(nameParts) { 60 continue 61 } 62 newNameParts = append(newNameParts, nameParts[node]) 63 } 64 newName := strings.Join(newNameParts, ".") 65 newSeries := series.RenamedTo(newName) 66 renamed = append(renamed, newSeries) 67 } 68 seriesList.Values = renamed 69 return ts.SeriesList(seriesList), nil 70 } 71 72 // aliasSub runs series names through a regex search/replace. 73 func aliasSub(ctx *common.Context, input singlePathSpec, search, replace string) (ts.SeriesList, error) { 74 return common.AliasSub(ctx, ts.SeriesList(input), search, replace) 75 }