context.tsx 13 KB

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