profileCharts.tsx 5.8 KB

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