Skip to content

Commit

Permalink
feat: register passive view of dashboards [DHIS2-7016] (#1572)
Browse files Browse the repository at this point in the history
log when a passive view of the dashboard occurs (user opens dashboard without specifically requesting the specific dashboard)

This implementation posts to data statistics when the dashboard app renders a dashboard in view mode for the first time (a passive view); any subsequent passive views, regardless of dashboard, are not registered. In line with the back-end implementation by @jimgrace, the dashboard app has been modified to always make these posts for passive views, whereas api/dataStatistics will count these passive views only if the newly added system setting keyCountPassiveDashboardViewsInUsageAnalytics=true.

This change is requested by PEPFAR to allow better oversight over which dashboards are being accessed by users. This change will not affect statistics unless a system admin chooses to set keyCountPassiveDashboardViewsInUsageAnalytics=true, so this PR does not change default behaviour for existing instances (https://jira.dhis2.org/browse/DHIS2-7016)

Co-authored-by: Thomas Zemp <[email protected]>
  • Loading branch information
jenniferarnesen and Thomas Zemp authored Feb 26, 2021
1 parent efea939 commit ddbf65d
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/actions/passiveViewRegistered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { REGISTER_PASSIVE_VIEW } from '../reducers/passiveViewRegistered'

export const acSetPassiveViewRegistered = () => ({
type: REGISTER_PASSIVE_VIEW,
})
18 changes: 18 additions & 0 deletions src/components/Dashboard/ViewDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import DashboardsBar from '../ControlBar/ViewControlBar/DashboardsBar'
import { sGetIsEditing } from '../../reducers/editDashboard'
import { sGetIsPrinting } from '../../reducers/printDashboard'
import { sGetSelectedId } from '../../reducers/selected'
import { sGetPassiveViewRegistered } from '../../reducers/passiveViewRegistered'
import { acClearEditDashboard } from '../../actions/editDashboard'
import { acClearPrintDashboard } from '../../actions/printDashboard'
import { acSetPassiveViewRegistered } from '../../actions/passiveViewRegistered'
import { apiPostDataStatistics } from '../../api/dataStatistics'

import classes from './styles/ViewDashboard.module.css'

Expand All @@ -35,6 +38,17 @@ export const ViewDashboard = props => {
})
}, [props.selectedId])

useEffect(() => {
if (!props.passiveViewRegistered) {
apiPostDataStatistics(
'PASSIVE_DASHBOARD_VIEW',
props.selectedId
).then(() => {
props.registerPassiveView()
})
}
}, [props.passiveViewRegistered])

const onExpandedChanged = expanded => setControlbarExpanded(expanded)

return (
Expand Down Expand Up @@ -64,18 +78,22 @@ ViewDashboard.propTypes = {
clearPrintDashboard: PropTypes.func,
dashboardIsEditing: PropTypes.bool,
dashboardIsPrinting: PropTypes.bool,
passiveViewRegistered: PropTypes.bool,
registerPassiveView: PropTypes.func,
selectedId: PropTypes.string,
}

const mapStateToProps = state => {
return {
dashboardIsEditing: sGetIsEditing(state),
dashboardIsPrinting: sGetIsPrinting(state),
passiveViewRegistered: sGetPassiveViewRegistered(state),
selectedId: sGetSelectedId(state),
}
}

export default connect(mapStateToProps, {
clearEditDashboard: acClearEditDashboard,
clearPrintDashboard: acClearPrintDashboard,
registerPassiveView: acSetPassiveViewRegistered,
})(ViewDashboard)
19 changes: 19 additions & 0 deletions src/components/Dashboard/__tests__/ViewDashboard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import { mount } from 'enzyme'
import toJson from 'enzyme-to-json'
import { ViewDashboard } from '../ViewDashboard'
import { apiPostDataStatistics } from '../../../api/dataStatistics'

jest.mock('react', () => ({
...jest.requireActual('react'),
Expand Down Expand Up @@ -38,6 +39,12 @@ jest.mock(
}
)

jest.mock('../../../api/dataStatistics', () => ({
apiPostDataStatistics: jest.fn(() => {
return new Promise(resolve => resolve(true))
}),
}))

describe('ViewDashboard', () => {
let props

Expand Down Expand Up @@ -72,4 +79,16 @@ describe('ViewDashboard', () => {
expect(props.clearEditDashboard).not.toHaveBeenCalled()
expect(props.clearPrintDashboard).toHaveBeenCalled()
})

it('does not post passive view to api if passive view has been registered', () => {
props.passiveViewRegistered = true
mount(<ViewDashboard {...props} />)
expect(apiPostDataStatistics).not.toHaveBeenCalled()
})

it('posts passive view to api if passive view has not been registered', () => {
props.passiveViewRegistered = false
mount(<ViewDashboard {...props} />)
expect(apiPostDataStatistics).toHaveBeenCalled()
})
})
2 changes: 2 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import itemFilters from './itemFilters'
import style from './style'
import dimensions from './dimensions'
import activeModalDimension from './activeModalDimension'
import passiveViewRegistered from './passiveViewRegistered'

export default combineReducers({
dashboards,
Expand All @@ -30,4 +31,5 @@ export default combineReducers({
alert,
dimensions,
activeModalDimension,
passiveViewRegistered,
})
13 changes: 13 additions & 0 deletions src/reducers/passiveViewRegistered.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const REGISTER_PASSIVE_VIEW = 'REGISTER_PASSIVE_VIEW'

export default (state = false, action) => {
switch (action.type) {
case REGISTER_PASSIVE_VIEW: {
return true
}
default:
return state
}
}

export const sGetPassiveViewRegistered = state => state.passiveViewRegistered

0 comments on commit ddbf65d

Please sign in to comment.