metricSearchBar.tsx 4.5 KB

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