Skip to content

Commit

Permalink
Update collections inheritance & add minor polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
blokhin committed Dec 15, 2023
1 parent f6f7325 commit 646a291
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Metis data management GUI: Backend for Frontend

<p class="what_is_metis"><dfn>Metis</dfn> is an open scientific framework, materials data organizer, and collaborative online platform for the nanotechnology research. It was designed for the materials research teams at the physical and virtual laboratories. Metis is an AI-ready solution, aiming to bring the recent advances of computer science into a rather conservative area of new materials development and quality control. Metis currently focuses on the X-ray powder diffraction and atomistic simulations. It was started in 2021 in BASF (Ludwigshafen am Rhein, Germany) by Bernd Hinrichsen and Evgeny Blokhin.</p>
<p class="what_is_metis"><dfn>Metis</dfn> is an open scientific framework, materials data organizer, and collaborative online platform for the nanotechnology research. It was designed for the offline physical and online virtual autonomous laboratories dealing with the materials science. Metis is an AI-ready solution, aiming to bring the recent advances of computer science into a rather conservative area of new materials development and quality control. Metis currently focuses on the X-ray powder diffraction and atomistic simulations. Its development was started in 2021 in BASF (Ludwigshafen am Rhein, Germany) by Bernd Hinrichsen and Evgeny Blokhin.</p>

<p align="center"><img src="https://github.com/basf/metis-backend/blob/master/logo.png" width="300" /></p>

Expand Down
3 changes: 2 additions & 1 deletion routes/[version]/calculations/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ async function getAndPrepareCalcResults(userId, calculations, progress, result)
const { id } = await db(USER_DATASOURCES_TABLE).where({ uuid: parent }).first('id');
if (!id) return { error: 'Absent parent datasource' };

// collections inheritance
const parentCollections = await selectUserCollections(
{ id: userId },
{ dataSourceIds: [id] }
Expand All @@ -112,7 +113,7 @@ async function getAndPrepareCalcResults(userId, calculations, progress, result)
dataSources.data.push(dataSource);
}

// get & prepare result datasources from sci. backend
// get & prepare result datasources from backend
const preparedData = await getAndPrepareDataSources(dataSources);
results = preparedData.data.map((dataSource) => ({ ...dataSource, progress }));
}
Expand Down
11 changes: 9 additions & 2 deletions routes/[version]/collections/_helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
COLLECTIONS_TYPES_TABLE,
SHARED_COLLECTION_VISIBILITY,
PRIVATE_COLLECTION_VISIBILITY,
USER_COLLECTIONS_TABLE,
USER_SHARED_COLLECTIONS_TABLE,
USER_DATASOURCES_TABLE,
Expand Down Expand Up @@ -97,7 +98,10 @@ async function changeOwnership(collectionId, userId) {
.select('userId')
.from(USER_COLLECTIONS_TABLE)
.where('id', collectionId);
if (!ownerId || ownerId[0].userId !== SERVICE_UID) return 'Invalid collection selected';

if (!ownerId || ownerId[0].userId !== SERVICE_UID || userId === SERVICE_UID) {
throw new Error('Unsuitable collection selected');
}

const targetIds = await db
.select('dataSourceId')
Expand All @@ -110,7 +114,10 @@ async function changeOwnership(collectionId, userId) {
});

await db(USER_DATASOURCES_TABLE).whereIn('id', targets).update({ userId });
await db(USER_COLLECTIONS_TABLE).where('id', collectionId).update({ userId });
await db(USER_COLLECTIONS_TABLE).where('id', collectionId).update({
userId,
visibility: PRIVATE_COLLECTION_VISIBILITY,
});

return false;
}
6 changes: 5 additions & 1 deletion routes/[version]/collections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
};

/**
* @api {put} /v0/collections Create a collection
* @api {put} /v0/collections Create or update a collection
* @apiName CreateCollection
* @apiGroup Collections
* @apiPermission API
Expand All @@ -25,6 +25,10 @@ async function put(req, res, next) {
});
}

if (req.body.id === null) {
return next({ status: StatusCodes.BAD_REQUEST, error: 'ID should be omitted.' });
}

const reqId = req.id;

res.status(StatusCodes.ACCEPTED).json({ reqId });
Expand Down
2 changes: 1 addition & 1 deletion routes/[version]/collections/ownership.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
};

/**
* @api {post} /v0/collections/ownership Get data into work and change ownership (experimental)
* @api {post} /v0/collections/ownership Take on the data into work changing ownership
* @apiName ListCollectionData
* @apiGroup Collections
* @apiPermission GUI_ONLY
Expand Down
16 changes: 14 additions & 2 deletions routes/[version]/datasources/_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const {
insertUserDataSource,
deleteUserDataSource,
selectDataSourcesIdMap,
selectUserCollections,
delsertDataSourceCollections,
} = require('../../../services/db');

const {
Expand All @@ -28,7 +30,7 @@ async function createAndSaveDataSource(userId, content, fmt, name) {
return insertUserDataSource(userId, { uuid: data.uuid });
}

async function importAndSaveDataSource(userId, externalId) {
async function importAndSaveDataSource(userId, externalId, parentId) {
let response;
try {
response = await importDataSource(externalId);
Expand All @@ -42,7 +44,17 @@ async function importAndSaveDataSource(userId, externalId) {
throw 'Datasource cannot be used';
}

return insertUserDataSource(userId, { uuid: response.data.uuid });
// collections inheritance
const parentCollections = await selectUserCollections(
{ id: userId },
{ dataSourceIds: [parentId] }
);
const collectionIds = parentCollections.data.map(({ id }) => id);
const dataSource = await insertUserDataSource(userId, { uuid: response.data.uuid });

await delsertDataSourceCollections(dataSource.id, collectionIds);

return dataSource;
}

/**
Expand Down
16 changes: 13 additions & 3 deletions routes/[version]/datasources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const { StatusCodes } = require('http-status-codes');
const { checkAuth } = require('../../../middlewares/auth');
const { getUserDataSources } = require('../../../middlewares/db');

const { USERS_TABLE, selectFirstUser, selectUserCollections } = require('../../../services/db');

const { getAndPrepareCollections } = require('../collections/_helpers');

const {
createAndSaveDataSource,
getAndPrepareDataSources,
Expand Down Expand Up @@ -94,17 +98,17 @@ async function get(req, res, next) {
* @apiUse SSEStreamResponse
*/
async function put(req, res, next) {
if (!req.body.id) {
if (!req.body.id || !req.body.parent) {
return next({
status: StatusCodes.BAD_REQUEST,
error: 'Required field *id* is not provided.',
error: 'Required fields are not provided.',
});
}

const reqId = req.id;
res.status(StatusCodes.ACCEPTED).json({ reqId });

const datasource = await importAndSaveDataSource(req.user.id, req.body.id);
const datasource = await importAndSaveDataSource(req.user.id, req.body.id, req.body.parent);
if (!datasource)
return next({
status: StatusCodes.UNPROCESSABLE_ENTITY,
Expand All @@ -115,4 +119,10 @@ async function put(req, res, next) {
const data = await getAndPrepareDataSources(req.session.datasources);

res.sse.sendTo({ reqId, ...data }, 'datasources');

const user = await selectFirstUser({ [`${USERS_TABLE}.id`]: req.user.id });
const filters = await selectUserCollections(user);
const preparedFilters = await getAndPrepareCollections(filters);

res.sse.sendTo({ reqId, ...preparedFilters }, 'filters');
}
1 change: 0 additions & 1 deletion routes/[version]/webhooks/calc_update.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const {

const { is_valid_uuid } = require('./_helpers');
const { getAndPrepareCalcResults, deleteAndClearCalculation } = require('../calculations/_helpers');

const { getAndPrepareDataSources } = require('../datasources/_helpers');
const { getAndPrepareCollections } = require('../collections/_helpers');

Expand Down
5 changes: 3 additions & 2 deletions services/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const ADMIN_USER_ROLE = 'admin';
const PUBLIC_COLLECTION_VISIBILITY = 'community';
const SHARED_COLLECTION_VISIBILITY = 'shared';
const PRIVATE_COLLECTION_VISIBILITY = 'private';

const VISIBILITY_ENUM = [
PRIVATE_COLLECTION_VISIBILITY,
SHARED_COLLECTION_VISIBILITY,
Expand Down Expand Up @@ -114,8 +115,8 @@ module.exports = {
insertUserDataSource,
deleteUserDataSource,
delsertDataSourceCollections,
selectDataSourceByUserId, // ??
selectDataSourcesByUserId, // ??
selectDataSourceByUserId, // FIXME?
selectDataSourcesByUserId, // FIXME?

selectUserCollections,
insertUserCollection,
Expand Down

0 comments on commit 646a291

Please sign in to comment.