profilesSummaryChart.tsx 5.3 KB

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