models.go 2.2 KB

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