Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load projects and samples #114

Merged
merged 17 commits into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions src/api/route-services/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const logger = require('../../utils/logging');
const { OK, NotFoundError } = require('../../utils/responses');

const SamplesService = require('./samples');
const ExperimentService = require('./experiment');

const samplesService = new SamplesService();

const experimentService = new ExperimentService();
class ProjectsService {
constructor() {
this.tableName = `projects-${config.clusterEnv}`;
Expand All @@ -28,13 +29,13 @@ class ProjectsService {

const dynamodb = createDynamoDbInstance();
const response = await dynamodb.getItem(params).promise();

if (response.Item) {
const prettyResponse = convertToJsObject(response.Item);
return prettyResponse.projects;
}

throw new NotFoundError('Project not found');
logger.log('Project not found');
return response;
}

async updateProject(projectUuid, project) {
Expand Down Expand Up @@ -65,6 +66,80 @@ class ProjectsService {
}
}

/**
* Finds all projects referenced in experiments.
*/
async getProjects() {
// Get project data from the experiments table. Only return
// those tables that have a project ID associated with them.
const params = {
TableName: experimentService.experimentsTableName,
ExpressionAttributeNames: {
'#pid': 'projectId',
},
FilterExpression: 'attribute_exists(projectId)',
ProjectionExpression: '#pid',
};

const dynamodb = createDynamoDbInstance();
const response = await dynamodb.scan(params).promise();
Copy link
Member

@cosa65 cosa65 May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This scan will need to be over the experiments table (and only getting projectId), then with the projectIds you find there you'll need ot get the projects from projects table (some of the experiments won't have projectIds so you could deal with that by adding a FilterExpression with attribute_exists in the params).

For loading projects based on the projectIds you got from experiments you could use BatchGetItem https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#batchGetItem-property,
also keep in mind that the experiments table has projectId field while projects table has projectUuid, so there's a decision here, either using projectId and changing the projects and samples tables or projectUuid and changing the experiments table. (or just doing a _.transform() between the result of the experiments scan and the projects batch get to change projectId -> projectUuid and leave this matter for some future refactoring) .


if (!response.Items) {
throw new NotFoundError('No projects available!');
}

const projectIds = response.Items.map((entry) => convertToJsObject(entry).projectId);
return this.getProjectsFromIds(new Set(projectIds));
}

/**
* Returns information about a group of projects.
*
* @param {Set} projectIds A Set of projectId values that are to be queried.
* @returns An object containing descriptions of projects.
*/
async getProjectsFromIds(projectIds) {
const dynamodb = createDynamoDbInstance();
const params = {
RequestItems: {
[this.tableName]: {
Keys: [...projectIds].map((projectUuid) => convertToDynamoDbRecord({ projectUuid })),
},
},
};

const data = await dynamodb.batchGetItem(params).promise();
const existingProjectIds = new Set(data.Responses[this.tableName].map((entry) => {
const newData = convertToJsObject(entry);
return newData.projects.uuid;
}));

// Build up projects that do not exist in Dynamo yet.
const projects = [...projectIds]
.filter((entry) => (
!existingProjectIds.has(entry)
))
.map((emptyProject) => {
const newProject = {};

const id = emptyProject;
newProject.name = id;
newProject.uuid = id;
newProject.samples = [];
newProject.metadataKeys = [];
newProject.experiments = [id];

return newProject;
});

data.Responses[this.tableName].forEach((entry) => {
const newData = convertToJsObject(entry);
projects.push(newData.projects);
});

return projects;
}

async deleteProject(projectUuid) {
logger.log(`Deleting project with id ${projectUuid}`);
const marshalledKey = convertToDynamoDbRecord({
Expand Down
6 changes: 4 additions & 2 deletions src/api/route-services/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ class SamplesService {
const dynamodb = createDynamoDbInstance();

const response = await dynamodb.query(params).promise();
const items = response.Items;

if (response.Items) {
if (items) {
const prettyResponse = response.Items.map((item) => convertToJsObject(item));
return prettyResponse;
}
Comment on lines +30 to 35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just keep line 32 as response.Items? that's how it's also used in the next line


throw new NotFoundError('Samples not found');
throw new NotFoundError('Samples not found!');
}


async getSamplesByExperimentId(experimentId) {
logger.log(`Gettings samples using experimentId : ${experimentId}`);
const marshalledKey = convertToDynamoDbRecord({
Expand Down
5 changes: 5 additions & 0 deletions src/api/routes/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ module.exports = {
.then((data) => res.json(data))
.catch(next);
},

'projects#get': (req, res, next) => {
projectsService.getProjects().then((response) => res.json(response)).catch(next);
},

};
18 changes: 17 additions & 1 deletion src/specs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -589,4 +589,20 @@ paths:
experimentId:
type: string
samples:
$ref: ./models/api-body-schemas/Samples.v1.yaml
$ref: ./models/api-body-schemas/Samples.v1.yaml
'/projects':
get:
summary: ''
operationId: getProjects
x-eov-operation-id: projects#get
ivababukova marked this conversation as resolved.
Show resolved Hide resolved
x-eov-operation-handler: routes/projects
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: ./models/api-body-schemas/Project.v1.yaml
description: Get the projects