macros.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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("DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), %s) ) as time_sec", args[0]), nil
  54. case "__timeFilter":
  55. if len(args) == 0 {
  56. return "", fmt.Errorf("missing time column argument for macro %v", name)
  57. }
  58. 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
  59. case "__timeFrom":
  60. return fmt.Sprintf("DATEADD(second, %d+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01')", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil
  61. case "__timeTo":
  62. return fmt.Sprintf("DATEADD(second, %d+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01')", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  63. case "__unixEpochFilter":
  64. if len(args) == 0 {
  65. return "", fmt.Errorf("missing time column argument for macro %v", name)
  66. }
  67. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  68. case "__unixEpochFrom":
  69. return fmt.Sprintf("%d", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil
  70. case "__unixEpochTo":
  71. return fmt.Sprintf("%d", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil
  72. default:
  73. return "", fmt.Errorf("Unknown macro %v", name)
  74. }
  75. }