mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* clean up intervalv2 functions * use roundInterval from grafana-plugin-sdk-go * use from grafana-plugin-sdk-go * have intervalv2 in publicdashboards and remove tsdb/intervalv2 * legacydata cleanup * remove unused variables * Update pkg/tsdb/legacydata/interval/interval.go Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com> --------- Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package intervalv2
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
|
|
)
|
|
|
|
var (
|
|
DefaultRes int64 = 1500
|
|
defaultMinInterval = time.Millisecond * 1
|
|
)
|
|
|
|
type Interval struct {
|
|
Text string
|
|
Value time.Duration
|
|
}
|
|
|
|
type intervalCalculator struct {
|
|
minInterval time.Duration
|
|
}
|
|
|
|
type Calculator interface {
|
|
Calculate(timerange backend.TimeRange, minInterval time.Duration, maxDataPoints int64) Interval
|
|
CalculateSafeInterval(timerange backend.TimeRange, resolution int64) Interval
|
|
}
|
|
|
|
type CalculatorOptions struct {
|
|
MinInterval time.Duration
|
|
}
|
|
|
|
func NewCalculator(opts ...CalculatorOptions) *intervalCalculator {
|
|
calc := &intervalCalculator{}
|
|
|
|
for _, o := range opts {
|
|
if o.MinInterval == 0 {
|
|
calc.minInterval = defaultMinInterval
|
|
} else {
|
|
calc.minInterval = o.MinInterval
|
|
}
|
|
}
|
|
|
|
return calc
|
|
}
|
|
|
|
func (i *Interval) Milliseconds() int64 {
|
|
return i.Value.Nanoseconds() / int64(time.Millisecond)
|
|
}
|
|
|
|
func (ic *intervalCalculator) Calculate(timerange backend.TimeRange, minInterval time.Duration, maxDataPoints int64) Interval {
|
|
to := timerange.To.UnixNano()
|
|
from := timerange.From.UnixNano()
|
|
resolution := maxDataPoints
|
|
if resolution == 0 {
|
|
resolution = DefaultRes
|
|
}
|
|
|
|
calculatedInterval := time.Duration((to - from) / resolution)
|
|
|
|
if calculatedInterval < minInterval {
|
|
return Interval{Text: gtime.FormatInterval(minInterval), Value: minInterval}
|
|
}
|
|
|
|
rounded := gtime.RoundInterval(calculatedInterval)
|
|
|
|
return Interval{Text: gtime.FormatInterval(rounded), Value: rounded}
|
|
}
|
|
|
|
func (ic *intervalCalculator) CalculateSafeInterval(timerange backend.TimeRange, safeRes int64) Interval {
|
|
to := timerange.To.UnixNano()
|
|
from := timerange.From.UnixNano()
|
|
safeInterval := time.Duration((to - from) / safeRes)
|
|
|
|
rounded := gtime.RoundInterval(safeInterval)
|
|
return Interval{Text: gtime.FormatInterval(rounded), Value: rounded}
|
|
}
|