models.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package tsdb
  2. import (
  3. "github.com/grafana/grafana/pkg/components/simplejson"
  4. "github.com/grafana/grafana/pkg/models"
  5. "gopkg.in/guregu/null.v3"
  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:"error"`
  40. RefId string `json:"refId"`
  41. Series TimeSeriesSlice `json:"series"`
  42. }
  43. type TimeSeries struct {
  44. Name string `json:"name"`
  45. Points TimeSeriesPoints `json:"points"`
  46. }
  47. type TimePoint [2]null.Float
  48. type TimeSeriesPoints []TimePoint
  49. type TimeSeriesSlice []*TimeSeries
  50. func NewQueryResult() *QueryResult {
  51. return &QueryResult{
  52. Series: make(TimeSeriesSlice, 0),
  53. }
  54. }
  55. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  56. return TimePoint{value, null.FloatFrom(timestamp)}
  57. }
  58. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  59. points := make(TimeSeriesPoints, 0)
  60. for i := 0; i < len(values); i += 2 {
  61. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  62. }
  63. return points
  64. }
  65. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  66. return &TimeSeries{
  67. Name: name,
  68. Points: points,
  69. }
  70. }