durationChart.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import {Component, Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import * as ReactRouter from 'react-router';
  4. import {withTheme} from '@emotion/react';
  5. import {Location, Query} from 'history';
  6. import {Client} from 'app/api';
  7. import AreaChart from 'app/components/charts/areaChart';
  8. import ChartZoom from 'app/components/charts/chartZoom';
  9. import ErrorPanel from 'app/components/charts/errorPanel';
  10. import EventsRequest from 'app/components/charts/eventsRequest';
  11. import ReleaseSeries from 'app/components/charts/releaseSeries';
  12. import {HeaderTitleLegend} from 'app/components/charts/styles';
  13. import TransitionChart from 'app/components/charts/transitionChart';
  14. import TransparentLoadingMask from 'app/components/charts/transparentLoadingMask';
  15. import {getInterval, getSeriesSelection} from 'app/components/charts/utils';
  16. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  17. import Placeholder from 'app/components/placeholder';
  18. import QuestionTooltip from 'app/components/questionTooltip';
  19. import {IconWarning} from 'app/icons';
  20. import {t, tct} from 'app/locale';
  21. import {OrganizationSummary} from 'app/types';
  22. import {getUtcToLocalDateObject} from 'app/utils/dates';
  23. import {axisLabelFormatter, tooltipFormatter} from 'app/utils/discover/charts';
  24. import EventView from 'app/utils/discover/eventView';
  25. import getDynamicText from 'app/utils/getDynamicText';
  26. import {Theme} from 'app/utils/theme';
  27. import withApi from 'app/utils/withApi';
  28. import {filterToField, SpanOperationBreakdownFilter} from './filter';
  29. const QUERY_KEYS = [
  30. 'environment',
  31. 'project',
  32. 'query',
  33. 'start',
  34. 'end',
  35. 'statsPeriod',
  36. ] as const;
  37. type ViewProps = Pick<EventView, typeof QUERY_KEYS[number]>;
  38. type Props = ReactRouter.WithRouterProps &
  39. ViewProps & {
  40. theme: Theme;
  41. api: Client;
  42. location: Location;
  43. organization: OrganizationSummary;
  44. queryExtra: Query;
  45. currentFilter: SpanOperationBreakdownFilter;
  46. };
  47. function generateYAxisValues(filter: SpanOperationBreakdownFilter) {
  48. if (filter === SpanOperationBreakdownFilter.None) {
  49. return ['p50()', 'p75()', 'p95()', 'p99()', 'p100()'];
  50. }
  51. const field = filterToField(filter);
  52. return [
  53. `p50(${field})`,
  54. `p75(${field})`,
  55. `p95(${field})`,
  56. `p99(${field})`,
  57. `p100(${field})`,
  58. ];
  59. }
  60. /**
  61. * Fetch and render a stacked area chart that shows duration
  62. * percentiles over the past 7 days
  63. */
  64. class DurationChart extends Component<Props> {
  65. handleLegendSelectChanged = legendChange => {
  66. const {location} = this.props;
  67. const {selected} = legendChange;
  68. const unselected = Object.keys(selected).filter(key => !selected[key]);
  69. const to = {
  70. ...location,
  71. query: {
  72. ...location.query,
  73. unselectedSeries: unselected,
  74. },
  75. };
  76. browserHistory.push(to);
  77. };
  78. render() {
  79. const {
  80. theme,
  81. api,
  82. project,
  83. environment,
  84. location,
  85. organization,
  86. query,
  87. statsPeriod,
  88. router,
  89. queryExtra,
  90. currentFilter,
  91. } = this.props;
  92. const start = this.props.start ? getUtcToLocalDateObject(this.props.start) : null;
  93. const end = this.props.end ? getUtcToLocalDateObject(this.props.end) : null;
  94. const {utc} = getParams(location.query);
  95. const legend = {
  96. right: 10,
  97. top: 5,
  98. selected: getSeriesSelection(location),
  99. };
  100. const datetimeSelection = {
  101. start,
  102. end,
  103. period: statsPeriod,
  104. };
  105. const chartOptions = {
  106. grid: {
  107. left: '10px',
  108. right: '10px',
  109. top: '40px',
  110. bottom: '0px',
  111. },
  112. seriesOptions: {
  113. showSymbol: false,
  114. },
  115. tooltip: {
  116. trigger: 'axis' as const,
  117. valueFormatter: tooltipFormatter,
  118. },
  119. yAxis: {
  120. axisLabel: {
  121. color: theme.chartLabel,
  122. // p50() coerces the axis to be time based
  123. formatter: (value: number) => axisLabelFormatter(value, 'p50()'),
  124. },
  125. },
  126. };
  127. const headerTitle =
  128. currentFilter === SpanOperationBreakdownFilter.None
  129. ? t('Duration Breakdown')
  130. : tct('Span Operation Breakdown - [operationName]', {
  131. operationName: currentFilter,
  132. });
  133. return (
  134. <Fragment>
  135. <HeaderTitleLegend>
  136. {headerTitle}
  137. <QuestionTooltip
  138. size="sm"
  139. position="top"
  140. title={t(
  141. `Duration Breakdown reflects transaction durations by percentile over time.`
  142. )}
  143. />
  144. </HeaderTitleLegend>
  145. <ChartZoom
  146. router={router}
  147. period={statsPeriod}
  148. start={start}
  149. end={end}
  150. utc={utc === 'true'}
  151. >
  152. {zoomRenderProps => (
  153. <EventsRequest
  154. api={api}
  155. organization={organization}
  156. period={statsPeriod}
  157. project={project}
  158. environment={environment}
  159. start={start}
  160. end={end}
  161. interval={getInterval(datetimeSelection, true)}
  162. showLoading={false}
  163. query={query}
  164. includePrevious={false}
  165. yAxis={generateYAxisValues(currentFilter)}
  166. partial
  167. >
  168. {({results, errored, loading, reloading}) => {
  169. if (errored) {
  170. return (
  171. <ErrorPanel>
  172. <IconWarning color="gray300" size="lg" />
  173. </ErrorPanel>
  174. );
  175. }
  176. const colors =
  177. (results && theme.charts.getColorPalette(results.length - 2)) || [];
  178. // Create a list of series based on the order of the fields,
  179. // We need to flip it at the end to ensure the series stack right.
  180. const series = results
  181. ? results
  182. .map((values, i: number) => {
  183. return {
  184. ...values,
  185. color: colors[i],
  186. lineStyle: {
  187. opacity: 0,
  188. },
  189. };
  190. })
  191. .reverse()
  192. : [];
  193. return (
  194. <ReleaseSeries
  195. start={start}
  196. end={end}
  197. queryExtra={queryExtra}
  198. period={statsPeriod}
  199. utc={utc === 'true'}
  200. projects={project}
  201. environments={environment}
  202. >
  203. {({releaseSeries}) => (
  204. <TransitionChart loading={loading} reloading={reloading}>
  205. <TransparentLoadingMask visible={reloading} />
  206. {getDynamicText({
  207. value: (
  208. <AreaChart
  209. {...zoomRenderProps}
  210. {...chartOptions}
  211. legend={legend}
  212. onLegendSelectChanged={this.handleLegendSelectChanged}
  213. series={[...series, ...releaseSeries]}
  214. />
  215. ),
  216. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  217. })}
  218. </TransitionChart>
  219. )}
  220. </ReleaseSeries>
  221. );
  222. }}
  223. </EventsRequest>
  224. )}
  225. </ChartZoom>
  226. </Fragment>
  227. );
  228. }
  229. }
  230. export default withApi(withTheme(ReactRouter.withRouter(DurationChart)));