1
2
3
4
5
6
7
8
9
10
11
12
13
14 package model
15
16 import (
17 "encoding/json"
18 "fmt"
19 "strconv"
20 "strings"
21 )
22
23 type FloatString float64
24
25 func (v FloatString) String() string {
26 return strconv.FormatFloat(float64(v), 'f', -1, 64)
27 }
28
29 func (v FloatString) MarshalJSON() ([]byte, error) {
30 return json.Marshal(v.String())
31 }
32
33 func (v *FloatString) UnmarshalJSON(b []byte) error {
34 if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
35 return fmt.Errorf("float value must be a quoted string")
36 }
37 f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64)
38 if err != nil {
39 return err
40 }
41 *v = FloatString(f)
42 return nil
43 }
44
45 type HistogramBucket struct {
46 Boundaries int32
47 Lower FloatString
48 Upper FloatString
49 Count FloatString
50 }
51
52 func (s HistogramBucket) MarshalJSON() ([]byte, error) {
53 b, err := json.Marshal(s.Boundaries)
54 if err != nil {
55 return nil, err
56 }
57 l, err := json.Marshal(s.Lower)
58 if err != nil {
59 return nil, err
60 }
61 u, err := json.Marshal(s.Upper)
62 if err != nil {
63 return nil, err
64 }
65 c, err := json.Marshal(s.Count)
66 if err != nil {
67 return nil, err
68 }
69 return []byte(fmt.Sprintf("[%s,%s,%s,%s]", b, l, u, c)), nil
70 }
71
72 func (s *HistogramBucket) UnmarshalJSON(buf []byte) error {
73 tmp := []interface{}{&s.Boundaries, &s.Lower, &s.Upper, &s.Count}
74 wantLen := len(tmp)
75 if err := json.Unmarshal(buf, &tmp); err != nil {
76 return err
77 }
78 if gotLen := len(tmp); gotLen != wantLen {
79 return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
80 }
81 return nil
82 }
83
84 func (s *HistogramBucket) Equal(o *HistogramBucket) bool {
85 return s == o || (s.Boundaries == o.Boundaries && s.Lower == o.Lower && s.Upper == o.Upper && s.Count == o.Count)
86 }
87
88 func (b HistogramBucket) String() string {
89 var sb strings.Builder
90 lowerInclusive := b.Boundaries == 1 || b.Boundaries == 3
91 upperInclusive := b.Boundaries == 0 || b.Boundaries == 3
92 if lowerInclusive {
93 sb.WriteRune('[')
94 } else {
95 sb.WriteRune('(')
96 }
97 fmt.Fprintf(&sb, "%g,%g", b.Lower, b.Upper)
98 if upperInclusive {
99 sb.WriteRune(']')
100 } else {
101 sb.WriteRune(')')
102 }
103 fmt.Fprintf(&sb, ":%v", b.Count)
104 return sb.String()
105 }
106
107 type HistogramBuckets []*HistogramBucket
108
109 func (s HistogramBuckets) Equal(o HistogramBuckets) bool {
110 if len(s) != len(o) {
111 return false
112 }
113
114 for i, bucket := range s {
115 if !bucket.Equal(o[i]) {
116 return false
117 }
118 }
119 return true
120 }
121
122 type SampleHistogram struct {
123 Count FloatString `json:"count"`
124 Sum FloatString `json:"sum"`
125 Buckets HistogramBuckets `json:"buckets"`
126 }
127
128 func (s SampleHistogram) String() string {
129 return fmt.Sprintf("Count: %f, Sum: %f, Buckets: %v", s.Count, s.Sum, s.Buckets)
130 }
131
132 func (s *SampleHistogram) Equal(o *SampleHistogram) bool {
133 return s == o || (s.Count == o.Count && s.Sum == o.Sum && s.Buckets.Equal(o.Buckets))
134 }
135
136 type SampleHistogramPair struct {
137 Timestamp Time
138
139 Histogram *SampleHistogram
140 }
141
142 func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
143 if s.Histogram == nil {
144 return nil, fmt.Errorf("histogram is nil")
145 }
146 t, err := json.Marshal(s.Timestamp)
147 if err != nil {
148 return nil, err
149 }
150 v, err := json.Marshal(s.Histogram)
151 if err != nil {
152 return nil, err
153 }
154 return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
155 }
156
157 func (s *SampleHistogramPair) UnmarshalJSON(buf []byte) error {
158 tmp := []interface{}{&s.Timestamp, &s.Histogram}
159 wantLen := len(tmp)
160 if err := json.Unmarshal(buf, &tmp); err != nil {
161 return err
162 }
163 if gotLen := len(tmp); gotLen != wantLen {
164 return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
165 }
166 if s.Histogram == nil {
167 return fmt.Errorf("histogram is null")
168 }
169 return nil
170 }
171
172 func (s SampleHistogramPair) String() string {
173 return fmt.Sprintf("%s @[%s]", s.Histogram, s.Timestamp)
174 }
175
176 func (s *SampleHistogramPair) Equal(o *SampleHistogramPair) bool {
177 return s == o || (s.Histogram.Equal(o.Histogram) && s.Timestamp.Equal(o.Timestamp))
178 }
179
View as plain text