vitalsCardsDiscoverQuery.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import * as React from 'react';
  2. import pick from 'lodash/pick';
  3. import {MetaType} from 'app/utils/discover/eventView';
  4. import {WebVital} from 'app/utils/discover/fields';
  5. import GenericDiscoverQuery, {
  6. DiscoverQueryPropsWithContext,
  7. GenericChildrenProps,
  8. } from 'app/utils/discover/genericDiscoverQuery';
  9. import {PERFORMANCE_URL_PARAM} from 'app/utils/performance/constants';
  10. import withApi from 'app/utils/withApi';
  11. export type TableDataRow = {
  12. id: string;
  13. [key: string]: React.ReactText;
  14. };
  15. export type TableData = {
  16. data: Array<TableDataRow>;
  17. meta?: MetaType;
  18. };
  19. export type VitalData = {
  20. poor: number;
  21. meh: number;
  22. good: number;
  23. total: number;
  24. p75: number | null;
  25. };
  26. export type VitalsData = Record<string, VitalData>;
  27. type VitalsProps = {
  28. vitals: WebVital[];
  29. };
  30. type RequestProps = DiscoverQueryPropsWithContext & VitalsProps;
  31. type ChildrenProps = Omit<GenericChildrenProps<VitalsProps>, 'tableData'> & {
  32. vitalsData: VitalsData | null;
  33. };
  34. type Props = RequestProps & {
  35. children: (props: ChildrenProps) => React.ReactNode;
  36. };
  37. function getRequestPayload(props: RequestProps) {
  38. const {eventView, vitals} = props;
  39. const apiPayload = eventView?.getEventsAPIPayload(props.location);
  40. return {
  41. vital: vitals,
  42. ...pick(apiPayload, ['query', ...Object.values(PERFORMANCE_URL_PARAM)]),
  43. };
  44. }
  45. function VitalsCardsDiscoverQuery(props: Props) {
  46. return (
  47. <GenericDiscoverQuery<VitalsData, VitalsProps>
  48. getRequestPayload={getRequestPayload}
  49. route="events-vitals"
  50. {...props}
  51. >
  52. {({tableData, ...rest}) => {
  53. return props.children({vitalsData: tableData, ...rest});
  54. }}
  55. </GenericDiscoverQuery>
  56. );
  57. }
  58. export default withApi(VitalsCardsDiscoverQuery);