Skip to content

Commit

Permalink
- optimized snapshot creation
Browse files Browse the repository at this point in the history
- optimized snapshot restoration
- fixed bugs when switching between snapshots in one project
  • Loading branch information
boriskovar-m2ms committed Jun 10, 2024
1 parent 634fd8d commit e68fab1
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 107 deletions.
2 changes: 2 additions & 0 deletions docker-compose.dev.vector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ services:
PROXY_FORWARDED_PROTO_HEADER: 'http'
LEGACY_URL: 'https://fragalysis.diamond.ac.uk'
DEPLOYMENT_MODE: 'development'
DISABLE_RESTRICT_PROPOSALS_TO_MEMBERSHIP: 'True'
PUBLIC_TAS: 'lb18145-1'
# INFECTIONS: 'structure-download'
depends_on:
database:
Expand Down
170 changes: 86 additions & 84 deletions js/components/snapshot/redux/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,94 +310,96 @@ export const createNewSnapshot = ({
let project = { projectID: session_project, authorID: author };
console.log('created snapshot id: ' + res.data.id);

return Promise.resolve(dispatch(saveCurrentActionsList(snapshot, project, nglViewList))).then(async () => {
if (disableRedirect === false) {
if (selectedSnapshotToSwitch != null) {
if (createDiscourse) {
dispatch(createSnapshotDiscoursePost(res.data.id));
return Promise.resolve(dispatch(saveCurrentActionsList(snapshot, project, nglViewList, false))).then(
async () => {
if (disableRedirect === false) {
if (selectedSnapshotToSwitch != null) {
if (createDiscourse) {
dispatch(createSnapshotDiscoursePost(res.data.id));
}
//window.location.replace(`${URLS.projects}${session_project}/${selectedSnapshotToSwitch}`);
await dispatch(changeSnapshot(session_project, selectedSnapshotToSwitch, nglViewList, stage));
dispatch(setOpenSnapshotSavingDialog(false));
dispatch(setIsLoadingSnapshotDialog(false));
} else {
// A hacky way of changing the URL without triggering react-router
window.history.replaceState(
null,
null,
`${URLS.projects}${session_project}/${
selectedSnapshotToSwitch === null ? res.data.id : selectedSnapshotToSwitch
}`
);
api({ url: `${base_url}/api/session-projects/${session_project}/` })
.then(async projectResponse => {
const response = await api({
url: `${base_url}/api/snapshots/?session_project=${session_project}`
});

const length = response.data.results.length;
if (length === 0) {
dispatch(resetCurrentSnapshot());
} else {
const createdSnapshot =
response.data.results && response.data.results.find(r => r.id === res.data.id);
console.log('new snapshot id: ' + JSON.stringify(createdSnapshot?.id));

if (createdSnapshot !== undefined && createdSnapshot !== null) {
// If the tree fails to load, bail out first without modifying the store
await dispatch(loadSnapshotTree(projectResponse.data.id));
await dispatch(
setCurrentSnapshot({
id: createdSnapshot.id,
type: createdSnapshot.type,
title: createdSnapshot.title,
author: createdSnapshot.author,
description: createdSnapshot.description,
created: createdSnapshot.created,
children: createdSnapshot.children,
parent: createdSnapshot.parent,
data: '[]'
})
);
await dispatch(
setCurrentProject({
projectID: projectResponse.data.id,
authorID: projectResponse.data.author || null,
title: projectResponse.data.title,
description: projectResponse.data.description,
targetID: projectResponse.data.target.id,
tags: JSON.parse(projectResponse.data.tags)
})
);
if (createDiscourse) {
dispatch(createSnapshotDiscoursePost());
}
dispatch(setOpenSnapshotSavingDialog(false));
dispatch(setIsLoadingSnapshotDialog(false));
dispatch(setSnapshotJustSaved(projectResponse.data.id));
dispatch(setDialogCurrentStep());
}
}
})
.catch(error => {
dispatch(resetCurrentSnapshot());
dispatch(setIsLoadingSnapshotDialog(false));
console.log(`Error while saving snapshot: ${error}`);
});
}
//window.location.replace(`${URLS.projects}${session_project}/${selectedSnapshotToSwitch}`);
await dispatch(changeSnapshot(session_project, selectedSnapshotToSwitch, nglViewList, stage));
} else {
dispatch(setOpenSnapshotSavingDialog(false));
dispatch(setIsLoadingSnapshotDialog(false));
} else {
// A hacky way of changing the URL without triggering react-router
window.history.replaceState(
null,
null,
`${URLS.projects}${session_project}/${
selectedSnapshotToSwitch === null ? res.data.id : selectedSnapshotToSwitch
}`
);
api({ url: `${base_url}/api/session-projects/${session_project}/` })
.then(async projectResponse => {
const response = await api({
url: `${base_url}/api/snapshots/?session_project=${session_project}`
});

const length = response.data.results.length;
if (length === 0) {
dispatch(resetCurrentSnapshot());
} else {
const createdSnapshot =
response.data.results && response.data.results.find(r => r.id === res.data.id);
console.log('new snapshot id: ' + JSON.stringify(createdSnapshot?.id));

if (createdSnapshot !== undefined && createdSnapshot !== null) {
// If the tree fails to load, bail out first without modifying the store
await dispatch(loadSnapshotTree(projectResponse.data.id));
await dispatch(
setCurrentSnapshot({
id: createdSnapshot.id,
type: createdSnapshot.type,
title: createdSnapshot.title,
author: createdSnapshot.author,
description: createdSnapshot.description,
created: createdSnapshot.created,
children: createdSnapshot.children,
parent: createdSnapshot.parent,
data: '[]'
})
);
await dispatch(
setCurrentProject({
projectID: projectResponse.data.id,
authorID: projectResponse.data.author || null,
title: projectResponse.data.title,
description: projectResponse.data.description,
targetID: projectResponse.data.target.id,
tags: JSON.parse(projectResponse.data.tags)
})
);
if (createDiscourse) {
dispatch(createSnapshotDiscoursePost());
}
dispatch(setOpenSnapshotSavingDialog(false));
dispatch(setIsLoadingSnapshotDialog(false));
dispatch(setSnapshotJustSaved(projectResponse.data.id));
dispatch(setDialogCurrentStep());
}
}
dispatch(
setSharedSnapshot({
title,
description,
url: `${base_url}${URLS.projects}${session_project}/${res.data.id}`
})
.catch(error => {
dispatch(resetCurrentSnapshot());
dispatch(setIsLoadingSnapshotDialog(false));
console.log(`Error while saving snapshot: ${error}`);
});
);
return res.data.id;
}
} else {
dispatch(setOpenSnapshotSavingDialog(false));
dispatch(setIsLoadingSnapshotDialog(false));
dispatch(
setSharedSnapshot({
title,
description,
url: `${base_url}${URLS.projects}${session_project}/${res.data.id}`
})
);
return res.data.id;
}
});
);
}
});
})
Expand Down Expand Up @@ -508,7 +510,7 @@ export const createNewSnapshotWithoutStateModification = ({

let snapshot = { id: res.data.id, title: title };
let project = { projectID: session_project, authorID: author };
dispatch(saveCurrentActionsList(snapshot, project, nglViewList, true));
dispatch(saveCurrentActionsList(snapshot, project, nglViewList, true, true));
}
});
});
Expand Down Expand Up @@ -566,7 +568,7 @@ export const saveAndShareSnapshot = (nglViewList, showDialog = true, axuData = {
const parent = null;
const session_project = projectID;

await dispatch(sendTrackingActionsByProjectId(projectID, author));
await dispatch(sendTrackingActionsByProjectId(projectID, author, true));

await dispatch(
createNewSnapshotWithoutStateModification({
Expand Down
7 changes: 7 additions & 0 deletions js/reducers/tracking/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ export const setProjectActionListLoaded = isLoaded => {
};
};

export const setSnapshotActionsDownloaded = isDownloaded => {
return {
type: constants.SET_SNAPSHOT_ACTIONS_DOWNLOADED,
isDownloaded: isDownloaded
};
};

export const setSnapshotImageActionList = function(snapshotActionImageList) {
return {
type: constants.SET_SNAPSOT_IMAGE_ACTIONS_LIST,
Expand Down
3 changes: 2 additions & 1 deletion js/reducers/tracking/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const constants = {
SET_UNDO_REDO_ACTIONS_LIST: prefix + 'SET_UNDO_REDO_ACTIONS_LIST',
SET_PROJECT_ACTIONS_LIST_LOADED: prefix + 'SET_PROJECT_ACTIONS_LIST_LOADED',
SET_SKIP_ORIENTATION_CHANGE: prefix + 'SET_SKIP_ORIENTATION_CHANGE', //when snapshot is switched we want to skip orientation change so it doesn't blink through multiple states
SET_IS_SNAPSHOT_DIRTY: prefix + 'SET_IS_SNAPSHOT_DIRTY'
SET_IS_SNAPSHOT_DIRTY: prefix + 'SET_IS_SNAPSHOT_DIRTY',
SET_SNAPSHOT_ACTIONS_DOWNLOADED: prefix + 'SET_SNAPSHOT_ACTIONS_DOWNLOADED'
};

export const actionType = {
Expand Down
60 changes: 43 additions & 17 deletions js/reducers/tracking/dispatchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
setIsTrackingCompoundsRestoring,
setIsUndoRedoAction,
setProjectActionListLoaded,
setIsSnapshotDirty
setIsSnapshotDirty,
setSnapshotActionsDownloaded
} from './actions';
import { createInitAction } from './trackingActions';
import { actionType, actionObjectType, NUM_OF_SECONDS_TO_IGNORE_MERGE, mapTypesStrings } from './constants';
Expand Down Expand Up @@ -169,6 +170,7 @@ import {
import { turnSide } from '../../components/preview/viewerControls/redux/actions';
import { getQualityOffActions } from './utils';
import { compoundsColors } from '../../components/preview/compounds/redux/constants';
import { isEqual, uniqWith } from 'lodash';

export const addCurrentActionsListToSnapshot = (snapshot, project, nglViewList) => async (dispatch, getState) => {
let projectID = project && project.projectID;
Expand All @@ -177,19 +179,27 @@ export const addCurrentActionsListToSnapshot = (snapshot, project, nglViewList)
await dispatch(setSnapshotToActions(actionList, snapshot, projectID, project, nglViewList, true));
};

export const saveCurrentActionsList = (snapshot, project, nglViewList, all = false) => async (dispatch, getState) => {
export const saveCurrentActionsList = (snapshot, project, nglViewList, all, isAnonymousSnapshot = false) => async (
dispatch,
getState
) => {
let projectID = project && project.projectID;
let actionList = await dispatch(getTrackingActions(projectID));
let actionList = await dispatch(getTrackingActions(isAnonymousSnapshot ? null : projectID));

if (all === false) {
dispatch(setSnapshotToActions(actionList, snapshot, projectID, project, nglViewList, false));
} else {
dispatch(setSnapshotToAllActions(actionList, snapshot, projectID));
if (!isAnonymousSnapshot) {
if (all === false) {
dispatch(setSnapshotToActions(actionList, snapshot, projectID, project, nglViewList, false));
} else {
dispatch(setSnapshotToAllActions(actionList, snapshot, projectID));
}
}
await dispatch(saveActionsList(project, snapshot, actionList, nglViewList));
await dispatch(saveActionsList(project, snapshot, actionList, nglViewList /*, isAnonymousSnapshot*/));
};

const saveActionsList = (project, snapshot, actionList, nglViewList) => async (dispatch, getState) => {
const saveActionsList = (project, snapshot, actionList, nglViewList, isAnonymousSnapshot = false) => async (
dispatch,
getState
) => {
const state = getState();
const snapshotID = snapshot && snapshot.id;
if (snapshotID) {
Expand Down Expand Up @@ -526,7 +536,9 @@ const saveActionsList = (project, snapshot, actionList, nglViewList) => async (d
currentActions.push(Object.assign({ ...trackAction }));
}

await dispatch(saveSnapshotAction(snapshot, project, currentActions));
if (!isAnonymousSnapshot) {
await dispatch(saveSnapshotAction(snapshot, project, currentActions));
}
await dispatch(saveTrackingActions(currentActions, snapshotID));
dispatch(setCurrentActionsList(currentActions));
}
Expand Down Expand Up @@ -905,8 +917,10 @@ export const restoreCurrentActionsList = snapshotID => async (dispatch, getState
};

const restoreTrackingActions = snapshotID => async (dispatch, getState) => {
const state = getState();
const areSnapshotActionsDownloaded = state.trackingReducers.areSnapshotActionsDownloaded;
// console.log(`snapshotDebug - restoreTrackingActions - start`);
if (snapshotID) {
if (snapshotID && !areSnapshotActionsDownloaded) {
try {
// console.log(`snapshotDebug - restoreTrackingActions - before getting actions`);
return api({
Expand All @@ -923,6 +937,7 @@ const restoreTrackingActions = snapshotID => async (dispatch, getState) => {

let snapshotActions = [...listToSet];
dispatch(setCurrentActionsList(snapshotActions));
dispatch(setSnapshotActionsDownloaded(true));
// console.log(`snapshotDebug - restoreTrackingActions - end - success`);
return Promise.resolve(snapshotActions);
// return Promise.resolve();
Expand Down Expand Up @@ -969,10 +984,16 @@ const restoreTargetActions = orderedActionList => (dispatch, getState) => {
};

export const restoreAfterTargetActions = (stages, projectId, snapshotId) => async (dispatch, getState) => {
const currentActionList = await dispatch(restoreTrackingActions(snapshotId));

const state = getState();

const areSnapshotActionsDownloaded = state.trackingReducers.areSnapshotActionsDownloaded;
let currentActionList = [];
if (!areSnapshotActionsDownloaded) {
currentActionList = await dispatch(restoreTrackingActions(snapshotId));
} else {
currentActionList = state.trackingReducers.current_actions_list;
}

// const currentActionList = state.trackingReducers.current_actions_list;
const orderedActionList = currentActionList.sort((a, b) => a.timestamp - b.timestamp);
// console.log(`snapshotDebug - restoreAfterTargetActions - no. of actions: ${orderedActionList.length}`);
Expand Down Expand Up @@ -3739,7 +3760,7 @@ export const checkSendTrackingActions = (save = false) => async (dispatch, getSt
const sendActions = state.trackingReducers.send_actions_list;
const length = sendActions.length;

if (length >= CONSTANTS.COUNT_SEND_TRACK_ACTIONS || save) {
if (/*length >= CONSTANTS.COUNT_SEND_TRACK_ACTIONS || */ save) {
await dispatch(sendTrackingActions(sendActions, currentProject, true));
}
};
Expand Down Expand Up @@ -3797,6 +3818,8 @@ const getTrackingActions = (projectID, withTreeSeparation) => (dispatch, getStat
const currentProject = state.projectReducers.currentProject;
const currentProjectID = currentProject && currentProject.projectID;
const sendActions = state.trackingReducers.send_actions_list;
const currentActionList = state.trackingReducers.current_actions_list;
const trackActionsList = state.trackingReducers.track_actions_list;

if (projectID) {
dispatch(setIsActionsLoading(true));
Expand Down Expand Up @@ -3837,7 +3860,10 @@ const getTrackingActions = (projectID, withTreeSeparation) => (dispatch, getStat
dispatch(setIsActionsLoading(false));
});
} else {
let projectActions = [...sendActions];
// let projectActions = [...sendActions];
// const projectActions = [];
let projectActions = [...currentActionList, ...sendActions, ...trackActionsList];
projectActions = uniqWith(projectActions, isEqual);
dispatch(setProjectActionList(projectActions));
return Promise.resolve(projectActions);
}
Expand Down Expand Up @@ -3906,14 +3932,14 @@ const copyActionsToProject = (toProject, setActionList = true, clear = false) =>
}
};

export const sendTrackingActionsByProjectId = (projectID, authorID) => async (dispatch, getState) => {
export const sendTrackingActionsByProjectId = (projectID, authorID, offline = false) => async (dispatch, getState) => {
const state = getState();
const currentProject = state.projectReducers.currentProject;
const currentProjectID = currentProject && currentProject.projectID;

const project = { projectID, authorID };

await dispatch(getTrackingActions(currentProjectID));
await dispatch(getTrackingActions(offline ? null : currentProjectID));
await dispatch(copyActionsToProject(project, false, currentProjectID && currentProjectID != null ? true : false));
};

Expand Down
Loading

0 comments on commit e68fab1

Please sign in to comment.