queries.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. index={index}
  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. projects: selection.projects,
  69. datetime: selection.datetime,
  70. environments: selection.environments,
  71. }}
  72. />
  73. }
  74. />
  75. </Row>
  76. ))}
  77. </Wrapper>
  78. <ButtonBar addQuerySymbolSpacing={showQuerySymbols}>
  79. <Button size="sm" icon={<IconAdd isCircled />} onClick={addWidget}>
  80. Add query
  81. </Button>
  82. <SwitchWrapper>
  83. {t('One chart per query')}
  84. <SwitchButton
  85. isActive={isMultiChartMode}
  86. toggle={() => setIsMultiChartMode(!isMultiChartMode)}
  87. />
  88. </SwitchWrapper>
  89. </ButtonBar>
  90. </Fragment>
  91. );
  92. }
  93. interface Props {
  94. onChange: (data: Partial<MetricWidgetQueryParams>) => void;
  95. projects: number[];
  96. widget: MetricWidgetQueryParams;
  97. contextMenu?: React.ReactNode;
  98. symbol?: React.ReactNode;
  99. }
  100. export function Query({widget, projects, onChange, contextMenu, symbol}: Props) {
  101. return (
  102. <QueryWrapper hasSymbol={!!symbol}>
  103. {symbol}
  104. <QueryBuilder
  105. onChange={onChange}
  106. metricsQuery={{
  107. mri: widget.mri,
  108. op: widget.op,
  109. groupBy: widget.groupBy,
  110. query: widget.query,
  111. }}
  112. displayType={widget.displayType}
  113. isEdit
  114. projects={projects}
  115. />
  116. {contextMenu}
  117. </QueryWrapper>
  118. );
  119. }
  120. const QueryWrapper = styled('div')<{hasSymbol: boolean}>`
  121. display: grid;
  122. gap: ${space(1)};
  123. padding-bottom: ${space(1)};
  124. grid-template-columns: 1fr max-content;
  125. ${p => p.hasSymbol && `grid-template-columns: min-content 1fr max-content;`}
  126. `;
  127. const StyledQuerySymbol = styled(QuerySymbol)<{isClickable: boolean}>`
  128. margin-top: 10px;
  129. ${p => p.isClickable && `cursor: pointer;`}
  130. `;
  131. const Wrapper = styled('div')<{showQuerySymbols: boolean}>`
  132. `;
  133. const Row = styled('div')`
  134. display: contents;
  135. `;
  136. const ButtonBar = styled('div')<{addQuerySymbolSpacing: boolean}>`
  137. align-items: center;
  138. display: flex;
  139. padding-bottom: ${space(2)};
  140. padding-top: ${space(1)};
  141. gap: ${space(2)};
  142. ${p =>
  143. p.addQuerySymbolSpacing &&
  144. `
  145. padding-left: ${space(1)};
  146. margin-left: ${space(2)};
  147. `}
  148. `;
  149. const SwitchWrapper = styled('label')`
  150. display: flex;
  151. margin: 0;
  152. align-items: center;
  153. gap: ${space(1)};
  154. `;