utils.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {Organization} from 'sentry/types';
  2. import {objectIsEmpty} from 'sentry/utils';
  3. import localStorage from 'sentry/utils/localStorage';
  4. import {
  5. canUseMetricsData,
  6. MEPState,
  7. MetricsEnhancedSettingContext,
  8. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  9. import {PROJECT_PERFORMANCE_TYPE} from '../../utils';
  10. import {PerformanceWidgetSetting} from './widgetDefinitions';
  11. export const eventsRequestQueryProps = [
  12. 'children',
  13. 'organization',
  14. 'yAxis',
  15. 'period',
  16. 'start',
  17. 'end',
  18. 'environment',
  19. 'project',
  20. 'referrer',
  21. ] as const;
  22. function setWidgetStorageObject(localObject: Record<string, string>) {
  23. localStorage.setItem(getContainerLocalStorageObjectKey, JSON.stringify(localObject));
  24. }
  25. const mepQueryParamBase = {};
  26. export function getMEPQueryParams(mepContext: MetricsEnhancedSettingContext) {
  27. let queryParams = {};
  28. const base = mepQueryParamBase;
  29. if (mepContext.shouldQueryProvideMEPAutoParams) {
  30. queryParams = {
  31. ...queryParams,
  32. ...base,
  33. dataset: 'metricsEnhanced',
  34. };
  35. }
  36. if (mepContext.shouldQueryProvideMEPTransactionParams) {
  37. queryParams = {...queryParams, ...base, dataset: 'discover'};
  38. }
  39. if (mepContext.shouldQueryProvideMEPMetricParams) {
  40. queryParams = {...queryParams, ...base, dataset: 'metrics'};
  41. }
  42. // Disallow any performance request from using aggregates since they aren't currently possible in all visualizations and we don't want to mix modes.
  43. return objectIsEmpty(queryParams) ? undefined : queryParams;
  44. }
  45. export function getMetricOnlyQueryParams() {
  46. return {...mepQueryParamBase, dataset: 'metrics'};
  47. }
  48. export const WIDGET_MAP_DENY_LIST = [
  49. PerformanceWidgetSetting.MOST_RELATED_ERRORS,
  50. PerformanceWidgetSetting.MOST_RELATED_ISSUES,
  51. ];
  52. /**
  53. * Some widgets, such as Related Issues, are inherently not possible w/ metrics at the moment since they use event.type:error under the hood.
  54. */
  55. export function getMEPParamsIfApplicable(
  56. mepContext: MetricsEnhancedSettingContext,
  57. widget: PerformanceWidgetSetting
  58. ) {
  59. if (WIDGET_MAP_DENY_LIST.includes(widget)) {
  60. return undefined;
  61. }
  62. return getMEPQueryParams(mepContext);
  63. }
  64. const getContainerLocalStorageObjectKey = 'landing-chart-container';
  65. const getContainerKey = (
  66. index: number,
  67. performanceType: PROJECT_PERFORMANCE_TYPE,
  68. height: number
  69. ) => `landing-chart-container#${performanceType}#${height}#${index}`;
  70. function getWidgetStorageObject() {
  71. const localObject = JSON.parse(
  72. localStorage.getItem(getContainerLocalStorageObjectKey) || '{}'
  73. );
  74. return localObject;
  75. }
  76. export const getChartSetting = (
  77. index: number,
  78. height: number,
  79. performanceType: PROJECT_PERFORMANCE_TYPE,
  80. defaultType: PerformanceWidgetSetting,
  81. forceDefaultChartSetting?: boolean // Used for testing.
  82. ): PerformanceWidgetSetting => {
  83. if (forceDefaultChartSetting) {
  84. return defaultType;
  85. }
  86. const key = getContainerKey(index, performanceType, height);
  87. const localObject = getWidgetStorageObject();
  88. const value = localObject?.[key];
  89. if (
  90. value &&
  91. Object.values(PerformanceWidgetSetting).includes(value as PerformanceWidgetSetting)
  92. ) {
  93. const _value: PerformanceWidgetSetting = value as PerformanceWidgetSetting;
  94. return _value;
  95. }
  96. return defaultType;
  97. };
  98. export const _setChartSetting = (
  99. index: number,
  100. height: number,
  101. performanceType: PROJECT_PERFORMANCE_TYPE,
  102. setting: PerformanceWidgetSetting
  103. ) => {
  104. const key = getContainerKey(index, performanceType, height);
  105. const localObject = getWidgetStorageObject();
  106. localObject[key] = setting;
  107. setWidgetStorageObject(localObject);
  108. };
  109. const DISALLOWED_CHARTS_METRICS = [
  110. PerformanceWidgetSetting.DURATION_HISTOGRAM,
  111. PerformanceWidgetSetting.FCP_HISTOGRAM,
  112. PerformanceWidgetSetting.LCP_HISTOGRAM,
  113. PerformanceWidgetSetting.FID_HISTOGRAM,
  114. ];
  115. export function filterAllowedChartsMetrics(
  116. organization: Organization,
  117. allowedCharts: PerformanceWidgetSetting[],
  118. mepSetting: MetricsEnhancedSettingContext
  119. ) {
  120. if (
  121. !canUseMetricsData(organization) ||
  122. organization.features.includes('performance-mep-reintroduce-histograms') ||
  123. mepSetting.metricSettingState === MEPState.transactionsOnly
  124. ) {
  125. return allowedCharts;
  126. }
  127. return allowedCharts.filter(c => !DISALLOWED_CHARTS_METRICS.includes(c));
  128. }