github.com/m3db/m3@v1.5.0/src/metrics/transformation/binary.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 const ( 29 nanosPerSecond = time.Second / time.Nanosecond 30 ) 31 32 var ( 33 // allows to use a single transform fn ref (instead of 34 // taking reference to it each time when converting to iface). 35 transformPerSecondFn = BinaryTransformFn(perSecond) 36 transformIncreaseFn = BinaryTransformFn(increase) 37 ) 38 39 func transformPerSecond() BinaryTransform { 40 return transformPerSecondFn 41 } 42 43 // perSecond computes the derivative between consecutive datapoints, taking into 44 // account the time interval between the values. 45 // Note: 46 // * It skips NaN values. 47 // * It assumes the timestamps are monotonically increasing, and values are non-decreasing. 48 // If either of the two conditions is not met, an empty datapoint is returned. 49 func perSecond(prev, curr Datapoint, flags FeatureFlags) Datapoint { 50 if prev.TimeNanos >= curr.TimeNanos || math.IsNaN(prev.Value) || math.IsNaN(curr.Value) { 51 return emptyDatapoint 52 } 53 diff := curr.Value - prev.Value 54 if diff < 0 { 55 return emptyDatapoint 56 } 57 rate := diff * float64(nanosPerSecond) / float64(curr.TimeNanos-prev.TimeNanos) 58 return Datapoint{TimeNanos: curr.TimeNanos, Value: rate} 59 } 60 61 func transformIncrease() BinaryTransform { 62 return transformIncreaseFn 63 } 64 65 // increase computes the difference between consecutive datapoints, unlike 66 // perSecond it does not account for the time interval between the values. 67 // Note: 68 // * It skips NaN values. If the previous value is a NaN value, it uses a previous value of 0. 69 // * It assumes the timestamps are monotonically increasing, and values are non-decreasing. 70 // If either of the two conditions is not met, an empty datapoint is returned. 71 func increase(prev, curr Datapoint, _ FeatureFlags) Datapoint { 72 if prev.TimeNanos >= curr.TimeNanos { 73 return emptyDatapoint 74 } 75 if math.IsNaN(curr.Value) { 76 return emptyDatapoint 77 } 78 79 if math.IsNaN(prev.Value) { 80 prev.Value = 0 81 } 82 83 diff := curr.Value - prev.Value 84 if diff < 0 { 85 return emptyDatapoint 86 } 87 return Datapoint{TimeNanos: curr.TimeNanos, Value: diff} 88 }