@@ -15,16 +15,24 @@ import { produce } from 'immer';
1515import {
1616 DatasourceSelect ,
1717 DatasourceSelectProps ,
18+ replaceVariables ,
19+ useAllVariableValues ,
1820 useDatasource ,
1921 useDatasourceClient ,
2022 useDatasourceSelectValueToSelector ,
23+ useSuggestedStepMs ,
24+ useTimeRange ,
2125} from '@perses-dev/plugin-system' ;
2226import { useId } from '@perses-dev/components' ;
2327import { FormControl , Stack , TextField } from '@mui/material' ;
24- import { ReactElement } from 'react' ;
28+ import { ReactElement , useContext , useMemo } from 'react' ;
29+ import { PanelEditorContext } from '@perses-dev/dashboards' ;
2530import {
2631 DEFAULT_PROM ,
2732 DurationString ,
33+ getDurationStringSeconds ,
34+ getPrometheusTimeRange ,
35+ getRangeStep ,
2836 isDefaultPromSelector ,
2937 isPrometheusDatasourceSelector ,
3038 PROM_DATASOURCE_KIND ,
@@ -39,7 +47,6 @@ import {
3947 useFormatState ,
4048 useMinStepState ,
4149} from './query-editor-model' ;
42-
4350/**
4451 * The options editor component for editing a PrometheusTimeSeriesQuery's spec.
4552 */
@@ -91,6 +98,37 @@ export function PrometheusTimeSeriesQueryEditor(props: PrometheusTimeSeriesQuery
9198 throw new Error ( 'Got unexpected non-Prometheus datasource selector' ) ;
9299 } ;
93100
101+ const variableState = useAllVariableValues ( ) ;
102+ const { absoluteTimeRange } = useTimeRange ( ) ;
103+ const panelEditorContext = useContext ( PanelEditorContext ) ;
104+ const suggestedStepMs = useSuggestedStepMs ( panelEditorContext ?. preview . previewPanelWidth ) ;
105+
106+ const minStepMs = useMemo ( ( ) => {
107+ /* Try catch is necessary, because when the minStep value is being typed, it will be valid when the duration unit is added. Example: 2m = 2 + m */
108+ try {
109+ const durationsSeconds = getDurationStringSeconds (
110+ replaceVariables ( minStepPlaceholder , variableState ) as DurationString
111+ ) ;
112+ return durationsSeconds !== undefined ? durationsSeconds * 1000 : undefined ;
113+ } catch {
114+ return undefined ;
115+ }
116+ } , [ variableState , minStepPlaceholder ] ) ;
117+
118+ const intervalMs = useMemo ( ( ) => {
119+ const minStepSeconds = ( minStepMs ?? 0 ) / 1000 ;
120+ return getRangeStep ( getPrometheusTimeRange ( absoluteTimeRange ) , minStepSeconds , undefined , suggestedStepMs ) * 1000 ;
121+ } , [ absoluteTimeRange , minStepMs , suggestedStepMs ] ) ;
122+
123+ const treeViewMetadata = useMemo ( ( ) => {
124+ return minStepMs && intervalMs
125+ ? {
126+ minStepMs,
127+ intervalMs,
128+ }
129+ : undefined ;
130+ } , [ minStepMs , intervalMs ] ) ;
131+
94132 return (
95133 < Stack spacing = { 2 } >
96134 < FormControl margin = "dense" fullWidth = { false } >
@@ -111,6 +149,7 @@ export function PrometheusTimeSeriesQueryEditor(props: PrometheusTimeSeriesQuery
111149 onChange = { handleQueryChange }
112150 onBlur = { handleQueryBlur }
113151 isReadOnly = { isReadonly }
152+ treeViewMetadata = { treeViewMetadata }
114153 />
115154 < Stack direction = "row" spacing = { 2 } >
116155 < TextField
0 commit comments