models.go 1.9 KB

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