generatePerformanceEventView.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import type {Location} from 'history';
  2. import {COL_WIDTH_UNDEFINED} from 'sentry/components/gridEditable';
  3. import {wrapQueryInWildcards} from 'sentry/components/performance/searchBar';
  4. import {t} from 'sentry/locale';
  5. import type {NewQuery, Organization, PageFilters} from 'sentry/types';
  6. import EventView from 'sentry/utils/discover/eventView';
  7. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  8. import {decodeScalar} from 'sentry/utils/queryString';
  9. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  10. import {STARFISH_TYPE_FOR_PROJECT} from 'sentry/views/starfish/allowedProjects';
  11. import {StarfishType} from 'sentry/views/starfish/types';
  12. const DEFAULT_STATS_PERIOD = '7d';
  13. const TOKEN_KEYS_SUPPORTED_IN_LIMITED_SEARCH = ['transaction'];
  14. export const TIME_SPENT_IN_SERVICE = 'time_spent_percentage()';
  15. const getDefaultStatsPeriod = (organization: Organization) => {
  16. if (organization?.features?.includes('performance-landing-page-stats-period')) {
  17. return '14d';
  18. }
  19. return DEFAULT_STATS_PERIOD;
  20. };
  21. function prepareQueryForLandingPage(searchQuery, withStaticFilters) {
  22. const conditions = new MutableSearch(searchQuery);
  23. // If there is a bare text search, we want to treat it as a search
  24. // on the transaction name.
  25. if (conditions.freeText.length > 0) {
  26. const parsedFreeText = conditions.freeText.join(' ');
  27. // the query here is a user entered condition, no need to escape it
  28. conditions.setFilterValues(
  29. 'transaction',
  30. [wrapQueryInWildcards(parsedFreeText)],
  31. false
  32. );
  33. conditions.freeText = [];
  34. }
  35. if (withStaticFilters) {
  36. conditions.tokens = conditions.tokens.filter(
  37. token => token.key && TOKEN_KEYS_SUPPORTED_IN_LIMITED_SEARCH.includes(token.key)
  38. );
  39. }
  40. return conditions.formatString();
  41. }
  42. export function generateWebServiceEventView(
  43. location: Location,
  44. {withStaticFilters = false} = {},
  45. organization: Organization,
  46. selection: PageFilters
  47. ) {
  48. const {query} = location;
  49. const project = selection.projects[0];
  50. const starfishType = STARFISH_TYPE_FOR_PROJECT[project] || StarfishType.BACKEND;
  51. const getSavedQuery = () => {
  52. switch (starfishType) {
  53. case StarfishType.MOBILE:
  54. return generateMobileServiceSavedQuery(location);
  55. case StarfishType.BACKEND:
  56. default:
  57. return generateWebServiceSavedQuery(location);
  58. }
  59. };
  60. const savedQuery = getSavedQuery();
  61. const hasStartAndEnd = query.start && query.end;
  62. const widths = Array(savedQuery.fields.length).fill(COL_WIDTH_UNDEFINED);
  63. widths[savedQuery.fields.length - 1] = '110';
  64. savedQuery.widths = widths;
  65. if (!query.statsPeriod && !hasStartAndEnd) {
  66. savedQuery.range = getDefaultStatsPeriod(organization);
  67. }
  68. const searchQuery = decodeScalar(query.query, '');
  69. savedQuery.query = `${savedQuery.query} ${prepareQueryForLandingPage(
  70. searchQuery,
  71. withStaticFilters
  72. )}`;
  73. const eventView = EventView.fromNewQueryWithLocation(savedQuery, location);
  74. return eventView;
  75. }
  76. export function generateMobileServiceSavedQuery(location: Location) {
  77. const {query} = location;
  78. const orderby = decodeScalar(query.sort, `-eps`);
  79. const fields = [
  80. 'transaction',
  81. 'eps()',
  82. 'p75(measurements.frames_slow_rate)',
  83. 'p75(measurements.time_to_initial_display)',
  84. ];
  85. const savedQuery: NewQuery = {
  86. id: undefined,
  87. name: t('Performance'),
  88. query: 'event.type:transaction transaction.op:ui.load',
  89. fields,
  90. version: 2,
  91. dataset: DiscoverDatasets.METRICS,
  92. };
  93. savedQuery.orderby = orderby;
  94. return savedQuery;
  95. }
  96. function generateWebServiceSavedQuery(location: Location) {
  97. const {query} = location;
  98. const orderby = decodeScalar(query.sort, `-time_spent_percentage`);
  99. const fields = [
  100. 'transaction',
  101. 'http.method',
  102. 'tps()',
  103. 'avg(transaction.duration)',
  104. 'http_error_count()',
  105. 'time_spent_percentage()',
  106. 'sum(transaction.duration)',
  107. ];
  108. const savedQuery: NewQuery = {
  109. id: undefined,
  110. name: t('Performance'),
  111. query: 'event.type:transaction has:http.method transaction.op:http.server',
  112. fields,
  113. version: 2,
  114. dataset: DiscoverDatasets.METRICS,
  115. };
  116. savedQuery.orderby = orderby;
  117. return savedQuery;
  118. }