-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from all commits
cf6ef4d
e04ad25
7924b14
eb510b7
cac0d44
2aa883b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
}); | ||
}); |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a generic There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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 |
---|---|---|
@@ -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, | ||
})); | ||
}; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!