trendsDiscoverQuery.tsx 2.8 KB

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