models.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. }
  26. type BatchTiming struct {
  27. TimeElapsed int64
  28. }
  29. type BatchResult struct {
  30. Error error
  31. QueryResults map[string]*QueryResult
  32. Timings *BatchTiming
  33. }
  34. func (br *BatchResult) WithError(err error) *BatchResult {
  35. br.Error = err
  36. return br
  37. }
  38. type QueryResult struct {
  39. Error error `json:"-"`
  40. ErrorString string `json:"error"`
  41. RefId string `json:"refId"`
  42. Series TimeSeriesSlice `json:"series"`
  43. }
  44. type TimeSeries struct {
  45. Name string `json:"name"`
  46. Points TimeSeriesPoints `json:"points"`
  47. Tags map[string]string `json:"tags"`
  48. }
  49. type TimePoint [2]null.Float
  50. type TimeSeriesPoints []TimePoint
  51. type TimeSeriesSlice []*TimeSeries
  52. func NewQueryResult() *QueryResult {
  53. return &QueryResult{
  54. Series: make(TimeSeriesSlice, 0),
  55. }
  56. }
  57. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  58. return TimePoint{value, null.FloatFrom(timestamp)}
  59. }
  60. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  61. points := make(TimeSeriesPoints, 0)
  62. for i := 0; i < len(values); i += 2 {
  63. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  64. }
  65. return points
  66. }
  67. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  68. return &TimeSeries{
  69. Name: name,
  70. Points: points,
  71. }
  72. }