macros.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. )
  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 FROM_UNIXTIME(%d) AND FROM_UNIXTIME(%d)", args[0], m.timeRange.GetFromAsSecondsEpoch(), m.timeRange.GetToAsSecondsEpoch()), nil
  53. case "__timeFrom":
  54. return fmt.Sprintf("FROM_UNIXTIME(%d)", m.timeRange.GetFromAsSecondsEpoch()), nil
  55. case "__timeTo":
  56. return fmt.Sprintf("FROM_UNIXTIME(%d)", m.timeRange.GetToAsSecondsEpoch()), nil
  57. case "__timeGroup":
  58. if len(args) < 2 {
  59. return "", fmt.Errorf("macro %v needs time column and interval", name)
  60. }
  61. interval, err := gtime.ParseInterval(strings.Trim(args[1], `'"`))
  62. if err != nil {
  63. return "", fmt.Errorf("error parsing interval %v", args[1])
  64. }
  65. if len(args) == 3 {
  66. err := tsdb.SetupFillmode(m.query, interval, args[2])
  67. if err != nil {
  68. return "", err
  69. }
  70. }
  71. return fmt.Sprintf("UNIX_TIMESTAMP(%s) DIV %.0f * %.0f", args[0], interval.Seconds(), interval.Seconds()), nil
  72. case "__timeGroupAlias":
  73. tg, err := m.evaluateMacro("__timeGroup", args)
  74. if err == nil {
  75. return tg + " AS \"time\"", err
  76. }
  77. return "", err
  78. case "__unixEpochFilter":
  79. if len(args) == 0 {
  80. return "", fmt.Errorf("missing time column argument for macro %v", name)
  81. }
  82. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsSecondsEpoch(), args[0], m.timeRange.GetToAsSecondsEpoch()), nil
  83. case "__unixEpochNanoFilter":
  84. if len(args) == 0 {
  85. return "", fmt.Errorf("missing time column argument for macro %v", name)
  86. }
  87. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsTimeUTC().UnixNano(), args[0], m.timeRange.GetToAsTimeUTC().UnixNano()), nil
  88. case "__unixEpochNanoFrom":
  89. return fmt.Sprintf("%d", m.timeRange.GetFromAsTimeUTC().UnixNano()), nil
  90. case "__unixEpochNanoTo":
  91. return fmt.Sprintf("%d", m.timeRange.GetToAsTimeUTC().UnixNano()), nil
  92. case "__unixEpochGroup":
  93. if len(args) < 2 {
  94. return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
  95. }
  96. interval, err := gtime.ParseInterval(strings.Trim(args[1], `'`))
  97. if err != nil {
  98. return "", fmt.Errorf("error parsing interval %v", args[1])
  99. }
  100. if len(args) == 3 {
  101. err := tsdb.SetupFillmode(m.query, interval, args[2])
  102. if err != nil {
  103. return "", err
  104. }
  105. }
  106. return fmt.Sprintf("%s DIV %v * %v", args[0], interval.Seconds(), interval.Seconds()), nil
  107. case "__unixEpochGroupAlias":
  108. tg, err := m.evaluateMacro("__unixEpochGroup", args)
  109. if err == nil {
  110. return tg + " AS \"time\"", err
  111. }
  112. return "", err
  113. default:
  114. return "", fmt.Errorf("Unknown macro %v", name)
  115. }
  116. }