index.tsx 4.0 KB

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