Skip to content

Commit

Permalink
[Observability] Persist time range across apps (#79258)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
cauemarcondes and kibanamachine authored Oct 6, 2020
1 parent b8f4ea1 commit d8b4472
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/observability/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"kibanaVersion": "kibana",
"configPath": ["xpack", "observability"],
"optionalPlugins": ["licensing", "home", "usageCollection"],
"requiredPlugins": ["data"],
"ui": true,
"server": true,
"requiredBundles": ["data", "kibanaReact", "kibanaUtils"]
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/observability/public/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const renderApp = (

ReactDOM.render(
<KibanaContextProvider services={{ ...core, ...plugins }}>
<PluginContext.Provider value={{ core }}>
<PluginContext.Provider value={{ core, plugins }}>
<Router history={history}>
<EuiThemeProvider darkMode={isDarkMode}>
<i18nCore.Context>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

import { EuiSuperDatePicker } from '@elastic/eui';
import React from 'react';
import React, { useEffect } from 'react';
import { useHistory, useLocation } from 'react-router-dom';
import { UI_SETTINGS, useKibanaUISettings } from '../../../hooks/use_kibana_ui_settings';
import { usePluginContext } from '../../../hooks/use_plugin_context';
import { fromQuery, toQuery } from '../../../utils/url';

export interface TimePickerTime {
Expand All @@ -34,6 +35,14 @@ interface Props {
export function DatePicker({ rangeFrom, rangeTo, refreshPaused, refreshInterval }: Props) {
const location = useLocation();
const history = useHistory();
const { plugins } = usePluginContext();

useEffect(() => {
plugins.data.query.timefilter.timefilter.setTime({
from: rangeFrom,
to: rangeTo,
});
}, [plugins, rangeFrom, rangeTo]);

const timePickerQuickRanges = useKibanaUISettings<TimePickerQuickRange[]>(
UI_SETTINGS.TIMEPICKER_QUICK_RANGES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

import { createContext } from 'react';
import { CoreStart } from 'kibana/public';
import { ObservabilityPluginSetupDeps } from '../plugin';

export interface PluginContextValue {
core: CoreStart;
plugins: ObservabilityPluginSetupDeps;
}

export const PluginContext = createContext({} as PluginContextValue);
15 changes: 10 additions & 5 deletions x-pack/plugins/observability/public/pages/overview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ function calculateBucketSize({ start, end }: { start?: number; end?: number }) {
}

export function OverviewPage({ routeParams }: Props) {
const timePickerTime = useKibanaUISettings<TimePickerTime>(UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS);
const { core, plugins } = usePluginContext();

// read time from state and update the url
const timePickerSharedState = plugins.data.query.timefilter.timefilter.getTime();

const timePickerDefaults = useKibanaUISettings<TimePickerTime>(
UI_SETTINGS.TIMEPICKER_TIME_DEFAULTS
);

const relativeTime = {
start: routeParams.query.rangeFrom ?? timePickerTime.from,
end: routeParams.query.rangeTo ?? timePickerTime.to,
start: routeParams.query.rangeFrom || timePickerSharedState.from || timePickerDefaults.from,
end: routeParams.query.rangeTo || timePickerSharedState.to || timePickerDefaults.to,
};

const absoluteTime = {
Expand All @@ -52,8 +59,6 @@ export function OverviewPage({ routeParams }: Props) {
useTrackPageview({ app: 'observability', path: 'overview' });
useTrackPageview({ app: 'observability', path: 'overview', delay: 15000 });

const { core } = usePluginContext();

const { data: alerts = [], status: alertStatus } = useFetcher(() => {
return getObservabilityAlerts({ core });
}, [core]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MemoryRouter } from 'react-router-dom';
import { UI_SETTINGS } from '../../../../../../src/plugins/data/public';
import { PluginContext } from '../../context/plugin_context';
import { registerDataHandler, unregisterDataHandler } from '../../data_handler';
import { ObservabilityPluginSetupDeps } from '../../plugin';
import { EuiThemeProvider } from '../../typings';
import { OverviewPage } from './';
import { alertsFetchData } from './mock/alerts.mock';
Expand All @@ -36,7 +37,18 @@ const withCore = makeDecorator({

return (
<MemoryRouter>
<PluginContext.Provider value={{ core: options as CoreStart }}>
<PluginContext.Provider
value={{
core: options as CoreStart,
plugins: ({
data: {
query: {
timefilter: { timefilter: { setTime: () => {}, getTime: () => ({}) } },
},
},
} as unknown) as ObservabilityPluginSetupDeps,
}}
>
<EuiThemeProvider>{storyFn(context)}</EuiThemeProvider>
</PluginContext.Provider>
</MemoryRouter>
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/observability/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { BehaviorSubject } from 'rxjs';
import { i18n } from '@kbn/i18n';
import { DataPublicPluginSetup } from '../../../../src/plugins/data/public';
import {
AppMountParameters,
AppUpdater,
Expand All @@ -25,6 +26,7 @@ export interface ObservabilityPluginSetup {

export interface ObservabilityPluginSetupDeps {
home?: HomePublicPluginSetup;
data: DataPublicPluginSetup;
}

export type ObservabilityPluginStart = void;
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/observability/public/utils/test_helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { of } from 'rxjs';
import { PluginContext } from '../context/plugin_context';
import { EuiThemeProvider } from '../typings';
import { KibanaContextProvider } from '../../../../../src/plugins/kibana_react/public';
import { ObservabilityPluginSetupDeps } from '../plugin';

export const core = ({
http: {
Expand All @@ -23,10 +24,14 @@ export const core = ({
},
} as unknown) as CoreStart;

const plugins = ({
data: { query: { timefilter: { timefilter: { setTime: jest.fn() } } } },
} as unknown) as ObservabilityPluginSetupDeps;

export const render = (component: React.ReactNode) => {
return testLibRender(
<KibanaContextProvider services={{ ...core }}>
<PluginContext.Provider value={{ core }}>
<PluginContext.Provider value={{ core, plugins }}>
<EuiThemeProvider>{component}</EuiThemeProvider>
</PluginContext.Provider>
</KibanaContextProvider>
Expand Down

0 comments on commit d8b4472

Please sign in to comment.