models.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package tsdb
  2. import (
  3. "github.com/grafana/grafana/pkg/components/null"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/models"
  6. )
  7. type Query struct {
  8. RefId string
  9. Model *simplejson.Json
  10. Depends []string
  11. DataSource *models.DataSource
  12. Results []*TimeSeries
  13. Exclude bool
  14. MaxDataPoints int64
  15. IntervalMs int64
  16. }
  17. type QuerySlice []*Query
  18. type Request struct {
  19. TimeRange *TimeRange
  20. Queries QuerySlice
  21. }
  22. type Response struct {
  23. BatchTimings []*BatchTiming `json:"timings"`
  24. Results map[string]*QueryResult `json:"results"`
  25. Message string `json:"message,omitempty"`
  26. }
  27. type BatchTiming struct {
  28. TimeElapsed int64
  29. }
  30. type BatchResult struct {
  31. Error error
  32. QueryResults map[string]*QueryResult
  33. Timings *BatchTiming
  34. }
  35. func (br *BatchResult) WithError(err error) *BatchResult {
  36. br.Error = err
  37. return br
  38. }
  39. type QueryResult struct {
  40. Error error `json:"-"`
  41. ErrorString string `json:"error,omitempty"`
  42. RefId string `json:"refId"`
  43. Meta *simplejson.Json `json:"meta,omitempty"`
  44. Series TimeSeriesSlice `json:"series"`
  45. Tables []*Table `json:"tables"`
  46. }
  47. type TimeSeries struct {
  48. Name string `json:"name"`
  49. Points TimeSeriesPoints `json:"points"`
  50. Tags map[string]string `json:"tags,omitempty"`
  51. }
  52. type Table struct {
  53. Columns []TableColumn `json:"columns"`
  54. Rows []RowValues `json:"rows"`
  55. }
  56. type TableColumn struct {
  57. Text string `json:"text"`
  58. }
  59. type RowValues []interface{}
  60. type TimePoint [2]null.Float
  61. type TimeSeriesPoints []TimePoint
  62. type TimeSeriesSlice []*TimeSeries
  63. func NewQueryResult() *QueryResult {
  64. return &QueryResult{
  65. Series: make(TimeSeriesSlice, 0),
  66. }
  67. }
  68. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  69. return TimePoint{value, null.FloatFrom(timestamp)}
  70. }
  71. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  72. points := make(TimeSeriesPoints, 0)
  73. for i := 0; i < len(values); i += 2 {
  74. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  75. }
  76. return points
  77. }
  78. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  79. return &TimeSeries{
  80. Name: name,
  81. Points: points,
  82. }
  83. }