github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/functions/temporal/functions.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 temporal
    22  
    23  import (
    24  	"fmt"
    25  	"math"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/query/executor/transform"
    29  	"github.com/m3db/m3/src/query/ts"
    30  )
    31  
    32  const (
    33  	// ResetsType returns the number of counter resets within the provided time
    34  	// range as a time series. Any decrease in the value between two consecutive
    35  	// datapoints is interpreted as a counter reset.
    36  	// ResetsTemporalType should only be used with counters.
    37  	ResetsType = "resets"
    38  
    39  	// ChangesType returns the number of times a value changes within the
    40  	// provided time range for a given time series.
    41  	ChangesType = "changes"
    42  )
    43  
    44  type comparisonFunc func(a, b float64) bool
    45  
    46  type functionProcessor struct {
    47  	compFunc comparisonFunc
    48  }
    49  
    50  func (f functionProcessor) initialize(
    51  	_ time.Duration,
    52  	_ transform.Options,
    53  ) processor {
    54  	return &functionNode{ 
    55  		comparisonFunc: f.compFunc,
    56  	}
    57  }
    58  
    59  // NewFunctionOp creates a new base temporal transform for functions
    60  func NewFunctionOp(args []interface{}, optype string) (transform.Params, error) {
    61  	var compFunc comparisonFunc
    62  
    63  	switch optype {
    64  	case ResetsType:
    65  		compFunc = func(a, b float64) bool { return a < b }
    66  	case ChangesType:
    67  		compFunc = func(a, b float64) bool { return a != b }
    68  	default:
    69  		return nil, fmt.Errorf("unknown function type: %s", optype)
    70  	}
    71  
    72  	duration, ok := args[0].(time.Duration)
    73  	if !ok {
    74  		return emptyOp, fmt.
    75  			Errorf("unable to cast to scalar argument: %v for %s", args[0], optype)
    76  	}
    77  
    78  	f := functionProcessor{
    79  		compFunc: compFunc,
    80  	}
    81  
    82  	return newBaseOp(duration, optype, f)
    83  }
    84  
    85  type functionNode struct { 
    86  	comparisonFunc comparisonFunc
    87  }
    88  
    89  func (f *functionNode) process(datapoints ts.Datapoints, _ iterationBounds) float64 {
    90  	if len(datapoints) == 0 {
    91  		return math.NaN()
    92  	}
    93  
    94  	allNaNs := true
    95  	result := 0.0
    96  	prev := datapoints[0].Value
    97  
    98  	for _, curr := range datapoints[1:] {
    99  		if math.IsNaN(curr.Value) {
   100  			continue
   101  		}
   102  
   103  		allNaNs = false
   104  		if !math.IsNaN(prev) {
   105  			if f.comparisonFunc(curr.Value, prev) {
   106  				result++
   107  			}
   108  		}
   109  
   110  		prev = curr.Value
   111  	}
   112  
   113  	if allNaNs {
   114  		return math.NaN()
   115  	}
   116  
   117  	return result
   118  }