getSeriesEventView.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import sortBy from 'lodash/sortBy';
  2. import type {PageFilters} from 'sentry/types/core';
  3. import EventView from 'sentry/utils/discover/eventView';
  4. import {parseFunction} from 'sentry/utils/discover/fields';
  5. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  6. import {intervalToMilliseconds} from 'sentry/utils/duration/intervalToMilliseconds';
  7. import type {MutableSearch} from 'sentry/utils/tokenizeSearch';
  8. import {getIntervalForMetricFunction} from 'sentry/views/performance/database/getIntervalForMetricFunction';
  9. import {DEFAULT_INTERVAL} from 'sentry/views/performance/database/settings';
  10. export function getSeriesEventView(
  11. search: MutableSearch | undefined,
  12. fields: string[] = [],
  13. pageFilters: PageFilters,
  14. yAxis: string[],
  15. topEvents?: number,
  16. dataset?: DiscoverDatasets
  17. ) {
  18. // Pick the highest possible interval for the given yAxis selection. Find the ideal interval for each function, then choose the largest one. This results in the lowest granularity, but best performance.
  19. const interval = sortBy(
  20. yAxis.map(yAxisFunctionName => {
  21. const parseResult = parseFunction(yAxisFunctionName);
  22. if (!parseResult) {
  23. return DEFAULT_INTERVAL;
  24. }
  25. return getIntervalForMetricFunction(parseResult.name, pageFilters.datetime);
  26. }),
  27. result => {
  28. return intervalToMilliseconds(result);
  29. }
  30. ).at(-1);
  31. return EventView.fromNewQueryWithPageFilters(
  32. {
  33. name: '',
  34. query: search?.formatString() ?? undefined,
  35. fields,
  36. yAxis,
  37. dataset: dataset || DiscoverDatasets.SPANS_METRICS,
  38. interval,
  39. topEvents: topEvents?.toString(),
  40. version: 2,
  41. },
  42. pageFilters
  43. );
  44. }