query.go 3.6 KB

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