index.tsx 4.1 KB

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