-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[App Search] Add AnalyticsChart component to EnginesOverview (#87752)
* Set up Kibana charts plugin dependency - required for using shared colors/themes/etc. - see https://github.com/elastic/kibana/tree/master/src/plugins/charts * Add reusable AnalyticsChart component + util for converting data from our server API to data that Elastic Charts can use * Update EngineOverview to use AnalyticsChart + remove now-unnecessary endDate value (we don't really need it just IMO) * [PR feedback] Return type * [Self feedback] naming - remove pluralization
- Loading branch information
Constance
authored
Jan 12, 2021
1 parent
a1c2422
commit 04739d8
Showing
17 changed files
with
229 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...h/public/applications/app_search/components/analytics/components/analytics_chart.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
61 changes: 61 additions & 0 deletions
61
...search/public/applications/app_search/components/analytics/components/analytics_chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
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> | ||
); | ||
}; |
7 changes: 7 additions & 0 deletions
7
...enterprise_search/public/applications/app_search/components/analytics/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ugins/enterprise_search/public/applications/app_search/components/analytics/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
]); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...ck/plugins/enterprise_search/public/applications/app_search/components/analytics/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.