Skip to content

Commit

Permalink
[Metrics UI] Refactor With* containers to hooks (#59503) (#63494)
Browse files Browse the repository at this point in the history
* [Metrics UI] Refactor containers to hooks

* clean up depends; move useInterval out of useWaffleTime;

* converting WithWaffleFilters to useWaffleFilters

* Removing WithWaffleOptions

* Refactor WithWaffleViewState to useWaffleViewState

* Removing obsolete files

* Fixing race condition with complext state

* Adding undefined to RisonValue; unwinding changes trying to work around bad type

* Switching to context

* Change assertion to ignore the length of the current URL

* Fixing test frameork to accept urls longer then 230 characters

* Fixes #59395; Refactor WithMetricsTime to hook; Fixes brushing on metric detail page; fixes refresh button on metric detail page

* Fixing tests with adding timeRange

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
simianhacker and elasticmachine committed Apr 14, 2020
1 parent de2099e commit 2ba01e1
Show file tree
Hide file tree
Showing 87 changed files with 1,087 additions and 2,149 deletions.
24 changes: 5 additions & 19 deletions test/functional/page_objects/common_page.ts
Expand Up @@ -247,25 +247,11 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo
}

currentUrl = (await browser.getCurrentUrl()).replace(/\/\/\w+:\w+@/, '//');
const maxAdditionalLengthOnNavUrl = 230;

// On several test failures at the end of the TileMap test we try to navigate back to
// Visualize so we can create the next Vertical Bar Chart, but we can see from the
// logging and the screenshot that it's still on the TileMap page. Why didn't the "get"
// with a new timestamped URL go? I thought that sleep(700) between the get and the
// refresh would solve the problem but didn't seem to always work.
// So this hack fails the navSuccessful check if the currentUrl doesn't match the
// appUrl plus up to 230 other chars.
// Navigating to Settings when there is a default index pattern has a URL length of 196
// (from debug output). Some other tabs may also be long. But a rather simple configured
// visualization is about 1000 chars long. So at least we catch that case.

// Browsers don't show the ':port' if it's 80 or 443 so we have to
// remove that part so we can get a match in the tests.
const navSuccessful = new RegExp(
appUrl.replace(':80/', '/').replace(':443/', '/') +
`.{0,${maxAdditionalLengthOnNavUrl}}$`
).test(currentUrl);

const navSuccessful = currentUrl
.replace(':80/', '/')
.replace(':443/', '/')
.startsWith(appUrl);

if (!navSuccessful) {
const msg = `App failed to load: ${appName} in ${defaultFindTimeout}ms appUrl=${appUrl} currentUrl=${currentUrl}`;
Expand Down
2 changes: 1 addition & 1 deletion test/typings/rison_node.d.ts
Expand Up @@ -18,7 +18,7 @@
*/

declare module 'rison-node' {
export type RisonValue = null | boolean | number | string | RisonObject | RisonArray;
export type RisonValue = undefined | null | boolean | number | string | RisonObject | RisonArray;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface RisonArray extends Array<RisonValue> {}
Expand Down
2 changes: 1 addition & 1 deletion typings/rison_node.d.ts
Expand Up @@ -18,7 +18,7 @@
*/

declare module 'rison-node' {
export type RisonValue = null | boolean | number | string | RisonObject | RisonArray;
export type RisonValue = undefined | null | boolean | number | string | RisonObject | RisonArray;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface RisonArray extends Array<RisonValue> {}
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/infra/common/http_api/metadata_api.ts
Expand Up @@ -11,6 +11,10 @@ export const InfraMetadataRequestRT = rt.type({
nodeId: rt.string,
nodeType: ItemTypeRT,
sourceId: rt.string,
timeRange: rt.type({
from: rt.number,
to: rt.number,
}),
});

export const InfraMetadataFeatureRT = rt.type({
Expand Down
Expand Up @@ -20,7 +20,7 @@ import { withTheme } from '../../../../observability/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { MetadataDetails } from '../../../public/pages/metrics/components/metadata_details';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, theme, onChangeRangeTime }: LayoutPropsWithTheme) => (
<React.Fragment>
<MetadataDetails
fields={[
Expand All @@ -42,6 +42,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection
id="awsEC2CpuUtilization"
Expand Down
Expand Up @@ -18,7 +18,7 @@ import { withTheme } from '../../../../observability/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LayoutContent } from '../../../public/pages/metrics/components/layout_content';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<LayoutContent>
<Section
Expand All @@ -30,6 +30,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection
id="awsRDSCpuTotal"
Expand Down
Expand Up @@ -18,7 +18,7 @@ import { withTheme } from '../../../../observability/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LayoutContent } from '../../../public/pages/metrics/components/layout_content';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<LayoutContent>
<Section
Expand All @@ -30,6 +30,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection
id="awsS3BucketSize"
Expand Down
Expand Up @@ -18,7 +18,7 @@ import { withTheme } from '../../../../observability/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LayoutContent } from '../../../public/pages/metrics/components/layout_content';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<LayoutContent>
<Section
Expand All @@ -30,6 +30,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection
id="awsSQSMessagesVisible"
Expand Down
Expand Up @@ -22,7 +22,7 @@ import { LayoutContent } from '../../../public/pages/metrics/components/layout_c
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { MetadataDetails } from '../../../public/pages/metrics/components/metadata_details';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<MetadataDetails />
<LayoutContent>
Expand All @@ -40,6 +40,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection id="containerOverview">
<GaugesSectionVis
Expand Down
8 changes: 5 additions & 3 deletions x-pack/plugins/infra/common/inventory_models/host/layout.tsx
Expand Up @@ -24,7 +24,7 @@ import { MetadataDetails } from '../../../public/pages/metrics/components/metada
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LayoutContent } from '../../../public/pages/metrics/components/layout_content';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<MetadataDetails
fields={[
Expand Down Expand Up @@ -52,6 +52,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection id="hostSystemOverview">
<GaugesSectionVis
Expand Down Expand Up @@ -242,6 +243,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection id="hostK8sOverview">
<GaugesSectionVis
Expand Down Expand Up @@ -371,8 +373,8 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
/>
</SubSection>
</Section>
<Aws.Layout metrics={metrics} />
<Ngnix.Layout metrics={metrics} />
<Aws.Layout metrics={metrics} onChangeRangeTime={onChangeRangeTime} />
<Ngnix.Layout metrics={metrics} onChangeRangeTime={onChangeRangeTime} />
</LayoutContent>
</React.Fragment>
));
5 changes: 3 additions & 2 deletions x-pack/plugins/infra/common/inventory_models/pod/layout.tsx
Expand Up @@ -23,7 +23,7 @@ import { MetadataDetails } from '../../../public/pages/metrics/components/metada
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { LayoutContent } from '../../../public/pages/metrics/components/layout_content';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<MetadataDetails />
<LayoutContent>
Expand All @@ -38,6 +38,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection id="podOverview">
<GaugesSectionVis
Expand Down Expand Up @@ -161,7 +162,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
/>
</SubSection>
</Section>
<Nginx.Layout metrics={metrics} />
<Nginx.Layout metrics={metrics} onChangeRangeTime={onChangeRangeTime} />
</LayoutContent>
</React.Fragment>
));
Expand Up @@ -18,7 +18,7 @@ import { ChartSectionVis } from '../../../../public/pages/metrics/components/cha
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { withTheme } from '../../../../../observability/public';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<Section
navLabel="AWS"
Expand All @@ -29,6 +29,7 @@ export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
}
)}
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection id="awsOverview">
<GaugesSectionVis
Expand Down
Expand Up @@ -16,9 +16,14 @@ import { ChartSectionVis } from '../../../../public/pages/metrics/components/cha
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { withTheme } from '../../../../../observability/public';

export const Layout = withTheme(({ metrics, theme }: LayoutPropsWithTheme) => (
export const Layout = withTheme(({ metrics, onChangeRangeTime, theme }: LayoutPropsWithTheme) => (
<React.Fragment>
<Section navLabel="Nginx" sectionLabel="Nginx" metrics={metrics}>
<Section
navLabel="Nginx"
sectionLabel="Nginx"
metrics={metrics}
onChangeRangeTime={onChangeRangeTime}
>
<SubSection
id="nginxHits"
label={i18n.translate(
Expand Down
10 changes: 8 additions & 2 deletions x-pack/plugins/infra/common/saved_objects/inventory_view.ts
Expand Up @@ -7,7 +7,7 @@
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { ElasticsearchMappingOf } from '../../server/utils/typed_elasticsearch_mappings';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { WaffleViewState } from '../../public/containers/waffle/with_waffle_view_state';
import { WaffleViewState } from '../../public/pages/inventory_view/hooks/use_waffle_view_state';

export const inventoryViewSavedObjectType = 'inventory-view';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
Expand Down Expand Up @@ -102,7 +102,7 @@ export const inventoryViewSavedObjectMappings: {
type: 'boolean',
},
time: {
type: 'integer',
type: 'long',
},
autoReload: {
type: 'boolean',
Expand All @@ -117,6 +117,12 @@ export const inventoryViewSavedObjectMappings: {
},
},
},
accountId: {
type: 'keyword',
},
region: {
type: 'keyword',
},
},
},
};
33 changes: 9 additions & 24 deletions x-pack/plugins/infra/public/apps/start_app.tsx
Expand Up @@ -8,19 +8,14 @@ import { createBrowserHistory } from 'history';
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import { Provider as ReduxStoreProvider } from 'react-redux';
import { BehaviorSubject } from 'rxjs';
import { pluck } from 'rxjs/operators';
import { CoreStart, AppMountParameters } from 'kibana/public';

// TODO use theme provided from parentApp when kibana supports it
import { EuiErrorBoundary } from '@elastic/eui';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { EuiThemeProvider } from '../../../observability/public/typings/eui_styled_components';
import { InfraFrontendLibs } from '../lib/lib';
import { createStore } from '../store';
import { ApolloClientContext } from '../utils/apollo_context';
import { ReduxStateContextProvider } from '../utils/redux_context';
import { HistoryContext } from '../utils/history_context';
import {
useUiSetting$,
Expand All @@ -43,32 +38,22 @@ export async function startApp(
) {
const { element, appBasePath } = params;
const history = createBrowserHistory({ basename: appBasePath });
const libs$ = new BehaviorSubject(libs);
const store = createStore({
apolloClient: libs$.pipe(pluck('apolloClient')),
observableApi: libs$.pipe(pluck('observableApi')),
});

const InfraPluginRoot: React.FunctionComponent = () => {
const [darkMode] = useUiSetting$<boolean>('theme:darkMode');

return (
<core.i18n.Context>
<EuiErrorBoundary>
<TriggersActionsProvider triggersActionsUI={triggersActionsUI}>
<ReduxStoreProvider store={store}>
<ReduxStateContextProvider>
<ApolloProvider client={libs.apolloClient}>
<ApolloClientContext.Provider value={libs.apolloClient}>
<EuiThemeProvider darkMode={darkMode}>
<HistoryContext.Provider value={history}>
<Router history={history} />
</HistoryContext.Provider>
</EuiThemeProvider>
</ApolloClientContext.Provider>
</ApolloProvider>
</ReduxStateContextProvider>
</ReduxStoreProvider>
<ApolloProvider client={libs.apolloClient}>
<ApolloClientContext.Provider value={libs.apolloClient}>
<EuiThemeProvider darkMode={darkMode}>
<HistoryContext.Provider value={history}>
<Router history={history} />
</HistoryContext.Provider>
</EuiThemeProvider>
</ApolloClientContext.Provider>
</ApolloProvider>
</TriggersActionsProvider>
</EuiErrorBoundary>
</core.i18n.Context>
Expand Down

0 comments on commit 2ba01e1

Please sign in to comment.