utils.tsx 5.5 KB

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