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