histogramQuery.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import {Fragment} from 'react';
  2. import omit from 'lodash/omit';
  3. import GenericDiscoverQuery, {
  4. DiscoverQueryProps,
  5. GenericChildrenProps,
  6. } from 'sentry/utils/discover/genericDiscoverQuery';
  7. import {DataFilter, HistogramData} from 'sentry/utils/performance/histogram/types';
  8. type Histograms = Record<string, HistogramData>;
  9. type HistogramProps = {
  10. fields: string[];
  11. numBuckets: number;
  12. dataFilter?: DataFilter;
  13. didReceiveMultiAxis?: (axisCounts: Record<string, number>) => void;
  14. max?: number;
  15. min?: number;
  16. precision?: number;
  17. };
  18. type RequestProps = DiscoverQueryProps & HistogramProps;
  19. export type HistogramQueryChildrenProps = Omit<
  20. GenericChildrenProps<HistogramProps>,
  21. 'tableData'
  22. > & {
  23. histograms: Histograms | null;
  24. };
  25. type Props = RequestProps & {
  26. children: (props: HistogramQueryChildrenProps) => React.ReactNode;
  27. };
  28. function getHistogramRequestPayload(props: RequestProps) {
  29. const {fields, numBuckets, min, max, precision, dataFilter, eventView, location} =
  30. props;
  31. const baseApiPayload = {
  32. field: fields,
  33. numBuckets,
  34. min,
  35. max,
  36. precision,
  37. dataFilter,
  38. };
  39. const additionalApiPayload = omit(eventView.getEventsAPIPayload(location), [
  40. 'field',
  41. 'sort',
  42. 'per_page',
  43. ]);
  44. const apiPayload = Object.assign(baseApiPayload, additionalApiPayload);
  45. return apiPayload;
  46. }
  47. function HistogramQuery(props: Props) {
  48. const {children, fields, didReceiveMultiAxis} = props;
  49. function didFetch(data: Histograms) {
  50. if (didReceiveMultiAxis) {
  51. const counts: Record<string, number> = {};
  52. Object.entries(data).forEach(
  53. ([key, values]) =>
  54. (counts[key] = values.length
  55. ? values.reduce((prev, curr) => prev + curr.count, 0)
  56. : 0)
  57. );
  58. didReceiveMultiAxis(counts);
  59. }
  60. }
  61. if (fields.length === 0) {
  62. return (
  63. <Fragment>
  64. {children({
  65. isLoading: false,
  66. error: null,
  67. pageLinks: null,
  68. histograms: {},
  69. })}
  70. </Fragment>
  71. );
  72. }
  73. return (
  74. <GenericDiscoverQuery<Histograms, HistogramProps>
  75. route="events-histogram"
  76. getRequestPayload={getHistogramRequestPayload}
  77. didFetch={didFetch}
  78. {...omit(props, 'children')}
  79. >
  80. {({tableData, ...rest}) => {
  81. return props.children({histograms: tableData, ...rest});
  82. }}
  83. </GenericDiscoverQuery>
  84. );
  85. }
  86. export default HistogramQuery;