macros.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package postgres
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/tsdb"
  10. )
  11. //const rsString = `(?:"([^"]*)")`;
  12. const rsIdentifier = `([_a-zA-Z0-9]+)`
  13. const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
  14. type postgresMacroEngine struct {
  15. timeRange *tsdb.TimeRange
  16. query *tsdb.Query
  17. timescaledb bool
  18. }
  19. func newPostgresMacroEngine(datasource *models.DataSource) tsdb.SqlMacroEngine {
  20. engine := &postgresMacroEngine{}
  21. engine.timescaledb = datasource.JsonData.Get("timescaledb").MustBool(false)
  22. return engine
  23. }
  24. func (m *postgresMacroEngine) Interpolate(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) {
  25. m.timeRange = timeRange
  26. m.query = query
  27. rExp, _ := regexp.Compile(sExpr)
  28. var macroError error
  29. sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
  30. // detect if $__timeGroup is supposed to add AS time for pre 5.3 compatibility
  31. // if there is a ',' directly after the macro call $__timeGroup is probably used
  32. // in the old way. Inside window function ORDER BY $__timeGroup will be followed
  33. // by ')'
  34. if groups[1] == "__timeGroup" {
  35. if index := strings.Index(sql, groups[0]); index >= 0 {
  36. index += len(groups[0])
  37. if len(sql) > index {
  38. // check for character after macro expression
  39. if sql[index] == ',' {
  40. groups[1] = "__timeGroupAlias"
  41. }
  42. }
  43. }
  44. }
  45. args := strings.Split(groups[2], ",")
  46. for i, arg := range args {
  47. args[i] = strings.Trim(arg, " ")
  48. }
  49. res, err := m.evaluateMacro(groups[1], args)
  50. if err != nil && macroError == nil {
  51. macroError = err
  52. return "macro_error()"
  53. }
  54. return res
  55. })
  56. if macroError != nil {
  57. return "", macroError
  58. }
  59. return sql, nil
  60. }
  61. func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
  62. result := ""
  63. lastIndex := 0
  64. for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
  65. groups := []string{}
  66. for i := 0; i < len(v); i += 2 {
  67. groups = append(groups, str[v[i]:v[i+1]])
  68. }
  69. result += str[lastIndex:v[0]] + repl(groups)
  70. lastIndex = v[1]
  71. }
  72. return result + str[lastIndex:]
  73. }
  74. func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, error) {
  75. switch name {
  76. case "__time":
  77. if len(args) == 0 {
  78. return "", fmt.Errorf("missing time column argument for macro %v", name)
  79. }
  80. return fmt.Sprintf("%s AS \"time\"", args[0]), nil
  81. case "__timeEpoch":
  82. if len(args) == 0 {
  83. return "", fmt.Errorf("missing time column argument for macro %v", name)
  84. }
  85. return fmt.Sprintf("extract(epoch from %s) as \"time\"", args[0]), nil
  86. case "__timeFilter":
  87. if len(args) == 0 {
  88. return "", fmt.Errorf("missing time column argument for macro %v", name)
  89. }
  90. return fmt.Sprintf("%s BETWEEN '%s' AND '%s'", args[0], m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339), m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), nil
  91. case "__timeFrom":
  92. return fmt.Sprintf("'%s'", m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339)), nil
  93. case "__timeTo":
  94. return fmt.Sprintf("'%s'", m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), nil
  95. case "__timeGroup":
  96. if len(args) < 2 {
  97. return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
  98. }
  99. interval, err := time.ParseDuration(strings.Trim(args[1], `'`))
  100. if err != nil {
  101. return "", fmt.Errorf("error parsing interval %v", args[1])
  102. }
  103. if len(args) == 3 {
  104. m.query.Model.Set("fill", true)
  105. m.query.Model.Set("fillInterval", interval.Seconds())
  106. switch args[2] {
  107. case "NULL":
  108. m.query.Model.Set("fillMode", "null")
  109. case "previous":
  110. m.query.Model.Set("fillMode", "previous")
  111. default:
  112. m.query.Model.Set("fillMode", "value")
  113. floatVal, err := strconv.ParseFloat(args[2], 64)
  114. if err != nil {
  115. return "", fmt.Errorf("error parsing fill value %v", args[2])
  116. }
  117. m.query.Model.Set("fillValue", floatVal)
  118. }
  119. }
  120. if m.timescaledb {
  121. return fmt.Sprintf("time_bucket('%vs',%s)", interval.Seconds(), args[0]), nil
  122. } else {
  123. return fmt.Sprintf("floor(extract(epoch from %s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
  124. }
  125. case "__timeGroupAlias":
  126. tg, err := m.evaluateMacro("__timeGroup", args)
  127. if err == nil {
  128. return tg + " AS \"time\"", err
  129. }
  130. return "", err
  131. case "__unixEpochFilter":
  132. if len(args) == 0 {
  133. return "", fmt.Errorf("missing time column argument for macro %v", name)
  134. }
  135. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsSecondsEpoch(), args[0], m.timeRange.GetToAsSecondsEpoch()), nil
  136. case "__unixEpochFrom":
  137. return fmt.Sprintf("%d", m.timeRange.GetFromAsSecondsEpoch()), nil
  138. case "__unixEpochTo":
  139. return fmt.Sprintf("%d", m.timeRange.GetToAsSecondsEpoch()), nil
  140. default:
  141. return "", fmt.Errorf("Unknown macro %v", name)
  142. }
  143. }