utils.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Location, LocationDescriptor, Query} from 'history';
  2. import Duration from 'app/components/duration';
  3. import {ALL_ACCESS_PROJECTS} from 'app/constants/globalSelectionHeader';
  4. import {backend, frontend} from 'app/data/platformCategories';
  5. import {GlobalSelection, OrganizationSummary, Project} from 'app/types';
  6. import {defined} from 'app/utils';
  7. import {statsPeriodToDays} from 'app/utils/dates';
  8. import EventView from 'app/utils/discover/eventView';
  9. import {getDuration} from 'app/utils/formatters';
  10. import getCurrentSentryReactTransaction from 'app/utils/getCurrentSentryReactTransaction';
  11. import {decodeScalar} from 'app/utils/queryString';
  12. import {tokenizeSearch} from 'app/utils/tokenizeSearch';
  13. /**
  14. * Performance type can used to determine a default view or which specific field should be used by default on pages
  15. * where we don't want to wait for transaction data to return to determine how to display aspects of a page.
  16. */
  17. export enum PROJECT_PERFORMANCE_TYPE {
  18. ANY = 'any', // Fallback to transaction duration
  19. FRONTEND = 'frontend',
  20. BACKEND = 'backend',
  21. FRONTEND_OTHER = 'frontend_other',
  22. }
  23. const FRONTEND_PLATFORMS: string[] = [...frontend];
  24. const BACKEND_PLATFORMS: string[] = [...backend];
  25. export function platformToPerformanceType(
  26. projects: Project[],
  27. projectIds: readonly number[]
  28. ) {
  29. if (projectIds.length === 0 || projectIds[0] === ALL_ACCESS_PROJECTS) {
  30. return PROJECT_PERFORMANCE_TYPE.ANY;
  31. }
  32. const selectedProjects = projects.filter(p => projectIds.includes(parseInt(p.id, 10)));
  33. if (selectedProjects.length === 0 || selectedProjects.some(p => !p.platform)) {
  34. return PROJECT_PERFORMANCE_TYPE.ANY;
  35. }
  36. if (
  37. selectedProjects.every(project =>
  38. FRONTEND_PLATFORMS.includes(project.platform as string)
  39. )
  40. ) {
  41. return PROJECT_PERFORMANCE_TYPE.FRONTEND;
  42. }
  43. if (
  44. selectedProjects.every(project =>
  45. BACKEND_PLATFORMS.includes(project.platform as string)
  46. )
  47. ) {
  48. return PROJECT_PERFORMANCE_TYPE.BACKEND;
  49. }
  50. return PROJECT_PERFORMANCE_TYPE.ANY;
  51. }
  52. /**
  53. * Used for transaction summary to determine appropriate columns on a page, since there is no display field set for the page.
  54. */
  55. export function platformAndConditionsToPerformanceType(
  56. projects: Project[],
  57. eventView: EventView
  58. ) {
  59. const performanceType = platformToPerformanceType(projects, eventView.project);
  60. if (performanceType === PROJECT_PERFORMANCE_TYPE.FRONTEND) {
  61. const conditions = tokenizeSearch(eventView.query);
  62. const ops = conditions.getTagValues('!transaction.op');
  63. if (ops.some(op => op === 'pageload')) {
  64. return PROJECT_PERFORMANCE_TYPE.FRONTEND_OTHER;
  65. }
  66. }
  67. return performanceType;
  68. }
  69. /**
  70. * Used for transaction summary to check the view itself, since it can have conditions which would exclude it from having vitals aside from platform.
  71. */
  72. export function isSummaryViewFrontendPageLoad(eventView: EventView, projects: Project[]) {
  73. return (
  74. platformAndConditionsToPerformanceType(projects, eventView) ===
  75. PROJECT_PERFORMANCE_TYPE.FRONTEND
  76. );
  77. }
  78. export function getPerformanceLandingUrl(organization: OrganizationSummary): string {
  79. return `/organizations/${organization.slug}/performance/`;
  80. }
  81. export function getPerformanceTrendsUrl(organization: OrganizationSummary): string {
  82. return `/organizations/${organization.slug}/performance/trends/`;
  83. }
  84. export function getTransactionSearchQuery(location: Location, query: string = '') {
  85. return decodeScalar(location.query.query, query).trim();
  86. }
  87. export function getTransactionDetailsUrl(
  88. organization: OrganizationSummary,
  89. eventSlug: string,
  90. transaction: string,
  91. query: Query
  92. ): LocationDescriptor {
  93. return {
  94. pathname: `/organizations/${organization.slug}/performance/${eventSlug}/`,
  95. query: {
  96. ...query,
  97. transaction,
  98. },
  99. };
  100. }
  101. export function getTransactionComparisonUrl({
  102. organization,
  103. baselineEventSlug,
  104. regressionEventSlug,
  105. transaction,
  106. query,
  107. }: {
  108. organization: OrganizationSummary;
  109. baselineEventSlug: string;
  110. regressionEventSlug: string;
  111. transaction: string;
  112. query: Query;
  113. }): LocationDescriptor {
  114. return {
  115. pathname: `/organizations/${organization.slug}/performance/compare/${baselineEventSlug}/${regressionEventSlug}/`,
  116. query: {
  117. ...query,
  118. transaction,
  119. },
  120. };
  121. }
  122. export function addRoutePerformanceContext(selection: GlobalSelection) {
  123. const transaction = getCurrentSentryReactTransaction();
  124. const days = statsPeriodToDays(
  125. selection.datetime.period,
  126. selection.datetime.start,
  127. selection.datetime.end
  128. );
  129. const oneDay = 86400;
  130. const seconds = Math.floor(days * oneDay);
  131. transaction?.setTag('query.period', seconds.toString());
  132. let groupedPeriod = '>30d';
  133. if (seconds <= oneDay) groupedPeriod = '<=1d';
  134. else if (seconds <= oneDay * 7) groupedPeriod = '<=7d';
  135. else if (seconds <= oneDay * 14) groupedPeriod = '<=14d';
  136. else if (seconds <= oneDay * 30) groupedPeriod = '<=30d';
  137. transaction?.setTag('query.period.grouped', groupedPeriod);
  138. }
  139. export function getTransactionName(location: Location): string | undefined {
  140. const {transaction} = location.query;
  141. return decodeScalar(transaction);
  142. }
  143. type DurationProps = {abbreviation?: boolean};
  144. type SecondsProps = {seconds: number} & DurationProps;
  145. type MillisecondsProps = {milliseconds: number} & DurationProps;
  146. type PerformanceDurationProps = SecondsProps | MillisecondsProps;
  147. const hasMilliseconds = (props: PerformanceDurationProps): props is MillisecondsProps => {
  148. return defined((props as MillisecondsProps).milliseconds);
  149. };
  150. export function PerformanceDuration(props: SecondsProps);
  151. export function PerformanceDuration(props: MillisecondsProps);
  152. export function PerformanceDuration(props: PerformanceDurationProps) {
  153. const normalizedSeconds = hasMilliseconds(props)
  154. ? props.milliseconds / 1000
  155. : props.seconds;
  156. return (
  157. <Duration
  158. abbreviation={props.abbreviation}
  159. seconds={normalizedSeconds}
  160. fixedDigits={normalizedSeconds > 1 ? 2 : 0}
  161. />
  162. );
  163. }
  164. export function getPerformanceDuration(milliseconds: number) {
  165. return getDuration(milliseconds / 1000, milliseconds > 1000 ? 2 : 0, true);
  166. }