github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/aggregator/aggregation/counter.go (about)

     1  // Copyright (c) 2016 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 aggregation
    22  
    23  import (
    24  	"math"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/metrics/aggregation"
    28  )
    29  
    30  // Counter aggregates counter values.
    31  type Counter struct {
    32  	Options
    33  
    34  	lastAt     time.Time
    35  	annotation []byte
    36  	sum        int64
    37  	sumSq      int64
    38  	count      int64
    39  	max        int64
    40  	min        int64
    41  }
    42  
    43  // NewCounter creates a new counter.
    44  func NewCounter(opts Options) Counter {
    45  	return Counter{
    46  		Options: opts,
    47  		max:     math.MinInt64,
    48  		min:     math.MaxInt64,
    49  	}
    50  }
    51  
    52  // Update updates the counter value.
    53  func (c *Counter) Update(timestamp time.Time, value int64, annotation []byte) {
    54  	if c.lastAt.IsZero() || timestamp.After(c.lastAt) {
    55  		// NB(r): Only set the last value if this value arrives
    56  		// after the wall clock timestamp of previous values, not
    57  		// the arrival time (i.e. order received).
    58  		c.lastAt = timestamp
    59  	} else {
    60  		c.Options.Metrics.Counter.IncValuesOutOfOrder()
    61  	}
    62  
    63  	c.sum += value
    64  
    65  	c.count++
    66  	if c.max < value {
    67  		c.max = value
    68  	}
    69  	if c.min > value {
    70  		c.min = value
    71  	}
    72  
    73  	if c.HasExpensiveAggregations {
    74  		c.sumSq += value * value
    75  	}
    76  
    77  	c.annotation = MaybeReplaceAnnotation(c.annotation, annotation)
    78  }
    79  
    80  // LastAt returns the time of the last value received.
    81  func (c *Counter) LastAt() time.Time { return c.lastAt }
    82  
    83  // Count returns the number of values received.
    84  func (c *Counter) Count() int64 { return c.count }
    85  
    86  // Sum returns the sum of counter values.
    87  func (c *Counter) Sum() int64 { return c.sum }
    88  
    89  // SumSq returns the squared sum of counter values.
    90  func (c *Counter) SumSq() int64 { return c.sumSq }
    91  
    92  // Mean returns the mean counter value.
    93  func (c *Counter) Mean() float64 {
    94  	if c.count == 0 {
    95  		return 0
    96  	}
    97  	return float64(c.sum) / float64(c.count)
    98  }
    99  
   100  // Stdev returns the standard deviation counter value.
   101  func (c *Counter) Stdev() float64 {
   102  	return stdev(c.count, float64(c.sumSq), float64(c.sum))
   103  }
   104  
   105  // Min returns the minimum counter value.
   106  func (c *Counter) Min() int64 { return c.min }
   107  
   108  // Max returns the maximum counter value.
   109  func (c *Counter) Max() int64 { return c.max }
   110  
   111  // ValueOf returns the value for the aggregation type.
   112  func (c *Counter) ValueOf(aggType aggregation.Type) float64 {
   113  	switch aggType {
   114  	case aggregation.Min:
   115  		return float64(c.Min())
   116  	case aggregation.Max:
   117  		return float64(c.Max())
   118  	case aggregation.Mean:
   119  		return c.Mean()
   120  	case aggregation.Count:
   121  		return float64(c.Count())
   122  	case aggregation.Sum:
   123  		return float64(c.Sum())
   124  	case aggregation.SumSq:
   125  		return float64(c.SumSq())
   126  	case aggregation.Stdev:
   127  		return c.Stdev()
   128  	default:
   129  		return 0
   130  	}
   131  }
   132  
   133  // Annotation returns the annotation associated with the counter.
   134  func (c *Counter) Annotation() []byte {
   135  	return c.annotation
   136  }
   137  
   138  // Close closes the counter.
   139  func (c *Counter) Close() {}