metricSearchBar.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import memoize from 'lodash/memoize';
  4. import type {SearchConfig} from 'sentry/components/searchSyntax/parser';
  5. import {
  6. BooleanOperator,
  7. FilterType,
  8. joinQuery,
  9. parseSearch,
  10. Token,
  11. } from 'sentry/components/searchSyntax/parser';
  12. import {treeTransformer} from 'sentry/components/searchSyntax/utils';
  13. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  14. import SmartSearchBar from 'sentry/components/smartSearchBar';
  15. import {t} from 'sentry/locale';
  16. import type {MRI, TagCollection} from 'sentry/types';
  17. import {SavedSearchType} from 'sentry/types';
  18. import {getUseCaseFromMRI} from 'sentry/utils/metrics/mri';
  19. import {useMetricsTags} from 'sentry/utils/metrics/useMetricsTags';
  20. import useApi from 'sentry/utils/useApi';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import usePageFilters from 'sentry/utils/usePageFilters';
  23. interface MetricSearchBarProps extends Partial<SmartSearchBarProps> {
  24. onChange: (value: string) => void;
  25. projectIds: string[];
  26. disabled?: boolean;
  27. mri?: MRI;
  28. query?: string;
  29. }
  30. const EMPTY_ARRAY = [];
  31. const EMPTY_SET = new Set<never>();
  32. const DISSALLOWED_LOGICAL_OPERATORS = new Set([BooleanOperator.OR]);
  33. export function ensureQuotedTextFilters(
  34. query: string,
  35. configOverrides?: Partial<SearchConfig>
  36. ) {
  37. const parsedSearch = parseSearch(query, configOverrides);
  38. if (!parsedSearch) {
  39. return query;
  40. }
  41. const newTree = treeTransformer({
  42. tree: parsedSearch,
  43. transform: token => {
  44. if (token.type === Token.FILTER && token.filter === FilterType.TEXT) {
  45. if (!token.value.quoted) {
  46. return {
  47. ...token,
  48. // joinQuery() does not access nested tokens, so we need to manipulate the text of the filter instead of it's value
  49. text: `${token.key.text}:"${token.value.text}"`,
  50. };
  51. }
  52. }
  53. return token;
  54. },
  55. });
  56. return joinQuery(newTree);
  57. }
  58. export function MetricSearchBar({
  59. mri,
  60. disabled,
  61. onChange,
  62. query,
  63. projectIds,
  64. ...props
  65. }: MetricSearchBarProps) {
  66. const org = useOrganization();
  67. const api = useApi();
  68. const {selection} = usePageFilters();
  69. const projectIdNumbers = useMemo(
  70. () => projectIds.map(id => parseInt(id, 10)),
  71. [projectIds]
  72. );
  73. const {data: tags = EMPTY_ARRAY, isLoading} = useMetricsTags(mri, projectIdNumbers);
  74. const supportedTags: TagCollection = useMemo(
  75. () => tags.reduce((acc, tag) => ({...acc, [tag.key]: tag}), {}),
  76. [tags]
  77. );
  78. const searchConfig = useMemo(
  79. () => ({
  80. booleanKeys: EMPTY_SET,
  81. dateKeys: EMPTY_SET,
  82. durationKeys: EMPTY_SET,
  83. numericKeys: EMPTY_SET,
  84. percentageKeys: EMPTY_SET,
  85. sizeKeys: EMPTY_SET,
  86. textOperatorKeys: EMPTY_SET,
  87. supportedTags,
  88. disallowedLogicalOperators: DISSALLOWED_LOGICAL_OPERATORS,
  89. disallowFreeText: true,
  90. }),
  91. [supportedTags]
  92. );
  93. const fetchTagValues = useMemo(() => {
  94. const fn = memoize((tagKey: string) => {
  95. // clear response from cache after 10 seconds
  96. setTimeout(() => {
  97. fn.cache.delete(tagKey);
  98. }, 10000);
  99. return api.requestPromise(`/organizations/${org.slug}/metrics/tags/${tagKey}/`, {
  100. query: {
  101. metric: mri,
  102. useCase: getUseCaseFromMRI(mri),
  103. project: selection.projects,
  104. },
  105. });
  106. });
  107. return fn;
  108. }, [api, mri, org.slug, selection.projects]);
  109. const getTagValues = useCallback(
  110. async (tag: any, search: string) => {
  111. const tagsValues = await fetchTagValues(tag.key);
  112. return tagsValues
  113. .filter(
  114. tv =>
  115. tv.value !== '' &&
  116. tv.value.toLocaleLowerCase().includes(search.toLocaleLowerCase())
  117. )
  118. .map(tv => tv.value);
  119. },
  120. [fetchTagValues]
  121. );
  122. const handleChange = useCallback(
  123. (value: string, {validSearch} = {validSearch: true}) => {
  124. if (!validSearch) {
  125. return;
  126. }
  127. onChange(ensureQuotedTextFilters(value, searchConfig));
  128. },
  129. [onChange, searchConfig]
  130. );
  131. return (
  132. <WideSearchBar
  133. disabled={disabled}
  134. maxMenuHeight={220}
  135. organization={org}
  136. onGetTagValues={getTagValues}
  137. // don't highlight tags while loading as we don't know yet if they are supported
  138. highlightUnsupportedTags={!isLoading}
  139. onClose={handleChange}
  140. onSearch={handleChange}
  141. placeholder={t('Filter by tags')}
  142. query={query}
  143. savedSearchType={SavedSearchType.METRIC}
  144. {...searchConfig}
  145. {...props}
  146. />
  147. );
  148. }
  149. const WideSearchBar = styled(SmartSearchBar)`
  150. width: 100%;
  151. opacity: ${p => (p.disabled ? '0.6' : '1')};
  152. `;