context.tsx 14 KB

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