utils.tsx 5.6 KB

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