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

[App Search] Add AnalyticsChart component to EnginesOverview #87752

Merged
merged 6 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion x-pack/plugins/enterprise_search/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "enterpriseSearch",
"version": "kibana",
"kibanaVersion": "kibana",
"requiredPlugins": ["features", "licensing"],
"requiredPlugins": ["features", "licensing", "charts"],
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you point me towards docs on this file/plugins for x-pack in general?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some (hopefully) helpful links:

LMK if you'd like me to explain more in person/more succinctly at the sync today!

"configPath": ["enterpriseSearch"],
"optionalPlugins": ["usageCollection", "security", "home", "spaces", "cloud"],
"server": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { chartPluginMock } from '../../../../../../src/plugins/charts/public/mocks';
import { mockHistory } from './';

export const mockKibanaValues = {
config: { host: 'http://localhost:3002' },
history: mockHistory,
charts: chartPluginMock.createStartContract(),
cloud: {
isCloudEnabled: false,
cloudDeploymentUrl: 'https://cloud.elastic.co/deployments/some-id',
},
history: mockHistory,
navigateToUrl: jest.fn(),
setBreadcrumbs: jest.fn(),
setDocTitle: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 { mockKibanaValues } from '../../../../__mocks__';

import React from 'react';
import { shallow } from 'enzyme';
import { Chart, Settings, LineSeries, Axis } from '@elastic/charts';

import { AnalyticsChart } from './';

describe('AnalyticsChart', () => {
const MOCK_DATA = [
{ x: '1970-01-01', y: 0 },
{ x: '1970-01-02', y: 1 },
{ x: '1970-01-03', y: 5 },
{ x: '1970-01-04', y: 50 },
{ x: '1970-01-05', y: 25 },
];

beforeAll(() => {
jest.clearAllMocks();
});

it('renders an Elastic line chart', () => {
const wrapper = shallow(
<AnalyticsChart height={300} lines={[{ id: 'test', data: MOCK_DATA }]} />
);

expect(wrapper.find(Chart).prop('size')).toEqual({ height: 300 });
expect(wrapper.find(Axis)).toHaveLength(2);
expect(mockKibanaValues.charts.theme.useChartsTheme).toHaveBeenCalled();
expect(mockKibanaValues.charts.theme.useChartsBaseTheme).toHaveBeenCalled();

expect(wrapper.find(LineSeries)).toHaveLength(1);
expect(wrapper.find(LineSeries).prop('id')).toEqual('test');
expect(wrapper.find(LineSeries).prop('data')).toEqual(MOCK_DATA);
});

it('renders multiple lines', () => {
const wrapper = shallow(
<AnalyticsChart
lines={[
{ id: 'line 1', data: MOCK_DATA },
{ id: 'line 2', data: MOCK_DATA },
{ id: 'line 3', data: MOCK_DATA },
]}
/>
);

expect(wrapper.find(LineSeries)).toHaveLength(3);
});

it('formats x-axis dates correctly', () => {
const wrapper = shallow(<AnalyticsChart lines={[{ id: 'test', data: MOCK_DATA }]} />);
const dateFormatter: Function = wrapper.find('#bottom-axis').prop('tickFormat');

expect(dateFormatter('1970-02-28')).toEqual('2/28');
});

it('formats tooltip dates correctly', () => {
const wrapper = shallow(<AnalyticsChart lines={[{ id: 'test', data: MOCK_DATA }]} />);
const dateFormatter: Function = (wrapper.find(Settings).prop('tooltip') as any).headerFormatter;

expect(dateFormatter({ value: '1970-12-03' })).toEqual('December 3, 1970');
Copy link
Contributor

Choose a reason for hiding this comment

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

What's your feeling on checking for a specific value in a test vs explicitly something like expect(dateFormatter({ value: '1970-12-03' })).toEqual(moment('1970-12-03').format(TOOLTIP_DATE_FORMAT))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I strongly prefer explicit tests over variables and would push back against the latter. Unit tests are meant to help developers understand the source code and in a perfect world should be quickly skimmable - this single line here perfectly gives a dev new to the code a very clear example of expected output without having to open a new file to look up TOOLTIP_DATE_FORMAT, moment formatting, etc.

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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 React from 'react';
import { useValues } from 'kea';

import moment from 'moment';
import { Chart, Settings, LineSeries, CurveType, Axis } from '@elastic/charts';

import { KibanaLogic } from '../../../../shared/kibana';

import { X_AXIS_DATE_FORMAT, TOOLTIP_DATE_FORMAT } from '../constants';

interface ChartPoint {
x: string; // Date string
y: number; // # of clicks, queries, etc.
}
export type ChartData = ChartPoint[];

interface Props {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is a generic Props the convention for our React components?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CC @JasonStoltz on this one - I think we've been doing this more and more recently in Kibana and it's because we're not really using/exporting component-specific props anymore. Interfaces or types that get reused/extended mostly live in types.ts files.

cee-chen marked this conversation as resolved.
Show resolved Hide resolved
height?: number;
lines: Array<{
id: string;
data: ChartData;
}>;
}
export const AnalyticsChart: React.FC<Props> = ({ height = 300, lines }) => {
const { charts } = useValues(KibanaLogic);

return (
<Chart size={{ height }}>
<Settings
theme={charts.theme.useChartsTheme()}
baseTheme={charts.theme.useChartsBaseTheme()}
tooltip={{
headerFormatter: (tooltip) => moment(tooltip.value).format(TOOLTIP_DATE_FORMAT),
}}
/>
{lines.map(({ id, data }) => (
<LineSeries
key={id}
id={id}
data={data}
xAccessor={'x'}
yAccessors={['y']}
curve={CurveType.CURVE_MONOTONE_X}
/>
))}
<Axis
id="bottom-axis"
position="bottom"
tickFormat={(d) => moment(d).format(X_AXIS_DATE_FORMAT)}
showGridLines
/>
<Axis id="left-axis" position="left" ticks={4} showGridLines />
</Chart>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* 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.
*/

export { AnalyticsChart } from './analytics_chart';
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export const TOTAL_CLICKS = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.analytics.totalClicks',
{ defaultMessage: 'Total clicks' }
);

// Moment date format conversions
export const SERVER_DATE_FORMAT = 'YYYY-MM-DD';
export const TOOLTIP_DATE_FORMAT = 'MMMM D, YYYY';
export const X_AXIS_DATE_FORMAT = 'M/D';
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
*/

export { ANALYTICS_TITLE } from './constants';
export { AnalyticsChart } from './components';
export { convertToChartData } from './utils';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { convertToChartData } from './utils';

describe('convertToChartData', () => {
it('converts server-side analytics data into an array of objects that Elastic Charts can consume', () => {
expect(
convertToChartData({
startDate: '1970-01-01',
data: [0, 1, 5, 50, 25],
})
).toEqual([
{ x: '1970-01-01', y: 0 },
{ x: '1970-01-02', y: 1 },
{ x: '1970-01-03', y: 5 },
{ x: '1970-01-04', y: 50 },
{ x: '1970-01-05', y: 25 },
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 moment from 'moment';

import { SERVER_DATE_FORMAT } from './constants';
import { ChartData } from './components/analytics_chart';

interface ConvertToChartData {
data: number[];
startDate: string;
}
export const convertToChartData = ({ data, startDate }: ConvertToChartData): ChartData => {
const date = moment(startDate, SERVER_DATE_FORMAT);
return data.map((y, index) => ({
x: moment(date).add(index, 'days').format(SERVER_DATE_FORMAT),
y,
}));
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';

import { EuiButtonTo } from '../../../../shared/react_router_helpers';
import { AnalyticsChart } from '../../analytics';

import { TotalCharts } from './total_charts';

Expand All @@ -21,7 +22,6 @@ describe('TotalCharts', () => {
setMockValues({
engineName: 'some-engine',
startDate: '1970-01-01',
endDate: '1970-01-08',
queriesPerDay: [0, 1, 2, 3, 5, 10, 50],
operationsPerDay: [0, 0, 0, 0, 0, 0, 0],
});
Expand All @@ -33,14 +33,14 @@ describe('TotalCharts', () => {

expect(chart.find('h2').text()).toEqual('Total queries');
expect(chart.find(EuiButtonTo).prop('to')).toEqual('/engines/some-engine/analytics');
// TODO: find chart component
expect(chart.find(AnalyticsChart)).toHaveLength(1);
});

it('renders the total API operations chart', () => {
const chart = wrapper.find('[data-test-subj="TotalApiOperationsChart"]');

expect(chart.find('h2').text()).toEqual('Total API operations');
expect(chart.find(EuiButtonTo).prop('to')).toEqual('/engines/some-engine/api-logs');
// TODO: find chart component
expect(chart.find(AnalyticsChart)).toHaveLength(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ import { ENGINE_ANALYTICS_PATH, ENGINE_API_LOGS_PATH, getEngineRoute } from '../
import { TOTAL_QUERIES, TOTAL_API_OPERATIONS } from '../../analytics/constants';
import { VIEW_ANALYTICS, VIEW_API_LOGS, LAST_7_DAYS } from '../constants';

import { AnalyticsChart, convertToChartData } from '../../analytics';

import { EngineLogic } from '../../engine';
import { EngineOverviewLogic } from '../';

export const TotalCharts: React.FC = () => {
const { engineName } = useValues(EngineLogic);
const engineRoute = getEngineRoute(engineName);

const {
// startDate,
// endDate,
// queriesPerDay,
// operationsPerDay,
} = useValues(EngineOverviewLogic);
const { startDate, queriesPerDay, operationsPerDay } = useValues(EngineOverviewLogic);

return (
<EuiFlexGroup>
Expand All @@ -58,12 +55,14 @@ export const TotalCharts: React.FC = () => {
</EuiPageContentHeaderSection>
</EuiPageContentHeader>
<EuiPageContentBody>
TODO: Analytics chart
{/* <EngineAnalytics
data={[queriesPerDay]}
startDate={new Date(startDate)}
endDate={new Date(endDate)}
/> */}
<AnalyticsChart
lines={[
{
id: TOTAL_QUERIES,
data: convertToChartData({ startDate, data: queriesPerDay }),
},
]}
/>
</EuiPageContentBody>
</EuiPageContent>
</EuiFlexItem>
Expand All @@ -85,12 +84,14 @@ export const TotalCharts: React.FC = () => {
</EuiPageContentHeaderSection>
</EuiPageContentHeader>
<EuiPageContentBody>
TODO: API Logs chart
{/* <EngineAnalytics
data={[operationsPerDay]}
startDate={new Date(startDate)}
endDate={new Date(endDate)}
/> */}
<AnalyticsChart
lines={[
{
id: TOTAL_API_OPERATIONS,
data: convertToChartData({ startDate, data: operationsPerDay }),
},
]}
/>
</EuiPageContentBody>
</EuiPageContent>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('EngineOverviewLogic', () => {
apiLogsUnavailable: true,
documentCount: 10,
startDate: '1970-01-30',
endDate: '1970-01-31',
operationsPerDay: [0, 0, 0, 0, 0, 0, 0],
queriesPerDay: [0, 0, 0, 0, 0, 25, 50],
totalClicks: 50,
Expand All @@ -40,7 +39,6 @@ describe('EngineOverviewLogic', () => {
apiLogsUnavailable: false,
documentCount: 0,
startDate: '',
endDate: '',
operationsPerDay: [],
queriesPerDay: [],
totalClicks: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ interface EngineOverviewApiData {
apiLogsUnavailable: boolean;
documentCount: number;
startDate: string;
endDate: string;
operationsPerDay: number[];
queriesPerDay: number[];
totalClicks: number;
Expand Down Expand Up @@ -61,12 +60,6 @@ export const EngineOverviewLogic = kea<MakeLogicType<EngineOverviewValues, Engin
setPolledData: (_, { startDate }) => startDate,
},
],
endDate: [
'',
{
setPolledData: (_, { endDate }) => endDate,
},
],
queriesPerDay: [
[],
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getContext } from 'kea';

import { coreMock } from 'src/core/public/mocks';
import { licensingMock } from '../../../licensing/public/mocks';
import { chartPluginMock } from '../../../../../src/plugins/charts/public/mocks';

import { renderApp, renderHeaderActions } from './';
import { EnterpriseSearch } from './enterprise_search';
Expand All @@ -20,7 +21,10 @@ describe('renderApp', () => {
const kibanaDeps = {
params: coreMock.createAppMountParamters(),
core: coreMock.createStart(),
plugins: { licensing: licensingMock.createStart() },
plugins: {
licensing: licensingMock.createStart(),
charts: chartPluginMock.createStartContract(),
},
} as any;
const pluginData = {
config: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const renderApp = (

const unmountKibanaLogic = mountKibanaLogic({
config,
charts: plugins.charts,
cloud: plugins.cloud || {},
history: params.history,
navigateToUrl: core.application.navigateToUrl,
Expand Down
Loading