Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: record MAP_VIEW statistics when saving and opening maps [v39] #3092

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -9,8 +9,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
8 changes: 7 additions & 1 deletion src/actions/map.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import log from 'loglevel';
import * as types from '../constants/actionTypes';
import { getFallbackBasemap } from '../constants/basemaps';
import { fetchMap } from '../util/requests';
import { fetchMap, dataStatisticsMutation } from '../util/requests';
import { addOrgUnitPaths } from '../util/helpers';
import { loadLayer } from './layers';

Expand Down Expand Up @@ -50,6 +50,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
14 changes: 12 additions & 2 deletions 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,7 +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 { fetchMap } from '../../util/requests';
import { fetchMap, dataStatisticsMutation } from '../../util/requests';
import { cleanMapConfig } from '../../util/favorites';
import { useSystemSettings } from '../SystemSettingsProvider';

Expand Down Expand Up @@ -48,6 +49,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 @@ -63,6 +68,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 @@ -96,8 +103,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
9 changes: 9 additions & 0 deletions src/util/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export const fetchMap = async (id, engine, keyDefaultBaseMap) =>
// },
// };

export const dataStatisticsMutation = {
resource: 'dataStatistics',
params: ({ id }) => ({
favorite: id,
eventType: 'MAP_VIEW',
}),
type: 'create',
};

export const fetchOrgUnits = async () => {
// TODO - use d2 until correct dataQuery format can be determined
const d2 = await getD2();
Expand Down
Loading