utils.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. 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()', 'avg()'],
  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. _location: Location
  114. ): LocationDescriptor => {
  115. const traceId = `${tableRow.trace}`;
  116. if (!traceId) {
  117. return {};
  118. }
  119. return getTraceDetailsUrl(organization, traceId, dateSelection, tableRow.timestamp);
  120. };
  121. }
  122. export function generateTransactionIdLink(transactionName?: string) {
  123. return (
  124. organization: Organization,
  125. tableRow: TableDataRow,
  126. location: Location,
  127. spanId?: string
  128. ): LocationDescriptor => {
  129. return generateLinkToEventInTraceView({
  130. eventId: tableRow.id,
  131. timestamp: tableRow.timestamp,
  132. traceSlug: tableRow.trace?.toString(),
  133. projectSlug: tableRow['project.name']?.toString(),
  134. location,
  135. organization,
  136. spanId,
  137. transactionName,
  138. });
  139. };
  140. }
  141. export function generateProfileLink() {
  142. return (
  143. organization: Organization,
  144. tableRow: TableDataRow,
  145. _location: Location | undefined
  146. ) => {
  147. const profileId = tableRow['profile.id'];
  148. if (!profileId) {
  149. return {};
  150. }
  151. return generateProfileFlamechartRoute({
  152. orgSlug: organization.slug,
  153. projectSlug: String(tableRow['project.name']),
  154. profileId: String(profileId),
  155. });
  156. };
  157. }
  158. export function generateReplayLink(routes: PlainRoute<any>[]) {
  159. const referrer = getRouteStringFromRoutes(routes);
  160. return (
  161. organization: Organization,
  162. tableRow: TableDataRow,
  163. _location: Location | undefined
  164. ): LocationDescriptor => {
  165. const replayId = tableRow.replayId;
  166. if (!replayId) {
  167. return {};
  168. }
  169. if (!tableRow.timestamp) {
  170. return {
  171. pathname: normalizeUrl(
  172. `/organizations/${organization.slug}/replays/${replayId}/`
  173. ),
  174. query: {
  175. referrer,
  176. },
  177. };
  178. }
  179. const transactionTimestamp = new Date(tableRow.timestamp).getTime();
  180. const transactionStartTimestamp = tableRow['transaction.duration']
  181. ? transactionTimestamp - (tableRow['transaction.duration'] as number)
  182. : undefined;
  183. return {
  184. pathname: normalizeUrl(`/organizations/${organization.slug}/replays/${replayId}/`),
  185. query: {
  186. event_t: transactionStartTimestamp,
  187. referrer,
  188. },
  189. };
  190. };
  191. }
  192. export const SidebarSpacer = styled('div')`
  193. margin-top: ${space(3)};
  194. `;