Skip to content

Commit

Permalink
Retrieve resurce status from backend instead of caching it inside loc…
Browse files Browse the repository at this point in the history
…al storage (#834)
  • Loading branch information
DavidQuartz authored Feb 16, 2022
1 parent 12a6b73 commit 092832f
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 70 deletions.
9 changes: 7 additions & 2 deletions geonode_mapstore_client/client/js/api/geonode/v2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ export const getResources = ({
...(sort && { sort: isArray(sort) ? sort : [ sort ]}),
page,
page_size: pageSize,
'filter{metadata_only}': false // exclude resources such as services
'filter{metadata_only}': false, // exclude resources such as services
include: ['executions']
}
})
.then(({ data }) => {
Expand Down Expand Up @@ -286,7 +287,11 @@ export const setFavoriteResource = (pk, favorite) => {
};

export const getResourceByPk = (pk) => {
return axios.get(parseDevHostname(`${endpoints[RESOURCES]}/${pk}`))
return axios.get(parseDevHostname(`${endpoints[RESOURCES]}/${pk}`), {
params: {
include: ['executions']
}
})
.then(({ data }) => data.resource);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const Cards = withResizeDetector(({

return (
<li
key={resource.pk}
key={resource.pk2 || resource.pk} // pk2 exists on clones to avoid duplicate keys
style={(layoutSpace(idx))}
>
<ResourceCard
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import expect from 'expect';
import MockAdapter from 'axios-mock-adapter';
import axios from '@mapstore/framework/libs/ajax';
import { testEpic } from '@mapstore/framework/epics/__tests__/epicTestUtils';
import {
STOP_ASYNC_PROCESS,
startAsyncProcess
} from '@js/actions/resourceservice';
import { gnMonitorAsyncProcesses } from '../resourceservice';

let mockAxios;

describe('resourceservice epics', () => {
beforeEach(done => {
global.__DEVTOOLS__ = true;
mockAxios = new MockAdapter(axios);
setTimeout(done);
});
afterEach(done => {
delete global.__DEVTOOLS__;
mockAxios.restore();
setTimeout(done);
});

it('test gnMonitorAsyncProcesses', (done) => {
const testState = {
resourceservice: {}
};
const actionsCount = 1;
mockAxios.onGet().reply(() => [200, { status: 'finished' }]);
testEpic(
gnMonitorAsyncProcesses,
actionsCount,
startAsyncProcess({ output: {status_url: 'test'}, resource: { pk: 1, name: 'test resource' }, processType: 'test process' }),
(actions) => {
try {
expect(actions.map(({ type }) => type))
.toEqual([
STOP_ASYNC_PROCESS
]);
} catch (e) {
done(e);
}
done();
},
testState
);
});
});
26 changes: 20 additions & 6 deletions geonode_mapstore_client/client/js/epics/gnsearch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2020, GeoSolutions Sas.
* All rights reserved.
*
Expand Down Expand Up @@ -28,7 +28,8 @@ import {
import {
resourceLoading,
setResource,
resourceError
resourceError,
processResources
} from '@js/actions/gnresource';
import {
LOCATION_CHANGE,
Expand All @@ -40,11 +41,14 @@ import {
} from '@js/utils/SearchUtils';
import url from 'url';
import { getCustomMenuFilters } from '@js/selectors/config';
import { STOP_ASYNC_PROCESS } from '@js/actions/resourceservice';
import { STOP_ASYNC_PROCESS, stopAsyncProcess } from '@js/actions/resourceservice';
import {
ProcessTypes,
ProcessStatus
} from '@js/utils/ResourceServiceUtils';
import { getCurrentProcesses } from '@js/selectors/resourceservice';
import { userSelector } from '@mapstore/framework/selectors/security';
import uuid from 'uuid';

const UPDATE_RESOURCES_REQUEST = 'GEONODE_SEARCH:UPDATE_RESOURCES_REQUEST';
const updateResourcesRequest = (payload, reset) => ({
Expand Down Expand Up @@ -125,7 +129,8 @@ const requestResourcesObservable = ({
reset,
location
}, store) => {
const customFilters = getCustomMenuFilters(store.getState());
const state = store.getState();
const customFilters = getCustomMenuFilters(state);
return Observable
.defer(() => getResources({
...params,
Expand All @@ -137,7 +142,13 @@ const requestResourcesObservable = ({
total,
isNextPageAvailable
}) => {
const currentUser = userSelector(state);
const processingResources = resources.filter((resource) => (resource.executions?.length > 0 && resource.executions[0].status_url && resource.executions[0].user === currentUser?.info?.preferred_username) && { ...resource, executions: resource.executions[0] });
return Observable.of(
...processingResources.map((resource) => {
const process = `${resource?.executions[0].func_name}Resource`;
return processResources(process, [resource], false);
}),
updateResources(resources, reset),
updateResourcesMetadata({
isNextPageAvailable,
Expand Down Expand Up @@ -165,6 +176,9 @@ export const gnsSearchResourcesOnLocationChangeEpic = (action$, store) =>
const { isFirstRendering, location } = action.payload || {};
const state = store.getState();

// stop ongoing processes everytime there is a location change or new request is made
getCurrentProcesses(state) > 0 && getCurrentProcesses(state).map((process) => stopAsyncProcess({ ...process, completed: true }));

const nextParams = state.gnsearch.nextParams;

const [previousParams, previousPage] = getParams(state.gnsearch.locationSearch, state.gnsearch.params);
Expand Down Expand Up @@ -266,8 +280,8 @@ export const gnWatchStopCopyProcessOnSearch = (action$, store) =>
.switchMap((resource) => {
const resources = store.getState().gnsearch?.resources || [];
const newResources = resources.reduce((acc, res) => {
if (res.pk === (pk + '')) {
return [...acc, { ...resource, '@temporary': true }, res];
if (res.pk === `${pk}`) {
return [...acc, { ...resource, '@temporary': true, pk2: uuid() }, res]; // pk2 is added to avoid duplicate pk in resources list
}
return [...acc, res];
}, []);
Expand Down
14 changes: 5 additions & 9 deletions geonode_mapstore_client/client/js/epics/resourceservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ export const gnMonitorAsyncProcesses = (action$, store) => {
}
return Observable
.interval(ProcessInterval[action?.payload?.processType] || 1000)
.switchMap(() => {
// avoid request after completion
if (isProcessCompleted(store.getState(), action.payload)) {
return Observable.empty();
}
return Observable.defer(() =>
.takeWhile(() => !isProcessCompleted(store.getState(), action.payload))
.exhaustMap(() => (!isProcessCompleted(store.getState(), action.payload)) ?
Observable.defer(() =>
axios.get(statusUrl)
.then(({ data }) => data)
.catch((error) => ({ error: error?.data?.detail || error?.statusText || error?.message || true }))
Expand All @@ -53,9 +50,8 @@ export const gnMonitorAsyncProcesses = (action$, store) => {
return Observable.of(stopAsyncProcess({ ...action.payload, output, completed: true }));
}
return Observable.of(updateAsyncProcess({ ...action.payload, output }));
});
})
.takeWhile(() => !isProcessCompleted(store.getState(), action.payload));
}) : Observable.empty()
);
});
};

Expand Down
20 changes: 3 additions & 17 deletions geonode_mapstore_client/client/js/reducers/resourceservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ import {
PROCESS_RESOURCES
} from '@js/actions/gnresource';

import {
getLocalStorageProcesses,
setLocalStorageProcesses
} from '@js/utils/LocalStorageUtils';

const defaultState = {
processes: getLocalStorageProcesses()
processes: []
};

function resourceservice(state = defaultState, action) {
Expand Down Expand Up @@ -54,6 +49,7 @@ function resourceservice(state = defaultState, action) {
]
};
}
case STOP_ASYNC_PROCESS:
case UPDATE_ASYNC_PROCESS: {
return {
...state,
Expand All @@ -64,19 +60,9 @@ function resourceservice(state = defaultState, action) {
: process)
};
}
case STOP_ASYNC_PROCESS: {
return {
...state,
processes: state.processes.map((process) =>
process?.resource?.pk === action?.payload?.resource?.pk
&& process?.processType === action?.payload?.processType
? action.payload
: process)
};
}
default:
return state;
}
}

export default setLocalStorageProcesses(resourceservice);
export default resourceservice;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2022, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

import expect from 'expect';
import { getCurrentProcesses } from '../resourceservice';

const testState = {
resourceservice: {
processes: [{ name: 'test process' }]
}
};

describe('resourceservice selector', () => {

it('test getCurrentProcesses', () => {
expect(getCurrentProcesses(testState)).toEqual([{ name: 'test process' }]);
});

});
16 changes: 12 additions & 4 deletions geonode_mapstore_client/client/js/selectors/resourceservice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import { getResourceData } from '@js/selectors/resource';
import { ProcessTypes } from '@js/utils/ResourceServiceUtils';

export const isProcessCompleted = (state, payload) =>
(state?.resourceservice?.processes?.find(process =>
export const isProcessCompleted = (state, payload) => {
const completedProcess = state?.resourceservice?.processes?.find(process =>
process?.resource?.pk === payload?.resource?.pk
&& process?.processType === payload?.processType
) || {}).completed;
&& process?.processType === payload?.processType
) || {};

return completedProcess.completed;
};

export const getCurrentResourcePermissionsLoading = (state) => {
const resource = getResourceData(state);
Expand Down Expand Up @@ -44,3 +47,8 @@ export const getCurrentResourceDeleteLoading = (state) => {
const isLoading = permissionsProcess ? !permissionsProcess?.completed : false;
return isLoading;
};

export const getCurrentProcesses = (state) => {
const processes = state?.resourceservice?.processes || [];
return processes;
};
31 changes: 0 additions & 31 deletions geonode_mapstore_client/client/js/utils/LocalStorageUtils.js

This file was deleted.

0 comments on commit 092832f

Please sign in to comment.