interval.go 5.4 KB

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