index.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import {Fragment} from 'react';
  2. // eslint-disable-next-line no-restricted-imports
  3. import {browserHistory, withRouter, WithRouterProps} from 'react-router';
  4. import {useTheme} from '@emotion/react';
  5. import {Location, Query} from 'history';
  6. import EventsRequest from 'sentry/components/charts/eventsRequest';
  7. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  8. import {getInterval, getSeriesSelection} from 'sentry/components/charts/utils';
  9. import {normalizeDateTimeParams} from 'sentry/components/organizations/pageFilters/parse';
  10. import QuestionTooltip from 'sentry/components/questionTooltip';
  11. import {t} from 'sentry/locale';
  12. import {OrganizationSummary} from 'sentry/types';
  13. import {getUtcToLocalDateObject} from 'sentry/utils/dates';
  14. import {
  15. getAggregateArg,
  16. getMeasurementSlug,
  17. WebVital,
  18. } from 'sentry/utils/discover/fields';
  19. import useApi from 'sentry/utils/useApi';
  20. import {ViewProps} from '../../../types';
  21. import Content from './content';
  22. type Props = WithRouterProps &
  23. ViewProps & {
  24. location: Location;
  25. organization: OrganizationSummary;
  26. queryExtra: Query;
  27. withoutZerofill: boolean;
  28. };
  29. function VitalsChart({
  30. project,
  31. environment,
  32. location,
  33. organization,
  34. query,
  35. statsPeriod,
  36. router,
  37. queryExtra,
  38. withoutZerofill,
  39. start: propsStart,
  40. end: propsEnd,
  41. }: Props) {
  42. const api = useApi();
  43. const theme = useTheme();
  44. const handleLegendSelectChanged = (legendChange: {
  45. name: string;
  46. selected: Record<string, boolean>;
  47. type: string;
  48. }) => {
  49. const {selected} = legendChange;
  50. const unselected = Object.keys(selected).filter(key => !selected[key]);
  51. const to = {
  52. ...location,
  53. query: {
  54. ...location.query,
  55. unselectedSeries: unselected,
  56. },
  57. };
  58. browserHistory.push(to);
  59. };
  60. const vitals = [WebVital.FCP, WebVital.LCP, WebVital.FID, WebVital.CLS];
  61. const start = propsStart ? getUtcToLocalDateObject(propsStart) : null;
  62. const end = propsEnd ? getUtcToLocalDateObject(propsEnd) : null;
  63. const utc = normalizeDateTimeParams(location.query).utc === 'true';
  64. const period = statsPeriod;
  65. const legend = {
  66. right: 10,
  67. top: 0,
  68. selected: getSeriesSelection(location),
  69. formatter: (seriesName: string) => {
  70. const arg = getAggregateArg(seriesName);
  71. if (arg !== null) {
  72. const slug = getMeasurementSlug(arg);
  73. if (slug !== null) {
  74. seriesName = slug.toUpperCase();
  75. }
  76. }
  77. return seriesName;
  78. },
  79. };
  80. const datetimeSelection = {start, end, period};
  81. const contentCommonProps = {
  82. theme,
  83. router,
  84. start,
  85. end,
  86. utc,
  87. legend,
  88. queryExtra,
  89. period,
  90. projects: project,
  91. environments: environment,
  92. onLegendSelectChanged: handleLegendSelectChanged,
  93. };
  94. const requestCommonProps = {
  95. api,
  96. start,
  97. end,
  98. project,
  99. environment,
  100. query,
  101. period,
  102. interval: getInterval(datetimeSelection, 'high'),
  103. };
  104. const header = (
  105. <HeaderTitleLegend>
  106. {t('Web Vitals Breakdown')}
  107. <QuestionTooltip
  108. size="sm"
  109. position="top"
  110. title={t(
  111. `Web Vitals Breakdown reflects the 75th percentile of web vitals over time.`
  112. )}
  113. />
  114. </HeaderTitleLegend>
  115. );
  116. const yAxis = vitals.map(v => `p75(${v})`);
  117. return (
  118. <Fragment>
  119. {header}
  120. <EventsRequest
  121. {...requestCommonProps}
  122. organization={organization}
  123. showLoading={false}
  124. includePrevious={false}
  125. yAxis={yAxis}
  126. partial
  127. withoutZerofill={withoutZerofill}
  128. referrer="api.performance.transaction-summary.vitals-chart"
  129. >
  130. {({results, errored, loading, reloading, timeframe: timeFrame}) => (
  131. <Content
  132. series={results}
  133. errored={errored}
  134. loading={loading}
  135. reloading={reloading}
  136. timeFrame={timeFrame}
  137. {...contentCommonProps}
  138. />
  139. )}
  140. </EventsRequest>
  141. </Fragment>
  142. );
  143. }
  144. export default withRouter(VitalsChart);