models.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package tsdb
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "net/http"
  6. "time"
  7. "github.com/grafana/grafana/pkg/components/simplejson"
  8. "gopkg.in/guregu/null.v3"
  9. )
  10. type Query struct {
  11. RefId string
  12. Model *simplejson.Json
  13. Depends []string
  14. DataSource *DataSourceInfo
  15. Results []*TimeSeries
  16. Exclude bool
  17. MaxDataPoints int64
  18. IntervalMs int64
  19. }
  20. type QuerySlice []*Query
  21. type Request struct {
  22. TimeRange *TimeRange
  23. Queries QuerySlice
  24. }
  25. type Response struct {
  26. BatchTimings []*BatchTiming `json:"timings"`
  27. Results map[string]*QueryResult `json:"results"`
  28. }
  29. type DataSourceInfo struct {
  30. Id int64
  31. Name string
  32. PluginId string
  33. Url string
  34. Password string
  35. User string
  36. Database string
  37. BasicAuth bool
  38. BasicAuthUser string
  39. BasicAuthPassword string
  40. JsonData *simplejson.Json
  41. }
  42. func (ds *DataSourceInfo) GetDefaultClient() *http.Client {
  43. tr := &http.Transport{
  44. Proxy: http.ProxyFromEnvironment,
  45. DialContext: (&net.Dialer{
  46. Timeout: 30 * time.Second,
  47. KeepAlive: 30 * time.Second,
  48. }).DialContext,
  49. MaxIdleConns: 100,
  50. IdleConnTimeout: 90 * time.Second,
  51. TLSHandshakeTimeout: 10 * time.Second,
  52. ExpectContinueTimeout: 1 * time.Second,
  53. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  54. }
  55. return &http.Client{
  56. Timeout: time.Duration(30 * time.Second),
  57. Transport: tr,
  58. }
  59. }
  60. type BatchTiming struct {
  61. TimeElapsed int64
  62. }
  63. type BatchResult struct {
  64. Error error
  65. QueryResults map[string]*QueryResult
  66. Timings *BatchTiming
  67. }
  68. func (br *BatchResult) WithError(err error) *BatchResult {
  69. br.Error = err
  70. return br
  71. }
  72. type QueryResult struct {
  73. Error error `json:"error"`
  74. RefId string `json:"refId"`
  75. Series TimeSeriesSlice `json:"series"`
  76. }
  77. type TimeSeries struct {
  78. Name string `json:"name"`
  79. Points TimeSeriesPoints `json:"points"`
  80. }
  81. type TimePoint [2]null.Float
  82. type TimeSeriesPoints []TimePoint
  83. type TimeSeriesSlice []*TimeSeries
  84. func NewQueryResult() *QueryResult {
  85. return &QueryResult{
  86. Series: make(TimeSeriesSlice, 0),
  87. }
  88. }
  89. func NewTimePoint(value null.Float, timestamp float64) TimePoint {
  90. return TimePoint{value, null.FloatFrom(timestamp)}
  91. }
  92. func NewTimeSeriesPointsFromArgs(values ...float64) TimeSeriesPoints {
  93. points := make(TimeSeriesPoints, 0)
  94. for i := 0; i < len(values); i += 2 {
  95. points = append(points, NewTimePoint(null.FloatFrom(values[i]), values[i+1]))
  96. }
  97. return points
  98. }
  99. func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
  100. return &TimeSeries{
  101. Name: name,
  102. Points: points,
  103. }
  104. }