profileCharts.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import {useMemo} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {AreaChart} from 'sentry/components/charts/areaChart';
  5. import ChartZoom from 'sentry/components/charts/chartZoom';
  6. import {HeaderTitle} from 'sentry/components/charts/styles';
  7. import {Panel} from 'sentry/components/panels';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {PageFilters} from 'sentry/types';
  11. import {Series} from 'sentry/types/echarts';
  12. import {axisLabelFormatter, tooltipFormatter} from 'sentry/utils/discover/charts';
  13. import {aggregateOutputType} from 'sentry/utils/discover/fields';
  14. import {useProfileEventsStats} from 'sentry/utils/profiling/hooks/useProfileEventsStats';
  15. import useRouter from 'sentry/utils/useRouter';
  16. interface ProfileChartsProps {
  17. query: string;
  18. compact?: boolean;
  19. hideCount?: boolean;
  20. selection?: PageFilters;
  21. }
  22. // We want p99 to be before p75 because echarts renders the series in order.
  23. // So if p75 is before p99, p99 will be rendered on top of p75 which will
  24. // cover it up.
  25. const SERIES_ORDER = ['count()', 'p99()', 'p95()', 'p75()'] as const;
  26. export function ProfileCharts({
  27. query,
  28. selection,
  29. hideCount,
  30. compact = false,
  31. }: ProfileChartsProps) {
  32. const router = useRouter();
  33. const theme = useTheme();
  34. const seriesOrder = useMemo(() => {
  35. if (hideCount) {
  36. return SERIES_ORDER.filter(s => s !== 'count()');
  37. }
  38. return SERIES_ORDER;
  39. }, [hideCount]);
  40. const profileStats = useProfileEventsStats({
  41. query,
  42. referrer: 'api.profiling.landing-chart',
  43. yAxes: seriesOrder,
  44. });
  45. const series: Series[] = useMemo(() => {
  46. if (profileStats.status !== 'success') {
  47. return [];
  48. }
  49. // the timestamps in the response is in seconds but echarts expects
  50. // a timestamp in milliseconds, so multiply by 1e3 to do the conversion
  51. const timestamps = profileStats.data[0].timestamps.map(ts => ts * 1e3);
  52. const allSeries = profileStats.data[0].data
  53. .filter(rawData => seriesOrder.includes(rawData.axis))
  54. .map(rawData => {
  55. if (timestamps.length !== rawData.values.length) {
  56. throw new Error('Invalid stats response');
  57. }
  58. if (rawData.axis === 'count()') {
  59. return {
  60. data: rawData.values.map((value, i) => ({
  61. name: timestamps[i]!,
  62. // the response value contains nulls when no data is
  63. // available, use 0 to represent it
  64. value: value ?? 0,
  65. })),
  66. seriesName: rawData.axis,
  67. xAxisIndex: 0,
  68. yAxisIndex: 0,
  69. };
  70. }
  71. return {
  72. data: rawData.values.map((value, i) => ({
  73. name: timestamps[i]!,
  74. // the response value contains nulls when no data
  75. // is available, use 0 to represent it
  76. value: value ?? 0,
  77. })),
  78. seriesName: rawData.axis,
  79. xAxisIndex: 1,
  80. yAxisIndex: 1,
  81. };
  82. });
  83. allSeries.sort((a, b) => {
  84. const idxA = seriesOrder.indexOf(a.seriesName as any);
  85. const idxB = seriesOrder.indexOf(b.seriesName as any);
  86. return idxA - idxB;
  87. });
  88. return allSeries;
  89. }, [profileStats, seriesOrder]);
  90. return (
  91. <ChartZoom router={router} {...selection?.datetime}>
  92. {zoomRenderProps => (
  93. <StyledPanel>
  94. <TitleContainer>
  95. {!hideCount && (
  96. <StyledHeaderTitle compact>{t('Profiles by Count')}</StyledHeaderTitle>
  97. )}
  98. <StyledHeaderTitle compact>{t('Profiles by Percentiles')}</StyledHeaderTitle>
  99. </TitleContainer>
  100. <AreaChart
  101. height={compact ? 150 : 300}
  102. series={series}
  103. grid={[
  104. {
  105. top: '32px',
  106. left: '24px',
  107. right: '52%',
  108. bottom: '16px',
  109. },
  110. {
  111. top: '32px',
  112. left: hideCount ? '24px' : '52%',
  113. right: '24px',
  114. bottom: '16px',
  115. },
  116. ]}
  117. legend={{
  118. right: 16,
  119. top: 12,
  120. data: seriesOrder.slice(),
  121. }}
  122. axisPointer={{
  123. link: [
  124. {
  125. xAxisIndex: [0, 1],
  126. },
  127. ],
  128. }}
  129. xAxes={[
  130. {
  131. show: !hideCount,
  132. gridIndex: 0,
  133. type: 'time' as const,
  134. },
  135. {
  136. gridIndex: 1,
  137. type: 'time' as const,
  138. },
  139. ]}
  140. yAxes={[
  141. {
  142. gridIndex: 0,
  143. scale: true,
  144. axisLabel: {
  145. color: theme.chartLabel,
  146. formatter(value: number) {
  147. return axisLabelFormatter(value, 'integer');
  148. },
  149. },
  150. },
  151. {
  152. gridIndex: 1,
  153. scale: true,
  154. axisLabel: {
  155. color: theme.chartLabel,
  156. formatter(value: number) {
  157. return axisLabelFormatter(value, 'duration');
  158. },
  159. },
  160. },
  161. ]}
  162. tooltip={{
  163. valueFormatter: (value, label) =>
  164. tooltipFormatter(value, aggregateOutputType(label)),
  165. }}
  166. isGroupedByDate
  167. showTimeInTooltip
  168. {...zoomRenderProps}
  169. />
  170. </StyledPanel>
  171. )}
  172. </ChartZoom>
  173. );
  174. }
  175. const StyledPanel = styled(Panel)`
  176. padding-top: ${space(2)};
  177. `;
  178. const TitleContainer = styled('div')`
  179. width: 100%;
  180. display: flex;
  181. flex-direction: row;
  182. `;
  183. const StyledHeaderTitle = styled(HeaderTitle)<{compact?: boolean}>`
  184. flex-grow: 1;
  185. margin-left: ${space(2)};
  186. font-size: ${p => (p.compact ? p.theme.fontSizeSmall : undefined)};
  187. `;