value.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. "sort"
  18. "strconv"
  19. "strings"
  20. )
  21. // A SampleValue is a representation of a value for a given sample at a given
  22. // time.
  23. type SampleValue float64
  24. // MarshalJSON implements json.Marshaler.
  25. func (v SampleValue) MarshalJSON() ([]byte, error) {
  26. return json.Marshal(v.String())
  27. }
  28. // UnmarshalJSON implements json.Unmarshaler.
  29. func (v *SampleValue) UnmarshalJSON(b []byte) error {
  30. if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
  31. return fmt.Errorf("sample value must be a quoted string")
  32. }
  33. f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64)
  34. if err != nil {
  35. return err
  36. }
  37. *v = SampleValue(f)
  38. return nil
  39. }
  40. func (v SampleValue) Equal(o SampleValue) bool {
  41. return v == o
  42. }
  43. func (v SampleValue) String() string {
  44. return strconv.FormatFloat(float64(v), 'f', -1, 64)
  45. }
  46. // SamplePair pairs a SampleValue with a Timestamp.
  47. type SamplePair struct {
  48. Timestamp Time
  49. Value SampleValue
  50. }
  51. // MarshalJSON implements json.Marshaler.
  52. func (s SamplePair) MarshalJSON() ([]byte, error) {
  53. t, err := json.Marshal(s.Timestamp)
  54. if err != nil {
  55. return nil, err
  56. }
  57. v, err := json.Marshal(s.Value)
  58. if err != nil {
  59. return nil, err
  60. }
  61. return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
  62. }
  63. // UnmarshalJSON implements json.Unmarshaler.
  64. func (s *SamplePair) UnmarshalJSON(b []byte) error {
  65. v := [...]json.Unmarshaler{&s.Timestamp, &s.Value}
  66. return json.Unmarshal(b, &v)
  67. }
  68. // Equal returns true if this SamplePair and o have equal Values and equal
  69. // Timestamps.
  70. func (s *SamplePair) Equal(o *SamplePair) bool {
  71. return s == o || (s.Value == o.Value && s.Timestamp.Equal(o.Timestamp))
  72. }
  73. func (s SamplePair) String() string {
  74. return fmt.Sprintf("%s @[%s]", s.Value, s.Timestamp)
  75. }
  76. // Sample is a sample pair associated with a metric.
  77. type Sample struct {
  78. Metric Metric `json:"metric"`
  79. Value SampleValue `json:"value"`
  80. Timestamp Time `json:"timestamp"`
  81. }
  82. // Equal compares first the metrics, then the timestamp, then the value.
  83. func (s *Sample) Equal(o *Sample) bool {
  84. if s == o {
  85. return true
  86. }
  87. if !s.Metric.Equal(o.Metric) {
  88. return false
  89. }
  90. if !s.Timestamp.Equal(o.Timestamp) {
  91. return false
  92. }
  93. if s.Value != o.Value {
  94. return false
  95. }
  96. return true
  97. }
  98. func (s Sample) String() string {
  99. return fmt.Sprintf("%s => %s", s.Metric, SamplePair{
  100. Timestamp: s.Timestamp,
  101. Value: s.Value,
  102. })
  103. }
  104. // MarshalJSON implements json.Marshaler.
  105. func (s Sample) MarshalJSON() ([]byte, error) {
  106. v := struct {
  107. Metric Metric `json:"metric"`
  108. Value SamplePair `json:"value"`
  109. }{
  110. Metric: s.Metric,
  111. Value: SamplePair{
  112. Timestamp: s.Timestamp,
  113. Value: s.Value,
  114. },
  115. }
  116. return json.Marshal(&v)
  117. }
  118. // UnmarshalJSON implements json.Unmarshaler.
  119. func (s *Sample) UnmarshalJSON(b []byte) error {
  120. v := struct {
  121. Metric Metric `json:"metric"`
  122. Value SamplePair `json:"value"`
  123. }{
  124. Metric: s.Metric,
  125. Value: SamplePair{
  126. Timestamp: s.Timestamp,
  127. Value: s.Value,
  128. },
  129. }
  130. if err := json.Unmarshal(b, &v); err != nil {
  131. return err
  132. }
  133. s.Metric = v.Metric
  134. s.Timestamp = v.Value.Timestamp
  135. s.Value = v.Value.Value
  136. return nil
  137. }
  138. // Samples is a sortable Sample slice. It implements sort.Interface.
  139. type Samples []*Sample
  140. func (s Samples) Len() int {
  141. return len(s)
  142. }
  143. // Less compares first the metrics, then the timestamp.
  144. func (s Samples) Less(i, j int) bool {
  145. switch {
  146. case s[i].Metric.Before(s[j].Metric):
  147. return true
  148. case s[j].Metric.Before(s[i].Metric):
  149. return false
  150. case s[i].Timestamp.Before(s[j].Timestamp):
  151. return true
  152. default:
  153. return false
  154. }
  155. }
  156. func (s Samples) Swap(i, j int) {
  157. s[i], s[j] = s[j], s[i]
  158. }
  159. // Equal compares two sets of samples and returns true if they are equal.
  160. func (s Samples) Equal(o Samples) bool {
  161. if len(s) != len(o) {
  162. return false
  163. }
  164. for i, sample := range s {
  165. if !sample.Equal(o[i]) {
  166. return false
  167. }
  168. }
  169. return true
  170. }
  171. // SampleStream is a stream of Values belonging to an attached COWMetric.
  172. type SampleStream struct {
  173. Metric Metric `json:"metric"`
  174. Values []SamplePair `json:"values"`
  175. }
  176. func (ss SampleStream) String() string {
  177. vals := make([]string, len(ss.Values))
  178. for i, v := range ss.Values {
  179. vals[i] = v.String()
  180. }
  181. return fmt.Sprintf("%s =>\n%s", ss.Metric, strings.Join(vals, "\n"))
  182. }
  183. // Value is a generic interface for values resulting from a query evaluation.
  184. type Value interface {
  185. Type() ValueType
  186. String() string
  187. }
  188. func (Matrix) Type() ValueType { return ValMatrix }
  189. func (Vector) Type() ValueType { return ValVector }
  190. func (*Scalar) Type() ValueType { return ValScalar }
  191. func (*String) Type() ValueType { return ValString }
  192. type ValueType int
  193. const (
  194. ValNone ValueType = iota
  195. ValScalar
  196. ValVector
  197. ValMatrix
  198. ValString
  199. )
  200. // MarshalJSON implements json.Marshaler.
  201. func (et ValueType) MarshalJSON() ([]byte, error) {
  202. return json.Marshal(et.String())
  203. }
  204. func (et *ValueType) UnmarshalJSON(b []byte) error {
  205. var s string
  206. if err := json.Unmarshal(b, &s); err != nil {
  207. return err
  208. }
  209. switch s {
  210. case "<ValNone>":
  211. *et = ValNone
  212. case "scalar":
  213. *et = ValScalar
  214. case "vector":
  215. *et = ValVector
  216. case "matrix":
  217. *et = ValMatrix
  218. case "string":
  219. *et = ValString
  220. default:
  221. return fmt.Errorf("unknown value type %q", s)
  222. }
  223. return nil
  224. }
  225. func (e ValueType) String() string {
  226. switch e {
  227. case ValNone:
  228. return "<ValNone>"
  229. case ValScalar:
  230. return "scalar"
  231. case ValVector:
  232. return "vector"
  233. case ValMatrix:
  234. return "matrix"
  235. case ValString:
  236. return "string"
  237. }
  238. panic("ValueType.String: unhandled value type")
  239. }
  240. // Scalar is a scalar value evaluated at the set timestamp.
  241. type Scalar struct {
  242. Value SampleValue `json:"value"`
  243. Timestamp Time `json:"timestamp"`
  244. }
  245. func (s Scalar) String() string {
  246. return fmt.Sprintf("scalar: %v @[%v]", s.Value, s.Timestamp)
  247. }
  248. // MarshalJSON implements json.Marshaler.
  249. func (s Scalar) MarshalJSON() ([]byte, error) {
  250. v := strconv.FormatFloat(float64(s.Value), 'f', -1, 64)
  251. return json.Marshal([...]interface{}{s.Timestamp, string(v)})
  252. }
  253. // UnmarshalJSON implements json.Unmarshaler.
  254. func (s *Scalar) UnmarshalJSON(b []byte) error {
  255. var f string
  256. v := [...]interface{}{&s.Timestamp, &f}
  257. if err := json.Unmarshal(b, &v); err != nil {
  258. return err
  259. }
  260. value, err := strconv.ParseFloat(f, 64)
  261. if err != nil {
  262. return fmt.Errorf("error parsing sample value: %s", err)
  263. }
  264. s.Value = SampleValue(value)
  265. return nil
  266. }
  267. // String is a string value evaluated at the set timestamp.
  268. type String struct {
  269. Value string `json:"value"`
  270. Timestamp Time `json:"timestamp"`
  271. }
  272. func (s *String) String() string {
  273. return s.Value
  274. }
  275. // MarshalJSON implements json.Marshaler.
  276. func (s String) MarshalJSON() ([]byte, error) {
  277. return json.Marshal([]interface{}{s.Timestamp, s.Value})
  278. }
  279. // UnmarshalJSON implements json.Unmarshaler.
  280. func (s *String) UnmarshalJSON(b []byte) error {
  281. v := [...]interface{}{&s.Timestamp, &s.Value}
  282. return json.Unmarshal(b, &v)
  283. }
  284. // Vector is basically only an alias for Samples, but the
  285. // contract is that in a Vector, all Samples have the same timestamp.
  286. type Vector []*Sample
  287. func (vec Vector) String() string {
  288. entries := make([]string, len(vec))
  289. for i, s := range vec {
  290. entries[i] = s.String()
  291. }
  292. return strings.Join(entries, "\n")
  293. }
  294. func (vec Vector) Len() int { return len(vec) }
  295. func (vec Vector) Swap(i, j int) { vec[i], vec[j] = vec[j], vec[i] }
  296. // Less compares first the metrics, then the timestamp.
  297. func (vec Vector) Less(i, j int) bool {
  298. switch {
  299. case vec[i].Metric.Before(vec[j].Metric):
  300. return true
  301. case vec[j].Metric.Before(vec[i].Metric):
  302. return false
  303. case vec[i].Timestamp.Before(vec[j].Timestamp):
  304. return true
  305. default:
  306. return false
  307. }
  308. }
  309. // Equal compares two sets of samples and returns true if they are equal.
  310. func (vec Vector) Equal(o Vector) bool {
  311. if len(vec) != len(o) {
  312. return false
  313. }
  314. for i, sample := range vec {
  315. if !sample.Equal(o[i]) {
  316. return false
  317. }
  318. }
  319. return true
  320. }
  321. // Matrix is a list of time series.
  322. type Matrix []*SampleStream
  323. func (m Matrix) Len() int { return len(m) }
  324. func (m Matrix) Less(i, j int) bool { return m[i].Metric.Before(m[j].Metric) }
  325. func (m Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
  326. func (mat Matrix) String() string {
  327. matCp := make(Matrix, len(mat))
  328. copy(matCp, mat)
  329. sort.Sort(matCp)
  330. strs := make([]string, len(matCp))
  331. for i, ss := range matCp {
  332. strs[i] = ss.String()
  333. }
  334. return strings.Join(strs, "\n")
  335. }