sidebarCharts.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import {browserHistory} from 'react-router';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import color from 'color';
  5. import ChartZoom from 'sentry/components/charts/chartZoom';
  6. import MarkPoint from 'sentry/components/charts/components/markPoint';
  7. import ErrorPanel from 'sentry/components/charts/errorPanel';
  8. import EventsRequest from 'sentry/components/charts/eventsRequest';
  9. import {LineChart, LineChartProps} from 'sentry/components/charts/lineChart';
  10. import {SectionHeading} from 'sentry/components/charts/styles';
  11. import TransitionChart from 'sentry/components/charts/transitionChart';
  12. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  13. import {getInterval} from 'sentry/components/charts/utils';
  14. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  15. import Placeholder from 'sentry/components/placeholder';
  16. import QuestionTooltip from 'sentry/components/questionTooltip';
  17. import {IconWarning} from 'sentry/icons';
  18. import {t, tct} from 'sentry/locale';
  19. import {Organization} from 'sentry/types';
  20. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  21. import {tooltipFormatter} from 'sentry/utils/discover/charts';
  22. import EventView from 'sentry/utils/discover/eventView';
  23. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  24. import {QueryError} from 'sentry/utils/discover/genericDiscoverQuery';
  25. import {
  26. formatAbbreviatedNumber,
  27. formatFloat,
  28. formatPercentage,
  29. } from 'sentry/utils/formatters';
  30. import getDynamicText from 'sentry/utils/getDynamicText';
  31. import AnomaliesQuery from 'sentry/utils/performance/anomalies/anomaliesQuery';
  32. import {decodeScalar} from 'sentry/utils/queryString';
  33. import useApi from 'sentry/utils/useApi';
  34. import {useLocation} from 'sentry/utils/useLocation';
  35. import useRouter from 'sentry/utils/useRouter';
  36. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  37. import {getTermHelp, PERFORMANCE_TERM} from 'sentry/views/performance/data';
  38. import {
  39. anomaliesRouteWithQuery,
  40. ANOMALY_FLAG,
  41. anomalyToColor,
  42. } from '../transactionAnomalies/utils';
  43. type ContainerProps = {
  44. error: QueryError | null;
  45. eventView: EventView;
  46. isLoading: boolean;
  47. organization: Organization;
  48. totals: Record<string, number> | null;
  49. transactionName: string;
  50. };
  51. type Props = Pick<ContainerProps, 'organization' | 'isLoading' | 'error' | 'totals'> & {
  52. chartData: {
  53. chartOptions: Omit<LineChartProps, 'series'>;
  54. errored: boolean;
  55. loading: boolean;
  56. reloading: boolean;
  57. series: LineChartProps['series'];
  58. };
  59. eventView: EventView;
  60. transactionName: string;
  61. utc: boolean;
  62. end?: Date;
  63. start?: Date;
  64. statsPeriod?: string | null;
  65. };
  66. function SidebarCharts({
  67. organization,
  68. isLoading,
  69. error,
  70. totals,
  71. start,
  72. end,
  73. utc,
  74. statsPeriod,
  75. chartData,
  76. eventView,
  77. transactionName,
  78. }: Props) {
  79. const location = useLocation();
  80. const router = useRouter();
  81. const theme = useTheme();
  82. return (
  83. <RelativeBox>
  84. <ChartLabel top="0px">
  85. <ChartTitle>
  86. {t('Apdex')}
  87. <QuestionTooltip
  88. position="top"
  89. title={getTermHelp(organization, PERFORMANCE_TERM.APDEX)}
  90. size="sm"
  91. />
  92. </ChartTitle>
  93. <ChartSummaryValue
  94. data-test-id="apdex-summary-value"
  95. isLoading={isLoading}
  96. error={error}
  97. value={totals ? formatFloat(totals['apdex()'], 4) : null}
  98. />
  99. </ChartLabel>
  100. <ChartLabel top="160px">
  101. <ChartTitle>
  102. {t('Failure Rate')}
  103. <QuestionTooltip
  104. position="top"
  105. title={getTermHelp(organization, PERFORMANCE_TERM.FAILURE_RATE)}
  106. size="sm"
  107. />
  108. </ChartTitle>
  109. <ChartSummaryValue
  110. data-test-id="failure-rate-summary-value"
  111. isLoading={isLoading}
  112. error={error}
  113. value={totals ? formatPercentage(totals['failure_rate()']) : null}
  114. />
  115. </ChartLabel>
  116. <ChartLabel top="320px">
  117. <ChartTitle>
  118. {t('TPM')}
  119. <QuestionTooltip
  120. position="top"
  121. title={getTermHelp(organization, PERFORMANCE_TERM.TPM)}
  122. size="sm"
  123. />
  124. </ChartTitle>
  125. <ChartSummaryValue
  126. data-test-id="tpm-summary-value"
  127. isLoading={isLoading}
  128. error={error}
  129. value={
  130. totals
  131. ? tct('[tpm] tpm', {
  132. tpm: formatFloat(totals['tpm()'], 4),
  133. })
  134. : null
  135. }
  136. />
  137. </ChartLabel>
  138. <AnomaliesQuery
  139. location={location}
  140. organization={organization}
  141. eventView={eventView}
  142. >
  143. {results => (
  144. <ChartZoom
  145. router={router}
  146. period={statsPeriod}
  147. start={start}
  148. end={end}
  149. utc={utc}
  150. xAxisIndex={[0, 1, 2]}
  151. >
  152. {zoomRenderProps => {
  153. const {errored, loading, reloading, chartOptions, series} = chartData;
  154. if (errored) {
  155. return (
  156. <ErrorPanel height="580px">
  157. <IconWarning color="gray300" size="lg" />
  158. </ErrorPanel>
  159. );
  160. }
  161. if (organization.features.includes(ANOMALY_FLAG)) {
  162. const epmSeries = series.find(
  163. s => s.seriesName.includes('epm') || s.seriesName.includes('tpm')
  164. );
  165. if (epmSeries && results.data) {
  166. epmSeries.markPoint = MarkPoint({
  167. data: results.data.anomalies.map(a => ({
  168. name: a.id,
  169. yAxis: epmSeries.data.find(({name}) => name > (a.end + a.start) / 2)
  170. ?.value,
  171. // TODO: the above is O(n*m), remove after we change the api to include the midpoint of y.
  172. xAxis: a.start,
  173. itemStyle: {
  174. borderColor: color(anomalyToColor(a.confidence, theme)).string(),
  175. color: color(anomalyToColor(a.confidence, theme))
  176. .alpha(0.2)
  177. .rgb()
  178. .string(),
  179. },
  180. onClick: () => {
  181. const target = anomaliesRouteWithQuery({
  182. orgSlug: organization.slug,
  183. query: location.query,
  184. projectID: decodeScalar(location.query.project),
  185. transaction: transactionName,
  186. });
  187. browserHistory.push(normalizeUrl(target));
  188. },
  189. })),
  190. symbol: 'circle',
  191. symbolSize: 16,
  192. });
  193. }
  194. }
  195. return (
  196. <TransitionChart loading={loading} reloading={reloading} height="580px">
  197. <TransparentLoadingMask visible={reloading} />
  198. {getDynamicText({
  199. value: (
  200. <LineChart {...zoomRenderProps} {...chartOptions} series={series} />
  201. ),
  202. fixed: <Placeholder height="480px" testId="skeleton-ui" />,
  203. })}
  204. </TransitionChart>
  205. );
  206. }}
  207. </ChartZoom>
  208. )}
  209. </AnomaliesQuery>
  210. </RelativeBox>
  211. );
  212. }
  213. function SidebarChartsContainer({
  214. eventView,
  215. organization,
  216. isLoading,
  217. error,
  218. totals,
  219. transactionName,
  220. }: ContainerProps) {
  221. const location = useLocation();
  222. const router = useRouter();
  223. const api = useApi();
  224. const theme = useTheme();
  225. const colors = theme.charts.getColorPalette(3);
  226. const statsPeriod = eventView.statsPeriod;
  227. const start = eventView.start ? getUtcToLocalDateObject(eventView.start) : undefined;
  228. const end = eventView.end ? getUtcToLocalDateObject(eventView.end) : undefined;
  229. const project = eventView.project;
  230. const environment = eventView.environment;
  231. const query = eventView.query;
  232. const utc = normalizeDateTimeParams(location.query).utc === 'true';
  233. const axisLineConfig = {
  234. scale: true,
  235. axisLine: {
  236. show: false,
  237. },
  238. axisTick: {
  239. show: false,
  240. },
  241. splitLine: {
  242. show: false,
  243. },
  244. };
  245. const chartOptions: Omit<LineChartProps, 'series'> = {
  246. height: 480,
  247. grid: [
  248. {
  249. top: '60px',
  250. left: '10px',
  251. right: '10px',
  252. height: '100px',
  253. },
  254. {
  255. top: '220px',
  256. left: '10px',
  257. right: '10px',
  258. height: '100px',
  259. },
  260. {
  261. top: '380px',
  262. left: '10px',
  263. right: '10px',
  264. height: '120px',
  265. },
  266. ],
  267. axisPointer: {
  268. // Link each x-axis together.
  269. link: [{xAxisIndex: [0, 1, 2]}],
  270. },
  271. xAxes: Array.from(new Array(3)).map((_i, index) => ({
  272. gridIndex: index,
  273. type: 'time',
  274. show: false,
  275. })),
  276. yAxes: [
  277. {
  278. // apdex
  279. gridIndex: 0,
  280. interval: 0.2,
  281. axisLabel: {
  282. formatter: (value: number) => `${formatFloat(value, 1)}`,
  283. color: theme.chartLabel,
  284. },
  285. ...axisLineConfig,
  286. },
  287. {
  288. // failure rate
  289. gridIndex: 1,
  290. splitNumber: 4,
  291. interval: 0.5,
  292. max: 1.0,
  293. axisLabel: {
  294. formatter: (value: number) => formatPercentage(value, 0),
  295. color: theme.chartLabel,
  296. },
  297. ...axisLineConfig,
  298. },
  299. {
  300. // throughput
  301. gridIndex: 2,
  302. splitNumber: 4,
  303. axisLabel: {
  304. formatter: formatAbbreviatedNumber,
  305. color: theme.chartLabel,
  306. },
  307. ...axisLineConfig,
  308. },
  309. ],
  310. utc,
  311. isGroupedByDate: true,
  312. showTimeInTooltip: true,
  313. colors: [colors[0], colors[1], colors[2]],
  314. tooltip: {
  315. trigger: 'axis',
  316. truncate: 80,
  317. valueFormatter: (value, label) =>
  318. tooltipFormatter(value, aggregateOutputType(label)),
  319. nameFormatter(value: string) {
  320. return value === 'epm()' ? 'tpm()' : value;
  321. },
  322. },
  323. };
  324. const requestCommonProps = {
  325. api,
  326. start,
  327. end,
  328. period: statsPeriod,
  329. project,
  330. environment,
  331. query,
  332. };
  333. const contentCommonProps = {
  334. organization,
  335. router,
  336. error,
  337. isLoading,
  338. start,
  339. end,
  340. utc,
  341. totals,
  342. };
  343. const datetimeSelection = {
  344. start: start || null,
  345. end: end || null,
  346. period: statsPeriod,
  347. };
  348. return (
  349. <EventsRequest
  350. {...requestCommonProps}
  351. organization={organization}
  352. interval={getInterval(datetimeSelection)}
  353. showLoading={false}
  354. includePrevious={false}
  355. yAxis={['apdex()', 'failure_rate()', 'epm()']}
  356. partial
  357. referrer="api.performance.transaction-summary.sidebar-chart"
  358. >
  359. {({results, errored, loading, reloading}) => {
  360. const series = results
  361. ? results.map((values, i: number) => ({
  362. ...values,
  363. yAxisIndex: i,
  364. xAxisIndex: i,
  365. }))
  366. : [];
  367. return (
  368. <SidebarCharts
  369. {...contentCommonProps}
  370. transactionName={transactionName}
  371. eventView={eventView}
  372. chartData={{series, errored, loading, reloading, chartOptions}}
  373. />
  374. );
  375. }}
  376. </EventsRequest>
  377. );
  378. }
  379. type ChartValueProps = {
  380. 'data-test-id': string;
  381. error: QueryError | null;
  382. isLoading: boolean;
  383. value: React.ReactNode;
  384. };
  385. function ChartSummaryValue({error, isLoading, value, ...props}: ChartValueProps) {
  386. if (error) {
  387. return <div {...props}>{'\u2014'}</div>;
  388. }
  389. if (isLoading) {
  390. return <Placeholder height="24px" {...props} />;
  391. }
  392. return <ChartValue {...props}>{value}</ChartValue>;
  393. }
  394. const RelativeBox = styled('div')`
  395. position: relative;
  396. `;
  397. const ChartTitle = styled(SectionHeading)`
  398. margin: 0;
  399. `;
  400. const ChartLabel = styled('div')<{top: string}>`
  401. position: absolute;
  402. top: ${p => p.top};
  403. z-index: 1;
  404. `;
  405. const ChartValue = styled('div')`
  406. font-size: ${p => p.theme.fontSizeExtraLarge};
  407. `;
  408. export default SidebarChartsContainer;