usageStatsOrg.tsx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. import type {MouseEvent as ReactMouseEvent} from 'react';
  2. import {Fragment} from 'react';
  3. import styled from '@emotion/styled';
  4. import isEqual from 'lodash/isEqual';
  5. import moment from 'moment-timezone';
  6. import {navigateTo} from 'sentry/actionCreators/navigation';
  7. import type {TooltipSubLabel} from 'sentry/components/charts/components/tooltip';
  8. import OptionSelector from 'sentry/components/charts/optionSelector';
  9. import {InlineContainer, SectionHeading} from 'sentry/components/charts/styles';
  10. import type {DateTimeObject} from 'sentry/components/charts/utils';
  11. import {getSeriesApiInterval} from 'sentry/components/charts/utils';
  12. import {Flex} from 'sentry/components/container/flex';
  13. import DeprecatedAsyncComponent from 'sentry/components/deprecatedAsyncComponent';
  14. import ErrorBoundary from 'sentry/components/errorBoundary';
  15. import ExternalLink from 'sentry/components/links/externalLink';
  16. import NotAvailable from 'sentry/components/notAvailable';
  17. import QuestionTooltip from 'sentry/components/questionTooltip';
  18. import type {ScoreCardProps} from 'sentry/components/scoreCard';
  19. import ScoreCard from 'sentry/components/scoreCard';
  20. import SwitchButton from 'sentry/components/switchButton';
  21. import {DEFAULT_STATS_PERIOD} from 'sentry/constants';
  22. import {t, tct} from 'sentry/locale';
  23. import {space} from 'sentry/styles/space';
  24. import type {DataCategoryInfo, IntervalPeriod} from 'sentry/types/core';
  25. import type {WithRouterProps} from 'sentry/types/legacyReactRouter';
  26. import type {Organization} from 'sentry/types/organization';
  27. import {trackAnalytics} from 'sentry/utils/analytics';
  28. import {parsePeriodToHours} from 'sentry/utils/duration/parsePeriodToHours';
  29. import {hasDynamicSamplingCustomFeature} from 'sentry/utils/dynamicSampling/features';
  30. import {
  31. FORMAT_DATETIME_DAILY,
  32. FORMAT_DATETIME_HOURLY,
  33. getTooltipFormatter,
  34. } from './usageChart/utils';
  35. import {mapSeriesToChart} from './mapSeriesToChart';
  36. import type {UsageSeries} from './types';
  37. import type {ChartStats, UsageChartProps} from './usageChart';
  38. import UsageChart, {
  39. CHART_OPTIONS_DATA_TRANSFORM,
  40. ChartDataTransform,
  41. SeriesTypes,
  42. } from './usageChart';
  43. import UsageStatsPerMin from './usageStatsPerMin';
  44. import {isDisplayUtc} from './utils';
  45. export interface UsageStatsOrganizationProps extends WithRouterProps {
  46. dataCategory: DataCategoryInfo['plural'];
  47. dataCategoryApiName: DataCategoryInfo['apiName'];
  48. dataCategoryName: string;
  49. dataDatetime: DateTimeObject;
  50. handleChangeState: (state: {
  51. clientDiscard?: boolean;
  52. dataCategory?: DataCategoryInfo['plural'];
  53. pagePeriod?: string | null;
  54. transform?: ChartDataTransform;
  55. }) => void;
  56. isSingleProject: boolean;
  57. organization: Organization;
  58. projectIds: number[];
  59. chartTransform?: string;
  60. clientDiscard?: boolean;
  61. }
  62. type UsageStatsOrganizationState = {
  63. orgStats: UsageSeries | undefined;
  64. metricOrgStats?: UsageSeries | undefined;
  65. } & DeprecatedAsyncComponent['state'];
  66. /**
  67. * This component is replaced by EnhancedUsageStatsOrganization in getsentry, which inherits
  68. * heavily from this one. Take care if changing any existing function signatures to ensure backwards
  69. * compatibility.
  70. */
  71. class UsageStatsOrganization<
  72. P extends UsageStatsOrganizationProps = UsageStatsOrganizationProps,
  73. S extends UsageStatsOrganizationState = UsageStatsOrganizationState,
  74. > extends DeprecatedAsyncComponent<P, S> {
  75. componentDidUpdate(prevProps: UsageStatsOrganizationProps) {
  76. const {
  77. dataDatetime: prevDateTime,
  78. projectIds: prevProjectIds,
  79. dataCategoryApiName: prevDataCategoryApiName,
  80. } = prevProps;
  81. const {
  82. dataDatetime: currDateTime,
  83. projectIds: currProjectIds,
  84. dataCategoryApiName: currentDataCategoryApiName,
  85. } = this.props;
  86. if (
  87. prevDateTime.start !== currDateTime.start ||
  88. prevDateTime.end !== currDateTime.end ||
  89. prevDateTime.period !== currDateTime.period ||
  90. prevDateTime.utc !== currDateTime.utc ||
  91. prevDataCategoryApiName !== currentDataCategoryApiName ||
  92. !isEqual(prevProjectIds, currProjectIds)
  93. ) {
  94. this.reloadData();
  95. }
  96. }
  97. getEndpoints(): ReturnType<DeprecatedAsyncComponent['getEndpoints']> {
  98. return [['orgStats', this.endpointPath, {query: this.endpointQuery}]];
  99. }
  100. /** List of components to render on single-project view */
  101. get projectDetails(): JSX.Element[] {
  102. return [];
  103. }
  104. get endpointPath() {
  105. const {organization} = this.props;
  106. return `/organizations/${organization.slug}/stats_v2/`;
  107. }
  108. get endpointQueryDatetime() {
  109. const {dataDatetime} = this.props;
  110. const queryDatetime =
  111. dataDatetime.start && dataDatetime.end
  112. ? {
  113. start: dataDatetime.start,
  114. end: dataDatetime.end,
  115. utc: dataDatetime.utc,
  116. }
  117. : {
  118. statsPeriod: dataDatetime.period || DEFAULT_STATS_PERIOD,
  119. };
  120. return queryDatetime;
  121. }
  122. get endpointQuery() {
  123. const {dataDatetime, projectIds, dataCategoryApiName} = this.props;
  124. const queryDatetime = this.endpointQueryDatetime;
  125. const groupBy = ['outcome', 'reason'];
  126. const category: string[] = [dataCategoryApiName];
  127. if (
  128. hasDynamicSamplingCustomFeature(this.props.organization) &&
  129. dataCategoryApiName === 'span'
  130. ) {
  131. groupBy.push('category');
  132. category.push('span_indexed');
  133. }
  134. return {
  135. ...queryDatetime,
  136. interval: getSeriesApiInterval(dataDatetime),
  137. groupBy,
  138. project: projectIds,
  139. field: ['sum(quantity)'],
  140. category,
  141. };
  142. }
  143. get chartData(): {
  144. cardStats: {
  145. accepted?: string;
  146. accepted_stored?: string;
  147. filtered?: string;
  148. invalid?: string;
  149. rateLimited?: string;
  150. total?: string;
  151. };
  152. chartDateEnd: string;
  153. chartDateEndDisplay: string;
  154. chartDateInterval: IntervalPeriod;
  155. chartDateStart: string;
  156. chartDateStartDisplay: string;
  157. chartDateTimezoneDisplay: string;
  158. chartDateUtc: boolean;
  159. chartStats: ChartStats;
  160. chartSubLabels: TooltipSubLabel[];
  161. chartTransform: ChartDataTransform;
  162. dataError?: Error;
  163. } {
  164. return {
  165. ...mapSeriesToChart({
  166. orgStats: this.state.orgStats,
  167. chartDateInterval: this.chartDateRange.chartDateInterval,
  168. chartDateUtc: this.chartDateRange.chartDateUtc,
  169. dataCategory: this.props.dataCategory,
  170. endpointQuery: this.endpointQuery,
  171. }),
  172. ...this.chartDateRange,
  173. ...this.chartTransform,
  174. };
  175. }
  176. get chartTransform(): {chartTransform: ChartDataTransform} {
  177. const {chartTransform} = this.props;
  178. switch (chartTransform) {
  179. case ChartDataTransform.CUMULATIVE:
  180. case ChartDataTransform.PERIODIC:
  181. return {chartTransform};
  182. default:
  183. return {chartTransform: ChartDataTransform.PERIODIC};
  184. }
  185. }
  186. get chartDateRange(): {
  187. chartDateEnd: string;
  188. chartDateEndDisplay: string;
  189. chartDateInterval: IntervalPeriod;
  190. chartDateStart: string;
  191. chartDateStartDisplay: string;
  192. chartDateTimezoneDisplay: string;
  193. chartDateUtc: boolean;
  194. } {
  195. const {orgStats} = this.state;
  196. const {dataDatetime} = this.props;
  197. const interval = getSeriesApiInterval(dataDatetime);
  198. // Use fillers as loading/error states will not display datetime at all
  199. if (!orgStats || !orgStats.intervals) {
  200. return {
  201. chartDateInterval: interval,
  202. chartDateStart: '',
  203. chartDateEnd: '',
  204. chartDateUtc: true,
  205. chartDateStartDisplay: '',
  206. chartDateEndDisplay: '',
  207. chartDateTimezoneDisplay: '',
  208. };
  209. }
  210. const {intervals} = orgStats;
  211. const intervalHours = parsePeriodToHours(interval);
  212. // Keep datetime in UTC until we want to display it to users
  213. const startTime = moment(intervals[0]).utc();
  214. const endTime =
  215. intervals.length < 2
  216. ? moment(startTime) // when statsPeriod and interval is the same value
  217. : moment(intervals[intervals.length - 1]).utc();
  218. const useUtc = isDisplayUtc(dataDatetime);
  219. // If interval is a day or more, use UTC to format date. Otherwise, the date
  220. // may shift ahead/behind when converting to the user's local time.
  221. const FORMAT_DATETIME =
  222. intervalHours >= 24 ? FORMAT_DATETIME_DAILY : FORMAT_DATETIME_HOURLY;
  223. const xAxisStart = moment(startTime);
  224. const xAxisEnd = moment(endTime);
  225. const displayStart = useUtc ? moment(startTime).utc() : moment(startTime).local();
  226. const displayEnd = useUtc ? moment(endTime).utc() : moment(endTime).local();
  227. if (intervalHours < 24) {
  228. displayEnd.add(intervalHours, 'h');
  229. }
  230. return {
  231. chartDateInterval: interval,
  232. chartDateStart: xAxisStart.format(),
  233. chartDateEnd: xAxisEnd.format(),
  234. chartDateUtc: useUtc,
  235. chartDateStartDisplay: displayStart.format(FORMAT_DATETIME),
  236. chartDateEndDisplay: displayEnd.format(FORMAT_DATETIME),
  237. chartDateTimezoneDisplay: displayStart.format('Z'),
  238. };
  239. }
  240. get chartProps(): UsageChartProps {
  241. const {dataCategory, clientDiscard, handleChangeState} = this.props;
  242. const {error, errors, loading} = this.state;
  243. const {
  244. chartStats,
  245. dataError,
  246. chartDateInterval,
  247. chartDateStart,
  248. chartDateEnd,
  249. chartDateUtc,
  250. chartTransform,
  251. chartSubLabels,
  252. } = this.chartData;
  253. const hasError = error || !!dataError;
  254. const chartErrors: any = dataError ? {...errors, data: dataError} : errors; // TODO(ts): AsyncComponent
  255. const chartProps = {
  256. isLoading: loading,
  257. isError: hasError,
  258. errors: chartErrors,
  259. title: (
  260. <Fragment>
  261. {t('Project(s) Stats')}
  262. <QuestionTooltip
  263. size="xs"
  264. title={tct(
  265. 'You can find more information about each category in our [link:docs]',
  266. {
  267. link: (
  268. <ExternalLink
  269. href="https://docs.sentry.io/product/stats/#usage-stats"
  270. onClick={() => this.handleOnDocsClick('chart-title')}
  271. />
  272. ),
  273. }
  274. )}
  275. isHoverable
  276. />
  277. </Fragment>
  278. ),
  279. footer: this.renderChartFooter(),
  280. dataCategory,
  281. dataTransform: chartTransform,
  282. usageDateStart: chartDateStart,
  283. usageDateEnd: chartDateEnd,
  284. usageDateShowUtc: chartDateUtc,
  285. usageDateInterval: chartDateInterval,
  286. usageStats: chartStats,
  287. chartTooltip: {
  288. subLabels: chartSubLabels,
  289. skipZeroValuedSubLabels: true,
  290. trigger: 'axis',
  291. valueFormatter: getTooltipFormatter(dataCategory),
  292. },
  293. legendSelected: {[SeriesTypes.CLIENT_DISCARD]: !!clientDiscard},
  294. onLegendSelectChanged: ({name, selected}) => {
  295. if (name === SeriesTypes.CLIENT_DISCARD) {
  296. handleChangeState({clientDiscard: selected[name]});
  297. }
  298. },
  299. } as UsageChartProps;
  300. return chartProps;
  301. }
  302. handleOnDocsClick = (
  303. source:
  304. | 'card-accepted'
  305. | 'card-filtered'
  306. | 'card-rate-limited'
  307. | 'card-invalid'
  308. | 'chart-title'
  309. ) => {
  310. const {organization, dataCategory} = this.props;
  311. trackAnalytics('stats.docs_clicked', {
  312. organization,
  313. source,
  314. dataCategory,
  315. });
  316. };
  317. get cardMetadata() {
  318. const {
  319. dataCategory,
  320. dataCategoryName,
  321. organization,
  322. projectIds,
  323. router,
  324. dataCategoryApiName,
  325. } = this.props;
  326. const {total, accepted, accepted_stored, invalid, rateLimited, filtered} =
  327. this.chartData.cardStats;
  328. const dataCategoryNameLower = dataCategoryName.toLowerCase();
  329. const navigateToInboundFilterSettings = (event: ReactMouseEvent) => {
  330. event.preventDefault();
  331. const url = `/settings/${organization.slug}/projects/:projectId/filters/data-filters/`;
  332. if (router) {
  333. navigateTo(url, router);
  334. }
  335. };
  336. const cardMetadata: Record<string, ScoreCardProps> = {
  337. total: {
  338. title: tct('Total [dataCategory]', {dataCategory: dataCategoryName}),
  339. score: total,
  340. },
  341. accepted: {
  342. title: tct('Accepted [dataCategory]', {dataCategory: dataCategoryName}),
  343. help: tct(
  344. 'Accepted [dataCategory] were successfully processed by Sentry. For more information, read our [docsLink:docs].',
  345. {
  346. dataCategory: dataCategoryNameLower,
  347. docsLink: (
  348. <ExternalLink
  349. href="https://docs.sentry.io/product/stats/#accepted"
  350. onClick={() => this.handleOnDocsClick('card-accepted')}
  351. />
  352. ),
  353. }
  354. ),
  355. score: accepted,
  356. trend:
  357. dataCategoryApiName === 'span' && accepted_stored ? (
  358. t('%s stored', accepted_stored)
  359. ) : (
  360. <UsageStatsPerMin
  361. dataCategoryApiName={dataCategoryApiName}
  362. dataCategory={dataCategory}
  363. organization={organization}
  364. projectIds={projectIds}
  365. />
  366. ),
  367. },
  368. filtered: {
  369. title: tct('Filtered [dataCategory]', {dataCategory: dataCategoryName}),
  370. help: tct(
  371. 'Filtered [dataCategory] were blocked due to your [filterSettings: inbound data filter] rules. For more information, read our [docsLink:docs].',
  372. {
  373. dataCategory: dataCategoryNameLower,
  374. filterSettings: (
  375. <a href="#" onClick={event => navigateToInboundFilterSettings(event)} />
  376. ),
  377. docsLink: (
  378. <ExternalLink
  379. href="https://docs.sentry.io/product/stats/#filtered"
  380. onClick={() => this.handleOnDocsClick('card-filtered')}
  381. />
  382. ),
  383. }
  384. ),
  385. score: filtered,
  386. },
  387. rateLimited: {
  388. title: tct('Rate Limited [dataCategory]', {dataCategory: dataCategoryName}),
  389. help: tct(
  390. 'Rate Limited [dataCategory] were discarded due to rate limits or quota. For more information, read our [docsLink:docs].',
  391. {
  392. dataCategory: dataCategoryNameLower,
  393. docsLink: (
  394. <ExternalLink
  395. href="https://docs.sentry.io/product/stats/#rate-limited"
  396. onClick={() => this.handleOnDocsClick('card-rate-limited')}
  397. />
  398. ),
  399. }
  400. ),
  401. score: rateLimited,
  402. },
  403. invalid: {
  404. title: tct('Invalid [dataCategory]', {dataCategory: dataCategoryName}),
  405. help: tct(
  406. 'Invalid [dataCategory] were sent by the SDK and were discarded because the data did not meet the basic schema requirements. For more information, read our [docsLink:docs].',
  407. {
  408. dataCategory: dataCategoryNameLower,
  409. docsLink: (
  410. <ExternalLink
  411. href="https://docs.sentry.io/product/stats/#invalid"
  412. onClick={() => this.handleOnDocsClick('card-invalid')}
  413. />
  414. ),
  415. }
  416. ),
  417. score: invalid,
  418. },
  419. };
  420. return cardMetadata;
  421. }
  422. renderCards() {
  423. const {loading} = this.state;
  424. const cardMetadata = Object.values(this.cardMetadata);
  425. return cardMetadata.map((card, i) => (
  426. <StyledScoreCard
  427. key={i}
  428. title={card.title}
  429. score={loading ? undefined : card.score}
  430. help={card.help}
  431. trend={card.trend}
  432. isTooltipHoverable
  433. />
  434. ));
  435. }
  436. renderChart() {
  437. const {loading} = this.state;
  438. return <UsageChart {...this.chartProps} isLoading={loading} />;
  439. }
  440. renderChartFooter = () => {
  441. const {handleChangeState, clientDiscard} = this.props;
  442. const {loading, error} = this.state;
  443. const {
  444. chartDateInterval,
  445. chartTransform,
  446. chartDateStartDisplay,
  447. chartDateEndDisplay,
  448. chartDateTimezoneDisplay,
  449. } = this.chartData;
  450. return (
  451. <Footer>
  452. <InlineContainer>
  453. <FooterDate>
  454. <SectionHeading>{t('Date Range:')}</SectionHeading>
  455. <span>
  456. {loading || error ? (
  457. <NotAvailable />
  458. ) : (
  459. tct('[start] — [end] ([timezone] UTC, [interval] interval)', {
  460. start: chartDateStartDisplay,
  461. end: chartDateEndDisplay,
  462. timezone: chartDateTimezoneDisplay,
  463. interval: chartDateInterval,
  464. })
  465. )}
  466. </span>
  467. </FooterDate>
  468. </InlineContainer>
  469. <InlineContainer>
  470. {(this.chartData.chartStats.clientDiscard ?? []).length > 0 && (
  471. <Flex align="center" gap={space(1)}>
  472. <strong>{t('Show client-discarded data:')}</strong>
  473. <SwitchButton
  474. toggle={() => {
  475. handleChangeState({clientDiscard: !clientDiscard});
  476. }}
  477. isActive={clientDiscard}
  478. />
  479. </Flex>
  480. )}
  481. </InlineContainer>
  482. <InlineContainer>
  483. <OptionSelector
  484. title={t('Type')}
  485. selected={chartTransform}
  486. options={CHART_OPTIONS_DATA_TRANSFORM}
  487. onChange={(val: string) =>
  488. handleChangeState({transform: val as ChartDataTransform})
  489. }
  490. />
  491. </InlineContainer>
  492. </Footer>
  493. );
  494. };
  495. renderProjectDetails() {
  496. const {isSingleProject} = this.props;
  497. const projectDetails = this.projectDetails.map((projectDetailComponent, i) => (
  498. <ErrorBoundary mini key={i}>
  499. {projectDetailComponent}
  500. </ErrorBoundary>
  501. ));
  502. return isSingleProject ? projectDetails : null;
  503. }
  504. renderComponent() {
  505. return (
  506. <Fragment>
  507. <PageGrid>
  508. {this.renderCards()}
  509. <ChartWrapper data-test-id="usage-stats-chart">
  510. {this.renderChart()}
  511. </ChartWrapper>
  512. </PageGrid>
  513. {this.renderProjectDetails()}
  514. </Fragment>
  515. );
  516. }
  517. }
  518. export default UsageStatsOrganization;
  519. const PageGrid = styled('div')`
  520. display: grid;
  521. grid-template-columns: 1fr;
  522. gap: ${space(2)};
  523. @media (min-width: ${p => p.theme.breakpoints.small}) {
  524. grid-template-columns: repeat(2, 1fr);
  525. }
  526. @media (min-width: ${p => p.theme.breakpoints.large}) {
  527. grid-template-columns: repeat(5, 1fr);
  528. }
  529. `;
  530. const StyledScoreCard = styled(ScoreCard)`
  531. grid-column: auto / span 1;
  532. margin: 0;
  533. `;
  534. const ChartWrapper = styled('div')`
  535. grid-column: 1 / -1;
  536. `;
  537. const Footer = styled('div')`
  538. display: flex;
  539. flex-direction: row;
  540. flex-wrap: wrap;
  541. align-items: center;
  542. gap: ${space(1.5)};
  543. padding: ${space(1)} ${space(3)};
  544. border-top: 1px solid ${p => p.theme.border};
  545. > *:first-child {
  546. flex-grow: 1;
  547. }
  548. `;
  549. const FooterDate = styled('div')`
  550. display: flex;
  551. flex-direction: row;
  552. align-items: center;
  553. > ${SectionHeading} {
  554. margin-right: ${space(1.5)};
  555. }
  556. > span:last-child {
  557. font-weight: ${p => p.theme.fontWeightNormal};
  558. font-size: ${p => p.theme.fontSizeMedium};
  559. }
  560. `;