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

Kpi host #35065

Merged
merged 27 commits into from
May 9, 2019
Merged

Kpi host #35065

Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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

This file was deleted.

73 changes: 0 additions & 73 deletions x-pack/plugins/siem/public/components/card_items/index.test.tsx

This file was deleted.

79 changes: 0 additions & 79 deletions x-pack/plugins/siem/public/components/card_items/index.tsx

This file was deleted.

1 change: 1 addition & 0 deletions x-pack/plugins/siem/public/components/page/hosts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './authentications_table';
export * from './events_table';
export * from './hosts_table';
export * from './uncommon_process_table';
export * from './kpi_hosts';
166 changes: 166 additions & 0 deletions x-pack/plugins/siem/public/components/page/hosts/kpi_hosts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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 { EuiFlexGroup } from '@elastic/eui';
import { get, getOr } from 'lodash/fp';
import React from 'react';
import { pure } from 'recompose';
import { EuiLoadingSpinner } from '@elastic/eui';
import { EuiFlexItem } from '@elastic/eui';
import { KpiHostsData } from '../../../../graphql/types';
import {
AreaChartData,
BarChartData,
StatItem,
StatItems,
StatItemsComponent,
StatItemsProps,
} from '../../../stat_items';
import * as i18n from './translations';

interface KpiHostsProps {
data: KpiHostsData;
loading: boolean;
}

const euiColorVis0 = '#00B3A4';
const euiColorVis1 = '#3185FC';
const euiColorVis2 = '#DB1374';
const euiColorVis3 = '#490092';
const euiColorVis9 = '#920000';
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this is the 👍way to do it, but the following is used a bit around the codebase for importing the eui color constants:

import euiDarkVars from '@elastic/eui/dist/eui_theme_dark.json';
import euiLightVars from '@elastic/eui/dist/eui_theme_light.json';

As for determining the current theme, there's the context api:

<KibanaConfigContext.Consumer>
  {(config: Partial<AppKibanaFrameworkAdapter>) => {
    return config && config.darkMode ? euiDarkVars : euiLightVars;
  }}
</KibanaConfigContext.Consumer>

but I don't think we have a specific ThemeProvider for accessing color constants for the current theme outside what is wired up for use within styled-components (e.g. empty.tsx). That said, we may have some additional options when using the new elastic-charts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mm, thanks @spong that make sense! So far we haven't had kpis with different themes, and I couldn't find all the color codes here in existing configs. I've found#490092, #920000, should I replace them with import value? Still not sure what's preferable way of doing this.

Copy link
Member

Choose a reason for hiding this comment

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

So in addition to importing the colors, this would be for ensuring the right color is chosen depending on which application-level theme is currently enabled -- either the Kibana Light or Dark Theme.

To import these, you could wrap the KpiHostsComponent with the KibanaConfigContext.Consumer example above, and then pass in the appropriate euiDarkVars/euiLightVars for whichever theme is selected. You would then have to restructure your component a bit and move fieldTitleMapping within KpiHostsComponent so you could use the appropriate colors (e.g. euiDarkVars.euiColorVis3).

I just doubled checked the EUI colors used and it looks like they don't change between themes, so it's probably fine to leave things as they are. This is something we can re-visit when we update charting libraries.


const fieldTitleMapping: StatItems[] = [
{
fields: [
{
key: 'hosts',
value: null,
color: euiColorVis1,
icon: 'storage',
},
],
enableAreaChart: true,
grow: 2,
description: i18n.HOSTS,
},
{
fields: [
{
key: 'authSuccess',
description: i18n.AUTHENTICATION_SUCCESS,
value: null,
color: euiColorVis0,
icon: 'check',
},
{
key: 'authFailure',
description: i18n.AUTHENTICATION_FAILURE,
value: null,
color: euiColorVis9,
icon: 'cross',
},
],
enableAreaChart: true,
enableBarChart: true,
grow: 4,
description: i18n.AUTHENTICATION,
},
{
fields: [
{
key: 'uniqueSourceIps',
name: i18n.UNIQUE_SOURCE_IPS_ABBREVIATION,
description: i18n.UNIQUE_SOURCE_IPS,
value: null,
color: euiColorVis2,
icon: 'visMapCoordinate',
},
{
key: 'uniqueDestinationIps',
description: i18n.UNIQUE_DESTINATION_IPS,
value: null,
color: euiColorVis3,
icon: 'visMapCoordinate',
},
],
enableAreaChart: true,
enableBarChart: true,
grow: 4,
description: i18n.UNIQUE_IPS,
},
];

export const KpiHostsComponent = pure<KpiHostsProps>(({ data, loading }) => {
return loading ? (
<EuiFlexGroup justifyContent="center" alignItems="center" style={{ minHeight: 247 }}>
<EuiFlexItem grow={false}>
<EuiLoadingSpinner size="xl" />
</EuiFlexItem>
</EuiFlexGroup>
) : (
<EuiFlexGroup>
{fieldTitleMapping.map(stat => {
let statItemProps: StatItemsProps = {
...stat,
key: `kpi-hosts-summary-${stat.description}`,
};

if (stat.fields != null)
statItemProps = {
...statItemProps,
fields: addValueToFields(stat.fields, data),
};

if (stat.enableAreaChart)
statItemProps = {
...statItemProps,
areaChart: addValueToAreaChart(stat.fields, data),
};

if (stat.enableBarChart != null)
statItemProps = {
...statItemProps,
barChart: addValueToBarChart(stat.fields, data),
};

return <StatItemsComponent {...statItemProps} />;
})}
</EuiFlexGroup>
);
});

const addValueToFields = (fields: StatItem[], data: KpiHostsData): StatItem[] =>
fields.map(field => ({ ...field, value: get(field.key, data) }));

const addValueToAreaChart = (fields: StatItem[], data: KpiHostsData): AreaChartData[] =>
fields
.filter(field => get(`${field.key}Histogram`, data) != null)
.map(field => ({
...field,
value: get(`${field.key}Histogram`, data),
key: `${field.key}Histogram`,
}));

const addValueToBarChart = (fields: StatItem[], data: KpiHostsData): BarChartData[] => {
if (fields.length === 0) return [];
return fields.reduce((acc: BarChartData[], field: StatItem, idx: number) => {
const key = get('key', field);
const x: number | null = getOr(null, key, data);
const y: string = get(`${idx}.name`, fields) || getOr('', `${idx}.description`, fields);
const dataSet: BarChartData[] = [];
if (y != null)
dataSet.push({
...field,
value: [
{
x,
y,
},
],
});
return acc.concat(dataSet);
}, []);
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: I think you can you eliminate the push and extra array with:

    if (y != null) {
      return acc.concat([
        {
          ...field,
          value: [
            {
              x,
              y,
            },
          ],
        },
      ]);
    } else {
      return acc.concat([]);
    }

Also, your type for y above looks like it should never be null?

const y: string = get(`${idx}.name`, fields) || getOr('', `${idx}.description`, fields);

So wondering if you need the if (y != null) call or not? If so, I would change the type to be const y: string | null | undefined = get()

};
Loading