projectCharts.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. import {Component, Fragment} from 'react';
  2. import {browserHistory, InjectedRouter} from 'react-router';
  3. import {Theme, withTheme} from '@emotion/react';
  4. import {Location} from 'history';
  5. import {Client} from 'sentry/api';
  6. import {BarChart} from 'sentry/components/charts/barChart';
  7. import LoadingPanel from 'sentry/components/charts/loadingPanel';
  8. import OptionSelector from 'sentry/components/charts/optionSelector';
  9. import {
  10. ChartContainer,
  11. ChartControls,
  12. InlineContainer,
  13. SectionHeading,
  14. SectionValue,
  15. } from 'sentry/components/charts/styles';
  16. import {
  17. getDiffInMinutes,
  18. ONE_HOUR,
  19. ONE_WEEK,
  20. TWENTY_FOUR_HOURS,
  21. TWO_WEEKS,
  22. } from 'sentry/components/charts/utils';
  23. import {Panel} from 'sentry/components/panels';
  24. import Placeholder from 'sentry/components/placeholder';
  25. import {CHART_PALETTE} from 'sentry/constants/chartPalette';
  26. import {NOT_AVAILABLE_MESSAGES} from 'sentry/constants/notAvailableMessages';
  27. import {t} from 'sentry/locale';
  28. import {Organization, Project, SelectValue} from 'sentry/types';
  29. import {defined} from 'sentry/utils';
  30. import {trackAnalytics} from 'sentry/utils/analytics';
  31. import {decodeScalar} from 'sentry/utils/queryString';
  32. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  33. import withApi from 'sentry/utils/withApi';
  34. import {
  35. getSessionTermDescription,
  36. SessionTerm,
  37. } from 'sentry/views/releases/utils/sessionTerm';
  38. import {getTermHelp, PERFORMANCE_TERM} from '../performance/data';
  39. import ProjectBaseEventsChart from './charts/projectBaseEventsChart';
  40. import ProjectBaseSessionsChart from './charts/projectBaseSessionsChart';
  41. import ProjectErrorsBasicChart from './charts/projectErrorsBasicChart';
  42. export enum DisplayModes {
  43. APDEX = 'apdex',
  44. FAILURE_RATE = 'failure_rate',
  45. TPM = 'tpm',
  46. ERRORS = 'errors',
  47. TRANSACTIONS = 'transactions',
  48. STABILITY = 'crash_free',
  49. STABILITY_USERS = 'crash_free_users',
  50. ANR_RATE = 'anr_rate',
  51. FOREGROUND_ANR_RATE = 'foreground_anr_rate',
  52. SESSIONS = 'sessions',
  53. }
  54. type Props = {
  55. api: Client;
  56. chartId: string;
  57. chartIndex: number;
  58. hasSessions: boolean | null;
  59. hasTransactions: boolean;
  60. location: Location;
  61. organization: Organization;
  62. router: InjectedRouter;
  63. theme: Theme;
  64. visibleCharts: string[];
  65. project?: Project;
  66. projectId?: string;
  67. query?: string;
  68. };
  69. type State = {
  70. totalValues: number | null;
  71. };
  72. class ProjectCharts extends Component<Props, State> {
  73. state: State = {
  74. totalValues: null,
  75. };
  76. get defaultDisplayModes() {
  77. const {hasSessions, hasTransactions, organization, project} = this.props;
  78. if (!hasSessions && !hasTransactions) {
  79. return [DisplayModes.ERRORS];
  80. }
  81. if (hasSessions && !hasTransactions) {
  82. if (organization.features.includes('anr-rate') && project?.platform === 'android') {
  83. return [DisplayModes.STABILITY, DisplayModes.ANR_RATE];
  84. }
  85. return [DisplayModes.STABILITY, DisplayModes.ERRORS];
  86. }
  87. if (!hasSessions && hasTransactions) {
  88. return [DisplayModes.FAILURE_RATE, DisplayModes.APDEX];
  89. }
  90. if (organization.features.includes('anr-rate') && project?.platform === 'android') {
  91. return [DisplayModes.STABILITY, DisplayModes.ANR_RATE];
  92. }
  93. return [DisplayModes.STABILITY, DisplayModes.APDEX];
  94. }
  95. get otherActiveDisplayModes() {
  96. const {location, visibleCharts, chartId} = this.props;
  97. return visibleCharts
  98. .filter(visibleChartId => visibleChartId !== chartId)
  99. .map(urlKey => {
  100. return decodeScalar(
  101. location.query[urlKey],
  102. this.defaultDisplayModes[visibleCharts.findIndex(value => value === urlKey)]
  103. );
  104. });
  105. }
  106. get displayMode() {
  107. const {location, chartId, chartIndex} = this.props;
  108. const displayMode =
  109. decodeScalar(location.query[chartId]) || this.defaultDisplayModes[chartIndex];
  110. if (!Object.values(DisplayModes).includes(displayMode as DisplayModes)) {
  111. return this.defaultDisplayModes[chartIndex];
  112. }
  113. return displayMode;
  114. }
  115. get displayModes(): SelectValue<string>[] {
  116. const {organization, hasSessions, hasTransactions, project} = this.props;
  117. const hasPerformance = organization.features.includes('performance-view');
  118. const noPerformanceTooltip = NOT_AVAILABLE_MESSAGES.performance;
  119. const noHealthTooltip = NOT_AVAILABLE_MESSAGES.releaseHealth;
  120. const options = [
  121. {
  122. value: DisplayModes.STABILITY,
  123. label: t('Crash Free Sessions'),
  124. disabled:
  125. this.otherActiveDisplayModes.includes(DisplayModes.STABILITY) || !hasSessions,
  126. tooltip: !hasSessions ? noHealthTooltip : undefined,
  127. },
  128. {
  129. value: DisplayModes.STABILITY_USERS,
  130. label: t('Crash Free Users'),
  131. disabled:
  132. this.otherActiveDisplayModes.includes(DisplayModes.STABILITY_USERS) ||
  133. !hasSessions,
  134. tooltip: !hasSessions ? noHealthTooltip : undefined,
  135. },
  136. {
  137. value: DisplayModes.APDEX,
  138. label: t('Apdex'),
  139. disabled:
  140. this.otherActiveDisplayModes.includes(DisplayModes.APDEX) ||
  141. !hasPerformance ||
  142. !hasTransactions,
  143. tooltip:
  144. hasPerformance && hasTransactions
  145. ? getTermHelp(organization, PERFORMANCE_TERM.APDEX)
  146. : noPerformanceTooltip,
  147. },
  148. {
  149. value: DisplayModes.FAILURE_RATE,
  150. label: t('Failure Rate'),
  151. disabled:
  152. this.otherActiveDisplayModes.includes(DisplayModes.FAILURE_RATE) ||
  153. !hasPerformance ||
  154. !hasTransactions,
  155. tooltip:
  156. hasPerformance && hasTransactions
  157. ? getTermHelp(organization, PERFORMANCE_TERM.FAILURE_RATE)
  158. : noPerformanceTooltip,
  159. },
  160. {
  161. value: DisplayModes.TPM,
  162. label: t('Transactions Per Minute'),
  163. disabled:
  164. this.otherActiveDisplayModes.includes(DisplayModes.TPM) ||
  165. !hasPerformance ||
  166. !hasTransactions,
  167. tooltip:
  168. hasPerformance && hasTransactions
  169. ? getTermHelp(organization, PERFORMANCE_TERM.TPM)
  170. : noPerformanceTooltip,
  171. },
  172. {
  173. value: DisplayModes.ERRORS,
  174. label: t('Number of Errors'),
  175. disabled: this.otherActiveDisplayModes.includes(DisplayModes.ERRORS),
  176. },
  177. {
  178. value: DisplayModes.SESSIONS,
  179. label: t('Number of Sessions'),
  180. disabled:
  181. this.otherActiveDisplayModes.includes(DisplayModes.SESSIONS) || !hasSessions,
  182. tooltip: !hasSessions ? noHealthTooltip : undefined,
  183. },
  184. {
  185. value: DisplayModes.TRANSACTIONS,
  186. label: t('Number of Transactions'),
  187. disabled:
  188. this.otherActiveDisplayModes.includes(DisplayModes.TRANSACTIONS) ||
  189. !hasPerformance ||
  190. !hasTransactions,
  191. tooltip: hasPerformance && hasTransactions ? undefined : noPerformanceTooltip,
  192. },
  193. ];
  194. if (organization.features.includes('anr-rate') && project?.platform === 'android') {
  195. return [
  196. {
  197. value: DisplayModes.ANR_RATE,
  198. label: t('ANR Rate'),
  199. disabled:
  200. this.otherActiveDisplayModes.includes(DisplayModes.ANR_RATE) || !hasSessions,
  201. tooltip: !hasSessions ? noHealthTooltip : undefined,
  202. },
  203. {
  204. value: DisplayModes.FOREGROUND_ANR_RATE,
  205. label: t('Foreground ANR Rate'),
  206. disabled:
  207. this.otherActiveDisplayModes.includes(DisplayModes.FOREGROUND_ANR_RATE) ||
  208. !hasSessions,
  209. tooltip: !hasSessions ? noHealthTooltip : undefined,
  210. },
  211. ...options,
  212. ];
  213. }
  214. return options;
  215. }
  216. get summaryHeading() {
  217. switch (this.displayMode) {
  218. case DisplayModes.ERRORS:
  219. return t('Total Errors');
  220. case DisplayModes.STABILITY:
  221. case DisplayModes.SESSIONS:
  222. return t('Total Sessions');
  223. case DisplayModes.STABILITY_USERS:
  224. case DisplayModes.ANR_RATE:
  225. case DisplayModes.FOREGROUND_ANR_RATE:
  226. return t('Total Users');
  227. case DisplayModes.APDEX:
  228. case DisplayModes.FAILURE_RATE:
  229. case DisplayModes.TPM:
  230. case DisplayModes.TRANSACTIONS:
  231. default:
  232. return t('Total Transactions');
  233. }
  234. }
  235. get barChartInterval() {
  236. const {query} = this.props.location;
  237. const diffInMinutes = getDiffInMinutes({
  238. ...query,
  239. period: decodeScalar(query.statsPeriod),
  240. });
  241. if (diffInMinutes >= TWO_WEEKS) {
  242. return '1d';
  243. }
  244. if (diffInMinutes >= ONE_WEEK) {
  245. return '12h';
  246. }
  247. if (diffInMinutes > TWENTY_FOUR_HOURS) {
  248. return '6h';
  249. }
  250. if (diffInMinutes === TWENTY_FOUR_HOURS) {
  251. return '1h';
  252. }
  253. if (diffInMinutes <= ONE_HOUR) {
  254. return '1m';
  255. }
  256. return '15m';
  257. }
  258. handleDisplayModeChange = (value: string) => {
  259. const {location, chartId, chartIndex, organization} = this.props;
  260. trackAnalytics('project_detail.change_chart', {
  261. organization,
  262. metric: value,
  263. chart_index: chartIndex,
  264. });
  265. browserHistory.push({
  266. pathname: location.pathname,
  267. query: {...location.query, [chartId]: value},
  268. });
  269. };
  270. handleTotalValuesChange = (value: number | null) => {
  271. if (value !== this.state.totalValues) {
  272. this.setState({totalValues: value});
  273. }
  274. };
  275. render() {
  276. const {
  277. api,
  278. router,
  279. location,
  280. organization,
  281. theme,
  282. projectId,
  283. hasSessions,
  284. query,
  285. project,
  286. } = this.props;
  287. const {totalValues} = this.state;
  288. const hasDiscover = organization.features.includes('discover-basic');
  289. const displayMode = this.displayMode;
  290. const hasAnrRateFeature =
  291. organization.features.includes('anr-rate') && project?.platform === 'android';
  292. return (
  293. <Panel>
  294. <ChartContainer>
  295. {!defined(hasSessions) ? (
  296. <LoadingPanel />
  297. ) : (
  298. <Fragment>
  299. {displayMode === DisplayModes.APDEX && (
  300. <ProjectBaseEventsChart
  301. title={t('Apdex')}
  302. help={getTermHelp(organization, PERFORMANCE_TERM.APDEX)}
  303. query={new MutableSearch([
  304. 'event.type:transaction',
  305. query ?? '',
  306. ]).formatString()}
  307. yAxis="apdex()"
  308. field={['apdex()']}
  309. api={api}
  310. router={router}
  311. organization={organization}
  312. onTotalValuesChange={this.handleTotalValuesChange}
  313. colors={[CHART_PALETTE[0][0], theme.purple200]}
  314. />
  315. )}
  316. {displayMode === DisplayModes.FAILURE_RATE && (
  317. <ProjectBaseEventsChart
  318. title={t('Failure Rate')}
  319. help={getTermHelp(organization, PERFORMANCE_TERM.FAILURE_RATE)}
  320. query={new MutableSearch([
  321. 'event.type:transaction',
  322. query ?? '',
  323. ]).formatString()}
  324. yAxis="failure_rate()"
  325. field={[`failure_rate()`]}
  326. api={api}
  327. router={router}
  328. organization={organization}
  329. onTotalValuesChange={this.handleTotalValuesChange}
  330. colors={[theme.red300, theme.purple200]}
  331. />
  332. )}
  333. {displayMode === DisplayModes.TPM && (
  334. <ProjectBaseEventsChart
  335. title={t('Transactions Per Minute')}
  336. help={getTermHelp(organization, PERFORMANCE_TERM.TPM)}
  337. query={new MutableSearch([
  338. 'event.type:transaction',
  339. query ?? '',
  340. ]).formatString()}
  341. yAxis="tpm()"
  342. field={[`tpm()`]}
  343. api={api}
  344. router={router}
  345. organization={organization}
  346. onTotalValuesChange={this.handleTotalValuesChange}
  347. colors={[theme.yellow300, theme.purple200]}
  348. disablePrevious
  349. />
  350. )}
  351. {displayMode === DisplayModes.ERRORS &&
  352. (hasDiscover ? (
  353. <ProjectBaseEventsChart
  354. title={t('Number of Errors')}
  355. query={new MutableSearch([
  356. '!event.type:transaction',
  357. query ?? '',
  358. ]).formatString()}
  359. yAxis="count()"
  360. field={[`count()`]}
  361. api={api}
  362. router={router}
  363. organization={organization}
  364. onTotalValuesChange={this.handleTotalValuesChange}
  365. colors={[theme.purple300, theme.purple200]}
  366. interval={this.barChartInterval}
  367. chartComponent={BarChart}
  368. disableReleases
  369. />
  370. ) : (
  371. <ProjectErrorsBasicChart
  372. organization={organization}
  373. projectId={projectId}
  374. location={location}
  375. onTotalValuesChange={this.handleTotalValuesChange}
  376. />
  377. ))}
  378. {displayMode === DisplayModes.TRANSACTIONS && (
  379. <ProjectBaseEventsChart
  380. title={t('Number of Transactions')}
  381. query={new MutableSearch([
  382. 'event.type:transaction',
  383. query ?? '',
  384. ]).formatString()}
  385. yAxis="count()"
  386. field={[`count()`]}
  387. api={api}
  388. router={router}
  389. organization={organization}
  390. onTotalValuesChange={this.handleTotalValuesChange}
  391. colors={[theme.gray200, theme.purple200]}
  392. interval={this.barChartInterval}
  393. chartComponent={BarChart}
  394. disableReleases
  395. />
  396. )}
  397. {displayMode === DisplayModes.STABILITY && (
  398. <ProjectBaseSessionsChart
  399. title={t('Crash Free Sessions')}
  400. help={getSessionTermDescription(SessionTerm.STABILITY, null)}
  401. router={router}
  402. api={api}
  403. organization={organization}
  404. onTotalValuesChange={this.handleTotalValuesChange}
  405. displayMode={displayMode}
  406. query={query}
  407. />
  408. )}
  409. {hasAnrRateFeature && displayMode === DisplayModes.ANR_RATE && (
  410. <ProjectBaseSessionsChart
  411. title={t('ANR Rate')}
  412. help={getSessionTermDescription(SessionTerm.ANR_RATE, null)}
  413. router={router}
  414. api={api}
  415. organization={organization}
  416. onTotalValuesChange={this.handleTotalValuesChange}
  417. displayMode={displayMode}
  418. query={query}
  419. />
  420. )}
  421. {hasAnrRateFeature && displayMode === DisplayModes.FOREGROUND_ANR_RATE && (
  422. <ProjectBaseSessionsChart
  423. title={t('Foreground ANR Rate')}
  424. help={getSessionTermDescription(SessionTerm.FOREGROUND_ANR_RATE, null)}
  425. router={router}
  426. api={api}
  427. organization={organization}
  428. onTotalValuesChange={this.handleTotalValuesChange}
  429. displayMode={displayMode}
  430. query={query}
  431. />
  432. )}
  433. {displayMode === DisplayModes.STABILITY_USERS && (
  434. <ProjectBaseSessionsChart
  435. title={t('Crash Free Users')}
  436. help={getSessionTermDescription(SessionTerm.CRASH_FREE_USERS, null)}
  437. router={router}
  438. api={api}
  439. organization={organization}
  440. onTotalValuesChange={this.handleTotalValuesChange}
  441. displayMode={displayMode}
  442. query={query}
  443. />
  444. )}
  445. {displayMode === DisplayModes.SESSIONS && (
  446. <ProjectBaseSessionsChart
  447. title={t('Number of Sessions')}
  448. router={router}
  449. api={api}
  450. organization={organization}
  451. onTotalValuesChange={this.handleTotalValuesChange}
  452. displayMode={displayMode}
  453. disablePrevious
  454. query={query}
  455. />
  456. )}
  457. </Fragment>
  458. )}
  459. </ChartContainer>
  460. <ChartControls>
  461. {/* if hasSessions is not yet defined, it means that request is still in progress and we can't decide what default chart to show */}
  462. {defined(hasSessions) ? (
  463. <Fragment>
  464. <InlineContainer>
  465. <SectionHeading>{this.summaryHeading}</SectionHeading>
  466. <SectionValue>
  467. {typeof totalValues === 'number'
  468. ? totalValues.toLocaleString()
  469. : '\u2014'}
  470. </SectionValue>
  471. </InlineContainer>
  472. <InlineContainer>
  473. <OptionSelector
  474. title={t('Display')}
  475. selected={displayMode}
  476. options={this.displayModes}
  477. onChange={this.handleDisplayModeChange}
  478. />
  479. </InlineContainer>
  480. </Fragment>
  481. ) : (
  482. <Placeholder height="34px" />
  483. )}
  484. </ChartControls>
  485. </Panel>
  486. );
  487. }
  488. }
  489. export default withApi(withTheme(ProjectCharts));