value_float.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package model
  14. import (
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "math"
  19. "strconv"
  20. )
  21. // ZeroSamplePair is the pseudo zero-value of SamplePair used to signal a
  22. // non-existing sample pair. It is a SamplePair with timestamp Earliest and
  23. // value 0.0. Note that the natural zero value of SamplePair has a timestamp
  24. // of 0, which is possible to appear in a real SamplePair and thus not
  25. // suitable to signal a non-existing SamplePair.
  26. var ZeroSamplePair = SamplePair{Timestamp: Earliest}
  27. // A SampleValue is a representation of a value for a given sample at a given
  28. // time.
  29. type SampleValue float64
  30. // MarshalJSON implements json.Marshaler.
  31. func (v SampleValue) MarshalJSON() ([]byte, error) {
  32. return json.Marshal(v.String())
  33. }
  34. // UnmarshalJSON implements json.Unmarshaler.
  35. func (v *SampleValue) UnmarshalJSON(b []byte) error {
  36. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  37. return errors.New("sample value must be a quoted string")
  38. }
  39. f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64)
  40. if err != nil {
  41. return err
  42. }
  43. *v = SampleValue(f)
  44. return nil
  45. }
  46. // Equal returns true if the value of v and o is equal or if both are NaN. Note
  47. // that v==o is false if both are NaN. If you want the conventional float
  48. // behavior, use == to compare two SampleValues.
  49. func (v SampleValue) Equal(o SampleValue) bool {
  50. if v == o {
  51. return true
  52. }
  53. return math.IsNaN(float64(v)) && math.IsNaN(float64(o))
  54. }
  55. func (v SampleValue) String() string {
  56. return strconv.FormatFloat(float64(v), 'f', -1, 64)
  57. }
  58. // SamplePair pairs a SampleValue with a Timestamp.
  59. type SamplePair struct {
  60. Timestamp Time
  61. Value SampleValue
  62. }
  63. func (s SamplePair) MarshalJSON() ([]byte, error) {
  64. t, err := json.Marshal(s.Timestamp)
  65. if err != nil {
  66. return nil, err
  67. }
  68. v, err := json.Marshal(s.Value)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
  73. }
  74. // UnmarshalJSON implements json.Unmarshaler.
  75. func (s *SamplePair) UnmarshalJSON(b []byte) error {
  76. v := [...]json.Unmarshaler{&s.Timestamp, &s.Value}
  77. return json.Unmarshal(b, &v)
  78. }
  79. // Equal returns true if this SamplePair and o have equal Values and equal
  80. // Timestamps. The semantics of Value equality is defined by SampleValue.Equal.
  81. func (s *SamplePair) Equal(o *SamplePair) bool {
  82. return s == o || (s.Value.Equal(o.Value) && s.Timestamp.Equal(o.Timestamp))
  83. }
  84. func (s SamplePair) String() string {
  85. return fmt.Sprintf("%s @[%s]", s.Value, s.Timestamp)
  86. }