queries.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. import {Fragment, useCallback, useLayoutEffect, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as echarts from 'echarts/core';
  4. import {Button} from 'sentry/components/button';
  5. import SwitchButton from 'sentry/components/switchButton';
  6. import {Tooltip} from 'sentry/components/tooltip';
  7. import {IconAdd} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import {
  12. isMetricsQueryWidget,
  13. MetricExpressionType,
  14. type MetricsEquationWidget,
  15. type MetricsQuery,
  16. type MetricsQueryWidget,
  17. type MetricsWidget,
  18. } from 'sentry/utils/metrics/types';
  19. import useOrganization from 'sentry/utils/useOrganization';
  20. import usePageFilters from 'sentry/utils/usePageFilters';
  21. import {DDM_CHART_GROUP} from 'sentry/views/metrics/constants';
  22. import {useMetricsContext} from 'sentry/views/metrics/context';
  23. import {EquationSymbol} from 'sentry/views/metrics/equationSymbol';
  24. import {FormulaInput} from 'sentry/views/metrics/formulaInput';
  25. import {MetricFormulaContextMenu} from 'sentry/views/metrics/metricFormulaContextMenu';
  26. import {MetricQueryContextMenu} from 'sentry/views/metrics/metricQueryContextMenu';
  27. import {QueryBuilder} from 'sentry/views/metrics/queryBuilder';
  28. import {getQuerySymbol, QuerySymbol} from 'sentry/views/metrics/querySymbol';
  29. import {useFormulaDependencies} from 'sentry/views/metrics/utils/useFormulaDependencies';
  30. export function Queries() {
  31. const {
  32. widgets,
  33. updateWidget,
  34. setSelectedWidgetIndex,
  35. showQuerySymbols,
  36. selectedWidgetIndex,
  37. isMultiChartMode,
  38. setIsMultiChartMode,
  39. addWidget,
  40. toggleWidgetVisibility,
  41. } = useMetricsContext();
  42. const organization = useOrganization();
  43. const {selection} = usePageFilters();
  44. const formulaDependencies = useFormulaDependencies();
  45. // Make sure all charts are connected to the same group whenever the widgets definition changes
  46. useLayoutEffect(() => {
  47. echarts.connect(DDM_CHART_GROUP);
  48. }, [widgets]);
  49. const handleChange = useCallback(
  50. (index: number, widget: Partial<MetricsWidget>) => {
  51. updateWidget(index, widget);
  52. },
  53. [updateWidget]
  54. );
  55. const handleAddWidget = useCallback(
  56. (type: MetricExpressionType) => {
  57. trackAnalytics('ddm.widget.add', {
  58. organization,
  59. type: type === MetricExpressionType.QUERY ? 'query' : 'equation',
  60. });
  61. addWidget(type);
  62. },
  63. [addWidget, organization]
  64. );
  65. const querySymbols = useMemo(() => {
  66. const querySymbolSet = new Set<string>();
  67. for (const widget of widgets) {
  68. const symbol = getQuerySymbol(widget.id);
  69. if (isMetricsQueryWidget(widget)) {
  70. querySymbolSet.add(symbol);
  71. }
  72. }
  73. return querySymbolSet;
  74. }, [widgets]);
  75. const visibleWidgets = widgets.filter(widget => !widget.isHidden);
  76. return (
  77. <Fragment>
  78. <Wrapper showQuerySymbols={showQuerySymbols}>
  79. {widgets.map((widget, index) => (
  80. <Row
  81. key={`${widget.type}_${widget.id}`}
  82. onFocusCapture={() => setSelectedWidgetIndex(index)}
  83. >
  84. {isMetricsQueryWidget(widget) ? (
  85. <Query
  86. widget={widget}
  87. onChange={handleChange}
  88. onToggleVisibility={toggleWidgetVisibility}
  89. index={index}
  90. projects={selection.projects}
  91. showQuerySymbols={showQuerySymbols}
  92. isSelected={isMultiChartMode && index === selectedWidgetIndex}
  93. canBeHidden={visibleWidgets.length > 1}
  94. />
  95. ) : (
  96. <Formula
  97. availableVariables={querySymbols}
  98. onChange={handleChange}
  99. onToggleVisibility={toggleWidgetVisibility}
  100. index={index}
  101. widget={widget}
  102. showQuerySymbols={showQuerySymbols}
  103. isSelected={isMultiChartMode && index === selectedWidgetIndex}
  104. canBeHidden={visibleWidgets.length > 1}
  105. formulaDependencies={formulaDependencies}
  106. />
  107. )}
  108. </Row>
  109. ))}
  110. </Wrapper>
  111. <ButtonBar addQuerySymbolSpacing={showQuerySymbols}>
  112. <Button
  113. size="sm"
  114. icon={<IconAdd isCircled />}
  115. onClick={() => handleAddWidget(MetricExpressionType.QUERY)}
  116. >
  117. {t('Add metric')}
  118. </Button>
  119. <Button
  120. size="sm"
  121. icon={<IconAdd isCircled />}
  122. onClick={() => handleAddWidget(MetricExpressionType.EQUATION)}
  123. >
  124. {t('Add equation')}
  125. </Button>
  126. {widgets.length > 1 && (
  127. <SwitchWrapper>
  128. {t('One chart per metric')}
  129. <SwitchButton
  130. isActive={isMultiChartMode}
  131. toggle={() => setIsMultiChartMode(!isMultiChartMode)}
  132. />
  133. </SwitchWrapper>
  134. )}
  135. </ButtonBar>
  136. </Fragment>
  137. );
  138. }
  139. interface QueryProps {
  140. canBeHidden: boolean;
  141. index: number;
  142. isSelected: boolean;
  143. onChange: (index: number, data: Partial<MetricsWidget>) => void;
  144. onToggleVisibility: (index: number) => void;
  145. projects: number[];
  146. showQuerySymbols: boolean;
  147. widget: MetricsQueryWidget;
  148. }
  149. function Query({
  150. widget,
  151. projects,
  152. onChange,
  153. onToggleVisibility,
  154. index,
  155. isSelected,
  156. showQuerySymbols,
  157. canBeHidden,
  158. }: QueryProps) {
  159. const metricsQuery = useMemo(
  160. () => ({
  161. mri: widget.mri,
  162. op: widget.op,
  163. groupBy: widget.groupBy,
  164. query: widget.query,
  165. }),
  166. [widget.groupBy, widget.mri, widget.op, widget.query]
  167. );
  168. const handleToggle = useCallback(() => {
  169. onToggleVisibility(index);
  170. }, [index, onToggleVisibility]);
  171. const handleChange = useCallback(
  172. (data: Partial<MetricsQuery>) => {
  173. const changes: Partial<MetricsQueryWidget> = {...data};
  174. if (changes.mri || changes.groupBy) {
  175. changes.focusedSeries = undefined;
  176. }
  177. onChange(index, changes);
  178. },
  179. [index, onChange]
  180. );
  181. const isToggleDisabled = !canBeHidden && !widget.isHidden;
  182. return (
  183. <QueryWrapper hasSymbol={showQuerySymbols}>
  184. {showQuerySymbols && (
  185. <QueryToggle
  186. isHidden={widget.isHidden}
  187. disabled={isToggleDisabled}
  188. isSelected={isSelected}
  189. queryId={widget.id}
  190. onChange={handleToggle}
  191. type={MetricExpressionType.QUERY}
  192. />
  193. )}
  194. <QueryBuilder
  195. onChange={handleChange}
  196. metricsQuery={metricsQuery}
  197. projects={projects}
  198. />
  199. <MetricQueryContextMenu
  200. displayType={widget.displayType}
  201. widgetIndex={index}
  202. metricsQuery={{
  203. mri: widget.mri,
  204. query: widget.query,
  205. op: widget.op,
  206. groupBy: widget.groupBy,
  207. }}
  208. />
  209. </QueryWrapper>
  210. );
  211. }
  212. interface FormulaProps {
  213. availableVariables: Set<string>;
  214. canBeHidden: boolean;
  215. formulaDependencies: ReturnType<typeof useFormulaDependencies>;
  216. index: number;
  217. isSelected: boolean;
  218. onChange: (index: number, data: Partial<MetricsWidget>) => void;
  219. onToggleVisibility: (index: number) => void;
  220. showQuerySymbols: boolean;
  221. widget: MetricsEquationWidget;
  222. }
  223. function Formula({
  224. availableVariables,
  225. index,
  226. widget,
  227. onChange,
  228. onToggleVisibility,
  229. canBeHidden,
  230. isSelected,
  231. showQuerySymbols,
  232. formulaDependencies,
  233. }: FormulaProps) {
  234. const handleToggle = useCallback(() => {
  235. onToggleVisibility(index);
  236. }, [index, onToggleVisibility]);
  237. const handleChange = useCallback(
  238. (data: Partial<MetricsEquationWidget>) => {
  239. onChange(index, data);
  240. },
  241. [index, onChange]
  242. );
  243. const isToggleDisabled = !canBeHidden && !widget.isHidden;
  244. return (
  245. <QueryWrapper hasSymbol={showQuerySymbols}>
  246. {showQuerySymbols && (
  247. <QueryToggle
  248. isHidden={widget.isHidden}
  249. disabled={isToggleDisabled}
  250. isSelected={isSelected}
  251. queryId={widget.id}
  252. onChange={handleToggle}
  253. type={MetricExpressionType.EQUATION}
  254. />
  255. )}
  256. <FormulaInput
  257. availableVariables={availableVariables}
  258. value={widget.formula}
  259. onChange={formula => handleChange({formula})}
  260. />
  261. <MetricFormulaContextMenu
  262. widgetIndex={index}
  263. formulaWidget={widget}
  264. formulaDependencies={formulaDependencies}
  265. />
  266. </QueryWrapper>
  267. );
  268. }
  269. interface QueryToggleProps {
  270. disabled: boolean;
  271. isHidden: boolean;
  272. isSelected: boolean;
  273. onChange: (isHidden: boolean) => void;
  274. queryId: number;
  275. type: MetricExpressionType;
  276. }
  277. function QueryToggle({isHidden, queryId, disabled, onChange, type}: QueryToggleProps) {
  278. const tooltipTitle =
  279. type === MetricExpressionType.QUERY
  280. ? isHidden
  281. ? t('Show metric')
  282. : t('Hide metric')
  283. : isHidden
  284. ? t('Show equation')
  285. : t('Hide equation');
  286. return (
  287. <Tooltip
  288. title={!disabled ? tooltipTitle : t('At least one query must be visible')}
  289. delay={500}
  290. >
  291. {type === MetricExpressionType.QUERY ? (
  292. <StyledQuerySymbol
  293. isHidden={isHidden}
  294. queryId={queryId}
  295. isClickable={!disabled}
  296. aria-disabled={disabled}
  297. onClick={disabled ? undefined : () => onChange(!isHidden)}
  298. role="button"
  299. aria-label={tooltipTitle}
  300. />
  301. ) : (
  302. <StyledEquationSymbol
  303. isHidden={isHidden}
  304. equationId={queryId}
  305. isClickable={!disabled}
  306. aria-disabled={disabled}
  307. onClick={disabled ? undefined : () => onChange(!isHidden)}
  308. role="button"
  309. aria-label={tooltipTitle}
  310. />
  311. )}
  312. </Tooltip>
  313. );
  314. }
  315. const QueryWrapper = styled('div')<{hasSymbol: boolean}>`
  316. display: grid;
  317. gap: ${space(1)};
  318. padding-bottom: ${space(1)};
  319. grid-template-columns: 1fr max-content;
  320. ${p => p.hasSymbol && `grid-template-columns: min-content 1fr max-content;`}
  321. `;
  322. const StyledQuerySymbol = styled(QuerySymbol)<{isClickable: boolean}>`
  323. cursor: not-allowed;
  324. ${p => p.isClickable && `cursor: pointer;`}
  325. `;
  326. const StyledEquationSymbol = styled(EquationSymbol)<{isClickable: boolean}>`
  327. cursor: not-allowed;
  328. ${p => p.isClickable && `cursor: pointer;`}
  329. `;
  330. const Wrapper = styled('div')<{showQuerySymbols: boolean}>``;
  331. const Row = styled('div')`
  332. display: contents;
  333. `;
  334. const ButtonBar = styled('div')<{addQuerySymbolSpacing: boolean}>`
  335. align-items: center;
  336. display: flex;
  337. padding-bottom: ${space(2)};
  338. gap: ${space(2)};
  339. ${p =>
  340. p.addQuerySymbolSpacing &&
  341. `
  342. padding-left: ${space(1)};
  343. margin-left: 38px;
  344. `}
  345. `;
  346. const SwitchWrapper = styled('label')`
  347. display: flex;
  348. margin: 0;
  349. align-items: center;
  350. gap: ${space(1)};
  351. `;