context.tsx 14 KB

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