query.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package influxdb
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "regexp"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. )
  10. var (
  11. regexpOperatorPattern *regexp.Regexp = regexp.MustCompile(`^\/.*\/$`)
  12. regexpMeasurementPattern *regexp.Regexp = regexp.MustCompile(`^\/.*\/$`)
  13. )
  14. func (query *Query) Build(queryContext *tsdb.TsdbQuery) (string, error) {
  15. var res string
  16. if query.UseRawQuery && query.RawQuery != "" {
  17. res = query.RawQuery
  18. } else {
  19. res = query.renderSelectors(queryContext)
  20. res += query.renderMeasurement()
  21. res += query.renderWhereClause()
  22. res += query.renderTimeFilter(queryContext)
  23. res += query.renderGroupBy(queryContext)
  24. }
  25. calculator := tsdb.NewIntervalCalculator(&tsdb.IntervalOptions{})
  26. interval := calculator.Calculate(queryContext.TimeRange, query.Interval)
  27. res = strings.Replace(res, "$timeFilter", query.renderTimeFilter(queryContext), -1)
  28. res = strings.Replace(res, "$interval", interval.Text, -1)
  29. res = strings.Replace(res, "$__interval_ms", strconv.FormatInt(interval.Value.Nanoseconds()/int64(time.Millisecond), 10), -1)
  30. res = strings.Replace(res, "$__interval", interval.Text, -1)
  31. return res, nil
  32. }
  33. func (query *Query) renderTags() []string {
  34. var res []string
  35. for i, tag := range query.Tags {
  36. str := ""
  37. if i > 0 {
  38. if tag.Condition == "" {
  39. str += "AND"
  40. } else {
  41. str += tag.Condition
  42. }
  43. str += " "
  44. }
  45. //If the operator is missing we fall back to sensible defaults
  46. if tag.Operator == "" {
  47. if regexpOperatorPattern.Match([]byte(tag.Value)) {
  48. tag.Operator = "=~"
  49. } else {
  50. tag.Operator = "="
  51. }
  52. }
  53. textValue := ""
  54. // quote value unless regex or number
  55. if tag.Operator == "=~" || tag.Operator == "!~" {
  56. textValue = tag.Value
  57. } else if tag.Operator == "<" || tag.Operator == ">" {
  58. textValue = tag.Value
  59. } else {
  60. textValue = fmt.Sprintf("'%s'", tag.Value)
  61. }
  62. res = append(res, fmt.Sprintf(`%s"%s" %s %s`, str, tag.Key, tag.Operator, textValue))
  63. }
  64. return res
  65. }
  66. func (query *Query) renderTimeFilter(queryContext *tsdb.TsdbQuery) string {
  67. from := "now() - " + queryContext.TimeRange.From
  68. to := ""
  69. if queryContext.TimeRange.To != "now" && queryContext.TimeRange.To != "" {
  70. to = " and time < now() - " + strings.Replace(queryContext.TimeRange.To, "now-", "", 1)
  71. }
  72. return fmt.Sprintf("time > %s%s", from, to)
  73. }
  74. func (query *Query) renderSelectors(queryContext *tsdb.TsdbQuery) string {
  75. res := "SELECT "
  76. var selectors []string
  77. for _, sel := range query.Selects {
  78. stk := ""
  79. for _, s := range *sel {
  80. stk = s.Render(query, queryContext, stk)
  81. }
  82. selectors = append(selectors, stk)
  83. }
  84. return res + strings.Join(selectors, ", ")
  85. }
  86. func (query *Query) renderMeasurement() string {
  87. policy := ""
  88. if query.Policy == "" || query.Policy == "default" {
  89. policy = ""
  90. } else {
  91. policy = `"` + query.Policy + `".`
  92. }
  93. measurement := query.Measurement
  94. if !regexpMeasurementPattern.Match([]byte(measurement)) {
  95. measurement = fmt.Sprintf(`"%s"`, measurement)
  96. }
  97. return fmt.Sprintf(` FROM %s%s`, policy, measurement)
  98. }
  99. func (query *Query) renderWhereClause() string {
  100. res := " WHERE "
  101. conditions := query.renderTags()
  102. if len(conditions) > 0 {
  103. if len(conditions) > 1 {
  104. res += "(" + strings.Join(conditions, " ") + ")"
  105. } else {
  106. res += conditions[0]
  107. }
  108. res += " AND "
  109. }
  110. return res
  111. }
  112. func (query *Query) renderGroupBy(queryContext *tsdb.TsdbQuery) string {
  113. groupBy := ""
  114. for i, group := range query.GroupBy {
  115. if i == 0 {
  116. groupBy += " GROUP BY"
  117. }
  118. if i > 0 && group.Type != "fill" {
  119. groupBy += ", " //fill is so very special. fill is a creep, fill is a weirdo
  120. } else {
  121. groupBy += " "
  122. }
  123. groupBy += group.Render(query, queryContext, "")
  124. }
  125. return groupBy
  126. }