github.com/m3db/m3@v1.5.0/src/query/graphite/ts/datapoint.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 (
    24  	"math"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/query/graphite/stats"
    28  )
    29  
    30  // A Datapoint is a single data value reported at a given time
    31  type Datapoint struct {
    32  	Timestamp time.Time
    33  	Value     float64
    34  }
    35  
    36  // ValueIsNaN returns true iff underlying value is NaN
    37  func (d Datapoint) ValueIsNaN() bool { return math.IsNaN(d.Value) }
    38  
    39  // DatapointsByTimestamp is a sortable interface for datapoints
    40  type DatapointsByTimestamp []Datapoint
    41  
    42  // Len is the length of the datapoints
    43  func (p DatapointsByTimestamp) Len() int { return len(p) }
    44  
    45  // Less compares two datapoints by timestamp
    46  func (p DatapointsByTimestamp) Less(i, j int) bool { return p[i].Timestamp.Before(p[j].Timestamp) }
    47  
    48  // Swap swaps two datapoints
    49  func (p DatapointsByTimestamp) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    50  
    51  // ConsolidatedValue represents a time window of consolidated data
    52  type ConsolidatedValue struct {
    53  	// StartTime is the start time of the time window covered by this
    54  	// consolidation
    55  	StartTime time.Time
    56  
    57  	// EndTime is the end time of the time window covered by this
    58  	// consolidation
    59  	EndTime time.Time
    60  
    61  	// Values is the statistics for that consolidated time window
    62  	Values stats.Statistics
    63  }
    64  
    65  // ConsolidatedValuesByStartTime is a sortable interface for consolidated values
    66  type ConsolidatedValuesByStartTime []ConsolidatedValue
    67  
    68  // Len is the length of the values
    69  func (p ConsolidatedValuesByStartTime) Len() int { return len(p) }
    70  
    71  // Less compares two values by start time
    72  func (p ConsolidatedValuesByStartTime) Less(i, j int) bool {
    73  	return p[i].StartTime.Before(p[j].StartTime)
    74  }
    75  
    76  // Swap swaps two values
    77  func (p ConsolidatedValuesByStartTime) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    78  
    79  // Datapoints is a list of datapoints that implement the stats.Values interface.
    80  type Datapoints []Datapoint
    81  
    82  // Len is the length of the array.
    83  func (d Datapoints) Len() int { return len(d) }
    84  
    85  // ValueAt returns the value at the nth element.
    86  func (d Datapoints) ValueAt(n int) float64 { return d[n].Value }
    87  
    88  // AllNaN returns true if all the values are NaN
    89  func (d Datapoints) AllNaN() bool {
    90  	for _, dp := range d {
    91  		if !dp.ValueIsNaN() {
    92  			return false
    93  		}
    94  	}
    95  	return true
    96  }