github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/graphite/ts/series_reducer.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 ts 22 23 import "fmt" 24 25 // SeriesReducerApproach defines an approach to reduce a series to a single value. 26 type SeriesReducerApproach string 27 28 // The standard set of reducers 29 const ( 30 SeriesReducerAvg SeriesReducerApproach = "avg" 31 SeriesReducerSum SeriesReducerApproach = "sum" 32 SeriesReducerMin SeriesReducerApproach = "min" 33 SeriesReducerMax SeriesReducerApproach = "max" 34 SeriesReducerStdDev SeriesReducerApproach = "stddev" 35 SeriesReducerLast SeriesReducerApproach = "last" 36 37 SeriesReducerAverage SeriesReducerApproach = "average" // alias for "avg" 38 SeriesReducerTotal SeriesReducerApproach = "total" // alias for "sum" 39 SeriesReducerCurrent SeriesReducerApproach = "current" // alias for "last" 40 ) 41 42 // SeriesReducer reduces a series to a single value. 43 type SeriesReducer func(*Series) float64 44 45 // SafeReducer returns a boolean indicating whether it is a valid reducer, 46 // and if so, the SeriesReducer implementing the SeriesReducerApproach. 47 func (sa SeriesReducerApproach) SafeReducer() (SeriesReducer, bool) { 48 r, ok := seriesReducers[sa] 49 return r, ok 50 } 51 52 // Reducer returns the SeriesReducer implementing the SeriesReducerApproach. 53 func (sa SeriesReducerApproach) Reducer() SeriesReducer { 54 r, ok := sa.SafeReducer() 55 if !ok { 56 panic(fmt.Sprintf("No reducer func for %s", sa)) 57 } 58 return r 59 } 60 61 var seriesReducers = map[SeriesReducerApproach]SeriesReducer{ 62 SeriesReducerAvg: func(b *Series) float64 { return b.SafeAvg() }, 63 SeriesReducerAverage: func(b *Series) float64 { return b.SafeAvg() }, 64 SeriesReducerTotal: func(b *Series) float64 { return b.SafeSum() }, 65 SeriesReducerSum: func(b *Series) float64 { return b.SafeSum() }, 66 SeriesReducerMin: func(b *Series) float64 { return b.SafeMin() }, 67 SeriesReducerMax: func(b *Series) float64 { return b.SafeMax() }, 68 SeriesReducerStdDev: func(b *Series) float64 { return b.SafeStdDev() }, 69 SeriesReducerLast: func(b *Series) float64 { return b.SafeLastValue() }, 70 SeriesReducerCurrent: func(b *Series) float64 { return b.SafeLastValue() }, 71 }