discoverQuery.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. type DiscoverQueryPropsWithThresholds = DiscoverQueryProps & {
  31. transactionName?: string;
  32. transactionThreshold?: number;
  33. transactionThresholdMetric?: TransactionThresholdMetric;
  34. };
  35. type DiscoverQueryComponentProps = DiscoverQueryPropsWithThresholds & {
  36. children: (props: GenericChildrenProps<TableData>) => React.ReactNode;
  37. useEvents?: boolean;
  38. };
  39. function shouldRefetchData(
  40. prevProps: DiscoverQueryPropsWithThresholds,
  41. nextProps: DiscoverQueryPropsWithThresholds
  42. ) {
  43. return (
  44. prevProps.transactionName !== nextProps.transactionName ||
  45. prevProps.transactionThreshold !== nextProps.transactionThreshold ||
  46. prevProps.transactionThresholdMetric !== nextProps.transactionThresholdMetric
  47. );
  48. }
  49. function DiscoverQuery(props: DiscoverQueryComponentProps) {
  50. const endpoint = props.useEvents ? 'events' : 'eventsv2';
  51. const afterFetch = props.useEvents
  52. ? (data, _) => {
  53. const {fields, ...otherMeta} = data.meta ?? {};
  54. return {
  55. ...data,
  56. meta: {...fields, ...otherMeta},
  57. };
  58. }
  59. : undefined;
  60. return (
  61. <GenericDiscoverQuery<TableData, DiscoverQueryPropsWithThresholds>
  62. route={endpoint}
  63. shouldRefetchData={shouldRefetchData}
  64. afterFetch={afterFetch}
  65. {...props}
  66. />
  67. );
  68. }
  69. export function useDiscoverQuery(props: Omit<DiscoverQueryComponentProps, 'children'>) {
  70. const endpoint = props.useEvents ? 'events' : 'eventsv2';
  71. const afterFetch = props.useEvents
  72. ? (data, _) => {
  73. const {fields, ...otherMeta} = data.meta ?? {};
  74. return {
  75. ...data,
  76. meta: {...fields, ...otherMeta},
  77. };
  78. }
  79. : undefined;
  80. return useGenericDiscoverQuery<TableData, DiscoverQueryPropsWithThresholds>({
  81. route: endpoint,
  82. shouldRefetchData,
  83. afterFetch,
  84. ...props,
  85. });
  86. }
  87. export default DiscoverQuery;