macros.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package mysql
  2. import (
  3. "fmt"
  4. "regexp"
  5. "github.com/grafana/grafana/pkg/tsdb"
  6. )
  7. //const rsString = `(?:"([^"]*)")`;
  8. const rsIdentifier = `([_a-zA-Z0-9]+)`
  9. const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
  10. type MySqlMacroEngine struct {
  11. TimeRange *tsdb.TimeRange
  12. }
  13. func NewMysqlMacroEngine() tsdb.SqlMacroEngine {
  14. return &MySqlMacroEngine{}
  15. }
  16. func (m *MySqlMacroEngine) Interpolate(timeRange *tsdb.TimeRange, sql string) (string, error) {
  17. m.TimeRange = timeRange
  18. rExp, _ := regexp.Compile(sExpr)
  19. var macroError error
  20. sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
  21. res, err := m.evaluateMacro(groups[1], groups[2:])
  22. if err != nil && macroError == nil {
  23. macroError = err
  24. return "macro_error()"
  25. }
  26. return res
  27. })
  28. if macroError != nil {
  29. return "", macroError
  30. }
  31. return sql, nil
  32. }
  33. func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string {
  34. result := ""
  35. lastIndex := 0
  36. for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) {
  37. groups := []string{}
  38. for i := 0; i < len(v); i += 2 {
  39. groups = append(groups, str[v[i]:v[i+1]])
  40. }
  41. result += str[lastIndex:v[0]] + repl(groups)
  42. lastIndex = v[1]
  43. }
  44. return result + str[lastIndex:]
  45. }
  46. func (m *MySqlMacroEngine) evaluateMacro(name string, args []string) (string, error) {
  47. switch name {
  48. case "__time":
  49. if len(args) == 0 {
  50. return "", fmt.Errorf("missing time column argument for macro %v", name)
  51. }
  52. return fmt.Sprintf("UNIX_TIMESTAMP(%s) as time_sec", args[0]), nil
  53. case "__timeFilter":
  54. if len(args) == 0 {
  55. return "", fmt.Errorf("missing time column argument for macro %v", name)
  56. }
  57. return fmt.Sprintf("%s >= FROM_UNIXTIME(%d) AND %s <= FROM_UNIXTIME(%d)", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  58. case "__timeFrom":
  59. return fmt.Sprintf("FROM_UNIXTIME(%d)", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil
  60. case "__timeTo":
  61. return fmt.Sprintf("FROM_UNIXTIME(%d)", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  62. case "__unixEpochFilter":
  63. if len(args) == 0 {
  64. return "", fmt.Errorf("missing time column argument for macro %v", name)
  65. }
  66. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  67. case "__unixEpochFrom":
  68. return fmt.Sprintf("%d", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil
  69. case "__unixEpochTo":
  70. return fmt.Sprintf("%d", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  71. default:
  72. return "", fmt.Errorf("Unknown macro %v", name)
  73. }
  74. }