utils.tsx 3.4 KB

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