utils.tsx 4.8 KB

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