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