charts.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. import styled from '@emotion/styled';
  2. import type {Location} from 'history';
  3. import OptionSelector from 'sentry/components/charts/optionSelector';
  4. import {
  5. ChartContainer,
  6. ChartControls,
  7. InlineContainer,
  8. } from 'sentry/components/charts/styles';
  9. import Panel from 'sentry/components/panels/panel';
  10. import {t} from 'sentry/locale';
  11. import type {SelectValue} from 'sentry/types/core';
  12. import type {Organization} from 'sentry/types/organization';
  13. import type {Project} from 'sentry/types/project';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import {browserHistory} from 'sentry/utils/browserHistory';
  16. import type EventView from 'sentry/utils/discover/eventView';
  17. import {useMetricsCardinalityContext} from 'sentry/utils/performance/contexts/metricsCardinality';
  18. import {useMEPSettingContext} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  19. import {removeHistogramQueryStrings} from 'sentry/utils/performance/histogram';
  20. import {decodeScalar} from 'sentry/utils/queryString';
  21. import {getTransactionMEPParamsIfApplicable} from 'sentry/views/performance/transactionSummary/transactionOverview/utils';
  22. import {DisplayModes} from 'sentry/views/performance/transactionSummary/utils';
  23. import {TransactionsListOption} from 'sentry/views/releases/detail/overview';
  24. import {
  25. TrendFunctionField,
  26. TrendParameterColumn,
  27. TrendParameterLabel,
  28. } from '../../trends/types';
  29. import {TRENDS_FUNCTIONS, TRENDS_PARAMETERS} from '../../trends/utils';
  30. import {SpanOperationBreakdownFilter} from '../filter';
  31. import LatencyChartControls from './latencyChart/chartControls';
  32. import {ZOOM_END, ZOOM_START} from './latencyChart/utils';
  33. import DurationChart from './durationChart';
  34. import DurationPercentileChart from './durationPercentileChart';
  35. import LatencyChart from './latencyChart';
  36. import TrendChart from './trendChart';
  37. import UserMiseryChart from './userMiseryChart';
  38. import VitalsChart from './vitalsChart';
  39. function generateDisplayOptions(
  40. currentFilter: SpanOperationBreakdownFilter
  41. ): SelectValue<string>[] {
  42. if (currentFilter === SpanOperationBreakdownFilter.NONE) {
  43. return [
  44. {value: DisplayModes.DURATION, label: t('Duration Breakdown')},
  45. {value: DisplayModes.DURATION_PERCENTILE, label: t('Duration Percentiles')},
  46. {value: DisplayModes.LATENCY, label: t('Duration Distribution')},
  47. {value: DisplayModes.TREND, label: t('Trends')},
  48. {value: DisplayModes.VITALS, label: t('Web Vitals')},
  49. {value: DisplayModes.USER_MISERY, label: t('User Misery')},
  50. ];
  51. }
  52. // A span operation name breakdown has been chosen.
  53. return [
  54. {value: DisplayModes.DURATION, label: t('Span Operation Breakdown')},
  55. {value: DisplayModes.DURATION_PERCENTILE, label: t('Span Operation Percentiles')},
  56. {value: DisplayModes.LATENCY, label: t('Span Operation Distribution')},
  57. {value: DisplayModes.TREND, label: t('Trends')},
  58. {value: DisplayModes.VITALS, label: t('Web Vitals')},
  59. ];
  60. }
  61. const TREND_FUNCTIONS_OPTIONS: SelectValue<string>[] = TRENDS_FUNCTIONS.map(
  62. ({field, label}) => ({
  63. value: field,
  64. label,
  65. })
  66. );
  67. type Props = {
  68. currentFilter: SpanOperationBreakdownFilter;
  69. eventView: EventView;
  70. location: Location;
  71. organization: Organization;
  72. totalValue: number | null;
  73. withoutZerofill: boolean;
  74. project?: Project;
  75. };
  76. function TransactionSummaryCharts({
  77. totalValue,
  78. eventView,
  79. organization,
  80. location,
  81. currentFilter,
  82. withoutZerofill,
  83. project,
  84. }: Props) {
  85. function handleDisplayChange(value: string) {
  86. const display = decodeScalar(location.query.display, DisplayModes.DURATION);
  87. trackAnalytics('performance_views.transaction_summary.change_chart_display', {
  88. organization,
  89. from_chart: display,
  90. to_chart: value,
  91. });
  92. browserHistory.push({
  93. pathname: location.pathname,
  94. query: {
  95. ...removeHistogramQueryStrings(location, [ZOOM_START, ZOOM_END]),
  96. display: value,
  97. },
  98. });
  99. }
  100. function handleTrendDisplayChange(value: string) {
  101. browserHistory.push({
  102. pathname: location.pathname,
  103. query: {...location.query, trendFunction: value},
  104. });
  105. }
  106. function handleTrendColumnChange(value: string) {
  107. browserHistory.push({
  108. pathname: location.pathname,
  109. query: {
  110. ...location.query,
  111. trendParameter: value,
  112. },
  113. });
  114. }
  115. const TREND_PARAMETERS_OPTIONS: SelectValue<string>[] = TRENDS_PARAMETERS.map(
  116. ({label}) => ({
  117. value: label,
  118. label,
  119. })
  120. );
  121. let display = decodeScalar(location.query.display, DisplayModes.DURATION);
  122. let trendFunction = decodeScalar(
  123. location.query.trendFunction,
  124. TREND_FUNCTIONS_OPTIONS[0].value
  125. ) as TrendFunctionField;
  126. let trendParameter = decodeScalar(
  127. location.query.trendParameter,
  128. TREND_PARAMETERS_OPTIONS[0].value
  129. );
  130. if (!Object.values(DisplayModes).includes(display as DisplayModes)) {
  131. display = DisplayModes.DURATION;
  132. }
  133. if (!Object.values(TrendFunctionField).includes(trendFunction)) {
  134. trendFunction = TrendFunctionField.P50;
  135. }
  136. if (
  137. !Object.values(TrendParameterLabel).includes(trendParameter as TrendParameterLabel)
  138. ) {
  139. trendParameter = TrendParameterLabel.DURATION;
  140. }
  141. const trendColumn =
  142. TRENDS_PARAMETERS.find(parameter => parameter.label === trendParameter)?.column ||
  143. TrendParameterColumn.DURATION;
  144. const releaseQueryExtra = {
  145. yAxis: display === DisplayModes.VITALS ? 'countVital' : 'countDuration',
  146. showTransactions:
  147. display === DisplayModes.VITALS
  148. ? TransactionsListOption.SLOW_LCP
  149. : display === DisplayModes.DURATION
  150. ? TransactionsListOption.SLOW
  151. : undefined,
  152. };
  153. const mepSetting = useMEPSettingContext();
  154. const mepCardinalityContext = useMetricsCardinalityContext();
  155. const queryExtras = getTransactionMEPParamsIfApplicable(
  156. mepSetting,
  157. mepCardinalityContext,
  158. organization
  159. );
  160. const hasTransactionSummaryCleanupFlag = organization.features.includes(
  161. 'performance-transaction-summary-cleanup'
  162. );
  163. const displayOptions = generateDisplayOptions(currentFilter).filter(
  164. option =>
  165. (hasTransactionSummaryCleanupFlag && option.value !== DisplayModes.USER_MISERY) ||
  166. !hasTransactionSummaryCleanupFlag
  167. );
  168. return (
  169. <Panel>
  170. <ChartContainer data-test-id="transaction-summary-charts">
  171. {display === DisplayModes.LATENCY && (
  172. <LatencyChart
  173. organization={organization}
  174. location={location}
  175. query={eventView.query}
  176. project={eventView.project}
  177. environment={eventView.environment}
  178. start={eventView.start}
  179. end={eventView.end}
  180. statsPeriod={eventView.statsPeriod}
  181. currentFilter={currentFilter}
  182. totalCount={totalValue}
  183. />
  184. )}
  185. {display === DisplayModes.DURATION && (
  186. <DurationChart
  187. organization={organization}
  188. query={eventView.query}
  189. queryExtra={releaseQueryExtra}
  190. project={eventView.project}
  191. environment={eventView.environment}
  192. start={eventView.start}
  193. end={eventView.end}
  194. statsPeriod={eventView.statsPeriod}
  195. currentFilter={currentFilter}
  196. withoutZerofill={withoutZerofill}
  197. queryExtras={queryExtras}
  198. />
  199. )}
  200. {display === DisplayModes.DURATION_PERCENTILE && (
  201. <DurationPercentileChart
  202. organization={organization}
  203. location={location}
  204. query={eventView.query}
  205. project={eventView.project}
  206. environment={eventView.environment}
  207. start={eventView.start}
  208. end={eventView.end}
  209. statsPeriod={eventView.statsPeriod}
  210. currentFilter={currentFilter}
  211. queryExtras={queryExtras}
  212. />
  213. )}
  214. {display === DisplayModes.TREND && (
  215. <TrendChart
  216. eventView={eventView}
  217. trendFunction={trendFunction}
  218. trendParameter={trendColumn}
  219. organization={organization}
  220. query={eventView.query}
  221. queryExtra={releaseQueryExtra}
  222. project={eventView.project}
  223. environment={eventView.environment}
  224. start={eventView.start}
  225. end={eventView.end}
  226. statsPeriod={eventView.statsPeriod}
  227. withoutZerofill={withoutZerofill}
  228. projects={project ? [project] : []}
  229. withBreakpoint={organization.features.includes('performance-new-trends')}
  230. />
  231. )}
  232. {display === DisplayModes.VITALS && (
  233. <VitalsChart
  234. organization={organization}
  235. query={eventView.query}
  236. queryExtra={releaseQueryExtra}
  237. project={eventView.project}
  238. environment={eventView.environment}
  239. start={eventView.start}
  240. end={eventView.end}
  241. statsPeriod={eventView.statsPeriod}
  242. withoutZerofill={withoutZerofill}
  243. queryExtras={queryExtras}
  244. />
  245. )}
  246. {display === DisplayModes.USER_MISERY && (
  247. <UserMiseryChart
  248. organization={organization}
  249. query={eventView.query}
  250. queryExtra={releaseQueryExtra}
  251. project={eventView.project}
  252. environment={eventView.environment}
  253. start={eventView.start}
  254. end={eventView.end}
  255. statsPeriod={eventView.statsPeriod}
  256. withoutZerofill={withoutZerofill}
  257. />
  258. )}
  259. </ChartContainer>
  260. <ReversedChartControls>
  261. <InlineContainer>
  262. {display === DisplayModes.TREND && (
  263. <OptionSelector
  264. title={t('Percentile')}
  265. selected={trendFunction}
  266. options={TREND_FUNCTIONS_OPTIONS}
  267. onChange={handleTrendDisplayChange}
  268. />
  269. )}
  270. {display === DisplayModes.TREND && (
  271. <OptionSelector
  272. title={t('Parameter')}
  273. selected={trendParameter}
  274. options={TREND_PARAMETERS_OPTIONS}
  275. onChange={handleTrendColumnChange}
  276. />
  277. )}
  278. {display === DisplayModes.LATENCY && (
  279. <LatencyChartControls location={location} />
  280. )}
  281. <OptionSelector
  282. title={t('Display')}
  283. selected={display}
  284. options={displayOptions}
  285. onChange={handleDisplayChange}
  286. />
  287. </InlineContainer>
  288. </ReversedChartControls>
  289. </Panel>
  290. );
  291. }
  292. const ReversedChartControls = styled(ChartControls)`
  293. flex-direction: row-reverse;
  294. `;
  295. export default TransactionSummaryCharts;