discoverQuery.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import {EventsMetaType, MetaType} from 'sentry/utils/discover/eventView';
  2. import {TransactionThresholdMetric} from 'sentry/views/performance/transactionSummary/transactionThresholdModal';
  3. import GenericDiscoverQuery, {
  4. DiscoverQueryProps,
  5. GenericChildrenProps,
  6. useGenericDiscoverQuery,
  7. } from './genericDiscoverQuery';
  8. /**
  9. * An individual row in a DiscoverQuery result
  10. */
  11. export type TableDataRow = {
  12. [key: string]: React.ReactText;
  13. id: string;
  14. };
  15. /**
  16. * A DiscoverQuery result including rows and metadata.
  17. */
  18. export type TableData = {
  19. data: Array<TableDataRow>;
  20. meta?: MetaType;
  21. };
  22. /**
  23. * A DiscoverQuery result including rows and metadata from the events endpoint.
  24. */
  25. export type EventsTableData = {
  26. data: Array<TableDataRow>;
  27. meta?: EventsMetaType;
  28. };
  29. export type TableDataWithTitle = TableData & {title: string};
  30. export type DiscoverQueryPropsWithThresholds = DiscoverQueryProps & {
  31. transactionName?: string;
  32. transactionThreshold?: number;
  33. transactionThresholdMetric?: TransactionThresholdMetric;
  34. };
  35. export type DiscoverQueryComponentProps = DiscoverQueryPropsWithThresholds & {
  36. children: (props: GenericChildrenProps<TableData>) => React.ReactNode;
  37. };
  38. function shouldRefetchData(
  39. prevProps: DiscoverQueryPropsWithThresholds,
  40. nextProps: DiscoverQueryPropsWithThresholds
  41. ) {
  42. return (
  43. prevProps.transactionName !== nextProps.transactionName ||
  44. prevProps.transactionThreshold !== nextProps.transactionThreshold ||
  45. prevProps.transactionThresholdMetric !== nextProps.transactionThresholdMetric
  46. );
  47. }
  48. function DiscoverQuery(props: DiscoverQueryComponentProps) {
  49. const afterFetch = (data, _) => {
  50. const {fields, ...otherMeta} = data.meta ?? {};
  51. return {
  52. ...data,
  53. meta: {...fields, ...otherMeta},
  54. };
  55. };
  56. return (
  57. <GenericDiscoverQuery<TableData, DiscoverQueryPropsWithThresholds>
  58. route="events"
  59. shouldRefetchData={shouldRefetchData}
  60. afterFetch={afterFetch}
  61. {...props}
  62. />
  63. );
  64. }
  65. export function useDiscoverQuery(props: Omit<DiscoverQueryComponentProps, 'children'>) {
  66. const afterFetch = (data, _) => {
  67. const {fields, ...otherMeta} = data.meta ?? {};
  68. return {
  69. ...data,
  70. meta: {...fields, ...otherMeta},
  71. };
  72. };
  73. const res = useGenericDiscoverQuery<TableData, DiscoverQueryPropsWithThresholds>({
  74. route: 'events',
  75. shouldRefetchData,
  76. afterFetch,
  77. ...props,
  78. });
  79. const pageLinks = res.response?.getResponseHeader('Link') ?? undefined;
  80. return {...res, pageLinks};
  81. }
  82. export default DiscoverQuery;