useHasTtfdConfigured.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type {NewQuery} from 'sentry/types/organization';
  2. import EventView from 'sentry/utils/discover/eventView';
  3. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  4. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  5. import {useLocation} from 'sentry/utils/useLocation';
  6. import usePageFilters from 'sentry/utils/usePageFilters';
  7. import {useTableQuery} from 'sentry/views/performance/mobile/screenload/screens/screensTable';
  8. import {useReleaseSelection} from 'sentry/views/starfish/queries/useReleases';
  9. import {appendReleaseFilters} from 'sentry/views/starfish/utils/releaseComparison';
  10. export function useTTFDConfigured(additionalFilters?: string[]) {
  11. const location = useLocation();
  12. const {
  13. primaryRelease,
  14. secondaryRelease,
  15. isLoading: isReleasesLoading,
  16. } = useReleaseSelection();
  17. const {selection} = usePageFilters();
  18. const query = new MutableSearch([
  19. 'event.type:transaction',
  20. 'transaction.op:ui.load',
  21. ...(additionalFilters ?? []),
  22. ]);
  23. const queryString = appendReleaseFilters(query, primaryRelease, secondaryRelease);
  24. const newQuery: NewQuery = {
  25. name: '',
  26. fields: [
  27. `avg(measurements.time_to_initial_display)`,
  28. `avg(measurements.time_to_full_display)`,
  29. 'count()',
  30. ],
  31. query: queryString,
  32. dataset: DiscoverDatasets.METRICS,
  33. version: 2,
  34. projects: selection.projects,
  35. };
  36. const eventView = EventView.fromNewQueryWithLocation(newQuery, location);
  37. const result = useTableQuery({
  38. eventView,
  39. enabled: !isReleasesLoading,
  40. staleTime: Infinity,
  41. });
  42. const hasTTFD: boolean | undefined = result.data?.data?.length
  43. ? !(
  44. result.data.data?.[0]['avg(measurements.time_to_initial_display)'] !== 0 &&
  45. result.data.data?.[0]['avg(measurements.time_to_full_display)'] === 0
  46. )
  47. : undefined;
  48. return {...result, hasTTFD};
  49. }