interval.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package tsdb
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. var (
  7. defaultRes int64 = 1500
  8. minInterval time.Duration = 1 * time.Millisecond
  9. year time.Duration = time.Hour * 24 * 365
  10. day time.Duration = time.Hour * 24 * 365
  11. )
  12. type Interval struct {
  13. Text string
  14. Value time.Duration
  15. }
  16. func CalculateInterval(timerange *TimeRange) Interval {
  17. interval := time.Duration((timerange.MustGetTo().UnixNano() - timerange.MustGetFrom().UnixNano()) / defaultRes)
  18. if interval < minInterval {
  19. return Interval{Text: formatDuration(minInterval), Value: interval}
  20. }
  21. return Interval{Text: formatDuration(roundInterval(interval)), Value: interval}
  22. }
  23. func formatDuration(inter time.Duration) string {
  24. if inter >= year {
  25. return fmt.Sprintf("%dy", inter/year)
  26. }
  27. if inter >= day {
  28. return fmt.Sprintf("%dd", inter/day)
  29. }
  30. if inter >= time.Hour {
  31. return fmt.Sprintf("%dh", inter/time.Hour)
  32. }
  33. if inter >= time.Minute {
  34. return fmt.Sprintf("%dm", inter/time.Minute)
  35. }
  36. if inter >= time.Second {
  37. return fmt.Sprintf("%ds", inter/time.Second)
  38. }
  39. if inter >= time.Millisecond {
  40. return fmt.Sprintf("%dms", inter/time.Millisecond)
  41. }
  42. return "1ms"
  43. }
  44. func roundInterval(interval time.Duration) time.Duration {
  45. switch true {
  46. // 0.015s
  47. case interval <= 15*time.Millisecond:
  48. return time.Millisecond * 10 // 0.01s
  49. // 0.035s
  50. case interval <= 35*time.Millisecond:
  51. return time.Millisecond * 20 // 0.02s
  52. // 0.075s
  53. case interval <= 75*time.Millisecond:
  54. return time.Millisecond * 50 // 0.05s
  55. // 0.15s
  56. case interval <= 150*time.Millisecond:
  57. return time.Millisecond * 100 // 0.1s
  58. // 0.35s
  59. case interval <= 350*time.Millisecond:
  60. return time.Millisecond * 200 // 0.2s
  61. // 0.75s
  62. case interval <= 750*time.Millisecond:
  63. return time.Millisecond * 500 // 0.5s
  64. // 1.5s
  65. case interval <= 1500*time.Millisecond:
  66. return time.Millisecond * 1000 // 1s
  67. // 3.5s
  68. case interval <= 3500*time.Millisecond:
  69. return time.Millisecond * 2000 // 2s
  70. // 7.5s
  71. case interval <= 7500*time.Millisecond:
  72. return time.Millisecond * 5000 // 5s
  73. // 12.5s
  74. case interval <= 12500*time.Millisecond:
  75. return time.Millisecond * 10000 // 10s
  76. // 17.5s
  77. case interval <= 17500*time.Millisecond:
  78. return time.Millisecond * 15000 // 15s
  79. // 25s
  80. case interval <= 25000*time.Millisecond:
  81. return time.Millisecond * 20000 // 20s
  82. // 45s
  83. case interval <= 45000*time.Millisecond:
  84. return time.Millisecond * 30000 // 30s
  85. // 1.5m
  86. case interval <= 90000*time.Millisecond:
  87. return time.Millisecond * 60000 // 1m
  88. // 3.5m
  89. case interval <= 210000*time.Millisecond:
  90. return time.Millisecond * 120000 // 2m
  91. // 7.5m
  92. case interval <= 450000*time.Millisecond:
  93. return time.Millisecond * 300000 // 5m
  94. // 12.5m
  95. case interval <= 750000*time.Millisecond:
  96. return time.Millisecond * 600000 // 10m
  97. // 12.5m
  98. case interval <= 1050000*time.Millisecond:
  99. return time.Millisecond * 900000 // 15m
  100. // 25m
  101. case interval <= 1500000*time.Millisecond:
  102. return time.Millisecond * 1200000 // 20m
  103. // 45m
  104. case interval <= 2700000*time.Millisecond:
  105. return time.Millisecond * 1800000 // 30m
  106. // 1.5h
  107. case interval <= 5400000*time.Millisecond:
  108. return time.Millisecond * 3600000 // 1h
  109. // 2.5h
  110. case interval <= 9000000*time.Millisecond:
  111. return time.Millisecond * 7200000 // 2h
  112. // 4.5h
  113. case interval <= 16200000*time.Millisecond:
  114. return time.Millisecond * 10800000 // 3h
  115. // 9h
  116. case interval <= 32400000*time.Millisecond:
  117. return time.Millisecond * 21600000 // 6h
  118. // 24h
  119. case interval <= 86400000*time.Millisecond:
  120. return time.Millisecond * 43200000 // 12h
  121. // 48h
  122. case interval <= 172800000*time.Millisecond:
  123. return time.Millisecond * 86400000 // 24h
  124. // 1w
  125. case interval <= 604800000*time.Millisecond:
  126. return time.Millisecond * 86400000 // 24h
  127. // 3w
  128. case interval <= 1814400000*time.Millisecond:
  129. return time.Millisecond * 604800000 // 1w
  130. // 2y
  131. case interval < 3628800000*time.Millisecond:
  132. return time.Millisecond * 2592000000 // 30d
  133. default:
  134. return time.Millisecond * 31536000000 // 1y
  135. }
  136. }