metricSearchBar.tsx 4.5 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. 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. disabled?: boolean;
  25. mri?: MRI;
  26. projectIds?: string[];
  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, {
  72. ...selection,
  73. projects: projectIdNumbers,
  74. });
  75. const supportedTags: TagCollection = useMemo(
  76. () => tags.reduce((acc, tag) => ({...acc, [tag.key]: tag}), {}),
  77. [tags]
  78. );
  79. const searchConfig = useMemo(
  80. () => ({
  81. booleanKeys: EMPTY_SET,
  82. dateKeys: EMPTY_SET,
  83. durationKeys: EMPTY_SET,
  84. numericKeys: EMPTY_SET,
  85. percentageKeys: EMPTY_SET,
  86. sizeKeys: EMPTY_SET,
  87. textOperatorKeys: EMPTY_SET,
  88. supportedTags,
  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. `;