segmentExplorerQuery.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {EventQuery} from 'sentry/actionCreators/events';
  2. import {LocationQuery} from 'sentry/utils/discover/eventView';
  3. import GenericDiscoverQuery, {
  4. DiscoverQueryProps,
  5. GenericChildrenProps,
  6. } from 'sentry/utils/discover/genericDiscoverQuery';
  7. import withApi from 'sentry/utils/withApi';
  8. /**
  9. * An individual row in a Segment explorer result
  10. */
  11. export type TableDataRow = {
  12. aggregate: number;
  13. comparison: number;
  14. count: number;
  15. frequency: number;
  16. sumdelta: number;
  17. tags_key: string;
  18. tags_value: string;
  19. };
  20. export type TableData = {
  21. data: TableDataRow[];
  22. meta: {};
  23. };
  24. /**
  25. * A Segment Explorer result including rows and metadata.
  26. */
  27. type ChildrenProps = Omit<GenericChildrenProps<TableData>, 'tableData'> & {
  28. tableData: TableData | null;
  29. };
  30. type QueryProps = DiscoverQueryProps & {
  31. aggregateColumn: string;
  32. children: (props: ChildrenProps) => React.ReactNode;
  33. allTagKeys?: boolean;
  34. sort?: string | string[];
  35. tagKey?: string;
  36. };
  37. type FacetQuery = LocationQuery &
  38. EventQuery & {
  39. aggregateColumn?: string;
  40. allTagKeys?: boolean;
  41. sort?: string | string[];
  42. tagKey?: string;
  43. };
  44. export function getRequestFunction(_props: QueryProps) {
  45. const {aggregateColumn} = _props;
  46. function getTagExplorerRequestPayload(props: DiscoverQueryProps) {
  47. const {eventView} = props;
  48. const apiPayload: FacetQuery = eventView.getEventsAPIPayload(props.location);
  49. apiPayload.aggregateColumn = aggregateColumn;
  50. apiPayload.sort = _props.sort ? _props.sort : apiPayload.sort;
  51. if (_props.allTagKeys) {
  52. apiPayload.allTagKeys = _props.allTagKeys;
  53. }
  54. if (_props.tagKey) {
  55. apiPayload.tagKey = _props.tagKey;
  56. }
  57. return apiPayload;
  58. }
  59. return getTagExplorerRequestPayload;
  60. }
  61. function shouldRefetchData(prevProps: QueryProps, nextProps: QueryProps) {
  62. return (
  63. prevProps.aggregateColumn !== nextProps.aggregateColumn ||
  64. prevProps.sort !== nextProps.sort ||
  65. prevProps.allTagKeys !== nextProps.allTagKeys ||
  66. prevProps.tagKey !== nextProps.tagKey
  67. );
  68. }
  69. function SegmentExplorerQuery(props: QueryProps) {
  70. return (
  71. <GenericDiscoverQuery<TableData, QueryProps>
  72. route="events-facets-performance"
  73. getRequestPayload={getRequestFunction(props)}
  74. shouldRefetchData={shouldRefetchData}
  75. {...props}
  76. />
  77. );
  78. }
  79. export default withApi(SegmentExplorerQuery);