resultsChart.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import {Component, Fragment} from 'react';
  2. import {InjectedRouter} 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 AreaChart from 'app/components/charts/areaChart';
  8. import EventsChart from 'app/components/charts/eventsChart';
  9. import {getParams} from 'app/components/organizations/globalSelectionHeader/getParams';
  10. import {Panel} from 'app/components/panels';
  11. import Placeholder from 'app/components/placeholder';
  12. import {t} from 'app/locale';
  13. import {Organization} from 'app/types';
  14. import {getUtcToLocalDateObject} from 'app/utils/dates';
  15. import EventView from 'app/utils/discover/eventView';
  16. import {DisplayModes, TOP_N} from 'app/utils/discover/types';
  17. import getDynamicText from 'app/utils/getDynamicText';
  18. import {decodeScalar} from 'app/utils/queryString';
  19. import withApi from 'app/utils/withApi';
  20. import ChartFooter from './chartFooter';
  21. type ResultsChartProps = {
  22. api: Client;
  23. router: InjectedRouter;
  24. organization: Organization;
  25. eventView: EventView;
  26. location: Location;
  27. confirmedQuery: boolean;
  28. yAxisValue: string[];
  29. };
  30. class ResultsChart extends Component<ResultsChartProps> {
  31. shouldComponentUpdate(nextProps: ResultsChartProps) {
  32. const {eventView, ...restProps} = this.props;
  33. const {eventView: nextEventView, ...restNextProps} = nextProps;
  34. if (!eventView.isEqualTo(nextEventView)) {
  35. return true;
  36. }
  37. return !isEqual(restProps, restNextProps);
  38. }
  39. render() {
  40. const {api, eventView, location, organization, router, confirmedQuery, yAxisValue} =
  41. this.props;
  42. const hasPerformanceChartInterpolation = organization.features.includes(
  43. 'performance-chart-interpolation'
  44. );
  45. const hasConnectDiscoverAndDashboards = organization.features.includes(
  46. 'connect-discover-and-dashboards'
  47. );
  48. const globalSelection = eventView.getGlobalSelection();
  49. const start = globalSelection.datetime.start
  50. ? getUtcToLocalDateObject(globalSelection.datetime.start)
  51. : null;
  52. const end = globalSelection.datetime.end
  53. ? getUtcToLocalDateObject(globalSelection.datetime.end)
  54. : null;
  55. const {utc} = getParams(location.query);
  56. const apiPayload = eventView.getEventsAPIPayload(location);
  57. const display = eventView.getDisplayMode();
  58. const isTopEvents =
  59. display === DisplayModes.TOP5 || display === DisplayModes.DAILYTOP5;
  60. const isPeriod = display === DisplayModes.DEFAULT || display === DisplayModes.TOP5;
  61. const isDaily = display === DisplayModes.DAILYTOP5 || display === DisplayModes.DAILY;
  62. const isPrevious = display === DisplayModes.PREVIOUS;
  63. return (
  64. <Fragment>
  65. {getDynamicText({
  66. value: (
  67. <EventsChart
  68. api={api}
  69. router={router}
  70. query={apiPayload.query}
  71. organization={organization}
  72. showLegend
  73. yAxis={yAxisValue}
  74. projects={globalSelection.projects}
  75. environments={globalSelection.environments}
  76. start={start}
  77. end={end}
  78. period={globalSelection.datetime.period}
  79. disablePrevious={!isPrevious}
  80. disableReleases={!isPeriod}
  81. field={isTopEvents ? apiPayload.field : undefined}
  82. interval={eventView.interval}
  83. showDaily={isDaily}
  84. topEvents={isTopEvents ? TOP_N : undefined}
  85. orderby={isTopEvents ? decodeScalar(apiPayload.sort) : undefined}
  86. utc={utc === 'true'}
  87. confirmedQuery={confirmedQuery}
  88. withoutZerofill={hasPerformanceChartInterpolation}
  89. chartComponent={
  90. hasConnectDiscoverAndDashboards && !isDaily ? AreaChart : undefined
  91. }
  92. />
  93. ),
  94. fixed: <Placeholder height="200px" testId="skeleton-ui" />,
  95. })}
  96. </Fragment>
  97. );
  98. }
  99. }
  100. type ContainerProps = {
  101. api: Client;
  102. router: InjectedRouter;
  103. eventView: EventView;
  104. location: Location;
  105. organization: Organization;
  106. confirmedQuery: boolean;
  107. yAxis: string[];
  108. // chart footer props
  109. total: number | null;
  110. onAxisChange: (value: string[]) => void;
  111. onDisplayChange: (value: string) => void;
  112. };
  113. class ResultsChartContainer extends Component<ContainerProps> {
  114. shouldComponentUpdate(nextProps: ContainerProps) {
  115. const {eventView, ...restProps} = this.props;
  116. const {eventView: nextEventView, ...restNextProps} = nextProps;
  117. if (
  118. !eventView.isEqualTo(nextEventView) ||
  119. this.props.confirmedQuery !== nextProps.confirmedQuery
  120. ) {
  121. return true;
  122. }
  123. return !isEqual(restProps, restNextProps);
  124. }
  125. render() {
  126. const {
  127. api,
  128. eventView,
  129. location,
  130. router,
  131. total,
  132. onAxisChange,
  133. onDisplayChange,
  134. organization,
  135. confirmedQuery,
  136. yAxis,
  137. } = this.props;
  138. const hasQueryFeature = organization.features.includes('discover-query');
  139. const hasConnectDiscoverAndDashboards = organization.features.includes(
  140. 'connect-discover-and-dashboards'
  141. );
  142. const displayOptions = eventView
  143. .getDisplayOptions()
  144. .filter(opt => {
  145. // top5 modes are only available with larger packages in saas.
  146. // We remove instead of disable here as showing tooltips in dropdown
  147. // menus is clunky.
  148. if (
  149. [DisplayModes.TOP5, DisplayModes.DAILYTOP5].includes(
  150. opt.value as DisplayModes
  151. ) &&
  152. !hasQueryFeature
  153. ) {
  154. return false;
  155. }
  156. return true;
  157. })
  158. .map(opt => {
  159. // Can only use default display or total daily with multi y axis
  160. if (
  161. yAxis.length > 1 &&
  162. ![DisplayModes.DEFAULT, DisplayModes.DAILY].includes(opt.value as DisplayModes)
  163. ) {
  164. return {
  165. ...opt,
  166. disabled: true,
  167. tooltip: t(
  168. 'Change the Y-Axis dropdown to display only 1 function to use this view.'
  169. ),
  170. };
  171. }
  172. return opt;
  173. });
  174. const yAxisValue = hasConnectDiscoverAndDashboards ? yAxis : [eventView.getYAxis()];
  175. return (
  176. <StyledPanel>
  177. {(yAxisValue.length > 0 && (
  178. <ResultsChart
  179. api={api}
  180. eventView={eventView}
  181. location={location}
  182. organization={organization}
  183. router={router}
  184. confirmedQuery={confirmedQuery}
  185. yAxisValue={yAxisValue}
  186. />
  187. )) || <NoChartContainer>{t('No Y-Axis selected.')}</NoChartContainer>}
  188. <ChartFooter
  189. organization={organization}
  190. total={total}
  191. yAxisValue={yAxisValue}
  192. yAxisOptions={eventView.getYAxisOptions()}
  193. onAxisChange={onAxisChange}
  194. displayOptions={displayOptions}
  195. displayMode={eventView.getDisplayMode()}
  196. onDisplayChange={onDisplayChange}
  197. />
  198. </StyledPanel>
  199. );
  200. }
  201. }
  202. export default withApi(ResultsChartContainer);
  203. const StyledPanel = styled(Panel)`
  204. @media (min-width: ${p => p.theme.breakpoints[1]}) {
  205. margin: 0;
  206. }
  207. `;
  208. const NoChartContainer = styled('div')<{height?: string}>`
  209. display: flex;
  210. flex-direction: column;
  211. justify-content: center;
  212. align-items: center;
  213. flex: 1;
  214. flex-shrink: 0;
  215. overflow: hidden;
  216. height: ${p => p.height || '200px'};
  217. position: relative;
  218. border-color: transparent;
  219. margin-bottom: 0;
  220. color: ${p => p.theme.gray300};
  221. font-size: ${p => p.theme.fontSizeExtraLarge};
  222. `;