github.com/m3db/m3@v1.5.0/src/metrics/transformation/func.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 transformation 22 23 import ( 24 "math" 25 "time" 26 ) 27 28 var ( 29 emptyDatapoint = Datapoint{Value: math.NaN()} 30 ) 31 32 // Datapoint is a metric data point containing a timestamp in 33 // Unix nanoseconds since epoch and a value. 34 type Datapoint struct { 35 TimeNanos int64 36 Value float64 37 } 38 39 // IsEmpty returns whether this is an empty datapoint. 40 func (dp Datapoint) IsEmpty() bool { return math.IsNaN(dp.Value) } 41 42 // UnaryTransform is a unary transformation that takes a single 43 // datapoint as input and transforms it into a datapoint as output. 44 // It can keep state if it requires. 45 type UnaryTransform interface { 46 Evaluate(dp Datapoint) Datapoint 47 } 48 49 // UnaryTransformFn implements UnaryTransform as a function. 50 type UnaryTransformFn func(dp Datapoint) Datapoint 51 52 // Evaluate implements UnaryTransform as a function. 53 func (fn UnaryTransformFn) Evaluate(dp Datapoint) Datapoint { 54 return fn(dp) 55 } 56 57 // FeatureFlags holds options passed into transformations from 58 // the aggregator configuration file. 59 // nolint:gofumpt 60 type FeatureFlags struct { 61 } 62 63 // BinaryTransform is a binary transformation that takes the 64 // previous and the current datapoint as input and produces 65 // a single datapoint as the transformation result. 66 // It can keep state if it requires. 67 type BinaryTransform interface { 68 Evaluate(prev, curr Datapoint, flags FeatureFlags) Datapoint 69 } 70 71 // BinaryTransformFn implements BinaryTransform as a function. 72 type BinaryTransformFn func(prev, curr Datapoint, flags FeatureFlags) Datapoint 73 74 // Evaluate implements BinaryTransform as a function. 75 func (fn BinaryTransformFn) Evaluate(prev, curr Datapoint, flags FeatureFlags) Datapoint { 76 return fn(prev, curr, flags) 77 } 78 79 // UnaryMultiOutputTransform is like UnaryTransform, but can output an additional datapoint. 80 // The additional datapoint is not passed to subsequent transforms. 81 type UnaryMultiOutputTransform interface { 82 // Evaluate applies the transform on the provided datapoint. 83 Evaluate(dp Datapoint, resolution time.Duration) (Datapoint, Datapoint) 84 } 85 86 // UnaryMultiOutputTransformFn implements UnaryMultiOutputTransform as a function. 87 type UnaryMultiOutputTransformFn func(dp Datapoint, resolution time.Duration) (Datapoint, Datapoint) 88 89 // Evaluate applies the transform on the provided datapoint. 90 func (fn UnaryMultiOutputTransformFn) Evaluate(dp Datapoint, resolution time.Duration) (Datapoint, Datapoint) { 91 return fn(dp, resolution) 92 }