interval.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package tsdb
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/models"
  8. )
  9. var (
  10. defaultRes int64 = 1500
  11. defaultMinInterval time.Duration = 1 * time.Millisecond
  12. year time.Duration = time.Hour * 24 * 365
  13. day time.Duration = time.Hour * 24
  14. )
  15. type Interval struct {
  16. Text string
  17. Value time.Duration
  18. }
  19. type intervalCalculator struct {
  20. minInterval time.Duration
  21. }
  22. type IntervalCalculator interface {
  23. Calculate(timeRange *TimeRange, minInterval time.Duration) Interval
  24. }
  25. type IntervalOptions struct {
  26. MinInterval time.Duration
  27. }
  28. func NewIntervalCalculator(opt *IntervalOptions) *intervalCalculator {
  29. if opt == nil {
  30. opt = &IntervalOptions{}
  31. }
  32. calc := &intervalCalculator{}
  33. if opt.MinInterval == 0 {
  34. calc.minInterval = defaultMinInterval
  35. } else {
  36. calc.minInterval = opt.MinInterval
  37. }
  38. return calc
  39. }
  40. func (ic *intervalCalculator) Calculate(timerange *TimeRange, minInterval time.Duration) Interval {
  41. to := timerange.MustGetTo().UnixNano()
  42. from := timerange.MustGetFrom().UnixNano()
  43. interval := time.Duration((to - from) / defaultRes)
  44. if interval < minInterval {
  45. return Interval{Text: formatDuration(minInterval), Value: minInterval}
  46. }
  47. rounded := roundInterval(interval)
  48. return Interval{Text: formatDuration(rounded), Value: rounded}
  49. }
  50. func GetIntervalFrom(dsInfo *models.DataSource, queryModel *simplejson.Json, defaultInterval time.Duration) (time.Duration, error) {
  51. interval := queryModel.Get("interval").MustString("")
  52. if interval == "" && dsInfo.JsonData != nil {
  53. dsInterval := dsInfo.JsonData.Get("timeInterval").MustString("")
  54. if dsInterval != "" {
  55. interval = dsInterval
  56. }
  57. }
  58. if interval == "" {
  59. return defaultInterval, nil
  60. }
  61. interval = strings.Replace(strings.Replace(interval, "<", "", 1), ">", "", 1)
  62. parsedInterval, err := time.ParseDuration(interval)
  63. if err != nil {
  64. return time.Duration(0), err
  65. }
  66. return parsedInterval, nil
  67. }
  68. func formatDuration(inter time.Duration) string {
  69. if inter >= year {
  70. return fmt.Sprintf("%dy", inter/year)
  71. }
  72. if inter >= day {
  73. return fmt.Sprintf("%dd", inter/day)
  74. }
  75. if inter >= time.Hour {
  76. return fmt.Sprintf("%dh", inter/time.Hour)
  77. }
  78. if inter >= time.Minute {
  79. return fmt.Sprintf("%dm", inter/time.Minute)
  80. }
  81. if inter >= time.Second {
  82. return fmt.Sprintf("%ds", inter/time.Second)
  83. }
  84. if inter >= time.Millisecond {
  85. return fmt.Sprintf("%dms", inter/time.Millisecond)
  86. }
  87. return "1ms"
  88. }
  89. func roundInterval(interval time.Duration) time.Duration {
  90. switch true {
  91. // 0.015s
  92. case interval <= 15*time.Millisecond:
  93. return time.Millisecond * 10 // 0.01s
  94. // 0.035s
  95. case interval <= 35*time.Millisecond:
  96. return time.Millisecond * 20 // 0.02s
  97. // 0.075s
  98. case interval <= 75*time.Millisecond:
  99. return time.Millisecond * 50 // 0.05s
  100. // 0.15s
  101. case interval <= 150*time.Millisecond:
  102. return time.Millisecond * 100 // 0.1s
  103. // 0.35s
  104. case interval <= 350*time.Millisecond:
  105. return time.Millisecond * 200 // 0.2s
  106. // 0.75s
  107. case interval <= 750*time.Millisecond:
  108. return time.Millisecond * 500 // 0.5s
  109. // 1.5s
  110. case interval <= 1500*time.Millisecond:
  111. return time.Millisecond * 1000 // 1s
  112. // 3.5s
  113. case interval <= 3500*time.Millisecond:
  114. return time.Millisecond * 2000 // 2s
  115. // 7.5s
  116. case interval <= 7500*time.Millisecond:
  117. return time.Millisecond * 5000 // 5s
  118. // 12.5s
  119. case interval <= 12500*time.Millisecond:
  120. return time.Millisecond * 10000 // 10s
  121. // 17.5s
  122. case interval <= 17500*time.Millisecond:
  123. return time.Millisecond * 15000 // 15s
  124. // 25s
  125. case interval <= 25000*time.Millisecond:
  126. return time.Millisecond * 20000 // 20s
  127. // 45s
  128. case interval <= 45000*time.Millisecond:
  129. return time.Millisecond * 30000 // 30s
  130. // 1.5m
  131. case interval <= 90000*time.Millisecond:
  132. return time.Millisecond * 60000 // 1m
  133. // 3.5m
  134. case interval <= 210000*time.Millisecond:
  135. return time.Millisecond * 120000 // 2m
  136. // 7.5m
  137. case interval <= 450000*time.Millisecond:
  138. return time.Millisecond * 300000 // 5m
  139. // 12.5m
  140. case interval <= 750000*time.Millisecond:
  141. return time.Millisecond * 600000 // 10m
  142. // 12.5m
  143. case interval <= 1050000*time.Millisecond:
  144. return time.Millisecond * 900000 // 15m
  145. // 25m
  146. case interval <= 1500000*time.Millisecond:
  147. return time.Millisecond * 1200000 // 20m
  148. // 45m
  149. case interval <= 2700000*time.Millisecond:
  150. return time.Millisecond * 1800000 // 30m
  151. // 1.5h
  152. case interval <= 5400000*time.Millisecond:
  153. return time.Millisecond * 3600000 // 1h
  154. // 2.5h
  155. case interval <= 9000000*time.Millisecond:
  156. return time.Millisecond * 7200000 // 2h
  157. // 4.5h
  158. case interval <= 16200000*time.Millisecond:
  159. return time.Millisecond * 10800000 // 3h
  160. // 9h
  161. case interval <= 32400000*time.Millisecond:
  162. return time.Millisecond * 21600000 // 6h
  163. // 24h
  164. case interval <= 86400000*time.Millisecond:
  165. return time.Millisecond * 43200000 // 12h
  166. // 48h
  167. case interval <= 172800000*time.Millisecond:
  168. return time.Millisecond * 86400000 // 24h
  169. // 1w
  170. case interval <= 604800000*time.Millisecond:
  171. return time.Millisecond * 86400000 // 24h
  172. // 3w
  173. case interval <= 1814400000*time.Millisecond:
  174. return time.Millisecond * 604800000 // 1w
  175. // 2y
  176. case interval < 3628800000*time.Millisecond:
  177. return time.Millisecond * 2592000000 // 30d
  178. default:
  179. return time.Millisecond * 31536000000 // 1y
  180. }
  181. }