queryBuilder.tsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import {Fragment, memo, useCallback, useEffect, useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import type {SelectOption} from 'sentry/components/compactSelect';
  4. import {CompactSelect} from 'sentry/components/compactSelect';
  5. import Tag from 'sentry/components/tag';
  6. import {IconLightning, IconReleases} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {space} from 'sentry/styles/space';
  9. import type {MetricMeta, MetricsOperation, MRI} from 'sentry/types';
  10. import {trackAnalytics} from 'sentry/utils/analytics';
  11. import {
  12. getDefaultMetricDisplayType,
  13. isAllowedOp,
  14. isCustomMetric,
  15. isMeasurement,
  16. isSpanMetric,
  17. isTransactionDuration,
  18. } from 'sentry/utils/metrics';
  19. import {getReadableMetricType} from 'sentry/utils/metrics/formatters';
  20. import {formatMRI} from 'sentry/utils/metrics/mri';
  21. import type {
  22. MetricDisplayType,
  23. MetricsQuery,
  24. MetricWidgetQueryParams,
  25. } from 'sentry/utils/metrics/types';
  26. import {useBreakpoints} from 'sentry/utils/metrics/useBreakpoints';
  27. import {useIncrementQueryMetric} from 'sentry/utils/metrics/useIncrementQueryMetric';
  28. import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  29. import {useMetricsTags} from 'sentry/utils/metrics/useMetricsTags';
  30. import {middleEllipsis} from 'sentry/utils/middleEllipsis';
  31. import useKeyPress from 'sentry/utils/useKeyPress';
  32. import useOrganization from 'sentry/utils/useOrganization';
  33. import usePageFilters from 'sentry/utils/usePageFilters';
  34. import {MetricSearchBar} from 'sentry/views/ddm/metricSearchBar';
  35. type QueryBuilderProps = {
  36. displayType: MetricDisplayType;
  37. isEdit: boolean;
  38. metricsQuery: MetricsQuery;
  39. onChange: (data: Partial<MetricWidgetQueryParams>) => void;
  40. projects: number[];
  41. fixedWidth?: boolean;
  42. powerUserMode?: boolean;
  43. };
  44. const isShownByDefault = (metric: MetricMeta) =>
  45. isCustomMetric(metric) ||
  46. isTransactionDuration(metric) ||
  47. isMeasurement(metric) ||
  48. isSpanMetric(metric);
  49. function getOpsForMRI(mri: MRI, meta: MetricMeta[]) {
  50. return meta.find(metric => metric.mri === mri)?.operations.filter(isAllowedOp) ?? [];
  51. }
  52. export const QueryBuilder = memo(function QueryBuilder({
  53. metricsQuery,
  54. projects,
  55. displayType,
  56. powerUserMode,
  57. onChange,
  58. }: QueryBuilderProps) {
  59. const organization = useOrganization();
  60. const pageFilters = usePageFilters();
  61. const {data: meta} = useMetricsMeta(pageFilters.selection);
  62. const mriModeKeyPressed = useKeyPress('`', undefined, true);
  63. const [mriMode, setMriMode] = useState(powerUserMode); // power user mode that shows raw MRI instead of metrics names
  64. const breakpoints = useBreakpoints();
  65. useEffect(() => {
  66. if (mriModeKeyPressed && !powerUserMode) {
  67. setMriMode(!mriMode);
  68. }
  69. // eslint-disable-next-line react-hooks/exhaustive-deps
  70. }, [mriModeKeyPressed, powerUserMode]);
  71. const {data: tags = [], isLoading: tagsIsLoading} = useMetricsTags(metricsQuery.mri, {
  72. projects,
  73. });
  74. const displayedMetrics = useMemo(() => {
  75. if (mriMode) {
  76. return meta;
  77. }
  78. const isSelected = (metric: MetricMeta) => metric.mri === metricsQuery.mri;
  79. return meta
  80. .filter(metric => isShownByDefault(metric) || isSelected(metric))
  81. .sort(metric => (isSelected(metric) ? -1 : 1));
  82. }, [meta, metricsQuery.mri, mriMode]);
  83. const selectedMeta = useMemo(() => {
  84. return meta.find(metric => metric.mri === metricsQuery.mri);
  85. }, [meta, metricsQuery.mri]);
  86. const incrementQueryMetric = useIncrementQueryMetric({
  87. ...metricsQuery,
  88. displayType,
  89. });
  90. const handleMRIChange = useCallback(
  91. ({value}) => {
  92. const availableOps = getOpsForMRI(value, meta);
  93. const selectedOp = availableOps.includes(
  94. (metricsQuery.op ?? '') as MetricsOperation
  95. )
  96. ? metricsQuery.op
  97. : availableOps?.[0];
  98. const queryChanges = {
  99. mri: value,
  100. op: selectedOp,
  101. groupBy: undefined,
  102. displayType: getDefaultMetricDisplayType(value, selectedOp),
  103. };
  104. trackAnalytics('ddm.widget.metric', {organization});
  105. incrementQueryMetric('ddm.widget.metric', queryChanges);
  106. onChange({
  107. ...queryChanges,
  108. focusedSeries: undefined,
  109. });
  110. },
  111. [incrementQueryMetric, meta, metricsQuery.op, onChange, organization]
  112. );
  113. const handleOpChange = useCallback(
  114. ({value}) => {
  115. trackAnalytics('ddm.widget.operation', {organization});
  116. incrementQueryMetric('ddm.widget.operation', {op: value});
  117. onChange({
  118. op: value,
  119. });
  120. },
  121. [incrementQueryMetric, onChange, organization]
  122. );
  123. const handleGroupByChange = useCallback(
  124. (options: SelectOption<string>[]) => {
  125. trackAnalytics('ddm.widget.group', {organization});
  126. incrementQueryMetric('ddm.widget.group', {
  127. groupBy: options.map(o => o.value),
  128. });
  129. onChange({
  130. groupBy: options.map(o => o.value),
  131. focusedSeries: undefined,
  132. });
  133. },
  134. [incrementQueryMetric, onChange, organization]
  135. );
  136. const handleQueryChange = useCallback(
  137. (query: string) => {
  138. trackAnalytics('ddm.widget.filter', {organization});
  139. incrementQueryMetric('ddm.widget.filter', {query});
  140. onChange({query});
  141. },
  142. [incrementQueryMetric, onChange, organization]
  143. );
  144. const mriOptions = useMemo(
  145. () =>
  146. displayedMetrics.map<SelectOption<MRI>>(metric => ({
  147. label: mriMode ? metric.mri : formatMRI(metric.mri),
  148. // enable search by mri, name, unit (millisecond), type (c:), and readable type (counter)
  149. textValue: `${metric.mri}${getReadableMetricType(metric.type)}`,
  150. value: metric.mri,
  151. trailingItems: mriMode ? undefined : (
  152. <Fragment>
  153. <Tag tooltipText={t('Type')}>{getReadableMetricType(metric.type)}</Tag>
  154. <Tag tooltipText={t('Unit')}>{metric.unit}</Tag>
  155. </Fragment>
  156. ),
  157. })),
  158. [displayedMetrics, mriMode]
  159. );
  160. return (
  161. <QueryBuilderWrapper>
  162. <FlexBlock>
  163. <MetricSelect
  164. searchable
  165. sizeLimit={100}
  166. size="md"
  167. triggerLabel={middleEllipsis(
  168. formatMRI(metricsQuery.mri) ?? '',
  169. breakpoints.large ? (breakpoints.xlarge ? 70 : 45) : 30,
  170. /\.|-|_/
  171. )}
  172. placeholder={t('Select a metric')}
  173. options={mriOptions}
  174. value={metricsQuery.mri}
  175. onChange={handleMRIChange}
  176. shouldUseVirtualFocus
  177. />
  178. <FlexBlock>
  179. <OpSelect
  180. size="md"
  181. triggerProps={{prefix: t('Op')}}
  182. options={
  183. selectedMeta?.operations.filter(isAllowedOp).map(op => ({
  184. label: op,
  185. value: op,
  186. })) ?? []
  187. }
  188. disabled={!metricsQuery.mri}
  189. value={metricsQuery.op}
  190. onChange={handleOpChange}
  191. />
  192. <CompactSelect
  193. multiple
  194. size="md"
  195. triggerProps={{prefix: t('Group by')}}
  196. options={tags.map(tag => ({
  197. label: tag.key,
  198. value: tag.key,
  199. trailingItems: (
  200. <Fragment>
  201. {tag.key === 'release' && <IconReleases size="xs" />}
  202. {tag.key === 'transaction' && <IconLightning size="xs" />}
  203. </Fragment>
  204. ),
  205. }))}
  206. disabled={!metricsQuery.mri || tagsIsLoading}
  207. value={metricsQuery.groupBy}
  208. onChange={handleGroupByChange}
  209. />
  210. </FlexBlock>
  211. </FlexBlock>
  212. <SearchBarWrapper>
  213. <MetricSearchBar
  214. mri={metricsQuery.mri}
  215. disabled={!metricsQuery.mri}
  216. onChange={handleQueryChange}
  217. query={metricsQuery.query}
  218. />
  219. </SearchBarWrapper>
  220. </QueryBuilderWrapper>
  221. );
  222. });
  223. const QueryBuilderWrapper = styled('div')`
  224. display: flex;
  225. flex-grow: 1;
  226. gap: ${space(1)};
  227. flex-wrap: wrap;
  228. `;
  229. const FlexBlock = styled('div')`
  230. display: flex;
  231. gap: ${space(1)};
  232. flex-wrap: wrap;
  233. `;
  234. const MetricSelect = styled(CompactSelect)`
  235. min-width: 200px;
  236. & > button {
  237. width: 100%;
  238. }
  239. `;
  240. const OpSelect = styled(CompactSelect)`
  241. width: 120px;
  242. & > button {
  243. width: 100%;
  244. }
  245. `;
  246. const SearchBarWrapper = styled('div')`
  247. flex: 1;
  248. min-width: 200px;
  249. `;