utils.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import type {PlainRoute} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import type {Location, LocationDescriptor, Query} from 'history';
  4. import {space} from 'sentry/styles/space';
  5. import type {Organization} from 'sentry/types/organization';
  6. import type {TableDataRow} from 'sentry/utils/discover/discoverQuery';
  7. import {generateLinkToEventInTraceView} from 'sentry/utils/discover/urls';
  8. import getRouteStringFromRoutes from 'sentry/utils/getRouteStringFromRoutes';
  9. import {generateProfileFlamechartRoute} from 'sentry/utils/profiling/routes';
  10. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  11. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  12. import {getTraceDetailsUrl} from 'sentry/views/performance/traceDetails/utils';
  13. import {TraceViewSources} from '../newTraceDetails/traceMetadataHeader';
  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. location: Location
  115. ): LocationDescriptor => {
  116. const traceId = `${tableRow.trace}`;
  117. if (!traceId) {
  118. return {};
  119. }
  120. return getTraceDetailsUrl({
  121. organization,
  122. traceSlug: traceId,
  123. dateSelection,
  124. timestamp: tableRow.timestamp,
  125. location,
  126. source: TraceViewSources.PERFORMANCE_TRANSACTION_SUMMARY,
  127. });
  128. };
  129. }
  130. export function generateTransactionIdLink(transactionName?: string) {
  131. return (
  132. organization: Organization,
  133. tableRow: TableDataRow,
  134. location: Location,
  135. spanId?: string
  136. ): LocationDescriptor => {
  137. return generateLinkToEventInTraceView({
  138. eventId: tableRow.id,
  139. timestamp: tableRow.timestamp,
  140. traceSlug: tableRow.trace?.toString(),
  141. projectSlug: tableRow['project.name']?.toString(),
  142. location,
  143. organization,
  144. spanId,
  145. transactionName,
  146. source: TraceViewSources.PERFORMANCE_TRANSACTION_SUMMARY,
  147. });
  148. };
  149. }
  150. export function generateProfileLink() {
  151. return (
  152. organization: Organization,
  153. tableRow: TableDataRow,
  154. _location: Location | undefined
  155. ) => {
  156. const profileId = tableRow['profile.id'];
  157. if (!profileId) {
  158. return {};
  159. }
  160. return generateProfileFlamechartRoute({
  161. orgSlug: organization.slug,
  162. projectSlug: String(tableRow['project.name']),
  163. profileId: String(profileId),
  164. });
  165. };
  166. }
  167. export function generateReplayLink(routes: PlainRoute<any>[]) {
  168. const referrer = getRouteStringFromRoutes(routes);
  169. return (
  170. organization: Organization,
  171. tableRow: TableDataRow,
  172. _location: Location | undefined
  173. ): LocationDescriptor => {
  174. const replayId = tableRow.replayId;
  175. if (!replayId) {
  176. return {};
  177. }
  178. if (!tableRow.timestamp) {
  179. return {
  180. pathname: normalizeUrl(
  181. `/organizations/${organization.slug}/replays/${replayId}/`
  182. ),
  183. query: {
  184. referrer,
  185. },
  186. };
  187. }
  188. const transactionTimestamp = new Date(tableRow.timestamp).getTime();
  189. const transactionStartTimestamp = tableRow['transaction.duration']
  190. ? transactionTimestamp - (tableRow['transaction.duration'] as number)
  191. : undefined;
  192. return {
  193. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replayId}/`),
  194. query: {
  195. event_t: transactionStartTimestamp,
  196. referrer,
  197. },
  198. };
  199. };
  200. }
  201. export const SidebarSpacer = styled('div')`
  202. margin-top: ${space(3)};
  203. `;