index.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {
  2. FilterType,
  3. joinQuery,
  4. parseSearch,
  5. type SearchConfig,
  6. Token,
  7. } from 'sentry/components/searchSyntax/parser';
  8. import {
  9. MetricSeriesFilterUpdateType,
  10. type MetricsQuery,
  11. } from 'sentry/utils/metrics/types';
  12. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  13. import {addToFilter, excludeFromFilter} from '../../discover/table/cellAction';
  14. /**
  15. * Updates query string with tag values from the passed groupBys.
  16. */
  17. export function extendQueryWithGroupBys(
  18. query: string,
  19. groupBys: (Record<string, string> | undefined)[]
  20. ) {
  21. const mutableSearch = new MutableSearch(query);
  22. groupBys.forEach(groupBy => {
  23. if (!groupBy) {
  24. return;
  25. }
  26. Object.entries(groupBy).forEach(([key, value]) => {
  27. if (!value) {
  28. return;
  29. }
  30. addToFilter(mutableSearch, key, value);
  31. });
  32. });
  33. return mutableSearch.formatString();
  34. }
  35. /**
  36. * Wraps text filters of a search string in quotes if they are not already.
  37. */
  38. export function ensureQuotedTextFilters(
  39. query: string,
  40. configOverrides?: Partial<SearchConfig>
  41. ) {
  42. const parsedSearch = parseSearch(query, configOverrides);
  43. if (!parsedSearch) {
  44. return query;
  45. }
  46. for (let i = 0; i < parsedSearch.length; i++) {
  47. const token = parsedSearch[i];
  48. if (token.type === Token.FILTER && token.filter === FilterType.TEXT) {
  49. // joinQuery() does not access nested tokens, so we need to manipulate the text of the filter instead of it's value
  50. if (!token.value.quoted) {
  51. token.text = `${token.negated ? '!' : ''}${token.key.text}:"${token.value.text}"`;
  52. }
  53. const spaceToken = parsedSearch[i + 1];
  54. const afterSpaceToken = parsedSearch[i + 2];
  55. if (
  56. spaceToken &&
  57. afterSpaceToken &&
  58. spaceToken.type === Token.SPACES &&
  59. spaceToken.text === '' &&
  60. afterSpaceToken.type === Token.FILTER
  61. ) {
  62. // Ensure there is a space between two filters
  63. spaceToken.text = ' ';
  64. }
  65. }
  66. }
  67. return joinQuery(parsedSearch);
  68. }
  69. /**
  70. * Used when a user clicks on filter button in the series summary table. Applies
  71. * tag values to the filter string of the query. Removes the tags from query groupyBy
  72. */
  73. export function updateQueryWithSeriesFilter(
  74. query: MetricsQuery,
  75. groupBys: Record<string, string>,
  76. updateType: MetricSeriesFilterUpdateType
  77. ) {
  78. // TODO(metrics): This is a temporary solution to handle the case where the query has OR operator.
  79. // since addToFilter and excludeFromFilter do not handle it properly. We should refactor this to use
  80. // search syntax parser instead.
  81. const queryStr = query.query?.includes('OR') ? `(${query.query})` : query.query ?? '';
  82. const mutableSearch = new MutableSearch(queryStr);
  83. const groupByEntries = Object.entries(groupBys);
  84. groupByEntries.forEach(([key, value]) => {
  85. if (!value) {
  86. return;
  87. }
  88. updateType === MetricSeriesFilterUpdateType.ADD
  89. ? addToFilter(mutableSearch, key, value)
  90. : excludeFromFilter(mutableSearch, key, value);
  91. });
  92. const extendedQuery = mutableSearch.formatString();
  93. const newGroupBy = (query.groupBy ?? []).filter(tag => !groupBys[tag]);
  94. return {
  95. ...query,
  96. query: ensureQuotedTextFilters(extendedQuery),
  97. groupBy: newGroupBy,
  98. };
  99. }