resultsChart.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {Component, Fragment} from 'react';
  2. import * as ReactRouter from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location} from 'history';
  5. import isEqual from 'lodash/isEqual';
  6. import {Client} from 'app/api';
  7. import EventsChart from 'app/components/charts/eventsChart';
  8. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  9. import {Panel} from 'app/components/panels';
  10. import Placeholder from 'app/components/placeholder';
  11. import {Organization} from 'app/types';
  12. import {getUtcToLocalDateObject} from 'app/utils/dates';
  13. import EventView from 'app/utils/discover/eventView';
  14. import {DisplayModes, TOP_N} from 'app/utils/discover/types';
  15. import getDynamicText from 'app/utils/getDynamicText';
  16. import {decodeScalar} from 'app/utils/queryString';
  17. import withApi from 'app/utils/withApi';
  18. import ChartFooter from './chartFooter';
  19. type ResultsChartProps = {
  20. api: Client;
  21. router: ReactRouter.InjectedRouter;
  22. organization: Organization;
  23. eventView: EventView;
  24. location: Location;
  25. confirmedQuery: boolean;
  26. };
  27. class ResultsChart extends Component<ResultsChartProps> {
  28. shouldComponentUpdate(nextProps: ResultsChartProps) {
  29. const {eventView, ...restProps} = this.props;
  30. const {eventView: nextEventView, ...restNextProps} = nextProps;
  31. if (!eventView.isEqualTo(nextEventView)) {
  32. return true;
  33. }
  34. return !isEqual(restProps, restNextProps);
  35. }
  36. render() {
  37. const {api, eventView, location, organization, router, confirmedQuery} = this.props;
  38. const yAxisValue = eventView.getYAxis();
  39. const globalSelection = eventView.getGlobalSelection();
  40. const start = globalSelection.datetime.start
  41. ? getUtcToLocalDateObject(globalSelection.datetime.start)
  42. : null;
  43. const end = globalSelection.datetime.end
  44. ? getUtcToLocalDateObject(globalSelection.datetime.end)
  45. : null;
  46. const {utc} = getParams(location.query);
  47. const apiPayload = eventView.getEventsAPIPayload(location);
  48. const display = eventView.getDisplayMode();
  49. const isTopEvents =
  50. display === DisplayModes.TOP5 || display === DisplayModes.DAILYTOP5;
  51. const isPeriod = display === DisplayModes.DEFAULT || display === DisplayModes.TOP5;
  52. const isDaily = display === DisplayModes.DAILYTOP5 || display === DisplayModes.DAILY;
  53. const isPrevious = display === DisplayModes.PREVIOUS;
  54. return (
  55. <Fragment>
  56. {getDynamicText({
  57. value: (
  58. <EventsChart
  59. api={api}
  60. router={router}
  61. query={apiPayload.query}
  62. organization={organization}
  63. showLegend
  64. yAxis={yAxisValue}
  65. projects={globalSelection.projects}
  66. environments={globalSelection.environments}
  67. start={start}
  68. end={end}
  69. period={globalSelection.datetime.period}
  70. disablePrevious={!isPrevious}
  71. disableReleases={!isPeriod}
  72. field={isTopEvents ? apiPayload.field : undefined}
  73. interval={eventView.interval}
  74. showDaily={isDaily}
  75. topEvents={isTopEvents ? TOP_N : undefined}
  76. orderby={isTopEvents ? decodeScalar(apiPayload.sort) : undefined}
  77. utc={utc === 'true'}
  78. confirmedQuery={confirmedQuery}
  79. />
  80. ),
  81. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  82. })}
  83. </Fragment>
  84. );
  85. }
  86. }
  87. type ContainerProps = {
  88. api: Client;
  89. router: ReactRouter.InjectedRouter;
  90. eventView: EventView;
  91. location: Location;
  92. organization: Organization;
  93. confirmedQuery: boolean;
  94. // chart footer props
  95. total: number | null;
  96. onAxisChange: (value: string) => void;
  97. onDisplayChange: (value: string) => void;
  98. };
  99. class ResultsChartContainer extends Component<ContainerProps> {
  100. shouldComponentUpdate(nextProps: ContainerProps) {
  101. const {eventView, ...restProps} = this.props;
  102. const {eventView: nextEventView, ...restNextProps} = nextProps;
  103. if (
  104. !eventView.isEqualTo(nextEventView) ||
  105. this.props.confirmedQuery !== nextProps.confirmedQuery
  106. ) {
  107. return true;
  108. }
  109. return !isEqual(restProps, restNextProps);
  110. }
  111. render() {
  112. const {
  113. api,
  114. eventView,
  115. location,
  116. router,
  117. total,
  118. onAxisChange,
  119. onDisplayChange,
  120. organization,
  121. confirmedQuery,
  122. } = this.props;
  123. const yAxisValue = eventView.getYAxis();
  124. const hasQueryFeature = organization.features.includes('discover-query');
  125. const displayOptions = eventView.getDisplayOptions().filter(opt => {
  126. // top5 modes are only available with larger packages in saas.
  127. // We remove instead of disable here as showing tooltips in dropdown
  128. // menus is clunky.
  129. if (
  130. [DisplayModes.TOP5, DisplayModes.DAILYTOP5].includes(opt.value as DisplayModes) &&
  131. !hasQueryFeature
  132. ) {
  133. return false;
  134. }
  135. return true;
  136. });
  137. return (
  138. <StyledPanel>
  139. <ResultsChart
  140. api={api}
  141. eventView={eventView}
  142. location={location}
  143. organization={organization}
  144. router={router}
  145. confirmedQuery={confirmedQuery}
  146. />
  147. <ChartFooter
  148. total={total}
  149. yAxisValue={yAxisValue}
  150. yAxisOptions={eventView.getYAxisOptions()}
  151. onAxisChange={onAxisChange}
  152. displayOptions={displayOptions}
  153. displayMode={eventView.getDisplayMode()}
  154. onDisplayChange={onDisplayChange}
  155. />
  156. </StyledPanel>
  157. );
  158. }
  159. }
  160. export default withApi(ResultsChartContainer);
  161. export const StyledPanel = styled(Panel)`
  162. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  163. margin: 0;
  164. }
  165. `;