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

[APM] Chart units don't update when toggling the chart legends #74931

Merged
merged 12 commits into from
Aug 31, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ export class InnerCustomPlot extends PureComponent {
return i === _i ? !disabledValue : !!disabledValue;
});

if (typeof this.props.onToggleLegend === 'function') {
//Filters out disabled series
const availableSeries = this.props.series
.map((serie, index) => {
if (!nextSeriesEnabledState[index]) {
return serie;
}
})
.filter((serie) => serie);
this.props.onToggleLegend(availableSeries);
}

return {
seriesEnabledState: nextSeriesEnabledState,
};
Expand Down Expand Up @@ -235,6 +247,7 @@ InnerCustomPlot.propTypes = {
})
),
noHits: PropTypes.bool,
onToggleLegend: PropTypes.func,
};

InnerCustomPlot.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,24 @@ import { useChartsSync } from '../../../../../hooks/useChartsSync';
// @ts-ignore
import CustomPlot from '../../CustomPlot';

export interface Serie {
color: string;
title: React.ReactNode;
titleShort?: React.ReactNode;
data: Array<Coordinate | RectCoordinate>;
type: string;
}

interface Props {
series: Array<{
color: string;
title: React.ReactNode;
titleShort?: React.ReactNode;
data: Array<Coordinate | RectCoordinate>;
type: string;
}>;
series: Serie[];
truncateLegends?: boolean;
tickFormatY: (y: number) => React.ReactNode;
formatTooltipValue: (c: Coordinate) => React.ReactNode;
yMax?: string | number;
height?: number;
stacked?: boolean;
onHover?: () => void;
onToggleLegend?: (series: Serie[]) => void;
}

function TransactionLineChart(props: Props) {
Expand All @@ -40,6 +43,7 @@ function TransactionLineChart(props: Props) {
truncateLegends,
stacked = false,
onHover,
onToggleLegend,
} = props;

const syncedChartsProps = useChartsSync();
Expand All @@ -66,6 +70,7 @@ function TransactionLineChart(props: Props) {
height={height}
truncateLegends={truncateLegends}
{...(stacked ? { stackBy: 'y' } : {})}
onToggleLegend={onToggleLegend}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@ import {
EuiFlexItem,
EuiIconTip,
EuiPanel,
EuiSpacer,
EuiText,
EuiTitle,
EuiSpacer,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { Location } from 'history';
import React, { Component } from 'react';
import { isEmpty, flatten } from 'lodash';
import { flatten, isEmpty, mean } from 'lodash';
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n';
import {
TRANSACTION_PAGE_LOAD,
TRANSACTION_REQUEST,
TRANSACTION_ROUTE_CHANGE,
} from '../../../../../common/transaction_types';
import { Coordinate, TimeSeries } from '../../../../../typings/timeseries';
import { ITransactionChartData } from '../../../../selectors/chartSelectors';
import { LicenseContext } from '../../../../context/LicenseContext';
import { IUrlParams } from '../../../../context/UrlParamsContext/types';
import { ITransactionChartData } from '../../../../selectors/chartSelectors';
import {
tpmUnit,
TimeFormatter,
getDurationFormatter,
asDecimal,
getDurationFormatter,
TimeFormatter,
tpmUnit,
} from '../../../../utils/formatters';
import { MLJobLink } from '../../Links/MachineLearningLinks/MLJobLink';
import { LicenseContext } from '../../../../context/LicenseContext';
import { TransactionLineChart } from './TransactionLineChart';
import { isValidCoordinateValue } from '../../../../utils/isValidCoordinateValue';
import { MLJobLink } from '../../Links/MachineLearningLinks/MLJobLink';
import { BrowserLineChart } from './BrowserLineChart';
import { DurationByCountryMap } from './DurationByCountryMap';
import {
TRANSACTION_PAGE_LOAD,
TRANSACTION_ROUTE_CHANGE,
TRANSACTION_REQUEST,
} from '../../../../../common/transaction_types';
import { Serie, TransactionLineChart } from './TransactionLineChart';

interface TransactionChartProps {
charts: ITransactionChartData;
Expand Down Expand Up @@ -81,27 +81,42 @@ export function getMaxY(responseTimeSeries: TimeSeries[]) {
return Math.max(...numbers, 0);
}

export class TransactionCharts extends Component<TransactionChartProps> {
public getTPMFormatter = (t: number) => {
const { urlParams } = this.props;
function getAverageY(responseTimeSeries: TimeSeries[]) {
const averageSeries = responseTimeSeries.find(
(serie) => serie.title === 'Avg.'
);
if (averageSeries) {
const averageYValues = (averageSeries.data as Coordinate[])
.map((c) => c.y)
.filter((y) => y && isFinite(y));
return mean(averageYValues);
}
}

export function TransactionCharts({
charts,
location,
urlParams,
}: TransactionChartProps) {
const getTPMFormatter = (t: number) => {
cauemarcondes marked this conversation as resolved.
Show resolved Hide resolved
const unit = tpmUnit(urlParams.transactionType);
return `${asDecimal(t)} ${unit}`;
};

public getTPMTooltipFormatter = (p: Coordinate) => {
const getTPMTooltipFormatter = (p: Coordinate) => {
return isValidCoordinateValue(p.y)
? this.getTPMFormatter(p.y)
? getTPMFormatter(p.y)
: NOT_AVAILABLE_LABEL;
};

public renderMLHeader(hasValidMlLicense: boolean | undefined) {
const { mlJobId } = this.props.charts;
function renderMLHeader(hasValidMlLicense: boolean | undefined) {
const { mlJobId } = charts;

if (!hasValidMlLicense || !mlJobId) {
return null;
}

const { serviceName, kuery, transactionType } = this.props.urlParams;
const { serviceName, kuery, transactionType } = urlParams;
if (!serviceName) {
return null;
}
Expand Down Expand Up @@ -150,78 +165,89 @@ export class TransactionCharts extends Component<TransactionChartProps> {
);
}

public render() {
const { charts, urlParams } = this.props;
const { responseTimeSeries, tpmSeries } = charts;
const { transactionType } = urlParams;
const maxY = getMaxY(responseTimeSeries);
const formatter = getDurationFormatter(maxY);
const { responseTimeSeries, tpmSeries } = charts;
const { transactionType } = urlParams;
const [maxY, setMaxY] = useState(0);
useEffect(() => {
setMaxY(getMaxY(responseTimeSeries));
}, [responseTimeSeries]);

return (
<>
<EuiFlexGrid columns={2} gutterSize="s">
<EuiFlexItem data-cy={`transaction-duration-charts`}>
<EuiPanel>
<React.Fragment>
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem>
<EuiTitle size="xs">
<span>{responseTimeLabel(transactionType)}</span>
</EuiTitle>
</EuiFlexItem>
<LicenseContext.Consumer>
{(license) =>
this.renderMLHeader(license?.getFeature('ml').isAvailable)
}
</LicenseContext.Consumer>
</EuiFlexGroup>
<TransactionLineChart
series={responseTimeSeries}
tickFormatY={getResponseTimeTickFormatter(formatter)}
formatTooltipValue={getResponseTimeTooltipFormatter(
formatter
)}
/>
</React.Fragment>
</EuiPanel>
</EuiFlexItem>
const averageY = getAverageY(responseTimeSeries);

<EuiFlexItem style={{ flexShrink: 1 }}>
<EuiPanel>
<React.Fragment>
<EuiTitle size="xs">
<span>{tpmLabel(transactionType)}</span>
</EuiTitle>
<TransactionLineChart
series={tpmSeries}
tickFormatY={this.getTPMFormatter}
formatTooltipValue={this.getTPMTooltipFormatter}
truncateLegends
/>
</React.Fragment>
</EuiPanel>
</EuiFlexItem>
</EuiFlexGrid>
{transactionType === TRANSACTION_PAGE_LOAD && (
<>
<EuiSpacer size="s" />
<EuiFlexGrid columns={2} gutterSize="s">
<EuiFlexItem>
<EuiPanel>
<DurationByCountryMap />
</EuiPanel>
</EuiFlexItem>
<EuiFlexItem>
<EuiPanel>
<BrowserLineChart />
</EuiPanel>
</EuiFlexItem>
</EuiFlexGrid>
</>
)}
</>
);
}
const formatTooltip = getDurationFormatter(averageY ?? maxY);
const formatter = getDurationFormatter(maxY);

const onToggleLegend = (series: Serie[]) => {
if (!isEmpty(series)) {
setMaxY(getMaxY(series as TimeSeries[]));
Copy link
Member

Choose a reason for hiding this comment

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

I have some concerns here, mostly around us having to maintain state about CustomPlot in TransactionCharts, which could easily get out of sync. For instance, this happens when I enter a filter into the query bar that gives no results, and then go back:

image

The axis unit is not updated. Of course we can probably fix this even with this implementation, but it might be better to consider having state only one place (for instance, the consumers of CustomPlot.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dgieselaar I've rethought the solution to this problem. I removed the state (a04aaba#diff-da514a7cc4e8210906a22c7f824eaea9L170-L184) and I use asDuration to calculate what will be the unit shown in the y-axis based on the value passed by the CustomPlot. This fixes the issue of this ticket and also the bug you reported.

}
};

return (
<>
<EuiFlexGrid columns={2} gutterSize="s">
<EuiFlexItem data-cy={`transaction-duration-charts`}>
<EuiPanel>
<React.Fragment>
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem>
<EuiTitle size="xs">
<span>{responseTimeLabel(transactionType)}</span>
</EuiTitle>
</EuiFlexItem>
<LicenseContext.Consumer>
{(license) =>
renderMLHeader(license?.getFeature('ml').isAvailable)
}
</LicenseContext.Consumer>
</EuiFlexGroup>
<TransactionLineChart
onToggleLegend={onToggleLegend}
series={responseTimeSeries}
tickFormatY={getResponseTimeTickFormatter(formatter)}
formatTooltipValue={getResponseTimeTooltipFormatter(
formatTooltip
)}
/>
</React.Fragment>
</EuiPanel>
</EuiFlexItem>

<EuiFlexItem style={{ flexShrink: 1 }}>
<EuiPanel>
<React.Fragment>
<EuiTitle size="xs">
<span>{tpmLabel(transactionType)}</span>
</EuiTitle>
<TransactionLineChart
series={tpmSeries}
tickFormatY={getTPMFormatter}
formatTooltipValue={getTPMTooltipFormatter}
truncateLegends
/>
</React.Fragment>
</EuiPanel>
</EuiFlexItem>
</EuiFlexGrid>
{transactionType === TRANSACTION_PAGE_LOAD && (
<>
<EuiSpacer size="s" />
<EuiFlexGrid columns={2} gutterSize="s">
<EuiFlexItem>
<EuiPanel>
<DurationByCountryMap />
</EuiPanel>
</EuiFlexItem>
<EuiFlexItem>
<EuiPanel>
<BrowserLineChart />
</EuiPanel>
</EuiFlexItem>
</EuiFlexGrid>
</>
)}
</>
);
}

function tpmLabel(type?: string) {
Expand Down