macros.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package mssql
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "github.com/grafana/grafana/pkg/tsdb"
  8. )
  9. const rsIdentifier = `([_a-zA-Z0-9]+)`
  10. const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
  11. type msSqlMacroEngine struct {
  12. *tsdb.SqlMacroEngineBase
  13. timeRange *tsdb.TimeRange
  14. query *tsdb.Query
  15. }
  16. func newMssqlMacroEngine() tsdb.SqlMacroEngine {
  17. return &msSqlMacroEngine{SqlMacroEngineBase: tsdb.NewSqlMacroEngineBase()}
  18. }
  19. func (m *msSqlMacroEngine) 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 *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, error) {
  42. switch name {
  43. case "__time":
  44. if len(args) == 0 {
  45. return "", fmt.Errorf("missing time column argument for macro %v", name)
  46. }
  47. return fmt.Sprintf("%s AS time", args[0]), nil
  48. case "__timeEpoch":
  49. if len(args) == 0 {
  50. return "", fmt.Errorf("missing time column argument for macro %v", name)
  51. }
  52. return fmt.Sprintf("DATEDIFF(second, '1970-01-01', %s) AS time", 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 BETWEEN '%s' AND '%s'", args[0], m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339), m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), 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 := time.ParseDuration(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 := tsdb.SetupFillmode(m.query, interval, args[2])
  68. if err != nil {
  69. return "", err
  70. }
  71. }
  72. return fmt.Sprintf("FLOOR(DATEDIFF(second, '1970-01-01', %s)/%.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 "__unixEpochGroup":
  85. if len(args) < 2 {
  86. return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
  87. }
  88. interval, err := time.ParseDuration(strings.Trim(args[1], `'`))
  89. if err != nil {
  90. return "", fmt.Errorf("error parsing interval %v", args[1])
  91. }
  92. if len(args) == 3 {
  93. err := tsdb.SetupFillmode(m.query, interval, args[2])
  94. if err != nil {
  95. return "", err
  96. }
  97. }
  98. return fmt.Sprintf("FLOOR(%s/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
  99. case "__unixEpochGroupAlias":
  100. tg, err := m.evaluateMacro("__unixEpochGroup", args)
  101. if err == nil {
  102. return tg + " AS [time]", err
  103. }
  104. return "", err
  105. default:
  106. return "", fmt.Errorf("Unknown macro %v", name)
  107. }
  108. }