You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

101 lines
3.0 KiB

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