utils.tsx 5.4 KB

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