utils.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type {Theme} from '@emotion/react';
  2. import type {Location, Query} from 'history';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import type {AnomalyConfidence} from 'sentry/utils/performance/anomalies/anomaliesQuery';
  5. import {decodeScalar} from 'sentry/utils/queryString';
  6. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  7. import type {DomainView} from 'sentry/views/insights/pages/useFilters';
  8. import {getTransactionSummaryBaseUrl} from 'sentry/views/performance/transactionSummary/utils';
  9. export function generateAnomaliesRoute({
  10. orgSlug,
  11. view,
  12. }: {
  13. orgSlug: string;
  14. view?: DomainView;
  15. }): string {
  16. return `${getTransactionSummaryBaseUrl(orgSlug, view)}/anomalies/`;
  17. }
  18. export const ANOMALY_FLAG = 'performance-anomaly-detection-ui';
  19. export function anomaliesRouteWithQuery({
  20. orgSlug,
  21. transaction,
  22. projectID,
  23. query,
  24. view,
  25. }: {
  26. orgSlug: string;
  27. query: Query;
  28. transaction: string;
  29. projectID?: string | string[];
  30. view?: DomainView;
  31. }) {
  32. const pathname = generateAnomaliesRoute({
  33. orgSlug,
  34. view,
  35. });
  36. return {
  37. pathname,
  38. query: {
  39. transaction,
  40. project: projectID,
  41. environment: query.environment,
  42. statsPeriod: query.statsPeriod,
  43. start: query.start,
  44. end: query.end,
  45. query: query.query,
  46. },
  47. };
  48. }
  49. export function anomalyToColor(anomalyConfidence: AnomalyConfidence, theme: Theme) {
  50. // Map inside function so it's reactive to theme.
  51. const map: Record<AnomalyConfidence, string> = {
  52. high: theme.red300,
  53. low: theme.yellow300,
  54. };
  55. return map[anomalyConfidence];
  56. }
  57. export function generateAnomaliesEventView({
  58. location,
  59. transactionName,
  60. }: {
  61. location: Location;
  62. transactionName: string;
  63. }): EventView {
  64. const query = decodeScalar(location.query.query, '');
  65. const conditions = new MutableSearch(query);
  66. conditions.setFilterValues('transaction', [transactionName]);
  67. const eventView = EventView.fromNewQueryWithLocation(
  68. {
  69. id: undefined,
  70. version: 2,
  71. name: transactionName,
  72. fields: ['tpm()'], // TODO(k-fish): Modify depending on api url later.
  73. query: conditions.formatString(),
  74. projects: [],
  75. },
  76. location
  77. );
  78. return eventView;
  79. }