@@ -8,66 +8,41 @@ import {
88 EuiFlexGrid ,
99 EuiFlexGroup ,
1010 EuiFlexItem ,
11- EuiIconTip ,
1211 EuiPanel ,
1312 EuiSpacer ,
14- EuiText ,
1513 EuiTitle ,
1614} from '@elastic/eui' ;
1715import { i18n } from '@kbn/i18n' ;
1816import { Location } from 'history' ;
19- import { flatten , isEmpty } from 'lodash' ;
2017import React from 'react' ;
21- import styled from 'styled-components' ;
2218import { NOT_AVAILABLE_LABEL } from '../../../../../common/i18n' ;
2319import {
2420 TRANSACTION_PAGE_LOAD ,
2521 TRANSACTION_REQUEST ,
2622 TRANSACTION_ROUTE_CHANGE ,
2723} from '../../../../../common/transaction_types' ;
28- import { Coordinate , TimeSeries } from '../../../../../typings/timeseries' ;
24+ import { Coordinate } from '../../../../../typings/timeseries' ;
2925import { LicenseContext } from '../../../../context/LicenseContext' ;
3026import { IUrlParams } from '../../../../context/UrlParamsContext/types' ;
3127import { ITransactionChartData } from '../../../../selectors/chartSelectors' ;
32- import {
33- asDecimal ,
34- getDurationFormatter ,
35- tpmUnit ,
36- } from '../../../../utils/formatters' ;
28+ import { asDecimal , tpmUnit } from '../../../../utils/formatters' ;
3729import { isValidCoordinateValue } from '../../../../utils/isValidCoordinateValue' ;
38- import { MLJobLink } from '../../Links/MachineLearningLinks/MLJobLink' ;
3930import { BrowserLineChart } from './BrowserLineChart' ;
4031import { DurationByCountryMap } from './DurationByCountryMap' ;
32+ import {
33+ getResponseTimeTickFormatter ,
34+ getResponseTimeTooltipFormatter ,
35+ } from './helper' ;
36+ import { MLHeader } from './ml_header' ;
4137import { TransactionLineChart } from './TransactionLineChart' ;
38+ import { useFormatter } from './use_formatter' ;
4239
4340interface TransactionChartProps {
4441 charts : ITransactionChartData ;
4542 location : Location ;
4643 urlParams : IUrlParams ;
4744}
4845
49- const ShiftedIconWrapper = styled . span `
50- padding-right: 5px;
51- position: relative;
52- top: -1px;
53- display: inline-block;
54- ` ;
55-
56- const ShiftedEuiText = styled ( EuiText ) `
57- position: relative;
58- top: 5px;
59- ` ;
60-
61- export function getMaxY ( responseTimeSeries : TimeSeries [ ] ) {
62- const coordinates = flatten (
63- responseTimeSeries . map ( ( serie : TimeSeries ) => serie . data as Coordinate [ ] )
64- ) ;
65-
66- const numbers : number [ ] = coordinates . map ( ( c : Coordinate ) => ( c . y ? c . y : 0 ) ) ;
67-
68- return Math . max ( ...numbers , 0 ) ;
69- }
70-
7146export function TransactionCharts ( {
7247 charts,
7348 location,
@@ -84,82 +59,16 @@ export function TransactionCharts({
8459 : NOT_AVAILABLE_LABEL ;
8560 } ;
8661
87- function renderMLHeader ( hasValidMlLicense : boolean | undefined ) {
88- const { mlJobId } = charts ;
89-
90- if ( ! hasValidMlLicense || ! mlJobId ) {
91- return null ;
92- }
93-
94- const { serviceName, kuery, transactionType } = urlParams ;
95- if ( ! serviceName ) {
96- return null ;
97- }
98-
99- const hasKuery = ! isEmpty ( kuery ) ;
100- const icon = hasKuery ? (
101- < EuiIconTip
102- aria-label = "Warning"
103- type = "alert"
104- color = "warning"
105- content = "The Machine learning results are hidden when the search bar is used for filtering"
106- />
107- ) : (
108- < EuiIconTip
109- content = { i18n . translate (
110- 'xpack.apm.metrics.transactionChart.machineLearningTooltip' ,
111- {
112- defaultMessage :
113- 'The stream around the average duration shows the expected bounds. An annotation is shown for anomaly scores ≥ 75.' ,
114- }
115- ) }
116- />
117- ) ;
118-
119- return (
120- < EuiFlexItem grow = { false } >
121- < ShiftedEuiText size = "xs" >
122- < ShiftedIconWrapper > { icon } </ ShiftedIconWrapper >
123- < span >
124- { i18n . translate (
125- 'xpack.apm.metrics.transactionChart.machineLearningLabel' ,
126- {
127- defaultMessage : 'Machine learning:' ,
128- }
129- ) } { ' ' }
130- </ span >
131- < MLJobLink
132- jobId = { mlJobId }
133- serviceName = { serviceName }
134- transactionType = { transactionType }
135- >
136- View Job
137- </ MLJobLink >
138- </ ShiftedEuiText >
139- </ EuiFlexItem >
140- ) ;
141- }
142- const { responseTimeSeries, tpmSeries } = charts ;
14362 const { transactionType } = urlParams ;
144- const maxY = getMaxY ( responseTimeSeries ) ;
145- let formatter = getDurationFormatter ( maxY ) ;
14663
147- function onToggleLegend ( visibleSeries : TimeSeries [ ] ) {
148- if ( ! isEmpty ( visibleSeries ) ) {
149- // recalculate the formatter based on the max Y from the visible series
150- const maxVisibleY = getMaxY ( visibleSeries ) ;
151- formatter = getDurationFormatter ( maxVisibleY ) ;
152- }
153- }
64+ const { responseTimeSeries, tpmSeries } = charts ;
15465
155- function getResponseTimeTickFormatter ( t : number ) {
156- return formatter ( t ) . formatted ;
157- }
66+ const { formatter , setDisabledSeriesState } = useFormatter (
67+ responseTimeSeries
68+ ) ;
15869
159- function getResponseTimeTooltipFormatter ( coordinate : Coordinate ) {
160- return isValidCoordinateValue ( coordinate . y )
161- ? formatter ( coordinate . y ) . formatted
162- : NOT_AVAILABLE_LABEL ;
70+ function onToggleLegend ( disabledSeriesStates : boolean [ ] ) {
71+ setDisabledSeriesState ( disabledSeriesStates ) ;
16372 }
16473
16574 return (
@@ -175,15 +84,18 @@ export function TransactionCharts({
17584 </ EuiTitle >
17685 </ EuiFlexItem >
17786 < LicenseContext . Consumer >
178- { ( license ) =>
179- renderMLHeader ( license ?. getFeature ( 'ml' ) . isAvailable )
180- }
87+ { ( license ) => (
88+ < MLHeader
89+ hasValidMlLicense = { license ?. getFeature ( 'ml' ) . isAvailable }
90+ mlJobId = { charts . mlJobId }
91+ />
92+ ) }
18193 </ LicenseContext . Consumer >
18294 </ EuiFlexGroup >
18395 < TransactionLineChart
18496 series = { responseTimeSeries }
185- tickFormatY = { getResponseTimeTickFormatter }
186- formatTooltipValue = { getResponseTimeTooltipFormatter }
97+ tickFormatY = { getResponseTimeTickFormatter ( formatter ) }
98+ formatTooltipValue = { getResponseTimeTooltipFormatter ( formatter ) }
18799 onToggleLegend = { onToggleLegend }
188100 />
189101 </ React . Fragment >
0 commit comments