macros.go 3.3 KB

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