index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {
  2. MetricSeriesFilterUpdateType,
  3. type MetricsQuery,
  4. } from 'sentry/utils/metrics/types';
  5. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  6. import {addToFilter, excludeFromFilter} from '../../discover/table/cellAction';
  7. /**
  8. * Updates query string with tag values from the passed groupBys.
  9. */
  10. export function extendQueryWithGroupBys(
  11. query: string,
  12. groupBys: (Record<string, string> | undefined)[]
  13. ) {
  14. const mutableSearch = new MutableSearch(query);
  15. groupBys.forEach(groupBy => {
  16. if (!groupBy) {
  17. return;
  18. }
  19. Object.entries(groupBy).forEach(([key, value]) => {
  20. if (!value) {
  21. return;
  22. }
  23. addToFilter(mutableSearch, key, value);
  24. });
  25. });
  26. return mutableSearch.formatString();
  27. }
  28. /**
  29. * Used when a user clicks on filter button in the series summary table. Applies
  30. * tag values to the filter string of the query. Removes the tags from query groupyBy
  31. */
  32. export function updateQueryWithSeriesFilter(
  33. query: MetricsQuery,
  34. groupBys: Record<string, string>,
  35. updateType: MetricSeriesFilterUpdateType
  36. ) {
  37. // TODO(metrics): This is a temporary solution to handle the case where the query has OR operator.
  38. // since addToFilter and excludeFromFilter do not handle it properly. We should refactor this to use
  39. // search syntax parser instead.
  40. const queryStr = query.query?.includes('OR') ? `(${query.query})` : query.query ?? '';
  41. const mutableSearch = new MutableSearch(queryStr);
  42. const groupByEntries = Object.entries(groupBys);
  43. groupByEntries.forEach(([key, value]) => {
  44. if (!value) {
  45. return;
  46. }
  47. updateType === MetricSeriesFilterUpdateType.ADD
  48. ? addToFilter(mutableSearch, key, value)
  49. : excludeFromFilter(mutableSearch, key, value);
  50. });
  51. const extendedQuery = mutableSearch.formatString();
  52. const newGroupBy = (query.groupBy ?? []).filter(tag => !groupBys[tag]);
  53. return {
  54. ...query,
  55. query: extendedQuery,
  56. groupBy: newGroupBy,
  57. };
  58. }