trendsDiscoverQuery.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {Project} from 'sentry/types';
  2. import GenericDiscoverQuery, {
  3. DiscoverQueryProps,
  4. GenericChildrenProps,
  5. } from 'sentry/utils/discover/genericDiscoverQuery';
  6. import withProjects from 'sentry/utils/withProjects';
  7. import {
  8. TrendChangeType,
  9. TrendFunctionField,
  10. TrendsData,
  11. TrendsDataEvents,
  12. TrendsQuery,
  13. TrendView,
  14. } from 'sentry/views/performance/trends/types';
  15. import {
  16. generateTrendFunctionAsString,
  17. getCurrentTrendFunction,
  18. getCurrentTrendParameter,
  19. } from 'sentry/views/performance/trends/utils';
  20. export type TrendsRequest = {
  21. eventView: Partial<TrendView>;
  22. projects: Project[];
  23. trendChangeType?: TrendChangeType;
  24. trendFunctionField?: TrendFunctionField;
  25. };
  26. type RequestProps = DiscoverQueryProps & TrendsRequest;
  27. export type TrendDiscoveryChildrenProps = Omit<
  28. GenericChildrenProps<TrendsData>,
  29. 'tableData'
  30. > & {
  31. trendsData: TrendsData | null;
  32. };
  33. type Props = RequestProps & {
  34. children: (props: TrendDiscoveryChildrenProps) => React.ReactNode;
  35. };
  36. type EventChildrenProps = Omit<GenericChildrenProps<TrendsDataEvents>, 'tableData'> & {
  37. trendsData: TrendsDataEvents | null;
  38. };
  39. type EventProps = RequestProps & {
  40. children: (props: EventChildrenProps) => React.ReactNode;
  41. };
  42. export function getTrendsRequestPayload(props: RequestProps) {
  43. const {eventView, projects} = props;
  44. const apiPayload: TrendsQuery = eventView?.getEventsAPIPayload(props.location);
  45. const trendFunction = getCurrentTrendFunction(props.location, props.trendFunctionField);
  46. const trendParameter = getCurrentTrendParameter(
  47. props.location,
  48. projects,
  49. eventView.project
  50. );
  51. apiPayload.trendFunction = generateTrendFunctionAsString(
  52. trendFunction.field,
  53. trendParameter.column
  54. );
  55. apiPayload.trendType = eventView?.trendType || props.trendChangeType;
  56. apiPayload.interval = eventView?.interval;
  57. apiPayload.middle = eventView?.middle;
  58. return apiPayload;
  59. }
  60. function TrendsDiscoverQuery(props: Props) {
  61. return (
  62. <GenericDiscoverQuery<TrendsData, TrendsRequest>
  63. {...props}
  64. route="events-trends-stats"
  65. getRequestPayload={getTrendsRequestPayload}
  66. >
  67. {({tableData, ...rest}) => {
  68. return props.children({trendsData: tableData, ...rest});
  69. }}
  70. </GenericDiscoverQuery>
  71. );
  72. }
  73. function EventsDiscoverQuery(props: EventProps) {
  74. return (
  75. <GenericDiscoverQuery<TrendsDataEvents, TrendsRequest>
  76. {...props}
  77. route="events-trends"
  78. getRequestPayload={getTrendsRequestPayload}
  79. >
  80. {({tableData, ...rest}) => {
  81. return props.children({trendsData: tableData, ...rest});
  82. }}
  83. </GenericDiscoverQuery>
  84. );
  85. }
  86. export const TrendsEventsDiscoverQuery = withProjects(EventsDiscoverQuery);
  87. export default withProjects(TrendsDiscoverQuery);