...

Source file src/github.com/prometheus/common/model/value_float.go

Documentation: github.com/prometheus/common/model

     1  // Copyright 2013 The Prometheus Authors
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package model
    15  
    16  import (
    17  	"encoding/json"
    18  	"fmt"
    19  	"math"
    20  	"strconv"
    21  )
    22  
    23  // ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
    24  // non-existing sample pair. It is a SamplePair with timestamp Earliest and
    25  // value 0.0. Note that the natural zero value of SamplePair has a timestamp
    26  // of 0, which is possible to appear in a real SamplePair and thus not
    27  // suitable to signal a non-existing SamplePair.
    28  var ZeroSamplePair = SamplePair{Timestamp: Earliest}
    29  
    30  // A SampleValue is a representation of a value for a given sample at a given
    31  // time.
    32  type SampleValue float64
    33  
    34  // MarshalJSON implements json.Marshaler.
    35  func (v SampleValue) MarshalJSON() ([]byte, error) {
    36  	return json.Marshal(v.String())
    37  }
    38  
    39  // UnmarshalJSON implements json.Unmarshaler.
    40  func (v *SampleValue) UnmarshalJSON(b []byte) error {
    41  	if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
    42  		return fmt.Errorf("sample value must be a quoted string")
    43  	}
    44  	f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	*v = SampleValue(f)
    49  	return nil
    50  }
    51  
    52  // Equal returns true if the value of v and o is equal or if both are NaN. Note
    53  // that v==o is false if both are NaN. If you want the conventional float
    54  // behavior, use == to compare two SampleValues.
    55  func (v SampleValue) Equal(o SampleValue) bool {
    56  	if v == o {
    57  		return true
    58  	}
    59  	return math.IsNaN(float64(v)) && math.IsNaN(float64(o))
    60  }
    61  
    62  func (v SampleValue) String() string {
    63  	return strconv.FormatFloat(float64(v), 'f', -1, 64)
    64  }
    65  
    66  // SamplePair pairs a SampleValue with a Timestamp.
    67  type SamplePair struct {
    68  	Timestamp Time
    69  	Value     SampleValue
    70  }
    71  
    72  func (s SamplePair) MarshalJSON() ([]byte, error) {
    73  	t, err := json.Marshal(s.Timestamp)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	v, err := json.Marshal(s.Value)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
    82  }
    83  
    84  // UnmarshalJSON implements json.Unmarshaler.
    85  func (s *SamplePair) UnmarshalJSON(b []byte) error {
    86  	v := [...]json.Unmarshaler{&s.Timestamp, &s.Value}
    87  	return json.Unmarshal(b, &v)
    88  }
    89  
    90  // Equal returns true if this SamplePair and o have equal Values and equal
    91  // Timestamps. The semantics of Value equality is defined by SampleValue.Equal.
    92  func (s *SamplePair) Equal(o *SamplePair) bool {
    93  	return s == o || (s.Value.Equal(o.Value) && s.Timestamp.Equal(o.Timestamp))
    94  }
    95  
    96  func (s SamplePair) String() string {
    97  	return fmt.Sprintf("%s @[%s]", s.Value, s.Timestamp)
    98  }
    99  

View as plain text