value.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright 2017 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 promql
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "github.com/influxdata/promql/v2/pkg/labels"
  20. )
  21. // Value is a generic interface for values resulting from a query evaluation.
  22. type Value interface {
  23. Type() ValueType
  24. String() string
  25. }
  26. func (Matrix) Type() ValueType { return ValueTypeMatrix }
  27. func (Vector) Type() ValueType { return ValueTypeVector }
  28. func (Scalar) Type() ValueType { return ValueTypeScalar }
  29. func (String) Type() ValueType { return ValueTypeString }
  30. // ValueType describes a type of a value.
  31. type ValueType string
  32. // The valid value types.
  33. const (
  34. ValueTypeNone = "none"
  35. ValueTypeVector = "vector"
  36. ValueTypeScalar = "scalar"
  37. ValueTypeMatrix = "matrix"
  38. ValueTypeString = "string"
  39. )
  40. // String represents a string value.
  41. type String struct {
  42. T int64
  43. V string
  44. }
  45. func (s String) String() string {
  46. return s.V
  47. }
  48. func (s String) MarshalJSON() ([]byte, error) {
  49. return json.Marshal([...]interface{}{float64(s.T) / 1000, s.V})
  50. }
  51. // Scalar is a data point that's explicitly not associated with a metric.
  52. type Scalar struct {
  53. T int64
  54. V float64
  55. }
  56. func (s Scalar) String() string {
  57. v := strconv.FormatFloat(s.V, 'f', -1, 64)
  58. return fmt.Sprintf("scalar: %v @[%v]", v, s.T)
  59. }
  60. func (s Scalar) MarshalJSON() ([]byte, error) {
  61. v := strconv.FormatFloat(s.V, 'f', -1, 64)
  62. return json.Marshal([...]interface{}{float64(s.T) / 1000, v})
  63. }
  64. // Series is a stream of data points belonging to a metric.
  65. type Series struct {
  66. Metric labels.Labels `json:"metric"`
  67. Points []Point `json:"values"`
  68. }
  69. func (s Series) String() string {
  70. vals := make([]string, len(s.Points))
  71. for i, v := range s.Points {
  72. vals[i] = v.String()
  73. }
  74. return fmt.Sprintf("%s =>\n%s", s.Metric, strings.Join(vals, "\n"))
  75. }
  76. // Point represents a single data point for a given timestamp.
  77. type Point struct {
  78. T int64
  79. V float64
  80. }
  81. func (p Point) String() string {
  82. v := strconv.FormatFloat(p.V, 'f', -1, 64)
  83. return fmt.Sprintf("%v @[%v]", v, p.T)
  84. }
  85. // MarshalJSON implements json.Marshaler.
  86. func (p Point) MarshalJSON() ([]byte, error) {
  87. v := strconv.FormatFloat(p.V, 'f', -1, 64)
  88. return json.Marshal([...]interface{}{float64(p.T) / 1000, v})
  89. }
  90. // Sample is a single sample belonging to a metric.
  91. type Sample struct {
  92. Point
  93. Metric labels.Labels
  94. }
  95. func (s Sample) String() string {
  96. return fmt.Sprintf("%s => %s", s.Metric, s.Point)
  97. }
  98. func (s Sample) MarshalJSON() ([]byte, error) {
  99. v := struct {
  100. M labels.Labels `json:"metric"`
  101. V Point `json:"value"`
  102. }{
  103. M: s.Metric,
  104. V: s.Point,
  105. }
  106. return json.Marshal(v)
  107. }
  108. // Vector is basically only an alias for model.Samples, but the
  109. // contract is that in a Vector, all Samples have the same timestamp.
  110. type Vector []Sample
  111. func (vec Vector) String() string {
  112. entries := make([]string, len(vec))
  113. for i, s := range vec {
  114. entries[i] = s.String()
  115. }
  116. return strings.Join(entries, "\n")
  117. }
  118. // ContainsSameLabelset checks if a vector has samples with the same labelset
  119. // Such a behavior is semantically undefined
  120. // https://github.com/prometheus/prometheus/issues/4562
  121. func (vec Vector) ContainsSameLabelset() bool {
  122. l := make(map[uint64]struct{}, len(vec))
  123. for _, s := range vec {
  124. hash := s.Metric.Hash()
  125. if _, ok := l[hash]; ok {
  126. return true
  127. }
  128. l[hash] = struct{}{}
  129. }
  130. return false
  131. }
  132. // Matrix is a slice of Series that implements sort.Interface and
  133. // has a String method.
  134. type Matrix []Series
  135. func (m Matrix) String() string {
  136. // TODO(fabxc): sort, or can we rely on order from the querier?
  137. strs := make([]string, len(m))
  138. for i, ss := range m {
  139. strs[i] = ss.String()
  140. }
  141. return strings.Join(strs, "\n")
  142. }
  143. // TotalSamples returns the total number of samples in the series within a matrix.
  144. func (m Matrix) TotalSamples() int {
  145. numSamples := 0
  146. for _, series := range m {
  147. numSamples += len(series.Points)
  148. }
  149. return numSamples
  150. }
  151. func (m Matrix) Len() int { return len(m) }
  152. func (m Matrix) Less(i, j int) bool { return labels.Compare(m[i].Metric, m[j].Metric) < 0 }
  153. func (m Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
  154. // ContainsSameLabelset checks if a matrix has samples with the same labelset
  155. // Such a behavior is semantically undefined
  156. // https://github.com/prometheus/prometheus/issues/4562
  157. func (m Matrix) ContainsSameLabelset() bool {
  158. l := make(map[uint64]struct{}, len(m))
  159. for _, ss := range m {
  160. hash := ss.Metric.Hash()
  161. if _, ok := l[hash]; ok {
  162. return true
  163. }
  164. l[hash] = struct{}{}
  165. }
  166. return false
  167. }