Skip to content

Commit 620958c

Browse files
committed
[Observability] Persist time range across apps (#79258)
* using kibana persisted date when available to set the date time * fixing types * adding setTime when changin the dates in the url * renaming Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> # Conflicts: # x-pack/plugins/observability/public/context/plugin_context.tsx # x-pack/plugins/observability/public/pages/overview/index.tsx # x-pack/plugins/observability/public/pages/overview/overview.stories.tsx # x-pack/plugins/observability/public/utils/test_helper.tsx
1 parent e754fb7 commit 620958c

File tree

8 files changed

+57
-19
lines changed

8 files changed

+57
-19
lines changed

x-pack/plugins/observability/kibana.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"kibanaVersion": "kibana",
55
"configPath": ["xpack", "observability"],
66
"optionalPlugins": ["licensing", "home", "usageCollection"],
7+
"requiredPlugins": ["data"],
78
"ui": true,
89
"server": true,
910
"requiredBundles": ["data", "kibanaReact", "kibanaUtils"]

x-pack/plugins/observability/public/application/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const renderApp = (
6565

6666
ReactDOM.render(
6767
<KibanaContextProvider services={{ ...core, ...plugins }}>
68-
<PluginContext.Provider value={{ core }}>
68+
<PluginContext.Provider value={{ core, plugins }}>
6969
<Router history={history}>
7070
<EuiThemeProvider darkMode={isDarkMode}>
7171
<i18nCore.Context>

x-pack/plugins/observability/public/components/shared/data_picker/index.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
*/
66

77
import { EuiSuperDatePicker } from '@elastic/eui';
8-
import React from 'react';
8+
import React, { useEffect } from 'react';
99
import { useHistory, useLocation } from 'react-router-dom';
1010
import { UI_SETTINGS, useKibanaUISettings } from '../../../hooks/use_kibana_ui_settings';
11+
import { usePluginContext } from '../../../hooks/use_plugin_context';
1112
import { fromQuery, toQuery } from '../../../utils/url';
1213

1314
export interface TimePickerTime {
@@ -34,6 +35,14 @@ interface Props {
3435
export function DatePicker({ rangeFrom, rangeTo, refreshPaused, refreshInterval }: Props) {
3536
const location = useLocation();
3637
const history = useHistory();
38+
const { plugins } = usePluginContext();
39+
40+
useEffect(() => {
41+
plugins.data.query.timefilter.timefilter.setTime({
42+
from: rangeFrom,
43+
to: rangeTo,
44+
});
45+
}, [plugins, rangeFrom, rangeTo]);
3746

3847
const timePickerQuickRanges = useKibanaUISettings<TimePickerQuickRange[]>(
3948
UI_SETTINGS.TIMEPICKER_QUICK_RANGES

x-pack/plugins/observability/public/context/plugin_context.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66

77
import { createContext } from 'react';
8-
import { AppMountContext } from 'kibana/public';
8+
import { CoreStart } from 'kibana/public';
9+
import { ObservabilityPluginSetupDeps } from '../plugin';
910

1011
export interface PluginContextValue {
11-
core: AppMountContext['core'];
12+
core: CoreStart;
13+
plugins: ObservabilityPluginSetupDeps;
1214
}
1315

1416
export const PluginContext = createContext({} as PluginContextValue);

x-pack/plugins/observability/public/pages/overview/index.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function calculatetBucketSize({ start, end }: { start?: number; end?: number })
4040
}
4141

4242
export function OverviewPage({ routeParams }: Props) {
43-
const { core } = usePluginContext();
43+
const { core, plugins } = usePluginContext();
4444

4545
useTrackPageview({ app: 'observability', path: 'overview' });
4646
useTrackPageview({ app: 'observability', path: 'overview', delay: 15000 });
@@ -52,7 +52,11 @@ export function OverviewPage({ routeParams }: Props) {
5252
const { data: newsFeed } = useFetcher(() => getNewsFeed({ core }), [core]);
5353

5454
const theme = useContext(ThemeContext);
55-
const timePickerTime = useKibanaUISettings<TimePickerTime>(UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS);
55+
const timePickerDefaults = useKibanaUISettings<TimePickerTime>(
56+
UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS
57+
);
58+
// read time from state and update the url
59+
const timePickerSharedState = plugins.data.query.timefilter.timefilter.getTime();
5660

5761
const result = useFetcher(() => fetchHasData(), []);
5862
const hasData = result.data;
@@ -64,8 +68,8 @@ export function OverviewPage({ routeParams }: Props) {
6468
const { refreshInterval = 10000, refreshPaused = true } = routeParams.query;
6569

6670
const relativeTime = {
67-
start: routeParams.query.rangeFrom ?? timePickerTime.from,
68-
end: routeParams.query.rangeTo ?? timePickerTime.to,
71+
start: routeParams.query.rangeFrom || timePickerSharedState.from || timePickerDefaults.from,
72+
end: routeParams.query.rangeTo || timePickerSharedState.to || timePickerDefaults.to,
6973
};
7074

7175
const absoluteTime = {

x-pack/plugins/observability/public/pages/overview/overview.stories.tsx

+18-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
import { makeDecorator } from '@storybook/addons';
88
import { storiesOf } from '@storybook/react';
9-
import { AppMountContext } from 'kibana/public';
9+
import { CoreStart } from 'kibana/public';
1010
import React from 'react';
1111
import { MemoryRouter } from 'react-router-dom';
1212
import { UI_SETTINGS } from '../../../../../../src/plugins/data/public';
1313
import { PluginContext } from '../../context/plugin_context';
1414
import { registerDataHandler, unregisterDataHandler } from '../../data_handler';
15+
import { ObservabilityPluginSetupDeps } from '../../plugin';
1516
import { EuiThemeProvider } from '../../typings';
1617
import { OverviewPage } from './';
1718
import { alertsFetchData } from './mock/alerts.mock';
@@ -36,7 +37,18 @@ const withCore = makeDecorator({
3637

3738
return (
3839
<MemoryRouter>
39-
<PluginContext.Provider value={{ core: options as AppMountContext['core'] }}>
40+
<PluginContext.Provider
41+
value={{
42+
core: options as CoreStart,
43+
plugins: ({
44+
data: {
45+
query: {
46+
timefilter: { timefilter: { setTime: () => {}, getTime: () => ({}) } },
47+
},
48+
},
49+
} as unknown) as ObservabilityPluginSetupDeps,
50+
}}
51+
>
4052
<EuiThemeProvider>{storyFn(context)}</EuiThemeProvider>
4153
</PluginContext.Provider>
4254
</MemoryRouter>
@@ -119,23 +131,23 @@ const core = ({
119131
return euiSettings[key];
120132
},
121133
},
122-
} as unknown) as AppMountContext['core'];
134+
} as unknown) as CoreStart;
123135

124136
const coreWithAlerts = ({
125137
...core,
126138
http: {
127139
...core.http,
128140
get: alertsFetchData,
129141
},
130-
} as unknown) as AppMountContext['core'];
142+
} as unknown) as CoreStart;
131143

132144
const coreWithNewsFeed = ({
133145
...core,
134146
http: {
135147
...core.http,
136148
get: newsFeedFetchData,
137149
},
138-
} as unknown) as AppMountContext['core'];
150+
} as unknown) as CoreStart;
139151

140152
const coreAlertsThrowsError = ({
141153
...core,
@@ -145,7 +157,7 @@ const coreAlertsThrowsError = ({
145157
throw new Error('Error fetching Alerts data');
146158
},
147159
},
148-
} as unknown) as AppMountContext['core'];
160+
} as unknown) as CoreStart;
149161

150162
storiesOf('app/Overview', module)
151163
.addDecorator(withCore(core))

x-pack/plugins/observability/public/plugin.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import { BehaviorSubject } from 'rxjs';
88
import { i18n } from '@kbn/i18n';
9+
import { DataPublicPluginSetup } from '../../../../src/plugins/data/public';
910
import {
1011
AppMountParameters,
1112
AppUpdater,
@@ -25,6 +26,7 @@ export interface ObservabilityPluginSetup {
2526

2627
export interface ObservabilityPluginSetupDeps {
2728
home?: HomePublicPluginSetup;
29+
data: DataPublicPluginSetup;
2830
}
2931

3032
export type ObservabilityPluginStart = void;

x-pack/plugins/observability/public/utils/test_helper.tsx

+13-5
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@
55
*/
66
import React from 'react';
77
import { render as testLibRender } from '@testing-library/react';
8-
import { AppMountContext } from 'kibana/public';
8+
import { CoreStart } from 'kibana/public';
99
import { PluginContext } from '../context/plugin_context';
1010
import { EuiThemeProvider } from '../typings';
11+
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
12+
import { ObservabilityPluginSetupDeps } from '../plugin';
1113

1214
export const core = ({
1315
http: {
1416
basePath: {
1517
prepend: jest.fn(),
1618
},
1719
},
18-
} as unknown) as AppMountContext['core'];
20+
} as unknown) as CoreStart;
21+
22+
const plugins = ({
23+
data: { query: { timefilter: { timefilter: { setTime: jest.fn() } } } },
24+
} as unknown) as ObservabilityPluginSetupDeps;
1925

2026
export const render = (component: React.ReactNode) => {
2127
return testLibRender(
22-
<PluginContext.Provider value={{ core }}>
23-
<EuiThemeProvider>{component}</EuiThemeProvider>
24-
</PluginContext.Provider>
28+
<KibanaContextProvider services={{ ...core }}>
29+
<PluginContext.Provider value={{ core, plugins }}>
30+
<EuiThemeProvider>{component}</EuiThemeProvider>
31+
</PluginContext.Provider>
32+
</KibanaContextProvider>
2533
);
2634
};

0 commit comments

Comments
 (0)