histogramChart.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {Fragment} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import {BarChart, BarChartProps} from 'sentry/components/charts/barChart';
  6. import BarChartZoom from 'sentry/components/charts/barChartZoom';
  7. import ErrorPanel from 'sentry/components/charts/errorPanel';
  8. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  9. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  10. import Placeholder from 'sentry/components/placeholder';
  11. import QuestionTooltip from 'sentry/components/questionTooltip';
  12. import {IconWarning} from 'sentry/icons/iconWarning';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {Organization} from 'sentry/types';
  16. import {Series} from 'sentry/types/echarts';
  17. import EventView from 'sentry/utils/discover/eventView';
  18. import {formatAbbreviatedNumber} from 'sentry/utils/formatters';
  19. import getDynamicText from 'sentry/utils/getDynamicText';
  20. import HistogramQuery from 'sentry/utils/performance/histogram/histogramQuery';
  21. import {HistogramData} from 'sentry/utils/performance/histogram/types';
  22. import {
  23. computeBuckets,
  24. formatHistogramData,
  25. } from 'sentry/utils/performance/histogram/utils';
  26. import {DoubleHeaderContainer} from '../../styles';
  27. import {getFieldOrBackup} from '../display/utils';
  28. const NUM_BUCKETS = 50;
  29. const PRECISION = 0;
  30. type Props = {
  31. eventView: EventView;
  32. field: string;
  33. location: Location;
  34. onFilterChange: (minValue: number, maxValue: number) => void;
  35. organization: Organization;
  36. title: string;
  37. titleTooltip: string;
  38. usingBackupAxis: boolean;
  39. backupField?: string;
  40. didReceiveMultiAxis?: (axisCounts: Record<string, number>) => void;
  41. };
  42. export function HistogramChart(props: Props) {
  43. const {
  44. location,
  45. onFilterChange,
  46. organization,
  47. eventView,
  48. field,
  49. title,
  50. titleTooltip,
  51. didReceiveMultiAxis,
  52. backupField,
  53. usingBackupAxis,
  54. } = props;
  55. const _backupField = backupField ? [backupField] : [];
  56. return (
  57. <div>
  58. <DoubleHeaderContainer>
  59. <HeaderTitleLegend>
  60. {title}
  61. <QuestionTooltip position="top" size="sm" title={titleTooltip} />
  62. </HeaderTitleLegend>
  63. </DoubleHeaderContainer>
  64. <HistogramQuery
  65. location={location}
  66. orgSlug={organization.slug}
  67. eventView={eventView}
  68. numBuckets={NUM_BUCKETS}
  69. precision={PRECISION}
  70. fields={[field, ..._backupField]}
  71. dataFilter="exclude_outliers"
  72. didReceiveMultiAxis={didReceiveMultiAxis}
  73. >
  74. {results => {
  75. const _field = usingBackupAxis ? getFieldOrBackup(field, backupField) : field;
  76. const isLoading = results.isLoading;
  77. const isErrored = results.error !== null;
  78. const chartData = results.histograms?.[_field];
  79. if (isErrored) {
  80. return (
  81. <ErrorPanel height="250px">
  82. <IconWarning color="gray300" size="lg" />
  83. </ErrorPanel>
  84. );
  85. }
  86. if (!chartData) {
  87. return null;
  88. }
  89. return (
  90. <Chart
  91. isLoading={isLoading}
  92. isErrored={isErrored}
  93. chartData={chartData}
  94. location={location}
  95. onFilterChange={onFilterChange}
  96. field={_field}
  97. />
  98. );
  99. }}
  100. </HistogramQuery>
  101. </div>
  102. );
  103. }
  104. type ChartProps = {
  105. field: string;
  106. isErrored: boolean;
  107. isLoading: boolean;
  108. location: Location;
  109. onFilterChange: Props['onFilterChange'];
  110. chartData?: HistogramData;
  111. colors?: string[];
  112. disableChartPadding?: boolean;
  113. disableXAxis?: boolean;
  114. disableZoom?: boolean;
  115. grid?: BarChartProps['grid'];
  116. height?: number;
  117. };
  118. export function Chart(props: ChartProps) {
  119. const {
  120. isLoading,
  121. isErrored,
  122. chartData,
  123. location,
  124. field,
  125. onFilterChange,
  126. height,
  127. grid,
  128. disableXAxis,
  129. disableZoom,
  130. disableChartPadding,
  131. colors,
  132. } = props;
  133. const theme = useTheme();
  134. if (!chartData) {
  135. return null;
  136. }
  137. const series = {
  138. seriesName: t('Count'),
  139. data: formatHistogramData(chartData, {type: 'duration'}),
  140. };
  141. const xAxis = {
  142. type: 'category' as const,
  143. truncate: true,
  144. axisTick: {
  145. alignWithLabel: true,
  146. },
  147. };
  148. const allSeries: Series[] = [];
  149. if (!isLoading && !isErrored) {
  150. allSeries.push(series);
  151. }
  152. const yAxis = {
  153. type: 'value' as const,
  154. axisLabel: {
  155. color: theme.chartLabel,
  156. formatter: formatAbbreviatedNumber,
  157. },
  158. };
  159. return (
  160. <Fragment>
  161. <BarChartZoom
  162. minZoomWidth={10 ** -PRECISION * NUM_BUCKETS}
  163. location={location}
  164. paramStart={`${field}:>=`}
  165. paramEnd={`${field}:<=`}
  166. xAxisIndex={[0]}
  167. buckets={computeBuckets(chartData)}
  168. onHistoryPush={onFilterChange}
  169. >
  170. {zoomRenderProps => {
  171. return (
  172. <BarChartContainer hasPadding={!disableChartPadding}>
  173. <MaskContainer>
  174. <TransparentLoadingMask visible={isLoading} />
  175. {getDynamicText({
  176. value: (
  177. <BarChart
  178. height={height ?? 250}
  179. series={allSeries}
  180. xAxis={disableXAxis ? {show: false} : xAxis}
  181. yAxis={yAxis}
  182. colors={colors}
  183. grid={
  184. grid ?? {
  185. left: space(3),
  186. right: space(3),
  187. top: space(3),
  188. bottom: isLoading ? space(4) : space(1.5),
  189. }
  190. }
  191. stacked
  192. {...(disableZoom ? {} : zoomRenderProps)}
  193. />
  194. ),
  195. fixed: <Placeholder height="250px" testId="skeleton-ui" />,
  196. })}
  197. </MaskContainer>
  198. </BarChartContainer>
  199. );
  200. }}
  201. </BarChartZoom>
  202. </Fragment>
  203. );
  204. }
  205. const BarChartContainer = styled('div')<{hasPadding?: boolean}>`
  206. padding-top: ${p => (p.hasPadding ? space(1) : 0)};
  207. position: relative;
  208. `;
  209. const MaskContainer = styled('div')`
  210. position: relative;
  211. `;
  212. export default HistogramChart;