histogramQuery.tsx 2.4 KB

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