From eb7d9aa4a3ea14fce1c3df7e3b02bda8c4f3d7f8 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Thu, 21 Dec 2023 11:23:36 +0100 Subject: [PATCH 1/2] fix: record MAP_VIEW statistics when saving and opening maps --- cypress/integration/filemenu.cy.js | 16 ++++++++++++++++ cypress/integration/smoke.cy.js | 8 ++++++++ src/actions/map.js | 7 +++++++ src/components/app/FileMenu.js | 13 ++++++++++++- src/util/apiDataStatisitcs.js | 8 ++++++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/util/apiDataStatisitcs.js diff --git a/cypress/integration/filemenu.cy.js b/cypress/integration/filemenu.cy.js index be71685fe..05eb27f97 100644 --- a/cypress/integration/filemenu.cy.js +++ b/cypress/integration/filemenu.cy.js @@ -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', () => { @@ -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', () => { diff --git a/cypress/integration/smoke.cy.js b/cypress/integration/smoke.cy.js index 3328f46d4..f17693bbc 100644 --- a/cypress/integration/smoke.cy.js +++ b/cypress/integration/smoke.cy.js @@ -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(); diff --git a/src/actions/map.js b/src/actions/map.js index e1feaa0fb..d831a51ec 100644 --- a/src/actions/map.js +++ b/src/actions/map.js @@ -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'; @@ -50,6 +51,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(); diff --git a/src/components/app/FileMenu.js b/src/components/app/FileMenu.js index f8b6b8642..4e90d74de 100644 --- a/src/components/app/FileMenu.js +++ b/src/components/app/FileMenu.js @@ -1,3 +1,4 @@ +import log from 'loglevel'; import React from 'react'; import PropTypes from 'prop-types'; import i18n from '@dhis2/d2-i18n'; @@ -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'; @@ -48,6 +50,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, @@ -63,6 +69,8 @@ export const FileMenu = ({ map, newMap, tOpenMap, setMapProps, setAlert }) => { data: config, }); + postDataStatistics({ id: map.id }); + setAlert({ success: true, message: getSavedMessage(config.name) }); }; @@ -96,8 +104,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 ); diff --git a/src/util/apiDataStatisitcs.js b/src/util/apiDataStatisitcs.js new file mode 100644 index 000000000..bf6145971 --- /dev/null +++ b/src/util/apiDataStatisitcs.js @@ -0,0 +1,8 @@ +export const dataStatisticsMutation = { + resource: 'dataStatistics', + params: ({ id }) => ({ + favorite: id, + eventType: 'MAP_VIEW', + }), + type: 'create', +}; From 7711383dea23d7ce4ece558e72f6d90c9d53bb02 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Thu, 21 Dec 2023 11:46:16 +0100 Subject: [PATCH 2/2] fix: put request in util/requests --- src/actions/map.js | 3 +-- src/components/app/FileMenu.js | 3 +-- src/util/apiDataStatisitcs.js | 8 -------- src/util/requests.js | 9 +++++++++ 4 files changed, 11 insertions(+), 12 deletions(-) delete mode 100644 src/util/apiDataStatisitcs.js diff --git a/src/actions/map.js b/src/actions/map.js index d831a51ec..3eaecd90f 100644 --- a/src/actions/map.js +++ b/src/actions/map.js @@ -1,8 +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 { fetchMap, dataStatisticsMutation } from '../util/requests'; import { addOrgUnitPaths } from '../util/helpers'; import { loadLayer } from './layers'; diff --git a/src/components/app/FileMenu.js b/src/components/app/FileMenu.js index 4e90d74de..c513de13c 100644 --- a/src/components/app/FileMenu.js +++ b/src/components/app/FileMenu.js @@ -8,8 +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 { fetchMap, dataStatisticsMutation } from '../../util/requests'; import { cleanMapConfig } from '../../util/favorites'; import { useSystemSettings } from '../SystemSettingsProvider'; diff --git a/src/util/apiDataStatisitcs.js b/src/util/apiDataStatisitcs.js deleted file mode 100644 index bf6145971..000000000 --- a/src/util/apiDataStatisitcs.js +++ /dev/null @@ -1,8 +0,0 @@ -export const dataStatisticsMutation = { - resource: 'dataStatistics', - params: ({ id }) => ({ - favorite: id, - eventType: 'MAP_VIEW', - }), - type: 'create', -}; diff --git a/src/util/requests.js b/src/util/requests.js index d8b86fc5b..f90be49f3 100644 --- a/src/util/requests.js +++ b/src/util/requests.js @@ -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();