context.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import {
  2. createContext,
  3. useCallback,
  4. useContext,
  5. useEffect,
  6. useMemo,
  7. useState,
  8. } from 'react';
  9. import * as Sentry from '@sentry/react';
  10. import isEqual from 'lodash/isEqual';
  11. import type {FocusAreaSelection} from 'sentry/components/metrics/chart/types';
  12. import type {Field} from 'sentry/components/metrics/metricSamplesTable';
  13. import type {MetricMeta} from 'sentry/types/metrics';
  14. import {useInstantRef, useUpdateQuery} from 'sentry/utils/metrics';
  15. import {
  16. emptyMetricsFormulaWidget,
  17. emptyMetricsQueryWidget,
  18. NO_QUERY_ID,
  19. } from 'sentry/utils/metrics/constants';
  20. import {
  21. isMetricsEquationWidget,
  22. isMetricsQueryWidget,
  23. MetricExpressionType,
  24. type MetricsWidget,
  25. } from 'sentry/utils/metrics/types';
  26. import {useVirtualizedMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  27. import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
  28. import {useVirtualMetricsContext} from 'sentry/utils/metrics/virtualMetricsContext';
  29. import {decodeInteger, decodeScalar} from 'sentry/utils/queryString';
  30. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  31. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  32. import usePageFilters from 'sentry/utils/usePageFilters';
  33. import useRouter from 'sentry/utils/useRouter';
  34. import {getNewMetricsWidget} from 'sentry/views/metrics/utils/getNewMetricsWidget';
  35. import {parseMetricWidgetsQueryParam} from 'sentry/views/metrics/utils/parseMetricWidgetsQueryParam';
  36. import {useStructuralSharing} from 'sentry/views/metrics/utils/useStructuralSharing';
  37. export type FocusAreaProps = {
  38. onAdd?: (area: FocusAreaSelection) => void;
  39. onDraw?: () => void;
  40. onRemove?: () => void;
  41. selection?: FocusAreaSelection;
  42. };
  43. interface MetricsContextValue {
  44. addWidget: (type?: MetricExpressionType) => void;
  45. duplicateWidget: (index: number) => void;
  46. focusArea: FocusAreaProps;
  47. hasCustomMetrics: boolean;
  48. hasPerformanceMetrics: boolean;
  49. isDefaultQuery: boolean;
  50. isHasMetricsLoading: boolean;
  51. isMultiChartMode: boolean;
  52. removeWidget: (index: number) => void;
  53. selectedWidgetIndex: number;
  54. setDefaultQuery: (query: Record<string, any> | null) => void;
  55. setHighlightedSampleId: (sample?: string) => void;
  56. setIsMultiChartMode: (value: boolean) => void;
  57. setMetricsSamples: React.Dispatch<
  58. React.SetStateAction<MetricsSamplesResults<Field>['data'] | undefined>
  59. >;
  60. setSelectedWidgetIndex: (index: number) => void;
  61. showQuerySymbols: boolean;
  62. toggleWidgetVisibility: (index: number) => void;
  63. updateWidget: (index: number, data: Partial<Omit<MetricsWidget, 'type'>>) => void;
  64. widgets: MetricsWidget[];
  65. highlightedSampleId?: string;
  66. metricsSamples?: MetricsSamplesResults<Field>['data'];
  67. }
  68. export const MetricsContext = createContext<MetricsContextValue>({
  69. addWidget: () => {},
  70. duplicateWidget: () => {},
  71. focusArea: {},
  72. hasCustomMetrics: false,
  73. hasPerformanceMetrics: false,
  74. highlightedSampleId: undefined,
  75. isDefaultQuery: false,
  76. isMultiChartMode: false,
  77. isHasMetricsLoading: true,
  78. metricsSamples: [],
  79. removeWidget: () => {},
  80. selectedWidgetIndex: 0,
  81. setDefaultQuery: () => {},
  82. setHighlightedSampleId: () => {},
  83. setIsMultiChartMode: () => {},
  84. setMetricsSamples: () => {},
  85. setSelectedWidgetIndex: () => {},
  86. showQuerySymbols: false,
  87. updateWidget: () => {},
  88. widgets: [],
  89. toggleWidgetVisibility: () => {},
  90. });
  91. export function useMetricsContext() {
  92. return useContext(MetricsContext);
  93. }
  94. export function useMetricWidgets(
  95. firstCustomMeta: MetricMeta | undefined,
  96. defaultCondition?: number
  97. ) {
  98. const {getVirtualMRIQuery} = useVirtualMetricsContext();
  99. const {widgets: urlWidgets} = useLocationQuery({fields: {widgets: decodeScalar}});
  100. const updateQuery = useUpdateQuery();
  101. const widgets = useStructuralSharing(
  102. useMemo<MetricsWidget[]>(() => {
  103. const parseResult = parseMetricWidgetsQueryParam(urlWidgets);
  104. if (parseResult.length === 0) {
  105. const widget = getNewMetricsWidget(firstCustomMeta, defaultCondition);
  106. widget.id = 0;
  107. return [widget];
  108. }
  109. return parseResult.map(widget => {
  110. if (isMetricsEquationWidget(widget)) {
  111. return widget;
  112. }
  113. // Check if a virtual MRI exists for this mri and use it
  114. const virtualMRIQuery = getVirtualMRIQuery(widget.mri, widget.aggregation);
  115. if (!virtualMRIQuery) {
  116. return widget;
  117. }
  118. return {
  119. ...widget,
  120. mri: virtualMRIQuery.mri,
  121. aggregation: virtualMRIQuery.aggregation,
  122. condition: virtualMRIQuery.conditionId,
  123. };
  124. });
  125. }, [defaultCondition, firstCustomMeta, getVirtualMRIQuery, urlWidgets])
  126. );
  127. // We want to have it as a ref, so that we can use it in the setWidget callback
  128. // without needing to generate a new callback every time the location changes
  129. const currentWidgetsRef = useInstantRef(widgets);
  130. const setWidgets = useCallback(
  131. (newWidgets: React.SetStateAction<MetricsWidget[]>) => {
  132. const currentWidgets = currentWidgetsRef.current;
  133. const newData =
  134. typeof newWidgets === 'function' ? newWidgets(currentWidgets) : newWidgets;
  135. updateQuery({widgets: JSON.stringify(newData)});
  136. // We need to update the ref so that the next call to setWidgets in the same render cycle will have the updated value
  137. currentWidgetsRef.current = newData;
  138. },
  139. [updateQuery, currentWidgetsRef]
  140. );
  141. const updateWidget = useCallback(
  142. (index: number, data: Partial<Omit<MetricsWidget, 'type'>>) => {
  143. setWidgets(currentWidgets => {
  144. const newWidgets = [...currentWidgets];
  145. const oldWidget = currentWidgets[index];
  146. if (isMetricsQueryWidget(oldWidget)) {
  147. // Reset focused series if mri, query or groupBy changes
  148. if (
  149. ('mri' in data && data.mri !== oldWidget.mri) ||
  150. ('query' in data && data.query !== oldWidget.query) ||
  151. ('groupBy' in data && !isEqual(data.groupBy, oldWidget.groupBy))
  152. ) {
  153. data.focusedSeries = undefined;
  154. }
  155. }
  156. newWidgets[index] = {
  157. ...currentWidgets[index],
  158. ...data,
  159. };
  160. return newWidgets;
  161. });
  162. },
  163. [setWidgets]
  164. );
  165. const duplicateWidget = useCallback(
  166. (index: number) => {
  167. setWidgets(currentWidgets => {
  168. const newWidgets = [...currentWidgets];
  169. const newWidget = {...currentWidgets[index]};
  170. newWidget.id = NO_QUERY_ID;
  171. newWidgets.splice(index + 1, 0, newWidget);
  172. return newWidgets;
  173. });
  174. },
  175. [setWidgets]
  176. );
  177. const addWidget = useCallback(
  178. (type: MetricExpressionType = MetricExpressionType.QUERY) => {
  179. const lastIndexOfSameType = currentWidgetsRef.current.findLastIndex(
  180. w => w.type === type
  181. );
  182. if (lastIndexOfSameType > -1) {
  183. duplicateWidget(lastIndexOfSameType);
  184. } else {
  185. setWidgets(currentWidgets => [
  186. ...currentWidgets,
  187. type === MetricExpressionType.QUERY
  188. ? emptyMetricsQueryWidget
  189. : emptyMetricsFormulaWidget,
  190. ]);
  191. }
  192. },
  193. [currentWidgetsRef, duplicateWidget, setWidgets]
  194. );
  195. const removeWidget = useCallback(
  196. (index: number) => {
  197. setWidgets(currentWidgets => {
  198. let newWidgets = [...currentWidgets];
  199. newWidgets.splice(index, 1);
  200. // Ensure that a visible widget remains
  201. if (!newWidgets.find(w => !w.isHidden)) {
  202. newWidgets = newWidgets.map(w => ({...w, isHidden: false}));
  203. }
  204. return newWidgets;
  205. });
  206. },
  207. [setWidgets]
  208. );
  209. return {
  210. widgets,
  211. updateWidget,
  212. addWidget,
  213. removeWidget,
  214. duplicateWidget,
  215. setWidgets,
  216. };
  217. }
  218. const useDefaultQuery = () => {
  219. const router = useRouter();
  220. const [defaultQuery, setDefaultQuery] = useLocalStorageState<Record<
  221. string,
  222. any
  223. > | null>('ddm:default-query', null);
  224. useEffect(() => {
  225. if (defaultQuery && router.location.query.widgets === undefined) {
  226. router.replace({...router.location, query: defaultQuery});
  227. }
  228. // Only call on page load
  229. // eslint-disable-next-line react-hooks/exhaustive-deps
  230. }, []);
  231. return useMemo(
  232. () => ({
  233. defaultQuery,
  234. setDefaultQuery,
  235. isDefaultQuery: !!defaultQuery && isEqual(defaultQuery, router.location.query),
  236. }),
  237. [defaultQuery, router.location.query, setDefaultQuery]
  238. );
  239. };
  240. export function MetricsContextProvider({children}: {children: React.ReactNode}) {
  241. const router = useRouter();
  242. const updateQuery = useUpdateQuery();
  243. const {getConditions} = useVirtualMetricsContext();
  244. const {multiChartMode} = useLocationQuery({fields: {multiChartMode: decodeInteger}});
  245. const pageFilters = usePageFilters();
  246. const {data: metaCustom, isLoading: isMetaCustomLoading} = useVirtualizedMetricsMeta(
  247. pageFilters.selection,
  248. ['custom'],
  249. true,
  250. pageFilters.isReady
  251. );
  252. const {data: metaPerformance, isLoading: isMetaPerformanceLoading} =
  253. useVirtualizedMetricsMeta(
  254. pageFilters.selection,
  255. ['transactions', 'spans'],
  256. true,
  257. pageFilters.isReady
  258. );
  259. const isMultiChartMode = multiChartMode === 1;
  260. const firstCustomMetric: MetricMeta | undefined = metaCustom[0];
  261. const {setDefaultQuery, isDefaultQuery} = useDefaultQuery();
  262. const [selectedWidgetIndex, setSelectedWidgetIndex] = useState(0);
  263. const {widgets, updateWidget, addWidget, removeWidget, duplicateWidget, setWidgets} =
  264. useMetricWidgets(
  265. firstCustomMetric,
  266. firstCustomMetric && getConditions(firstCustomMetric.mri)[0]?.id
  267. );
  268. const [metricsSamples, setMetricsSamples] = useState<
  269. MetricsSamplesResults<Field>['data'] | undefined
  270. >();
  271. const [highlightedSampleId, setHighlightedSampleId] = useState<string | undefined>();
  272. const hasCustomMetrics = useMemo(() => {
  273. return !!metaCustom.length;
  274. }, [metaCustom]);
  275. const hasPerformanceMetrics = useMemo(() => {
  276. return !!metaPerformance.length;
  277. }, [metaPerformance]);
  278. const handleSetSelectedWidgetIndex = useCallback(
  279. (value: number) => {
  280. if (!isMultiChartMode) {
  281. return;
  282. }
  283. setSelectedWidgetIndex(value);
  284. },
  285. [isMultiChartMode]
  286. );
  287. const focusAreaSelection = useMemo<FocusAreaSelection | undefined>(
  288. () => router.location.query.focusArea && JSON.parse(router.location.query.focusArea),
  289. [router.location.query.focusArea]
  290. );
  291. const handleAddFocusArea = useCallback(
  292. (area: FocusAreaSelection) => {
  293. if (!area.range.start || !area.range.end) {
  294. Sentry.metrics.increment('ddm.enhance.range-undefined');
  295. return;
  296. }
  297. Sentry.metrics.increment('ddm.enhance.add');
  298. handleSetSelectedWidgetIndex(area.widgetIndex);
  299. updateQuery({focusArea: JSON.stringify(area)}, {replace: true});
  300. },
  301. [handleSetSelectedWidgetIndex, updateQuery]
  302. );
  303. const handleRemoveFocusArea = useCallback(() => {
  304. Sentry.metrics.increment('ddm.enhance.remove');
  305. updateQuery({focusArea: undefined}, {replace: true});
  306. }, [updateQuery]);
  307. const focusArea = useMemo<FocusAreaProps>(() => {
  308. return {
  309. selection: focusAreaSelection,
  310. onAdd: handleAddFocusArea,
  311. onRemove: handleRemoveFocusArea,
  312. };
  313. }, [focusAreaSelection, handleAddFocusArea, handleRemoveFocusArea]);
  314. const handleAddWidget = useCallback(
  315. (type?: MetricExpressionType) => {
  316. addWidget(type);
  317. handleSetSelectedWidgetIndex(widgets.length);
  318. },
  319. [addWidget, handleSetSelectedWidgetIndex, widgets.length]
  320. );
  321. const handleUpdateWidget = useCallback(
  322. (index: number, data: Partial<MetricsWidget>) => {
  323. updateWidget(index, data);
  324. handleSetSelectedWidgetIndex(index);
  325. if (index === focusAreaSelection?.widgetIndex) {
  326. handleRemoveFocusArea();
  327. }
  328. },
  329. [
  330. updateWidget,
  331. handleSetSelectedWidgetIndex,
  332. focusAreaSelection?.widgetIndex,
  333. handleRemoveFocusArea,
  334. ]
  335. );
  336. const handleDuplicate = useCallback(
  337. (index: number) => {
  338. duplicateWidget(index);
  339. handleSetSelectedWidgetIndex(index + 1);
  340. },
  341. [duplicateWidget, handleSetSelectedWidgetIndex]
  342. );
  343. const handleSetIsMultiChartMode = useCallback(
  344. (value: boolean) => {
  345. updateQuery({multiChartMode: value ? 1 : 0}, {replace: true});
  346. updateWidget(0, {focusedSeries: undefined});
  347. const firstVisibleWidgetIndex = widgets.findIndex(w => !w.isHidden);
  348. setSelectedWidgetIndex(firstVisibleWidgetIndex);
  349. },
  350. [updateQuery, updateWidget, widgets]
  351. );
  352. const toggleWidgetVisibility = useCallback(
  353. (index: number) => {
  354. if (index === selectedWidgetIndex) {
  355. const firstVisibleWidgetIndex = widgets.findIndex(w => !w.isHidden);
  356. setSelectedWidgetIndex(firstVisibleWidgetIndex);
  357. }
  358. if (!isMultiChartMode) {
  359. // Reset the focused series when hiding a widget
  360. setWidgets(currentWidgets => {
  361. return currentWidgets.map(w => ({...w, focusedSeries: undefined}));
  362. });
  363. }
  364. updateWidget(index, {isHidden: !widgets[index].isHidden});
  365. },
  366. [isMultiChartMode, selectedWidgetIndex, setWidgets, updateWidget, widgets]
  367. );
  368. const selectedWidget = widgets[selectedWidgetIndex];
  369. const isSelectionValid = selectedWidget && !selectedWidget.isHidden;
  370. const contextValue = useMemo<MetricsContextValue>(
  371. () => ({
  372. addWidget: handleAddWidget,
  373. selectedWidgetIndex: isSelectionValid
  374. ? selectedWidgetIndex
  375. : widgets.findIndex(w => !w.isHidden),
  376. setSelectedWidgetIndex: handleSetSelectedWidgetIndex,
  377. updateWidget: handleUpdateWidget,
  378. removeWidget,
  379. duplicateWidget: handleDuplicate,
  380. widgets,
  381. hasCustomMetrics,
  382. hasPerformanceMetrics,
  383. isHasMetricsLoading: isMetaCustomLoading || isMetaPerformanceLoading,
  384. focusArea,
  385. setDefaultQuery,
  386. isDefaultQuery,
  387. showQuerySymbols: widgets.length > 1,
  388. highlightedSampleId,
  389. setHighlightedSampleId,
  390. isMultiChartMode: isMultiChartMode,
  391. setIsMultiChartMode: handleSetIsMultiChartMode,
  392. metricsSamples,
  393. setMetricsSamples,
  394. toggleWidgetVisibility,
  395. }),
  396. [
  397. handleAddWidget,
  398. isSelectionValid,
  399. selectedWidgetIndex,
  400. widgets,
  401. handleSetSelectedWidgetIndex,
  402. handleUpdateWidget,
  403. removeWidget,
  404. handleDuplicate,
  405. hasCustomMetrics,
  406. hasPerformanceMetrics,
  407. focusArea,
  408. setDefaultQuery,
  409. isDefaultQuery,
  410. highlightedSampleId,
  411. isMultiChartMode,
  412. handleSetIsMultiChartMode,
  413. metricsSamples,
  414. toggleWidgetVisibility,
  415. isMetaCustomLoading,
  416. isMetaPerformanceLoading,
  417. ]
  418. );
  419. return (
  420. <MetricsContext.Provider value={contextValue}>{children}</MetricsContext.Provider>
  421. );
  422. }