queries.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import {Fragment, useCallback, useLayoutEffect} 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 {IconAdd} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {DDM_CHART_GROUP} from 'sentry/views/ddm/constants';
  12. import {useDDMContext} from 'sentry/views/ddm/context';
  13. import {MetricQueryContextMenu} from 'sentry/views/ddm/contextMenu';
  14. import {QueryBuilder} from 'sentry/views/ddm/queryBuilder';
  15. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  16. export function Queries() {
  17. const {
  18. widgets,
  19. updateWidget,
  20. setSelectedWidgetIndex,
  21. showQuerySymbols,
  22. selectedWidgetIndex,
  23. isMultiChartMode,
  24. setIsMultiChartMode,
  25. addWidget,
  26. } = useDDMContext();
  27. const {selection} = usePageFilters();
  28. // Make sure all charts are connected to the same group whenever the widgets definition changes
  29. useLayoutEffect(() => {
  30. echarts.connect(DDM_CHART_GROUP);
  31. }, [widgets]);
  32. const handleChange = useCallback(
  33. (index: number, widget: Partial<MetricWidgetQueryParams>) => {
  34. updateWidget(index, widget);
  35. },
  36. [updateWidget]
  37. );
  38. return (
  39. <Fragment>
  40. <Wrapper showQuerySymbols={showQuerySymbols}>
  41. {widgets.map((widget, index) => (
  42. <Row key={index} onFocusCapture={() => setSelectedWidgetIndex(index)}>
  43. <Query
  44. widget={widget}
  45. onChange={data => handleChange(index, data)}
  46. projects={selection.projects}
  47. symbol={
  48. showQuerySymbols && (
  49. <StyledQuerySymbol
  50. queryId={widget.id}
  51. isClickable={isMultiChartMode}
  52. isSelected={index === selectedWidgetIndex}
  53. onClick={() => setSelectedWidgetIndex(index)}
  54. role={isMultiChartMode ? 'button' : undefined}
  55. aria-label={t('Select query')}
  56. />
  57. )
  58. }
  59. contextMenu={
  60. <MetricQueryContextMenu
  61. displayType={widget.displayType}
  62. widgetIndex={index}
  63. metricsQuery={{
  64. mri: widget.mri,
  65. query: widget.query,
  66. op: widget.op,
  67. groupBy: widget.groupBy,
  68. }}
  69. />
  70. }
  71. />
  72. </Row>
  73. ))}
  74. </Wrapper>
  75. <ButtonBar addQuerySymbolSpacing={showQuerySymbols}>
  76. <Button size="sm" icon={<IconAdd isCircled />} onClick={addWidget}>
  77. Add query
  78. </Button>
  79. <SwitchWrapper>
  80. {t('One chart per query')}
  81. <SwitchButton
  82. isActive={isMultiChartMode}
  83. toggle={() => setIsMultiChartMode(!isMultiChartMode)}
  84. />
  85. </SwitchWrapper>
  86. </ButtonBar>
  87. </Fragment>
  88. );
  89. }
  90. interface Props {
  91. onChange: (data: Partial<MetricWidgetQueryParams>) => void;
  92. projects: number[];
  93. widget: MetricWidgetQueryParams;
  94. contextMenu?: React.ReactNode;
  95. symbol?: React.ReactNode;
  96. }
  97. export function Query({widget, projects, onChange, contextMenu, symbol}: Props) {
  98. return (
  99. <QueryWrapper hasSymbol={!!symbol}>
  100. {symbol}
  101. <QueryBuilder
  102. onChange={onChange}
  103. metricsQuery={{
  104. mri: widget.mri,
  105. op: widget.op,
  106. groupBy: widget.groupBy,
  107. query: widget.query,
  108. }}
  109. displayType={widget.displayType}
  110. isEdit
  111. projects={projects}
  112. />
  113. {contextMenu}
  114. </QueryWrapper>
  115. );
  116. }
  117. const QueryWrapper = styled('div')<{hasSymbol: boolean}>`
  118. display: grid;
  119. gap: ${space(1)};
  120. padding-bottom: ${space(1)};
  121. grid-template-columns: 1fr max-content;
  122. ${p => p.hasSymbol && `grid-template-columns: min-content 1fr max-content;`}
  123. `;
  124. const StyledQuerySymbol = styled(QuerySymbol)<{isClickable: boolean}>`
  125. margin-top: 10px;
  126. ${p => p.isClickable && `cursor: pointer;`}
  127. `;
  128. const Wrapper = styled('div')<{showQuerySymbols: boolean}>``;
  129. const Row = styled('div')`
  130. display: contents;
  131. `;
  132. const ButtonBar = styled('div')<{addQuerySymbolSpacing: boolean}>`
  133. align-items: center;
  134. display: flex;
  135. padding-bottom: ${space(2)};
  136. padding-top: ${space(1)};
  137. gap: ${space(2)};
  138. ${p =>
  139. p.addQuerySymbolSpacing &&
  140. `
  141. padding-left: ${space(1)};
  142. margin-left: ${space(2)};
  143. `}
  144. `;
  145. const SwitchWrapper = styled('label')`
  146. display: flex;
  147. margin: 0;
  148. align-items: center;
  149. gap: ${space(1)};
  150. `;