macros.go 4.1 KB

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