github.com/grafana/pyroscope@v1.18.0/pkg/querier/timeline/calculator.go (about) 1 package timeline 2 3 // Heavily inspired by https://github.com/grafana/grafana/blob/bc11a484ed97d9c87e8dd42347c2c34713e9e441/pkg/tsdb/intervalv2/intervalv2.go#L1 4 5 import ( 6 "time" 7 ) 8 9 var ( 10 DefaultRes int64 = 1500 11 DefaultMinInterval = time.Second * 15 12 ) 13 14 // CalcPointInterval calculates the appropriate interval between each point (aka step) 15 // Note that its main usage is with SelectSeries, therefore its 16 // * inputs are in ms 17 // * output is in seconds 18 func CalcPointInterval(fromMs int64, untilMs int64) float64 { 19 resolution := DefaultRes 20 21 fromNano := fromMs * 1000000 22 untilNano := untilMs * 1000000 23 calculatedIntervalNano := time.Duration((untilNano - fromNano) / resolution) 24 25 if calculatedIntervalNano < DefaultMinInterval { 26 return DefaultMinInterval.Seconds() 27 } 28 29 return roundInterval(calculatedIntervalNano).Seconds() 30 } 31 32 //nolint:gocyclo 33 func roundInterval(interval time.Duration) time.Duration { 34 // Notice that interval may be smaller than DefaultMinInterval, and therefore some branches may never be reached 35 // These branches are left in case the invariant changes 36 switch { 37 // 0.01s 38 case interval <= 10*time.Millisecond: 39 return time.Millisecond * 1 // 0.001s 40 // 0.015s 41 case interval <= 15*time.Millisecond: 42 return time.Millisecond * 10 // 0.01s 43 // 0.035s 44 case interval <= 35*time.Millisecond: 45 return time.Millisecond * 20 // 0.02s 46 // 0.075s 47 case interval <= 75*time.Millisecond: 48 return time.Millisecond * 50 // 0.05s 49 // 0.15s 50 case interval <= 150*time.Millisecond: 51 return time.Millisecond * 100 // 0.1s 52 // 0.35s 53 case interval <= 350*time.Millisecond: 54 return time.Millisecond * 200 // 0.2s 55 // 0.75s 56 case interval <= 750*time.Millisecond: 57 return time.Millisecond * 500 // 0.5s 58 // 1.5s 59 case interval <= 1500*time.Millisecond: 60 return time.Millisecond * 1000 // 1s 61 // 3.5s 62 case interval <= 3500*time.Millisecond: 63 return time.Millisecond * 2000 // 2s 64 // 7.5s 65 case interval <= 7500*time.Millisecond: 66 return time.Millisecond * 5000 // 5s 67 // 12.5s 68 case interval <= 12500*time.Millisecond: 69 return time.Millisecond * 10000 // 10s 70 // 17.5s 71 case interval <= 17500*time.Millisecond: 72 return time.Millisecond * 15000 // 15s 73 // 25s 74 case interval <= 25000*time.Millisecond: 75 return time.Millisecond * 20000 // 20s 76 // 45s 77 case interval <= 45000*time.Millisecond: 78 return time.Millisecond * 30000 // 30s 79 // 1.5m 80 case interval <= 90000*time.Millisecond: 81 return time.Millisecond * 60000 // 1m 82 // 3.5m 83 case interval <= 210000*time.Millisecond: 84 return time.Millisecond * 120000 // 2m 85 // 7.5m 86 case interval <= 450000*time.Millisecond: 87 return time.Millisecond * 300000 // 5m 88 // 12.5m 89 case interval <= 750000*time.Millisecond: 90 return time.Millisecond * 600000 // 10m 91 // 17.5m 92 case interval <= 1050000*time.Millisecond: 93 return time.Millisecond * 900000 // 15m 94 // 25m 95 case interval <= 1500000*time.Millisecond: 96 return time.Millisecond * 1200000 // 20m 97 // 45m 98 case interval <= 2700000*time.Millisecond: 99 return time.Millisecond * 1800000 // 30m 100 // 1.5h 101 case interval <= 5400000*time.Millisecond: 102 return time.Millisecond * 3600000 // 1h 103 // 2.5h 104 case interval <= 9000000*time.Millisecond: 105 return time.Millisecond * 7200000 // 2h 106 // 4.5h 107 case interval <= 16200000*time.Millisecond: 108 return time.Millisecond * 10800000 // 3h 109 // 9h 110 case interval <= 32400000*time.Millisecond: 111 return time.Millisecond * 21600000 // 6h 112 // 24h 113 case interval <= 86400000*time.Millisecond: 114 return time.Millisecond * 43200000 // 12h 115 // 48h 116 case interval <= 172800000*time.Millisecond: 117 return time.Millisecond * 86400000 // 24h 118 // 1w 119 case interval <= 604800000*time.Millisecond: 120 return time.Millisecond * 86400000 // 24h 121 // 3w 122 case interval <= 1814400000*time.Millisecond: 123 return time.Millisecond * 604800000 // 1w 124 // 2y 125 case interval < 3628800000*time.Millisecond: 126 return time.Millisecond * 2592000000 // 30d 127 default: 128 return time.Millisecond * 31536000000 // 1y 129 } 130 }