Skip to content

Commit

Permalink
fix: record MAP_VIEW statistics when saving and opening maps
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferarnesen committed Dec 21, 2023
1 parent 543764f commit aed1309
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cypress/integration/filemenu.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ describe('File menu', () => {
req.continue();
}).as('saveMap');

cy.intercept({ method: 'POST', url: /dataStatistics/ }).as(
'postDataStatistics'
);

saveNewMap(MAP_TITLE);

cy.wait('@saveMap')
.its('response.statusCode')
.should('eq', 201);

cy.wait('@postDataStatistics')
.its('response.statusCode')
.should('eq', 201);
});

it.skip('save existing as new map', () => {
Expand All @@ -67,11 +75,19 @@ describe('File menu', () => {
req.continue();
}).as('saveAsNewMap');

cy.intercept({ method: 'POST', url: /dataStatistics/ }).as(
'postDataStatistics'
);

saveAsNewMap(SAVEAS_MAP_TITLE);

cy.wait('@saveAsNewMap')
.its('response.statusCode')
.should('eq', 201);

cy.wait('@postDataStatistics')
.its('response.statusCode')
.should('eq', 201);
});

it.skip('save changes to existing map', () => {
Expand Down
8 changes: 8 additions & 0 deletions cypress/integration/smoke.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ context('Smoke Test', () => {
});

it('loads with map id', () => {
cy.intercept({ method: 'POST', url: /dataStatistics/ }).as(
'postDataStatistics'
);

cy.visit('/?id=ytkZY3ChM6J', EXTENDED_TIMEOUT); //ANC: 3rd visit coverage last year by district

cy.wait('@postDataStatistics')
.its('response.statusCode')
.should('eq', 201);

cy.get('canvas', EXTENDED_TIMEOUT).should('be.visible');

const Layer = new ThematicLayer();
Expand Down
7 changes: 7 additions & 0 deletions src/actions/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import log from 'loglevel';
import * as types from '../constants/actionTypes';
import { getFallbackBasemap } from '../constants/basemaps';
import { dataStatisticsMutation } from '../util/apiDataStatistics.js';
import { fetchMap } from '../util/requests';
import { addOrgUnitPaths } from '../util/helpers';
import { loadLayer } from './layers';
Expand Down Expand Up @@ -55,6 +56,12 @@ export const tOpenMap = (mapId, keyDefaultBaseMap, dataEngine) => async (
try {
const map = await fetchMap(mapId, dataEngine, keyDefaultBaseMap);

// record map view
dataEngine.mutate(dataStatisticsMutation, {
variables: { id: mapId },
onError: error => log.error('Error: ', error),
});

const basemapConfig =
getState().basemaps.find(bm => bm.id === map.basemap.id) ||
getFallbackBasemap();
Expand Down
13 changes: 12 additions & 1 deletion src/components/app/FileMenu.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from 'loglevel';
import React from 'react';
import PropTypes from 'prop-types';
import i18n from '@dhis2/d2-i18n';
Expand All @@ -7,6 +8,7 @@ import { useD2 } from '@dhis2/app-runtime-adapter-d2';
import { useDataMutation, useDataEngine } from '@dhis2/app-runtime';
import { newMap, tOpenMap, setMapProps } from '../../actions/map';
import { setAlert } from '../../actions/alerts';
import { dataStatisticsMutation } from '../../util/apiDataStatistics.js';
import { fetchMap } from '../../util/requests';
import { cleanMapConfig } from '../../util/favorites';
import { useSystemSettings } from '../SystemSettingsProvider';
Expand Down Expand Up @@ -49,6 +51,10 @@ export const FileMenu = ({ map, newMap, tOpenMap, setMapProps, setAlert }) => {
onError: e => setError({ message: getSaveFailureMessage(e.message) }),
});

const [postDataStatistics] = useDataMutation(dataStatisticsMutation, {
onError: e => log.error('Error:', e.message),
});

const saveMap = async () => {
const config = cleanMapConfig({
config: map,
Expand All @@ -64,6 +70,8 @@ export const FileMenu = ({ map, newMap, tOpenMap, setMapProps, setAlert }) => {
data: config,
});

postDataStatistics({ id: map.id });

setAlert({ success: true, message: getSavedMessage(config.name) });
};

Expand Down Expand Up @@ -97,8 +105,11 @@ export const FileMenu = ({ map, newMap, tOpenMap, setMapProps, setAlert }) => {
});

if (response.status === 'OK') {
const newMapId = response.response.uid;
postDataStatistics({ id: newMapId });

const newMapConfig = await fetchMap(
response.response.uid,
newMapId,
engine,
keyDefaultBaseMap
);
Expand Down
8 changes: 8 additions & 0 deletions src/util/apiDataStatisitcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const dataStatisticsMutation = {
resource: 'dataStatistics',
params: ({ id }) => ({
favorite: id,
eventType: 'MAP_VIEW',
}),
type: 'create',
};

0 comments on commit aed1309

Please sign in to comment.