From b294866992271fab224adb71297fe9f57fd3f115 Mon Sep 17 00:00:00 2001 From: Jen Jones Arnesen Date: Thu, 21 Dec 2023 16:47:10 +0100 Subject: [PATCH] fix: record MAP_VIEW statistics when saving and opening maps [v38] (#3093) * fix: record MAP_VIEW statistics when saving and opening maps * fix: put request in util/requests --- cypress/integration/filemenu.cy.js | 16 ++++++++++++++++ cypress/integration/smoke.cy.js | 8 ++++++++ src/actions/map.js | 8 +++++++- src/components/app/FileMenu.js | 14 ++++++++++++-- src/util/requests.js | 9 +++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) 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 35d514561..3c54e2790 100644 --- a/cypress/integration/smoke.cy.js +++ b/cypress/integration/smoke.cy.js @@ -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(); diff --git a/src/actions/map.js b/src/actions/map.js index ed8122742..418bc36c7 100644 --- a/src/actions/map.js +++ b/src/actions/map.js @@ -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'; @@ -55,6 +55,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 c647b6dfd..1c1d0b8ff 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,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'; import styles from './styles/FileMenu.module.css'; @@ -49,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, @@ -64,6 +69,8 @@ export const FileMenu = ({ map, newMap, tOpenMap, setMapProps, setAlert }) => { data: config, }); + postDataStatistics({ id: map.id }); + setAlert({ success: true, message: getSavedMessage(config.name) }); }; @@ -97,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/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();