macros.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package mssql
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. "github.com/grafana/grafana/pkg/components/gtime"
  8. "github.com/grafana/grafana/pkg/tsdb"
  9. )
  10. const rsIdentifier = `([_a-zA-Z0-9]+)`
  11. const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
  12. type msSqlMacroEngine struct {
  13. *tsdb.SqlMacroEngineBase
  14. timeRange *tsdb.TimeRange
  15. query *tsdb.Query
  16. }
  17. func newMssqlMacroEngine() tsdb.SqlMacroEngine {
  18. return &msSqlMacroEngine{SqlMacroEngineBase: tsdb.NewSqlMacroEngineBase()}
  19. }
  20. func (m *msSqlMacroEngine) Interpolate(query *tsdb.Query, timeRange *tsdb.TimeRange, sql string) (string, error) {
  21. m.timeRange = timeRange
  22. m.query = query
  23. rExp, _ := regexp.Compile(sExpr)
  24. var macroError error
  25. sql = m.ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
  26. args := strings.Split(groups[2], ",")
  27. for i, arg := range args {
  28. args[i] = strings.Trim(arg, " ")
  29. }
  30. res, err := m.evaluateMacro(groups[1], args)
  31. if err != nil && macroError == nil {
  32. macroError = err
  33. return "macro_error()"
  34. }
  35. return res
  36. })
  37. if macroError != nil {
  38. return "", macroError
  39. }
  40. return sql, nil
  41. }
  42. func (m *msSqlMacroEngine) evaluateMacro(name string, args []string) (string, error) {
  43. switch name {
  44. case "__time":
  45. if len(args) == 0 {
  46. return "", fmt.Errorf("missing time column argument for macro %v", name)
  47. }
  48. return fmt.Sprintf("%s AS time", args[0]), nil
  49. case "__timeEpoch":
  50. if len(args) == 0 {
  51. return "", fmt.Errorf("missing time column argument for macro %v", name)
  52. }
  53. return fmt.Sprintf("DATEDIFF(second, '1970-01-01', %s) AS time", 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 BETWEEN '%s' AND '%s'", args[0], m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339), m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), nil
  59. case "__timeFrom":
  60. return fmt.Sprintf("'%s'", m.timeRange.GetFromAsTimeUTC().Format(time.RFC3339)), nil
  61. case "__timeTo":
  62. return fmt.Sprintf("'%s'", m.timeRange.GetToAsTimeUTC().Format(time.RFC3339)), nil
  63. case "__timeGroup":
  64. if len(args) < 2 {
  65. return "", fmt.Errorf("macro %v needs time column and interval", name)
  66. }
  67. interval, err := gtime.ParseInterval(strings.Trim(args[1], `'"`))
  68. if err != nil {
  69. return "", fmt.Errorf("error parsing interval %v", args[1])
  70. }
  71. if len(args) == 3 {
  72. err := tsdb.SetupFillmode(m.query, interval, args[2])
  73. if err != nil {
  74. return "", err
  75. }
  76. }
  77. return fmt.Sprintf("FLOOR(DATEDIFF(second, '1970-01-01', %s)/%.0f)*%.0f", args[0], interval.Seconds(), interval.Seconds()), nil
  78. case "__timeGroupAlias":
  79. tg, err := m.evaluateMacro("__timeGroup", args)
  80. if err == nil {
  81. return tg + " AS [time]", err
  82. }
  83. return "", err
  84. case "__unixEpochFilter":
  85. if len(args) == 0 {
  86. return "", fmt.Errorf("missing time column argument for macro %v", name)
  87. }
  88. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsSecondsEpoch(), args[0], m.timeRange.GetToAsSecondsEpoch()), nil
  89. case "__unixEpochNanoFilter":
  90. if len(args) == 0 {
  91. return "", fmt.Errorf("missing time column argument for macro %v", name)
  92. }
  93. return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], m.timeRange.GetFromAsTimeUTC().UnixNano(), args[0], m.timeRange.GetToAsTimeUTC().UnixNano()), nil
  94. case "__unixEpochNanoFrom":
  95. return fmt.Sprintf("%d", m.timeRange.GetFromAsTimeUTC().UnixNano()), nil
  96. case "__unixEpochNanoTo":
  97. return fmt.Sprintf("%d", m.timeRange.GetToAsTimeUTC().UnixNano()), nil
  98. case "__unixEpochGroup":
  99. if len(args) < 2 {
  100. return "", fmt.Errorf("macro %v needs time column and interval and optional fill value", name)
  101. }
  102. interval, err := gtime.ParseInterval(strings.Trim(args[1], `'`))
  103. if err != nil {
  104. return "", fmt.Errorf("error parsing interval %v", args[1])
  105. }
  106. if len(args) == 3 {
  107. err := tsdb.SetupFillmode(m.query, interval, args[2])
  108. if err != nil {
  109. return "", err
  110. }
  111. }
  112. return fmt.Sprintf("FLOOR(%s/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
  113. case "__unixEpochGroupAlias":
  114. tg, err := m.evaluateMacro("__unixEpochGroup", args)
  115. if err == nil {
  116. return tg + " AS [time]", err
  117. }
  118. return "", err
  119. default:
  120. return "", fmt.Errorf("Unknown macro %v", name)
  121. }
  122. }