macros.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package mysql
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "github.com/grafana/grafana/pkg/tsdb"
  8. )
  9. const rsIdentifier = `([_a-zA-Z0-9]+)`
  10. const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
  11. type mySqlMacroEngine struct {
  12. *tsdb.SqlMacroEngineBase
  13. timeRange *tsdb.TimeRange
  14. query *tsdb.Query
  15. }
  16. func newMysqlMacroEngine() tsdb.SqlMacroEngine {
  17. return &mySqlMacroEngine{SqlMacroEngineBase: tsdb.NewSqlMacroEngineBase()}
  18. }
  19. func (m *mySqlMacroEngine) Interpolate(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) {
  20. m.timeRange = timeRange
  21. m.query = query
  22. rExp, _ := regexp.Compile(sExpr)
  23. var macroError error
  24. sql = m.ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
  25. args := strings.Split(groups[2], ",")
  26. for i, arg := range args {
  27. args[i] = strings.Trim(arg, " ")
  28. }
  29. res, err := m.evaluateMacro(groups[1], args)
  30. if err != nil && macroError == nil {
  31. macroError = err
  32. return "macro_error()"
  33. }
  34. return res
  35. })
  36. if macroError != nil {
  37. return "", macroError
  38. }
  39. return sql, nil
  40. }
  41. func (m *mySqlMacroEngine) evaluateMacro(name string, args []string) (string, error) {
  42. switch name {
  43. case "__timeEpoch", "__time":
  44. if len(args) == 0 {
  45. return "", fmt.Errorf("missing time column argument for macro %v", name)
  46. }
  47. return fmt.Sprintf("UNIX_TIMESTAMP(%s) as time_sec", args[0]), nil
  48. case "__timeFilter":
  49. if len(args) == 0 {
  50. return "", fmt.Errorf("missing time column argument for macro %v", name)
  51. }
  52. return fmt.Sprintf("%s BETWEEN '%s' AND '%s'", args[0], m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339), m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), nil
  53. case "__timeGroup":
  54. if len(args) < 2 {
  55. return "", fmt.Errorf("macro %v needs time column and interval", name)
  56. }
  57. interval, err := time.ParseDuration(strings.Trim(args[1], `'"`))
  58. if err != nil {
  59. return "", fmt.Errorf("error parsing interval %v", args[1])
  60. }
  61. if len(args) == 3 {
  62. err := tsdb.SetupFillmode(m.query, interval, args[2])
  63. if err != nil {
  64. return "", err
  65. }
  66. }
  67. return fmt.Sprintf("UNIX_TIMESTAMP(%s) DIV %.0f * %.0f", args[0], interval.Seconds(), interval.Seconds()), nil
  68. case "__timeGroupAlias":
  69. tg, err := m.evaluateMacro("__timeGroup", args)
  70. if err == nil {
  71. return tg + " AS \"time\"", err
  72. }
  73. return "", err
  74. case "__unixEpochFilter":
  75. if len(args) == 0 {
  76. return "", fmt.Errorf("missing time column argument for macro %v", name)
  77. }
  78. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsSecondsEpoch(), args[0], m.timeRange.GetToAsSecondsEpoch()), nil
  79. case "__unixEpochGroup":
  80. if len(args) < 2 {
  81. return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
  82. }
  83. interval, err := time.ParseDuration(strings.Trim(args[1], `'`))
  84. if err != nil {
  85. return "", fmt.Errorf("error parsing interval %v", args[1])
  86. }
  87. if len(args) == 3 {
  88. err := tsdb.SetupFillmode(m.query, interval, args[2])
  89. if err != nil {
  90. return "", err
  91. }
  92. }
  93. return fmt.Sprintf("%s DIV %v * %v", args[0], interval.Seconds(), interval.Seconds()), nil
  94. case "__unixEpochGroupAlias":
  95. tg, err := m.evaluateMacro("__unixEpochGroup", args)
  96. if err == nil {
  97. return tg + " AS \"time\"", err
  98. }
  99. return "", err
  100. default:
  101. return "", fmt.Errorf("Unknown macro %v", name)
  102. }
  103. }