models.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  46. type TimeSeries struct {
  47. Name string `json:"name"`
  48. Points TimeSeriesPoints `json:"points"`
  49. Tags map[string]string `json:"tags"`
  50. }
  51. type TimePoint [2]null.Float
  52. type TimeSeriesPoints []TimePoint
  53. type TimeSeriesSlice []*TimeSeries
  54. func NewQueryResult() *QueryResult {
  55. return &QueryResult{
  56. Series: make(TimeSeriesSlice, 0),
  57. }
  58. }
  59. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  60. return TimePoint{value, null.FloatFrom(timestamp)}
  61. }
  62. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  63. points := make(TimeSeriesPoints, 0)
  64. for i := 0; i < len(values); i += 2 {
  65. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  66. }
  67. return points
  68. }
  69. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  70. return &TimeSeries{
  71. Name: name,
  72. Points: points,
  73. }
  74. }