segmentExplorerQuery.tsx 2.3 KB

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