Skip to content

Commit 649d820

Browse files
[Metrics UI] Setup commonly used time ranges in timepicker (elastic#56701)
* [Metrics UI] Setup commonly used time ranges in timepicker * Fixing tests by mocking out useKibanaUISetting * add definition override to useKibanaUISetting for timepicker:quickRanges; reduce duplicate code for mapping quick ranges * Fixing types Co-authored-by: Elastic Machine <[email protected]>
1 parent 7e034c4 commit 649d820

File tree

5 files changed

+113
-38
lines changed

5 files changed

+113
-38
lines changed

x-pack/legacy/plugins/infra/public/components/metrics_explorer/toolbar.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { MetricsExplorerChartOptions as MetricsExplorerChartOptionsComponent } f
2626
import { SavedViewsToolbarControls } from '../saved_views/toolbar_control';
2727
import { MetricExplorerViewState } from '../../pages/infrastructure/metrics_explorer/use_metric_explorer_state';
2828
import { metricsExplorerViewSavedObjectType } from '../../../common/saved_objects/metrics_explorer_view';
29+
import { useKibanaUiSetting } from '../../utils/use_kibana_ui_setting';
30+
import { mapKibanaQuickRangesToDatePickerRanges } from '../../utils/map_timepicker_quickranges_to_datepicker_ranges';
2931

3032
interface Props {
3133
derivedIndexPattern: IIndexPattern;
@@ -59,6 +61,8 @@ export const MetricsExplorerToolbar = ({
5961
onViewStateChange,
6062
}: Props) => {
6163
const isDefaultOptions = options.aggregation === 'avg' && options.metrics.length === 0;
64+
const [timepickerQuickRanges] = useKibanaUiSetting('timepicker:quickRanges');
65+
const commonlyUsedRanges = mapKibanaQuickRangesToDatePickerRanges(timepickerQuickRanges);
6266
return (
6367
<Toolbar>
6468
<EuiFlexGroup alignItems="center">
@@ -134,6 +138,7 @@ export const MetricsExplorerToolbar = ({
134138
end={timeRange.to}
135139
onTimeChange={({ start, end }) => onTimeChange(start, end)}
136140
onRefresh={onRefresh}
141+
commonlyUsedRanges={commonlyUsedRanges}
137142
/>
138143
</EuiFlexItem>
139144
</EuiFlexGroup>

x-pack/legacy/plugins/infra/public/pages/metrics/components/time_controls.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
* or more contributor license agreements. Licensed under the Elastic License;
44
* you may not use this file except in compliance with the Elastic License.
55
*/
6+
jest.mock('../../../utils/use_kibana_ui_setting', () => ({
7+
_esModule: true,
8+
useKibanaUiSetting: jest.fn(() => [
9+
[
10+
{
11+
from: 'now/d',
12+
to: 'now/d',
13+
display: 'Today',
14+
},
15+
],
16+
]),
17+
}));
618

719
import React from 'react';
820
import { MetricsTimeControls } from './time_controls';

x-pack/legacy/plugins/infra/public/pages/metrics/components/time_controls.tsx

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
*/
66

77
import { EuiSuperDatePicker, OnRefreshChangeProps, OnTimeChangeProps } from '@elastic/eui';
8-
import React from 'react';
8+
import React, { useCallback } from 'react';
99
import euiStyled from '../../../../../../common/eui_styled_components';
1010
import { MetricsTimeInput } from '../containers/with_metrics_time';
11+
import { useKibanaUiSetting } from '../../../utils/use_kibana_ui_setting';
12+
import { mapKibanaQuickRangesToDatePickerRanges } from '../../../utils/map_timepicker_quickranges_to_datepicker_ranges';
1113

1214
interface MetricsTimeControlsProps {
1315
currentTimeRange: MetricsTimeInput;
@@ -19,41 +21,58 @@ interface MetricsTimeControlsProps {
1921
onRefresh: () => void;
2022
}
2123

22-
export class MetricsTimeControls extends React.Component<MetricsTimeControlsProps> {
23-
public render() {
24-
const { currentTimeRange, isLiveStreaming, refreshInterval } = this.props;
25-
return (
26-
<MetricsTimeControlsContainer>
27-
<EuiSuperDatePicker
28-
start={currentTimeRange.from}
29-
end={currentTimeRange.to}
30-
isPaused={!isLiveStreaming}
31-
refreshInterval={refreshInterval ? refreshInterval : 0}
32-
onTimeChange={this.handleTimeChange}
33-
onRefreshChange={this.handleRefreshChange}
34-
onRefresh={this.props.onRefresh}
35-
/>
36-
</MetricsTimeControlsContainer>
37-
);
38-
}
39-
40-
private handleTimeChange = ({ start, end }: OnTimeChangeProps) => {
41-
this.props.onChangeTimeRange({
42-
from: start,
43-
to: end,
44-
interval: '>=1m',
45-
});
46-
};
47-
48-
private handleRefreshChange = ({ isPaused, refreshInterval }: OnRefreshChangeProps) => {
49-
if (isPaused) {
50-
this.props.setAutoReload(false);
51-
} else {
52-
this.props.setRefreshInterval(refreshInterval);
53-
this.props.setAutoReload(true);
54-
}
55-
};
56-
}
24+
export const MetricsTimeControls = (props: MetricsTimeControlsProps) => {
25+
const [timepickerQuickRanges] = useKibanaUiSetting('timepicker:quickRanges');
26+
const {
27+
onChangeTimeRange,
28+
onRefresh,
29+
currentTimeRange,
30+
isLiveStreaming,
31+
refreshInterval,
32+
setAutoReload,
33+
setRefreshInterval,
34+
} = props;
35+
36+
const commonlyUsedRanges = mapKibanaQuickRangesToDatePickerRanges(timepickerQuickRanges);
37+
38+
const handleTimeChange = useCallback(
39+
({ start, end }: OnTimeChangeProps) => {
40+
onChangeTimeRange({
41+
from: start,
42+
to: end,
43+
interval: '>=1m',
44+
});
45+
},
46+
[onChangeTimeRange]
47+
);
48+
49+
const handleRefreshChange = useCallback(
50+
({ isPaused, refreshInterval: _refreshInterval }: OnRefreshChangeProps) => {
51+
if (isPaused) {
52+
setAutoReload(false);
53+
} else {
54+
setRefreshInterval(_refreshInterval);
55+
setAutoReload(true);
56+
}
57+
},
58+
[setAutoReload, setRefreshInterval]
59+
);
60+
61+
return (
62+
<MetricsTimeControlsContainer>
63+
<EuiSuperDatePicker
64+
start={currentTimeRange.from}
65+
end={currentTimeRange.to}
66+
isPaused={!isLiveStreaming}
67+
refreshInterval={refreshInterval ? refreshInterval : 0}
68+
onTimeChange={handleTimeChange}
69+
onRefreshChange={handleRefreshChange}
70+
onRefresh={onRefresh}
71+
commonlyUsedRanges={commonlyUsedRanges}
72+
/>
73+
</MetricsTimeControlsContainer>
74+
);
75+
};
5776

5877
const MetricsTimeControlsContainer = euiStyled.div`
5978
max-width: 750px;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import { EuiSuperDatePickerCommonRange } from '@elastic/eui';
8+
import { TimePickerQuickRange } from './use_kibana_ui_setting';
9+
10+
export const mapKibanaQuickRangesToDatePickerRanges = (
11+
timepickerQuickRanges: TimePickerQuickRange[] | undefined
12+
): EuiSuperDatePickerCommonRange[] =>
13+
timepickerQuickRanges
14+
? timepickerQuickRanges.map(r => ({
15+
start: r.from,
16+
end: r.to,
17+
label: r.display,
18+
}))
19+
: [];

x-pack/legacy/plugins/infra/public/utils/use_kibana_ui_setting.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,27 @@ import useObservable from 'react-use/lib/useObservable';
2525
* Unlike the `useState`, it doesn't give type guarantees for the value,
2626
* because the underlying `UiSettingsClient` doesn't support that.
2727
*/
28-
export const useKibanaUiSetting = (key: string, defaultValue?: any) => {
28+
29+
export interface TimePickerQuickRange {
30+
from: string;
31+
to: string;
32+
display: string;
33+
}
34+
35+
export function useKibanaUiSetting(
36+
key: 'timepicker:quickRanges',
37+
defaultValue?: TimePickerQuickRange[]
38+
): [
39+
TimePickerQuickRange[],
40+
(key: 'timepicker:quickRanges', value: TimePickerQuickRange[]) => Promise<boolean>
41+
];
42+
43+
export function useKibanaUiSetting(
44+
key: string,
45+
defaultValue?: any
46+
): [any, (key: string, value: any) => Promise<boolean>];
47+
48+
export function useKibanaUiSetting(key: string, defaultValue?: any) {
2949
const uiSettingsClient = npSetup.core.uiSettings;
3050

3151
const uiSetting$ = useMemo(() => uiSettingsClient.get$(key, defaultValue), [
@@ -41,4 +61,4 @@ export const useKibanaUiSetting = (key: string, defaultValue?: any) => {
4161
]);
4262

4363
return [uiSetting, setUiSetting];
44-
};
64+
}

0 commit comments

Comments
 (0)