Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Montiroing] Add monitoring toolbar with date picker #110612

Merged
merged 7 commits into from
Sep 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { MonitoringStartPluginDependencies } from '../types';
interface GlobalStateProviderProps {
query: MonitoringStartPluginDependencies['data']['query'];
toasts: MonitoringStartPluginDependencies['core']['notifications']['toasts'];
children: React.ReactNode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew that this had to be wrong 😅 thanks for fixing it!

}

interface State {
Expand All @@ -20,7 +19,11 @@ interface State {

export const GlobalStateContext = createContext({} as State);

export const GlobalStateProvider = ({ query, toasts, children }: GlobalStateProviderProps) => {
export const GlobalStateProvider: React.FC<GlobalStateProviderProps> = ({
query,
toasts,
children,
}) => {
// TODO: remove fakeAngularRootScope and fakeAngularLocation when angular is removed
const fakeAngularRootScope: Partial<ng.IRootScopeService> = {
$on: (
Expand Down
66 changes: 40 additions & 26 deletions x-pack/plugins/monitoring/public/application/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { MonitoringStartPluginDependencies } from '../types';
import { GlobalStateProvider } from './global_state_context';
import { createPreserveQueryHistory } from './preserve_query_history';
import { RouteInit } from './route_init';
import { MonitoringTimeContainer } from './pages/use_monitoring_time';
import { MonitoringToolbar } from '../components/shared/toolbar';

export const renderApp = (
core: CoreStart,
Expand All @@ -37,31 +39,38 @@ const MonitoringApp: React.FC<{
return (
<KibanaContextProvider services={{ ...core, ...plugins }}>
<GlobalStateProvider query={plugins.data.query} toasts={core.notifications.toasts}>
<Router history={history}>
<Switch>
<Route path="/no-data" component={NoData} />
<Route path="/loading" component={LoadingPage} />
<RouteInit
path="/license"
component={License}
codePaths={['all']}
fetchAllClusters={false}
/>
<RouteInit path="/home" component={Home} codePaths={['all']} fetchAllClusters={false} />
<RouteInit
path="/overview"
component={ClusterOverview}
codePaths={['all']}
fetchAllClusters={false}
/>
<Redirect
to={{
pathname: '/loading',
search: history.location.search,
}}
/>
</Switch>
</Router>
<MonitoringTimeContainer.Provider>
<Router history={history}>
<Switch>
<Route path="/no-data" component={NoData} />
<Route path="/loading" component={LoadingPage} />
<RouteInit
path="/license"
component={License}
codePaths={['all']}
fetchAllClusters={false}
/>
<RouteInit
path="/home"
component={Home}
codePaths={['all']}
fetchAllClusters={false}
/>
<RouteInit
path="/overview"
component={ClusterOverview}
codePaths={['all']}
fetchAllClusters={false}
/>
<Redirect
to={{
pathname: '/loading',
search: history.location.search,
}}
/>
</Switch>
</Router>
</MonitoringTimeContainer.Provider>
</GlobalStateProvider>
</KibanaContextProvider>
);
Expand All @@ -76,7 +85,12 @@ const Home: React.FC<{}> = () => {
};

const ClusterOverview: React.FC<{}> = () => {
return <div>Cluster overview page</div>;
return (
<div>
<MonitoringToolbar />
Cluster overview page
</div>
);
};

const License: React.FC<{}> = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { useTitle } from '../hooks/use_title';

interface PageTemplateProps {
title: string;
children: React.ReactNode;
}

export const PageTemplate = ({ title, children }: PageTemplateProps) => {
export const PageTemplate: React.FC<PageTemplateProps> = ({ title, children }) => {
useTitle('', title);

return <div>{children}</div>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { useCallback, useState } from 'react';
import createContainer from 'constate';

interface TimeOptions {
from: string;
to: string;
interval: string;
}

export const DEFAULT_TIMERANGE: TimeOptions = {
from: 'now-1h',
to: 'now',
interval: '>=10s',
};

export const useMonitoringTime = () => {
const defaultTimeRange = {
from: 'now-1h',
to: 'now',
interval: DEFAULT_TIMERANGE.interval,
};
const [refreshInterval, setRefreshInterval] = useState(5000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This value should be 10000.

const [isPaused, setIsPaused] = useState(false);
const [currentTimerange, setTimeRange] = useState<TimeOptions>(defaultTimeRange);

const handleTimeChange = useCallback(
(start: string, end: string) => {
setTimeRange({ ...currentTimerange, from: start, to: end });
},
[currentTimerange, setTimeRange]
);

return {
currentTimerange,
setTimeRange,
handleTimeChange,
setRefreshInterval,
refreshInterval,
setIsPaused,
isPaused,
};
};

export const MonitoringTimeContainer = createContainer(useMonitoringTime);
56 changes: 56 additions & 0 deletions x-pack/plugins/monitoring/public/components/shared/toolbar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { EuiFlexGroup, EuiFlexItem, EuiSuperDatePicker, OnRefreshChangeProps } from '@elastic/eui';
import React, { useContext, useCallback } from 'react';
import { MonitoringTimeContainer } from '../../application/pages/use_monitoring_time';

export const MonitoringToolbar = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This component should probably be used in the page template

const {
currentTimerange,
handleTimeChange,
setRefreshInterval,
refreshInterval,
setIsPaused,
isPaused,
} = useContext(MonitoringTimeContainer.Context);

const onTimeChange = useCallback(
(selectedTime: { start: string; end: string; isInvalid: boolean }) => {
if (selectedTime.isInvalid) {
return;
}
handleTimeChange(selectedTime.start, selectedTime.end);
},
[handleTimeChange]
);

const onRefreshChange = useCallback(
({ refreshInterval: ri, isPaused: isP }: OnRefreshChangeProps) => {
setRefreshInterval(ri);
setIsPaused(isP);
},
[setRefreshInterval, setIsPaused]
);

return (
<EuiFlexGroup gutterSize={'xl'} justifyContent={'spaceBetween'}>
<EuiFlexItem>Setup Button</EuiFlexItem>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later, but looks like the NoData screen doesn't show the setup button. Guessing we'll need a flag to hide it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matschaffer the setup button should not be rendered from the template. It's rendered from the setup lib.
In the page template, we only need the container that is used from that method, so the only thing we need to do is some cleanup here.

<EuiFlexItem grow={false}>
<EuiSuperDatePicker
start={currentTimerange.from}
end={currentTimerange.to}
onTimeChange={onTimeChange}
onRefresh={() => {}}
isPaused={isPaused}
refreshInterval={refreshInterval}
onRefreshChange={onRefreshChange}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};