charts.tsx 10 KB

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