utils.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import {PlainRoute} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {LocationDescriptor, Query} from 'history';
  4. import space from 'sentry/styles/space';
  5. import {Organization} from 'sentry/types';
  6. import {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  7. import {generateEventSlug} from 'sentry/utils/discover/urls';
  8. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  9. import {getTransactionDetailsUrl} from 'sentry/utils/performance/urls';
  10. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  11. import {getTraceDetailsUrl} from 'sentry/views/performance/traceDetails/utils';
  12. import {DisplayModes} from './transactionOverview/charts';
  13. export enum TransactionFilterOptions {
  14. FASTEST = 'fastest',
  15. SLOW = 'slow',
  16. OUTLIER = 'outlier',
  17. RECENT = 'recent',
  18. }
  19. export function generateTransactionSummaryRoute({orgSlug}: {orgSlug: string}): string {
  20. return `/organizations/${orgSlug}/performance/summary/`;
  21. }
  22. // normalizes search conditions by removing any redundant search conditions before presenting them in:
  23. // - query strings
  24. // - search UI
  25. export function normalizeSearchConditions(query: string): MutableSearch {
  26. const filterParams = normalizeSearchConditionsWithTransactionName(query);
  27. // no need to include transaction as its already in the query params
  28. filterParams.removeFilter('transaction');
  29. return filterParams;
  30. }
  31. // normalizes search conditions by removing any redundant search conditions, but retains any transaction name
  32. export function normalizeSearchConditionsWithTransactionName(
  33. query: string
  34. ): MutableSearch {
  35. const filterParams = new MutableSearch(query);
  36. // remove any event.type queries since it is implied to apply to only transactions
  37. filterParams.removeFilter('event.type');
  38. return filterParams;
  39. }
  40. export function transactionSummaryRouteWithQuery({
  41. orgSlug,
  42. transaction,
  43. projectID,
  44. query,
  45. unselectedSeries = 'p100()',
  46. display,
  47. trendFunction,
  48. trendColumn,
  49. showTransactions,
  50. additionalQuery,
  51. }: {
  52. orgSlug: string;
  53. query: Query;
  54. transaction: string;
  55. additionalQuery?: Record<string, string>;
  56. display?: DisplayModes;
  57. projectID?: string | string[];
  58. showTransactions?: TransactionFilterOptions;
  59. trendColumn?: string;
  60. trendFunction?: string;
  61. unselectedSeries?: string | string[];
  62. }) {
  63. const pathname = generateTransactionSummaryRoute({
  64. orgSlug,
  65. });
  66. let searchFilter: typeof query.query;
  67. if (typeof query.query === 'string') {
  68. searchFilter = normalizeSearchConditions(query.query).formatString();
  69. } else {
  70. searchFilter = query.query;
  71. }
  72. return {
  73. pathname,
  74. query: {
  75. transaction,
  76. project: projectID,
  77. environment: query.environment,
  78. statsPeriod: query.statsPeriod,
  79. start: query.start,
  80. end: query.end,
  81. query: searchFilter,
  82. unselectedSeries,
  83. showTransactions,
  84. display,
  85. trendFunction,
  86. trendColumn,
  87. referrer: 'performance-transaction-summary',
  88. ...additionalQuery,
  89. },
  90. };
  91. }
  92. export function generateTraceLink(dateSelection) {
  93. return (
  94. organization: Organization,
  95. tableRow: TableDataRow,
  96. _query: Query
  97. ): LocationDescriptor => {
  98. const traceId = `${tableRow.trace}`;
  99. if (!traceId) {
  100. return {};
  101. }
  102. return getTraceDetailsUrl(organization, traceId, dateSelection, {});
  103. };
  104. }
  105. export function generateTransactionLink(transactionName: string) {
  106. return (
  107. organization: Organization,
  108. tableRow: TableDataRow,
  109. query: Query,
  110. spanId?: string
  111. ): LocationDescriptor => {
  112. const eventSlug = generateEventSlug(tableRow);
  113. return getTransactionDetailsUrl(
  114. organization.slug,
  115. eventSlug,
  116. transactionName,
  117. query,
  118. spanId
  119. );
  120. };
  121. }
  122. export function generateReplayLink(routes: PlainRoute<any>[]) {
  123. return (
  124. organization: Organization,
  125. tableRow: TableDataRow,
  126. _query: Query | undefined
  127. ): LocationDescriptor => {
  128. const replayId = tableRow.replayId;
  129. if (!replayId) {
  130. return {};
  131. }
  132. const replaySlug = `${tableRow['project.name']}:${replayId}`;
  133. const referrer = getRouteStringFromRoutes(routes);
  134. if (!tableRow.timestamp) {
  135. return {
  136. pathname: `/organizations/${organization.slug}/replays/${replaySlug}/`,
  137. query: {
  138. referrer,
  139. },
  140. };
  141. }
  142. const transactionTimestamp = new Date(tableRow.timestamp).getTime();
  143. const transactionStartTimestamp =
  144. transactionTimestamp - (tableRow['transaction.duration'] as number);
  145. return {
  146. pathname: `/organizations/${organization.slug}/replays/${replaySlug}/`,
  147. query: {
  148. event_t: transactionStartTimestamp,
  149. referrer,
  150. },
  151. };
  152. };
  153. }
  154. export const SidebarSpacer = styled('div')`
  155. margin-top: ${space(3)};
  156. `;