models.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. DataSource *models.DataSource
  15. MaxDataPoints int64
  16. IntervalMs int64
  17. }
  18. type Response struct {
  19. Results map[string]*QueryResult `json:"results"`
  20. Message string `json:"message,omitempty"`
  21. }
  22. type QueryResult struct {
  23. Error error `json:"-"`
  24. ErrorString string `json:"error,omitempty"`
  25. RefId string `json:"refId"`
  26. Meta *simplejson.Json `json:"meta,omitempty"`
  27. Series TimeSeriesSlice `json:"series"`
  28. Tables []*Table `json:"tables"`
  29. }
  30. type TimeSeries struct {
  31. Name string `json:"name"`
  32. Points TimeSeriesPoints `json:"points"`
  33. Tags map[string]string `json:"tags,omitempty"`
  34. }
  35. type Table struct {
  36. Columns []TableColumn `json:"columns"`
  37. Rows []RowValues `json:"rows"`
  38. }
  39. type TableColumn struct {
  40. Text string `json:"text"`
  41. }
  42. type RowValues []interface{}
  43. type TimePoint [2]null.Float
  44. type TimeSeriesPoints []TimePoint
  45. type TimeSeriesSlice []*TimeSeries
  46. func NewQueryResult() *QueryResult {
  47. return &QueryResult{
  48. Series: make(TimeSeriesSlice, 0),
  49. }
  50. }
  51. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  52. return TimePoint{value, null.FloatFrom(timestamp)}
  53. }
  54. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  55. points := make(TimeSeriesPoints, 0)
  56. for i := 0; i < len(values); i += 2 {
  57. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  58. }
  59. return points
  60. }
  61. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  62. return &TimeSeries{
  63. Name: name,
  64. Points: points,
  65. }
  66. }