types.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package mqe
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/grafana/grafana/pkg/tsdb"
  6. )
  7. type MQEMetric struct {
  8. Metric string
  9. Alias string
  10. }
  11. type MQEQuery struct {
  12. Metrics []MQEMetric
  13. Hosts []string
  14. Apps []string
  15. AddAppToAlias bool
  16. AddHostToAlias bool
  17. TimeRange *tsdb.TimeRange
  18. UseRawQuery bool
  19. RawQuery string
  20. }
  21. //`os.disk.sda.io_time` where host in ('staples-lab-1') from 1479197578194 to 1479219178194
  22. func (q *MQEQuery) Build(availableSeries []string) ([]string, error) {
  23. var queries []string
  24. where := q.buildWhereClause()
  25. var metrics []
  26. for _, v := range q.Metrics {
  27. if noStar {
  28. metrics = append(metrics, v)
  29. continue
  30. }
  31. for _, a := range availableSeries {
  32. if match {
  33. metrics = append(metrics, a)
  34. }
  35. }
  36. }
  37. for _, v := range metrics {
  38. queries = append(queries,
  39. fmt.Sprintf(
  40. "`%s` %s from %v to %v",
  41. v.Metric,
  42. where,
  43. q.TimeRange.GetFromAsMsEpoch(),
  44. q.TimeRange.GetToAsMsEpoch()))
  45. }
  46. return queries, nil
  47. }
  48. func (q *MQEQuery) buildWhereClause() string {
  49. hasApps := len(q.Apps) > 0
  50. hasHosts := len(q.Hosts) > 0
  51. where := ""
  52. if hasHosts || hasApps {
  53. where += "where "
  54. }
  55. if hasApps {
  56. apps := strings.Join(q.Apps, "', '")
  57. where += fmt.Sprintf(" apps in ('%s')", apps)
  58. }
  59. if hasHosts && hasApps {
  60. where += " and"
  61. }
  62. if hasHosts {
  63. hosts := strings.Join(q.Hosts, "', '")
  64. where += fmt.Sprintf(" hosts in ('%s')", hosts)
  65. }
  66. return where
  67. }
  68. type TokenBody struct {
  69. Metrics []string
  70. }
  71. type TokenResponse struct {
  72. Success bool
  73. Body TokenBody
  74. }
  75. type MQEResponse struct {
  76. }