macros.go 4.0 KB

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