AnalyticsDrawer.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React, { PureComponent } from 'react';
  2. import { connect } from 'react-redux';
  3. import { SelectableValue, dateTime } from '@grafana/data';
  4. import { Drawer, Tab, TabsBar, TabContent, Themeable2, withTheme2 } from '@grafana/ui';
  5. import { UpgradeBox } from 'app/core/components/Upgrade/UpgradeBox';
  6. import { highlightTrial } from 'app/features/admin/utils';
  7. import { DashboardModel } from 'app/features/dashboard/state';
  8. import { AnalyticsTab, EnterpriseStoreState } from '../../types';
  9. import {
  10. DAILY_SUMMARY_DATE_FORMAT,
  11. DashboardDailySummaryDTO,
  12. UserViewDTO,
  13. getDashboardDailySummaries,
  14. getUserViews,
  15. } from '../api';
  16. import { setDrawerOpen, setDrawerTab } from '../state/reducers';
  17. import { getInsightsStyles, InsightsStyles } from '../styles';
  18. import AnalyticsStatsTab from './AnalyticsStatsTab';
  19. import AnalyticsUsersTab from './AnalyticsUsersTab';
  20. interface Props extends Themeable2 {
  21. dashboard: DashboardModel;
  22. drawerTab: AnalyticsTab;
  23. setDrawerOpen: typeof setDrawerOpen;
  24. setDrawerTab: typeof setDrawerTab;
  25. }
  26. interface State {
  27. drawerWidth: string;
  28. dailySummaries: DashboardDailySummaryDTO[];
  29. userViews: UserViewDTO[];
  30. }
  31. const USER_LIMIT = 30;
  32. class AnalyticsDrawer extends PureComponent<Props, State> {
  33. constructor(props: Props) {
  34. super(props);
  35. this.state = {
  36. drawerWidth: '50%',
  37. dailySummaries: [],
  38. userViews: [],
  39. };
  40. }
  41. async componentDidMount(): Promise<void> {
  42. const { dashboard } = this.props;
  43. const days = [];
  44. for (let i = 0; i < 30; i++) {
  45. days.push(dateTime(Date.now()).subtract(i, 'days').format(DAILY_SUMMARY_DATE_FORMAT));
  46. }
  47. if (dashboard?.id && dashboard.meta.url) {
  48. const dailySummaries = await getDashboardDailySummaries(dashboard.id, days);
  49. const userViews = await getUserViews(dashboard.id, USER_LIMIT);
  50. this.setState({ dailySummaries, userViews });
  51. }
  52. }
  53. onSelectTab = (item: SelectableValue<AnalyticsTab>) => {
  54. this.props.setDrawerTab(item.value || AnalyticsTab.Stats);
  55. };
  56. renderHeader(styles: InsightsStyles) {
  57. const tabs = [
  58. { label: 'Stats', value: AnalyticsTab.Stats },
  59. { label: 'Users and activity', value: AnalyticsTab.Users },
  60. ];
  61. return (
  62. <TabsBar className={styles.tabsBar}>
  63. {tabs.map((t, index) => (
  64. <Tab
  65. key={`${t.value}-${index}`}
  66. label={t.label}
  67. active={t.value === this.props.drawerTab}
  68. onChangeTab={() => this.onSelectTab(t)}
  69. />
  70. ))}
  71. </TabsBar>
  72. );
  73. }
  74. render() {
  75. const { dailySummaries, drawerWidth, userViews } = this.state;
  76. const { dashboard, drawerTab, setDrawerOpen, theme } = this.props;
  77. const styles = getInsightsStyles(theme);
  78. return (
  79. <Drawer
  80. scrollableContent
  81. title={`${dashboard.title} - analytics`}
  82. width={drawerWidth}
  83. onClose={() => setDrawerOpen(false)}
  84. subtitle={this.renderHeader(styles)}
  85. expandable
  86. >
  87. <TabContent className={styles.tabContent}>
  88. {highlightTrial() && (
  89. <UpgradeBox
  90. featureId={'dashboard-insights'}
  91. eventVariant={'trial'}
  92. featureName={'dashboard usage insights'}
  93. text={'Get full access to usage insights for free during your trial of Grafana Pro.'}
  94. />
  95. )}
  96. {drawerTab === AnalyticsTab.Stats && (
  97. <AnalyticsStatsTab dashboard={dashboard} dailySummaries={dailySummaries} />
  98. )}
  99. {drawerTab === AnalyticsTab.Users && (
  100. <AnalyticsUsersTab dashboard={dashboard} dailySummaries={dailySummaries} userViews={userViews} />
  101. )}
  102. </TabContent>
  103. </Drawer>
  104. );
  105. }
  106. }
  107. function mapStateToProps(state: EnterpriseStoreState) {
  108. return {
  109. drawerTab: state.metaAnalytics.drawerTab,
  110. };
  111. }
  112. const mapActionsToProps = {
  113. setDrawerOpen,
  114. setDrawerTab,
  115. };
  116. export default withTheme2(connect(mapStateToProps, mapActionsToProps)(AnalyticsDrawer));