Skip to content

Commit

Permalink
Refactor bytes formatting to allow use in our charts
Browse files Browse the repository at this point in the history
We need a formatting function to use with our charts, so this splits out
a hook from the original react component, allowing our charts to be
formatted as specified in the user's UI settings.
  • Loading branch information
rylnd committed Jan 7, 2020
1 parent 215f59c commit e071875
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import { PreferenceFormattedBytesComponent } from '.';
jest.mock('../../lib/kibana');
const mockUseUiSetting$ = useUiSetting$ as jest.Mock;

const DEFAULT_BYTES_FORMAT_VALUE = '0,0.[0]b'; // kibana's default for this setting
const bytes = '2806422';

describe('PreferenceFormattedBytes', () => {
test('renders correctly against snapshot', () => {
mockUseUiSetting$.mockImplementation(() => ['0,0.[0]b']);
mockUseUiSetting$.mockImplementation(() => [DEFAULT_BYTES_FORMAT_VALUE]);
const wrapper = shallow(<PreferenceFormattedBytesComponent value={bytes} />);

expect(toJson(wrapper)).toMatchSnapshot();
Expand All @@ -33,14 +34,14 @@ describe('PreferenceFormattedBytes', () => {
});

test('it renders bytes according to the default format', () => {
mockUseUiSetting$.mockImplementation(() => ['0,0.[0]b']);
mockUseUiSetting$.mockImplementation(() => [DEFAULT_BYTES_FORMAT_VALUE]);
const wrapper = mount(<PreferenceFormattedBytesComponent value={bytes} />);

expect(wrapper.text()).toEqual('2.7MB');
});

test('it renders bytes supplied as a number according to the default format', () => {
mockUseUiSetting$.mockImplementation(() => ['0,0.[0]b']);
mockUseUiSetting$.mockImplementation(() => [DEFAULT_BYTES_FORMAT_VALUE]);
const wrapper = mount(<PreferenceFormattedBytesComponent value={+bytes} />);

expect(wrapper.text()).toEqual('2.7MB');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@ import numeral from '@elastic/numeral';
import { DEFAULT_BYTES_FORMAT } from '../../../common/constants';
import { useUiSetting$ } from '../../lib/kibana';

export const PreferenceFormattedBytesComponent = ({ value }: { value: string | number }) => {
type Bytes = string | number;

export const formatBytes = (value: Bytes, format: string) => {
return numeral(value).format(format);
};

export const useFormatBytes = () => {
const [bytesFormat] = useUiSetting$<string>(DEFAULT_BYTES_FORMAT);
return <>{numeral(value).format(bytesFormat)}</>;

return (value: Bytes) => formatBytes(value, bytesFormat);
};

export const PreferenceFormattedBytesComponent = ({ value }: { value: Bytes }) => (
<>{useFormatBytes()(value)}</>
);

PreferenceFormattedBytesComponent.displayName = 'PreferenceFormattedBytesComponent';

export const PreferenceFormattedBytes = React.memo(PreferenceFormattedBytesComponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,3 @@ export const getCustomChartData = (
}, formattedChartData);
else return formattedChartData;
};

export const bytesFormatter = (value: number) => {
return numeral(value).format('0,0.[0]b');
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import React from 'react';
import { ScaleType } from '@elastic/charts';
import * as i18n from './translation';
import { MatrixHistogram } from '../../../matrix_histogram';
import { bytesFormatter } from '../../../matrix_histogram/utils';
import { MatrixOverOrdinalHistogramData } from '../../../../graphql/types';
import { MatrixHistogramBasicProps } from '../../../matrix_histogram/types';
import { useFormatBytes } from '../../../formatted_bytes';

export const NetworkDnsHistogram = (
props: MatrixHistogramBasicProps<MatrixOverOrdinalHistogramData>
) => {
const dataKey = 'histogram';
const { ...matrixOverTimeProps } = props;
const formatBytes = useFormatBytes();

return (
<MatrixHistogram
title={i18n.NETWORK_DNS_HISTOGRAM}
dataKey={dataKey}
scaleType={ScaleType.Ordinal}
yTickFormatter={bytesFormatter}
yTickFormatter={formatBytes}
showLegend={false}
{...matrixOverTimeProps}
/>
Expand Down

0 comments on commit e071875

Please sign in to comment.