Skip to content

Commit

Permalink
[Metrics UI] Move toolbars under inventory models
Browse files Browse the repository at this point in the history
  • Loading branch information
simianhacker committed Oct 30, 2019
1 parent 2280953 commit 82f3f16
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const metrics: InventoryMetrics = {
containerMemory,
},
snapshot: { cpu, memory, rx, tx },
defaultSnapshot: 'cpu',
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import React, { useMemo } from 'react';
import { EuiFlexItem } from '@elastic/eui';
import { ToolbarProps } from './toolbar';
import { WaffleMetricControls } from '../../waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../graphql/types';
import { toMetricOpt, toGroupByOpt } from './toolbar_wrapper';
import { ToolbarProps } from '../../../public/components/inventory/toolbars/toolbar';
import { WaffleMetricControls } from '../../../public/components/waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../../public/components/waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../public/graphql/types';
import {
toMetricOpt,
toGroupByOpt,
} from '../../../public/components/inventory/toolbars/toolbar_wrapper';

export const ContainerToolbarItems = (props: ToolbarProps) => {
const options = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ export const metrics: InventoryMetrics = {
hostDockerTop5ByCpu,
},
snapshot: { count, cpu, load, logRate, memory, rx, tx },
defaultSnapshot: 'cpu',
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import React, { useMemo } from 'react';
import { EuiFlexItem } from '@elastic/eui';
import { ToolbarProps } from './toolbar';
import { WaffleMetricControls } from '../../waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../graphql/types';
import { toGroupByOpt, toMetricOpt } from './toolbar_wrapper';
import { ToolbarProps } from '../../../public/components/inventory/toolbars/toolbar';
import { WaffleMetricControls } from '../../../public/components/waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../../public/components/waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../public/graphql/types';
import {
toGroupByOpt,
toMetricOpt,
} from '../../../public/components/inventory/toolbars/toolbar_wrapper';

export const HostToolbarItems = (props: ToolbarProps) => {
const metricOptions = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ interface Layouts {
[type: string]: ReactNode;
}

const Layouts: Layouts = {
const layouts: Layouts = {
host: HostLayout,
pod: PodLayout,
container: ContainerLayout,
};

export const findLayout = (type: InventoryItemType) => {
const Layout = idx(Layouts, _ => _[type]);
const Layout = idx(layouts, _ => _[type]);
if (!Layout) {
throw new Error(
i18n.translate('xpack.infra.inventoryModels.findLayout.error', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export const metrics: InventoryMetrics = {
podMemoryUsage,
},
snapshot: { cpu, memory, rx, tx },
defaultSnapshot: 'cpu',
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

import React, { useMemo } from 'react';
import { EuiFlexItem } from '@elastic/eui';
import { ToolbarProps } from './toolbar';
import { WaffleMetricControls } from '../../waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../graphql/types';
import { toGroupByOpt, toMetricOpt } from './toolbar_wrapper';
import { ToolbarProps } from '../../../public/components/inventory/toolbars/toolbar';
import { WaffleMetricControls } from '../../../public/components/waffle/waffle_metric_controls';
import { WaffleGroupByControls } from '../../../public/components/waffle/waffle_group_by_controls';
import { InfraSnapshotMetricType } from '../../../public/graphql/types';
import {
toGroupByOpt,
toMetricOpt,
} from '../../../public/components/inventory/toolbars/toolbar_wrapper';

export const PodToolbarItems = (props: ToolbarProps) => {
const options = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { awsNetworkBytes } from './tsvb/aws_network_bytes';
import { awsNetworkPackets } from './tsvb/aws_network_packets';
import { awsOverview } from './tsvb/aws_overview';
import { InventoryMetrics } from '../../types';
import { count } from './snapshot/count';

export const metrics: InventoryMetrics = {
tsvb: {
Expand All @@ -30,4 +31,8 @@ export const metrics: InventoryMetrics = {
awsNetworkPackets,
awsOverview,
},
snapshot: {
count,
},
defaultSnapshot: 'count',
};
36 changes: 36 additions & 0 deletions x-pack/legacy/plugins/infra/common/inventory_models/toolbars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { idx } from '@kbn/elastic-idx/target';
import { ReactNode, FunctionComponent } from 'react';
import { i18n } from '@kbn/i18n';
import { InventoryItemType } from './types';
import { HostToolbarItems } from './host/toolbar_items';
import { ContainerToolbarItems } from './container/toolbar_items';
import { PodToolbarItems } from './pod/toolbar_items';
import { ToolbarProps } from '../../public/components/inventory/toolbars/toolbar';

interface Toolbars {
[type: string]: ReactNode;
}

const toolbars: Toolbars = {
host: HostToolbarItems,
container: ContainerToolbarItems,
pod: PodToolbarItems,
};

export const findToolbar = (type: InventoryItemType) => {
const Toolbar = idx(toolbars, _ => _[type]);
if (!Toolbar) {
throw new Error(
i18n.translate('xpack.infra.inventoryModels.findToolbar.error', {
defaultMessage: "The toolbar you've attempted to find does not exist.",
})
);
}
return Toolbar as FunctionComponent<ToolbarProps>;
};
16 changes: 15 additions & 1 deletion x-pack/legacy/plugins/infra/common/inventory_models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import * as rt from 'io-ts';
import { InfraSnapshotMetricType } from '../graphql/types';

export const ItemTypeRT = rt.keyof({
host: null,
Expand Down Expand Up @@ -259,9 +260,22 @@ export const SnapshotModelRT = rt.record(
);
export type SnapshotModel = rt.TypeOf<typeof SnapshotModelRT>;

export const SnapshotMetricTypeRT = rt.keyof({
count: null,
cpu: null,
load: null,
memory: null,
tx: null,
rx: null,
logRate: null,
});

export type SnapshotMetricType = rt.TypeOf<typeof SnapshotMetricTypeRT>;

export interface InventoryMetrics {
tsvb: { [name: string]: TSVBMetricModelCreator };
snapshot?: { [name: string]: SnapshotModel };
snapshot: { [name: string]: SnapshotModel };
defaultSnapshot: SnapshotMetricType;
}

export interface InventoryModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import React, { FunctionComponent } from 'react';
import { StaticIndexPattern } from 'ui/index_patterns';
import { Action } from 'typescript-fsa';
import { EuiFlexItem } from '@elastic/eui';
import { findToolbar } from '../../../../common/inventory_models/toolbars';
import {
InfraNodeType,
InfraSnapshotMetricInput,
InfraSnapshotGroupbyInput,
} from '../../../graphql/types';
import { HostToolbarItems } from './host_toolbar_items';
import { PodToolbarItems } from './pod_toolbar_items';
import { ContainerToolbarItems } from './container_toolbar_items';
import { ToolbarWrapper } from './toolbar_wrapper';

import { waffleOptionsSelectors } from '../../../store';
Expand All @@ -35,7 +33,7 @@ export interface ToolbarProps {
nodeType: ReturnType<typeof waffleOptionsSelectors.selectNodeType>;
}

const wrapToolbarItems = (ToolbarItems: (props: ToolbarProps) => JSX.Element) => {
const wrapToolbarItems = (ToolbarItems: FunctionComponent<ToolbarProps>) => {
return (
<ToolbarWrapper>
{props => (
Expand All @@ -60,15 +58,7 @@ const wrapToolbarItems = (ToolbarItems: (props: ToolbarProps) => JSX.Element) =>
);
};

export const Toolbar = (props: { nodeType: InfraNodeType }) => {
switch (props.nodeType) {
case InfraNodeType.host:
return wrapToolbarItems(HostToolbarItems);
case InfraNodeType.pod:
return wrapToolbarItems(PodToolbarItems);
case InfraNodeType.container:
return wrapToolbarItems(ContainerToolbarItems);
default:
return null;
}
export const Toolbar = ({ nodeType }: { nodeType: InfraNodeType }) => {
const ToolbarItems = findToolbar(nodeType);
return wrapToolbarItems(ToolbarItems);
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
InfraNodeType,
InfraSnapshotGroupbyInput,
} from '../../graphql/types';
import { findInventoryModel } from '../../../common/inventory_models';

interface Props {
nodeType: InfraNodeType;
Expand All @@ -32,7 +33,10 @@ export const WaffleInventorySwitcher = (props: Props) => {
closePopover();
props.changeNodeType(nodeType);
props.changeGroupBy([]);
props.changeMetric({ type: InfraSnapshotMetricType.cpu });
const inventoryModel = findInventoryModel(nodeType);
props.changeMetric({
type: inventoryModel.metrics.defaultSnapshot as InfraSnapshotMetricType,
});
},
[props.changeGroupBy, props.changeNodeType, props.changeMetric]
);
Expand Down

0 comments on commit 82f3f16

Please sign in to comment.