queries.tsx 11 KB

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