github.com/m3db/m3@v1.5.0/src/query/functions/linear/clamp.go (about) 1 // Copyright (c) 2018 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 linear 22 23 import ( 24 "fmt" 25 "math" 26 27 "github.com/m3db/m3/src/query/block" 28 "github.com/m3db/m3/src/query/functions/lazy" 29 "github.com/m3db/m3/src/query/parser" 30 ) 31 32 const ( 33 // ClampMinType ensures all values except NaNs are greater 34 // than or equal to the provided argument. 35 ClampMinType = "clamp_min" 36 37 // ClampMaxType ensures all values except NaNs are lesser 38 // than or equal to provided argument. 39 ClampMaxType = "clamp_max" 40 ) 41 42 type clampOp struct { 43 opType string 44 scalar float64 45 } 46 47 func parseClampArgs(args []interface{}) (float64, error) { 48 if len(args) != 1 { 49 return 0, fmt.Errorf("invalid number of args for clamp: %d", len(args)) 50 } 51 52 scalar, ok := args[0].(float64) 53 if !ok { 54 return 0, fmt.Errorf("unable to cast to scalar argument: %v", args[0]) 55 } 56 57 return scalar, nil 58 } 59 60 func clampFn(max bool, roundTo float64) block.ValueTransform { 61 if max { 62 return func(v float64) float64 { return math.Min(v, roundTo) } 63 } 64 65 return func(v float64) float64 { return math.Max(v, roundTo) } 66 } 67 68 func removeName(meta []block.SeriesMeta) []block.SeriesMeta { 69 for i, m := range meta { 70 meta[i].Tags = m.Tags.WithoutName() 71 } 72 73 return meta 74 } 75 76 // NewClampOp creates a new clamp op based on the type and arguments 77 func NewClampOp(args []interface{}, opType string) (parser.Params, error) { 78 isMax := opType == ClampMaxType 79 if opType != ClampMinType && !isMax { 80 return nil, fmt.Errorf("unknown clamp type: %s", opType) 81 } 82 83 clampTo, err := parseClampArgs(args) 84 if err != nil { 85 return nil, err 86 } 87 88 fn := clampFn(isMax, clampTo) 89 lazyOpts := block.NewLazyOptions(). 90 SetValueTransform(fn). 91 SetSeriesMetaTransform(removeName) 92 return lazy.NewLazyOp(opType, lazyOpts) 93 }