queries.tsx 11 KB

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