query_builder.go 3.2 KB

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