projectBaseSessionsChart.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. import {Component, Fragment} from 'react';
  2. import type {Theme} from '@emotion/react';
  3. import {useTheme} from '@emotion/react';
  4. import type {LegendComponentOption, LineSeriesOption} from 'echarts';
  5. import isEqual from 'lodash/isEqual';
  6. import type {Client} from 'sentry/api';
  7. import {BarChart} from 'sentry/components/charts/barChart';
  8. import type {ZoomRenderProps} from 'sentry/components/charts/chartZoom';
  9. import ChartZoom from 'sentry/components/charts/chartZoom';
  10. import ErrorPanel from 'sentry/components/charts/errorPanel';
  11. import type {LineChartProps} from 'sentry/components/charts/lineChart';
  12. import {LineChart} from 'sentry/components/charts/lineChart';
  13. import ReleaseSeries from 'sentry/components/charts/releaseSeries';
  14. import StackedAreaChart from 'sentry/components/charts/stackedAreaChart';
  15. import {HeaderTitleLegend} from 'sentry/components/charts/styles';
  16. import TransitionChart from 'sentry/components/charts/transitionChart';
  17. import TransparentLoadingMask from 'sentry/components/charts/transparentLoadingMask';
  18. import {RELEASE_LINES_THRESHOLD} from 'sentry/components/charts/utils';
  19. import QuestionTooltip from 'sentry/components/questionTooltip';
  20. import {IconWarning} from 'sentry/icons';
  21. import {t} from 'sentry/locale';
  22. import type {PageFilters} from 'sentry/types/core';
  23. import type {EChartEventHandler, Series} from 'sentry/types/echarts';
  24. import type {Organization} from 'sentry/types/organization';
  25. import getDynamicText from 'sentry/utils/getDynamicText';
  26. import {MINUTES_THRESHOLD_TO_DISPLAY_SECONDS} from 'sentry/utils/sessions';
  27. import withPageFilters from 'sentry/utils/withPageFilters';
  28. import {displayCrashFreePercent} from 'sentry/views/releases/utils';
  29. import {sessionTerm} from 'sentry/views/releases/utils/sessionTerm';
  30. import {DisplayModes} from '../projectCharts';
  31. import ProjectSessionsAnrRequest from './projectSessionsAnrRequest';
  32. import ProjectSessionsChartRequest from './projectSessionsChartRequest';
  33. type Props = {
  34. api: Client;
  35. displayMode:
  36. | DisplayModes.SESSIONS
  37. | DisplayModes.STABILITY_USERS
  38. | DisplayModes.ANR_RATE
  39. | DisplayModes.FOREGROUND_ANR_RATE
  40. | DisplayModes.STABILITY;
  41. onTotalValuesChange: (value: number | null) => void;
  42. organization: Organization;
  43. selection: PageFilters;
  44. title: string;
  45. disablePrevious?: boolean;
  46. help?: string;
  47. query?: string;
  48. };
  49. function ProjectBaseSessionsChart({
  50. title,
  51. organization,
  52. selection,
  53. api,
  54. onTotalValuesChange,
  55. displayMode,
  56. help,
  57. disablePrevious,
  58. query,
  59. }: Props) {
  60. const theme = useTheme();
  61. const {projects, environments, datetime} = selection;
  62. const {start, end, period, utc} = datetime;
  63. const Request = [DisplayModes.ANR_RATE, DisplayModes.FOREGROUND_ANR_RATE].includes(
  64. displayMode
  65. )
  66. ? ProjectSessionsAnrRequest
  67. : ProjectSessionsChartRequest;
  68. return (
  69. <Fragment>
  70. {getDynamicText({
  71. value: (
  72. <ChartZoom period={period} start={start} end={end} utc={utc}>
  73. {zoomRenderProps => (
  74. <Request
  75. api={api}
  76. selection={selection}
  77. organization={organization}
  78. onTotalValuesChange={onTotalValuesChange}
  79. displayMode={displayMode}
  80. disablePrevious={disablePrevious}
  81. query={query}
  82. >
  83. {({
  84. errored,
  85. loading,
  86. reloading,
  87. timeseriesData,
  88. previousTimeseriesData,
  89. additionalSeries,
  90. }) => (
  91. <ReleaseSeries
  92. utc={utc}
  93. period={period}
  94. start={start}
  95. end={end}
  96. projects={projects}
  97. environments={environments}
  98. query={query}
  99. >
  100. {({releaseSeries}) => {
  101. if (errored) {
  102. return (
  103. <ErrorPanel>
  104. <IconWarning color="gray300" size="lg" />
  105. </ErrorPanel>
  106. );
  107. }
  108. return (
  109. <TransitionChart loading={loading} reloading={reloading}>
  110. <TransparentLoadingMask visible={reloading} />
  111. <HeaderTitleLegend>
  112. {title}
  113. {help && (
  114. <QuestionTooltip size="sm" position="top" title={help} />
  115. )}
  116. </HeaderTitleLegend>
  117. <Chart
  118. theme={theme}
  119. zoomRenderProps={zoomRenderProps}
  120. reloading={reloading}
  121. timeSeries={timeseriesData}
  122. previousTimeSeries={
  123. previousTimeseriesData
  124. ? [previousTimeseriesData]
  125. : undefined
  126. }
  127. releaseSeries={releaseSeries}
  128. displayMode={displayMode}
  129. additionalSeries={additionalSeries}
  130. />
  131. </TransitionChart>
  132. );
  133. }}
  134. </ReleaseSeries>
  135. )}
  136. </Request>
  137. )}
  138. </ChartZoom>
  139. ),
  140. fixed: `${title} Chart`,
  141. })}
  142. </Fragment>
  143. );
  144. }
  145. type ChartProps = {
  146. displayMode:
  147. | DisplayModes.SESSIONS
  148. | DisplayModes.STABILITY
  149. | DisplayModes.STABILITY_USERS
  150. | DisplayModes.ANR_RATE
  151. | DisplayModes.FOREGROUND_ANR_RATE;
  152. releaseSeries: Series[];
  153. reloading: boolean;
  154. theme: Theme;
  155. timeSeries: Series[];
  156. zoomRenderProps: ZoomRenderProps;
  157. additionalSeries?: LineSeriesOption[];
  158. previousTimeSeries?: Series[];
  159. };
  160. type ChartState = {
  161. forceUpdate: boolean;
  162. seriesSelection: Record<string, boolean>;
  163. };
  164. class Chart extends Component<ChartProps, ChartState> {
  165. state: ChartState = {
  166. seriesSelection: {},
  167. forceUpdate: false,
  168. };
  169. shouldComponentUpdate(nextProps: ChartProps, nextState: ChartState) {
  170. if (nextState.forceUpdate) {
  171. return true;
  172. }
  173. if (!isEqual(this.state.seriesSelection, nextState.seriesSelection)) {
  174. return true;
  175. }
  176. if (
  177. nextProps.releaseSeries !== this.props.releaseSeries &&
  178. !nextProps.reloading &&
  179. !this.props.reloading
  180. ) {
  181. return true;
  182. }
  183. if (this.props.reloading && !nextProps.reloading) {
  184. return true;
  185. }
  186. if (nextProps.timeSeries !== this.props.timeSeries) {
  187. return true;
  188. }
  189. return false;
  190. }
  191. // inspired by app/components/charts/eventsChart.tsx@handleLegendSelectChanged
  192. handleLegendSelectChanged: EChartEventHandler<{
  193. name: string;
  194. selected: Record<string, boolean>;
  195. type: 'legendselectchanged';
  196. }> = ({selected}) => {
  197. const seriesSelection = Object.keys(selected).reduce((state, key) => {
  198. state[key] = selected[key];
  199. return state;
  200. }, {});
  201. // we have to force an update here otherwise ECharts will
  202. // update its internal state and disable the series
  203. this.setState({seriesSelection, forceUpdate: true}, () =>
  204. this.setState({forceUpdate: false})
  205. );
  206. };
  207. get isCrashFree() {
  208. const {displayMode} = this.props;
  209. return [DisplayModes.STABILITY, DisplayModes.STABILITY_USERS].includes(displayMode);
  210. }
  211. get isAnr() {
  212. const {displayMode} = this.props;
  213. return [DisplayModes.ANR_RATE, DisplayModes.FOREGROUND_ANR_RATE].includes(
  214. displayMode
  215. );
  216. }
  217. get legend(): LegendComponentOption {
  218. const {theme, timeSeries, previousTimeSeries, releaseSeries, additionalSeries} =
  219. this.props;
  220. const {seriesSelection} = this.state;
  221. const hideReleasesByDefault =
  222. (releaseSeries[0] as any)?.markLine?.data.length >= RELEASE_LINES_THRESHOLD;
  223. const hideHealthyByDefault = timeSeries
  224. .filter(s => sessionTerm.healthy !== s.seriesName)
  225. .some(s => s.data.some(d => d.value > 0));
  226. const selected =
  227. Object.keys(seriesSelection).length === 0 &&
  228. (hideReleasesByDefault || hideHealthyByDefault)
  229. ? {
  230. [t('Releases')]: !hideReleasesByDefault,
  231. [sessionTerm.healthy]: !hideHealthyByDefault,
  232. }
  233. : seriesSelection;
  234. return {
  235. right: 10,
  236. top: 0,
  237. icon: 'circle',
  238. itemHeight: 8,
  239. itemWidth: 8,
  240. itemGap: 12,
  241. align: 'left' as const,
  242. textStyle: {
  243. color: theme.textColor,
  244. verticalAlign: 'top',
  245. fontSize: 11,
  246. fontFamily: theme.text.family,
  247. },
  248. data: [
  249. ...timeSeries.map(s => s.seriesName),
  250. ...(previousTimeSeries ?? []).map(s => s.seriesName),
  251. ...(additionalSeries ?? []).map(s => s.name?.toString() ?? ''),
  252. ...releaseSeries.map(s => s.seriesName),
  253. ],
  254. selected,
  255. };
  256. }
  257. get chartOptions(): Omit<LineChartProps, 'series'> {
  258. return {
  259. grid: {left: '10px', right: '10px', top: '40px', bottom: '0px'},
  260. seriesOptions: {
  261. showSymbol: false,
  262. },
  263. tooltip: {
  264. trigger: 'axis',
  265. truncate: 80,
  266. valueFormatter: (value: number | null) => {
  267. if (value === null) {
  268. return '\u2014';
  269. }
  270. if (this.isCrashFree) {
  271. return displayCrashFreePercent(value, 0, 3);
  272. }
  273. if (this.isAnr) {
  274. return displayCrashFreePercent(value, 0, 3, false);
  275. }
  276. return typeof value === 'number' ? value.toLocaleString() : value;
  277. },
  278. },
  279. yAxis: this.isCrashFree
  280. ? {
  281. axisLabel: {
  282. formatter: (value: number) => displayCrashFreePercent(value),
  283. },
  284. scale: true,
  285. max: 100,
  286. }
  287. : this.isAnr
  288. ? {
  289. axisLabel: {
  290. formatter: (value: number) => displayCrashFreePercent(value, 0, 3, false),
  291. },
  292. scale: true,
  293. }
  294. : {min: 0},
  295. };
  296. }
  297. render() {
  298. const {
  299. zoomRenderProps,
  300. timeSeries,
  301. previousTimeSeries,
  302. releaseSeries,
  303. additionalSeries,
  304. } = this.props;
  305. const ChartComponent = this.isCrashFree
  306. ? LineChart
  307. : this.isAnr
  308. ? BarChart
  309. : StackedAreaChart;
  310. return (
  311. <ChartComponent
  312. {...zoomRenderProps}
  313. {...this.chartOptions}
  314. legend={this.legend}
  315. series={
  316. Array.isArray(releaseSeries) && !this.isAnr
  317. ? [...timeSeries, ...releaseSeries]
  318. : timeSeries
  319. }
  320. additionalSeries={additionalSeries}
  321. previousPeriod={previousTimeSeries}
  322. onLegendSelectChanged={this.handleLegendSelectChanged}
  323. minutesThresholdToDisplaySeconds={MINUTES_THRESHOLD_TO_DISPLAY_SECONDS}
  324. transformSinglePointToBar
  325. />
  326. );
  327. }
  328. }
  329. export default withPageFilters(ProjectBaseSessionsChart);