utils.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import {browserHistory} from 'react-router';
  2. import type {Location} from 'history';
  3. import omit from 'lodash/omit';
  4. import {t} from 'sentry/locale';
  5. import type {Organization, Project} from 'sentry/types';
  6. import {trackAnalytics} from 'sentry/utils/analytics';
  7. import type EventView from 'sentry/utils/discover/eventView';
  8. import {
  9. formatAbbreviatedNumber,
  10. formatFloat,
  11. formatPercentage,
  12. getDuration,
  13. } from 'sentry/utils/formatters';
  14. import type {HistogramData} from 'sentry/utils/performance/histogram/types';
  15. import {decodeScalar} from 'sentry/utils/queryString';
  16. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  17. import type {AxisOption} from '../data';
  18. import {getTermHelp, PerformanceTerm} from '../data';
  19. import type {Rectangle} from '../transactionSummary/transactionVitals/types';
  20. import {platformToPerformanceType, ProjectPerformanceType} from '../utils';
  21. export const LEFT_AXIS_QUERY_KEY = 'left';
  22. export const RIGHT_AXIS_QUERY_KEY = 'right';
  23. type LandingDisplay = {
  24. field: LandingDisplayField;
  25. label: string;
  26. };
  27. export enum LandingDisplayField {
  28. ALL = 'all',
  29. FRONTEND_PAGELOAD = 'frontend_pageload',
  30. FRONTEND_OTHER = 'frontend_other',
  31. BACKEND = 'backend',
  32. MOBILE = 'mobile',
  33. }
  34. // TODO Abdullah Khan: Remove code for Web Vitals tab in performance landing
  35. // page when new starfish web vitals module is mature.
  36. export const LANDING_DISPLAYS = [
  37. {
  38. label: t('All Transactions'),
  39. field: LandingDisplayField.ALL,
  40. },
  41. {
  42. label: t('Frontend'),
  43. field: LandingDisplayField.FRONTEND_OTHER,
  44. },
  45. {
  46. label: t('Backend'),
  47. field: LandingDisplayField.BACKEND,
  48. },
  49. {
  50. label: t('Mobile'),
  51. field: LandingDisplayField.MOBILE,
  52. },
  53. ];
  54. export function excludeTransaction(
  55. transaction: string | React.ReactText,
  56. props: {eventView: EventView; location: Location}
  57. ) {
  58. const {eventView, location} = props;
  59. const searchConditions = new MutableSearch(eventView.query);
  60. searchConditions.addFilterValues('!transaction', [`${transaction}`]);
  61. browserHistory.push({
  62. pathname: location.pathname,
  63. query: {
  64. ...location.query,
  65. cursor: undefined,
  66. query: searchConditions.formatString(),
  67. },
  68. });
  69. }
  70. export function getLandingDisplayFromParam(location: Location) {
  71. const landingField = decodeScalar(location?.query?.landingDisplay);
  72. const display = LANDING_DISPLAYS.find(({field}) => field === landingField);
  73. return display;
  74. }
  75. export function getDefaultDisplayForPlatform(projects: Project[], eventView?: EventView) {
  76. const defaultDisplayField = getDefaultDisplayFieldForPlatform(projects, eventView);
  77. const defaultDisplay = LANDING_DISPLAYS.find(
  78. ({field}) => field === defaultDisplayField
  79. );
  80. return defaultDisplay || LANDING_DISPLAYS[0];
  81. }
  82. export function getCurrentLandingDisplay(
  83. location: Location,
  84. projects: Project[],
  85. eventView?: EventView
  86. ): LandingDisplay {
  87. const display = getLandingDisplayFromParam(location);
  88. if (display) {
  89. return display;
  90. }
  91. return getDefaultDisplayForPlatform(projects, eventView);
  92. }
  93. export function handleLandingDisplayChange(
  94. field: LandingDisplayField,
  95. location: Location,
  96. projects: Project[],
  97. organization: Organization,
  98. eventView?: EventView
  99. ) {
  100. // Transaction op can affect the display and show no results if it is explicitly set.
  101. const query = decodeScalar(location.query.query, '');
  102. const searchConditions = new MutableSearch(query);
  103. searchConditions.removeFilter('transaction.op');
  104. const queryWithConditions = {
  105. ...omit(location.query, ['landingDisplay', 'sort']),
  106. query: searchConditions.formatString(),
  107. };
  108. delete queryWithConditions[LEFT_AXIS_QUERY_KEY];
  109. delete queryWithConditions[RIGHT_AXIS_QUERY_KEY];
  110. const defaultDisplay = getDefaultDisplayFieldForPlatform(projects, eventView);
  111. const currentDisplay = getCurrentLandingDisplay(location, projects, eventView).field;
  112. const newQuery: {query: string; landingDisplay?: LandingDisplayField} =
  113. defaultDisplay === field
  114. ? {...queryWithConditions}
  115. : {...queryWithConditions, landingDisplay: field};
  116. trackAnalytics('performance_views.landingv3.display_change', {
  117. organization,
  118. change_to_display: field,
  119. default_display: defaultDisplay,
  120. current_display: currentDisplay,
  121. is_default: defaultDisplay === currentDisplay,
  122. });
  123. browserHistory.push({
  124. pathname: location.pathname,
  125. query: newQuery,
  126. });
  127. }
  128. export function getChartWidth(chartData: HistogramData, refPixelRect: Rectangle | null) {
  129. const distance = refPixelRect ? refPixelRect.point2.x - refPixelRect.point1.x : 0;
  130. const chartWidth = chartData.length * distance;
  131. return {
  132. chartWidth,
  133. };
  134. }
  135. export function getDefaultDisplayFieldForPlatform(
  136. projects: Project[],
  137. eventView?: EventView
  138. ) {
  139. if (!eventView) {
  140. return LandingDisplayField.ALL;
  141. }
  142. const projectIds = eventView.project;
  143. const performanceTypeToDisplay = {
  144. [ProjectPerformanceType.ANY]: LandingDisplayField.ALL,
  145. [ProjectPerformanceType.FRONTEND]: LandingDisplayField.FRONTEND_OTHER,
  146. [ProjectPerformanceType.BACKEND]: LandingDisplayField.BACKEND,
  147. [ProjectPerformanceType.MOBILE]: LandingDisplayField.MOBILE,
  148. };
  149. const performanceType = platformToPerformanceType(projects, projectIds);
  150. const landingField =
  151. performanceTypeToDisplay[performanceType] ?? LandingDisplayField.ALL;
  152. return landingField;
  153. }
  154. type VitalCardDetail = {
  155. formatter: (value: number) => string | number;
  156. title: string;
  157. tooltip: string;
  158. };
  159. export const vitalCardDetails = (
  160. organization: Organization
  161. ): {[key: string]: VitalCardDetail | undefined} => {
  162. return {
  163. 'p75(transaction.duration)': {
  164. title: t('Duration (p75)'),
  165. tooltip: getTermHelp(organization, PerformanceTerm.P75),
  166. formatter: value => getDuration(value / 1000, value >= 1000 ? 3 : 0, true),
  167. },
  168. 'tpm()': {
  169. title: t('Throughput'),
  170. tooltip: getTermHelp(organization, PerformanceTerm.THROUGHPUT),
  171. formatter: (value: number | string) => formatAbbreviatedNumber(value),
  172. },
  173. 'failure_rate()': {
  174. title: t('Failure Rate'),
  175. tooltip: getTermHelp(organization, PerformanceTerm.FAILURE_RATE),
  176. formatter: value => formatPercentage(value, 2),
  177. },
  178. 'apdex()': {
  179. title: t('Apdex'),
  180. tooltip: getTermHelp(organization, PerformanceTerm.APDEX),
  181. formatter: value => formatFloat(value, 4),
  182. },
  183. 'p75(measurements.frames_slow_rate)': {
  184. title: t('Slow Frames (p75)'),
  185. tooltip: getTermHelp(organization, PerformanceTerm.SLOW_FRAMES),
  186. formatter: value => formatPercentage(value, 2),
  187. },
  188. 'p75(measurements.frames_frozen_rate)': {
  189. title: t('Frozen Frames (p75)'),
  190. tooltip: getTermHelp(organization, PerformanceTerm.FROZEN_FRAMES),
  191. formatter: value => formatPercentage(value, 2),
  192. },
  193. 'p75(measurements.app_start_cold)': {
  194. title: t('Cold Start (p75)'),
  195. tooltip: getTermHelp(organization, PerformanceTerm.APP_START_COLD),
  196. formatter: value => getDuration(value / 1000, value >= 1000 ? 3 : 0, true),
  197. },
  198. 'p75(measurements.app_start_warm)': {
  199. title: t('Warm Start (p75)'),
  200. tooltip: getTermHelp(organization, PerformanceTerm.APP_START_WARM),
  201. formatter: value => getDuration(value / 1000, value >= 1000 ? 3 : 0, true),
  202. },
  203. 'p75(measurements.stall_percentage)': {
  204. title: t('Stall Percentage (p75)'),
  205. tooltip: getTermHelp(organization, PerformanceTerm.STALL_PERCENTAGE),
  206. formatter: value => formatPercentage(value, 2),
  207. },
  208. };
  209. };
  210. export function getDisplayAxes(options: AxisOption[], location: Location) {
  211. const leftDefault = options.find(opt => opt.isLeftDefault) || options[0];
  212. const rightDefault = options.find(opt => opt.isRightDefault) || options[1];
  213. const leftAxis =
  214. options.find(opt => opt.value === location.query[LEFT_AXIS_QUERY_KEY]) || leftDefault;
  215. const rightAxis =
  216. options.find(opt => opt.value === location.query[RIGHT_AXIS_QUERY_KEY]) ||
  217. rightDefault;
  218. return {
  219. leftAxis,
  220. rightAxis,
  221. };
  222. }
  223. export function checkIsReactNative(eventView) {
  224. // only react native should contain the stall percentage column
  225. return Boolean(
  226. eventView.getFields().find(field => field.includes('measurements.stall_percentage'))
  227. );
  228. }