({
location,
navigate,
pagefilters,
unspecifiedStrategy,
}: Options)
| 58 | } |
| 59 | |
| 60 | function useChartIntervalImpl({ |
| 61 | location, |
| 62 | navigate, |
| 63 | pagefilters, |
| 64 | unspecifiedStrategy, |
| 65 | }: Options): [ |
| 66 | string, |
| 67 | (interval: string) => void, |
| 68 | intervalOptions: Array<{label: string; value: string}>, |
| 69 | ] { |
| 70 | const {datetime} = pagefilters.selection; |
| 71 | |
| 72 | const {baseIntervalOptions, defaultInterval, minimumIntervalInHours, timeRangeInHours} = |
| 73 | useMemo(() => { |
| 74 | const diffInMinutes = getDiffInMinutes(datetime); |
| 75 | const options = getIntervalOptionsForPageFilter(datetime); |
| 76 | |
| 77 | // Compute the default from the ladder-derived options, before appending extras |
| 78 | let fallback: string; |
| 79 | switch (unspecifiedStrategy) { |
| 80 | case ChartIntervalUnspecifiedStrategy.USE_SMALLEST: |
| 81 | fallback = options[0]!.value; |
| 82 | break; |
| 83 | case ChartIntervalUnspecifiedStrategy.USE_BIGGEST: |
| 84 | fallback = options[options.length - 1]!.value; |
| 85 | break; |
| 86 | case ChartIntervalUnspecifiedStrategy.USE_SECOND_BIGGEST: |
| 87 | default: |
| 88 | fallback = |
| 89 | options[options.length - 2]?.value ?? options[options.length - 1]!.value; |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | if (diffInMinutes >= MINIMUM_DURATION_FOR_ONE_DAY_INTERVAL) { |
| 94 | options.push(ONE_DAY_OPTION); |
| 95 | } |
| 96 | |
| 97 | return { |
| 98 | baseIntervalOptions: options, |
| 99 | defaultInterval: fallback, |
| 100 | // The smallest interval allowed for the current time range. Anything more |
| 101 | // granular than this would produce too many buckets. |
| 102 | minimumIntervalInHours: parsePeriodToHours( |
| 103 | MINIMUM_INTERVAL.getInterval(diffInMinutes) |
| 104 | ), |
| 105 | // The full duration of the selected time range. An interval larger than |
| 106 | // this would produce a single (incomplete) bucket. |
| 107 | timeRangeInHours: diffInMinutes / 60, |
| 108 | }; |
| 109 | }, [datetime, unspecifiedStrategy]); |
| 110 | |
| 111 | const [interval, intervalOptions] = useMemo((): [ |
| 112 | string, |
| 113 | Array<{label: string; value: string}>, |
| 114 | ] => { |
| 115 | const decodedInterval = decodeScalar(location.query.interval); |
| 116 | |
| 117 | if (!decodedInterval) { |
no test coverage detected