Skip to content

Commit

Permalink
[perf logging] Add timing event when browser tab is hidden (#9733)
Browse files Browse the repository at this point in the history
* [perf logging] Add timing event when browser tab is hidden

* fix review comment
  • Loading branch information
Grace Guo committed May 5, 2020
1 parent d65d29c commit 0b963bd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 37 additions & 1 deletion superset-frontend/src/dashboard/components/Dashboard.jsx
Expand Up @@ -29,7 +29,11 @@ import {
dashboardInfoPropShape,
dashboardStatePropShape,
} from '../util/propShapes';
import { LOG_ACTIONS_MOUNT_DASHBOARD } from '../../logger/LogUtils';
import {
LOG_ACTIONS_HIDE_BROWSER_TAB,
LOG_ACTIONS_MOUNT_DASHBOARD,
Logger,
} from '../../logger/LogUtils';
import OmniContainer from '../../components/OmniContainer';
import { areObjectsEqual } from '../../reduxUtils';

Expand Down Expand Up @@ -81,6 +85,8 @@ class Dashboard extends React.PureComponent {
constructor(props) {
super(props);
this.appliedFilters = props.activeFilters || {};

this.onVisibilityChange = this.onVisibilityChange.bind(this);
}

componentDidMount() {
Expand All @@ -90,6 +96,15 @@ class Dashboard extends React.PureComponent {
eventData.target_id = directLinkComponentId;
}
this.props.actions.logEvent(LOG_ACTIONS_MOUNT_DASHBOARD, eventData);

// Handle browser tab visibility change
if (document.visibilityState === 'hidden') {
this.visibilityEventData = {
start_offset: Logger.getTimestamp(),
ts: new Date().getTime(),
};
}
window.addEventListener('visibilitychange', this.onVisibilityChange);
}

UNSAFE_componentWillReceiveProps(nextProps) {
Expand Down Expand Up @@ -158,6 +173,27 @@ class Dashboard extends React.PureComponent {
}
}

componentWillUnmount() {
window.removeEventListener('visibilitychange', this.onVisibilityChange);
}

onVisibilityChange() {
if (document.visibilityState === 'hidden') {
// from visible to hidden
this.visibilityEventData = {
start_offset: Logger.getTimestamp(),
ts: new Date().getTime(),
};
} else if (document.visibilityState === 'visible') {
// from hidden to visible
const logStart = this.visibilityEventData.start_offset;
this.props.actions.logEvent(LOG_ACTIONS_HIDE_BROWSER_TAB, {
...this.visibilityEventData,
duration: Logger.getTimestamp() - logStart,
});
}
}

// return charts in array
getAllCharts() {
return Object.values(this.props.charts);
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/logger/LogUtils.js
Expand Up @@ -19,6 +19,7 @@
// Log event names ------------------------------------------------------------
export const LOG_ACTIONS_LOAD_CHART = 'load_chart';
export const LOG_ACTIONS_RENDER_CHART = 'render_chart';
export const LOG_ACTIONS_HIDE_BROWSER_TAB = 'hide_browser_tab';

export const LOG_ACTIONS_MOUNT_DASHBOARD = 'mount_dashboard';
export const LOG_ACTIONS_MOUNT_EXPLORER = 'mount_explorer';
Expand All @@ -41,6 +42,7 @@ export const LOG_ACTIONS_OMNIBAR_TRIGGERED = 'omnibar_triggered';
export const LOG_EVENT_TYPE_TIMING = new Set([
LOG_ACTIONS_LOAD_CHART,
LOG_ACTIONS_RENDER_CHART,
LOG_ACTIONS_HIDE_BROWSER_TAB,
]);
export const LOG_EVENT_TYPE_USER = new Set([
LOG_ACTIONS_MOUNT_DASHBOARD,
Expand Down

0 comments on commit 0b963bd

Please sign in to comment.