macros.go 4.2 KB

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