useApplicationMetrics.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import EventView from 'sentry/utils/discover/eventView';
  2. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  3. import usePageFilters from 'sentry/utils/usePageFilters';
  4. import {getDateFilters} from 'sentry/views/starfish/utils/dates';
  5. import {getDateQueryFilter} from 'sentry/views/starfish/utils/getDateQueryFilter';
  6. import {useSpansQuery} from 'sentry/views/starfish/utils/useSpansQuery';
  7. export type ApplicationMetrics = {
  8. count: number;
  9. 'sum(span.duration)': number;
  10. };
  11. export const useApplicationMetrics = (_referrer = 'application-metrics') => {
  12. const pageFilters = usePageFilters();
  13. const {startTime, endTime} = getDateFilters(pageFilters);
  14. const dateFilters = getDateQueryFilter(startTime, endTime);
  15. const query = getQuery(dateFilters);
  16. const eventView = getEventView();
  17. // TODO: Add referrer
  18. const {isLoading, data} = useSpansQuery<ApplicationMetrics[]>({
  19. eventView,
  20. queryString: query,
  21. initialData: [],
  22. enabled: Boolean(query),
  23. });
  24. return {isLoading, data: data[0] ?? {}};
  25. };
  26. function getQuery(dateFilters: string) {
  27. return `
  28. SELECT
  29. count() as count,
  30. sum(exclusive_time) as "sum(span.duration)"
  31. FROM spans_experimental_starfish
  32. WHERE 1 = 1
  33. ${dateFilters}
  34. `;
  35. }
  36. function getEventView() {
  37. return EventView.fromSavedQuery({
  38. name: '',
  39. fields: ['sum(span.duration)'],
  40. dataset: DiscoverDatasets.SPANS_METRICS,
  41. projects: [1],
  42. version: 2,
  43. });
  44. }