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 5 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
46 changes: 43 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,45 @@ class ProjectsService {
}
}

async getProjects() {
const params = {
TableName: experimentService.experimentsTableName,
AttributesToGet: ['projectId'],
};

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) .

const projectIds = [];
response.Items.forEach((entry) => {
if (entry.projectId) {
StefanBabukov marked this conversation as resolved.
Show resolved Hide resolved
const newEntry = convertToJsObject(entry);
projectIds.push(newEntry.projectId);
}
});
const projects = [];

if (projectIds.length) {
return new Promise((resolve) => {
projectIds.forEach(async (id) => {
const newData = await this.getProject(id);
StefanBabukov marked this conversation as resolved.
Show resolved Hide resolved

if (!Object.keys(newData).length) {
newData.name = id;
newData.uuid = id;
newData.samples = [];
newData.metadataKeys = [];
newData.experiments = [id];
}
projects.push(newData);
if (projects.length === projectIds.length) {
resolve(projects);
}
});
});
}
throw new NotFoundError('no projects found');
}

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 ? response.Items[0] : response.Item;
marcellp marked this conversation as resolved.
Show resolved Hide resolved

if (response.Items) {
if (items) {
const prettyResponse = response.Items.map((item) => convertToJsObject(item));
return prettyResponse;
}

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