trendsDiscoverQuery.tsx 2.8 KB

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