diff --git a/serverless/packages/functions/src/datasets/user/delete_user_uploaded_data.ts b/serverless/packages/functions/src/datasets/user/delete_url.ts similarity index 100% rename from serverless/packages/functions/src/datasets/user/delete_user_uploaded_data.ts rename to serverless/packages/functions/src/datasets/user/delete_url.ts diff --git a/serverless/packages/functions/src/dbutils/put_trainspace.ts b/serverless/packages/functions/src/dbutils/put_trainspace.ts index 5a44b10fc..480e8df44 100644 --- a/serverless/packages/functions/src/dbutils/put_trainspace.ts +++ b/serverless/packages/functions/src/dbutils/put_trainspace.ts @@ -1,50 +1,23 @@ import { TrainStatus } from '../trainspace/constants'; -import { PutItemCommandInput } from "@aws-sdk/client-dynamodb"; +import { PutCommandInput } from "@aws-sdk/lib-dynamodb"; -export function create_trainspace(trainspace_id: string, uid: string, data_source: string, dataset_data: object, name: string, parameters_data: object, review_data: string) : PutItemCommandInput | null +export function create_trainspace(trainspaceId: string, user_id: string, dataSource: string, dataset_data: object, trainspaceName: string, parameters_data: object, reviewData: string) : PutCommandInput | null { - let output: PutItemCommandInput = + let output: PutCommandInput = { TableName : "trainspace", Item : { - trainspace_id : - { - S : trainspace_id - }, - uid : - { - S : uid - }, - created : - { - S : Date.now().toString() - }, - data_source : - { - S : data_source - }, - dataset_data : - { - S : JSON.stringify(dataset_data) - }, - name : - { - S : name - }, - parameters_data : - { - S : JSON.stringify(parameters_data) - }, - review_data : - { - S : review_data - }, - status : - { - S : TrainStatus.QUEUED - } + trainspace_id : trainspaceId, + uid : user_id, + created : Date.now().toString(), + data_source : dataSource, + dataset_data : JSON.stringify(dataset_data), + name : trainspaceName, + parameters_data : JSON.stringify(parameters_data), + review_data : reviewData, + status : TrainStatus.QUEUED } } return output; diff --git a/serverless/packages/functions/src/trainspace/constants.ts b/serverless/packages/functions/src/trainspace/constants.ts index 4c52894c7..410db01fd 100644 --- a/serverless/packages/functions/src/trainspace/constants.ts +++ b/serverless/packages/functions/src/trainspace/constants.ts @@ -1,6 +1,6 @@ export enum TrainStatus { - QUEUED = "QUEUE", + QUEUED = "QUEUED", STARTING="STARTING", UPLOADING="UPLOADING", TRAINING="TRAINING", diff --git a/serverless/packages/functions/src/trainspace/create_image_trainspace.ts b/serverless/packages/functions/src/trainspace/create_image_trainspace.ts index 655c70162..91764bd6d 100644 --- a/serverless/packages/functions/src/trainspace/create_image_trainspace.ts +++ b/serverless/packages/functions/src/trainspace/create_image_trainspace.ts @@ -1,8 +1,8 @@ import { APIGatewayProxyHandlerV2 } from "aws-lambda"; import parseJwt from "@dlp-sst-app/core/parseJwt"; import { v4 as uuidv4 } from 'uuid'; -import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb'; import { create_trainspace } from "../dbutils/put_trainspace"; export const handler: APIGatewayProxyHandlerV2 = async (event) => { @@ -11,9 +11,9 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { const eventBody = JSON.parse(event.body? event.body : ""); const trainspaceId = uuidv4(); - const putQueryBody = create_trainspace(trainspaceId, uid, "IMAGE", eventBody['dataset_data'], eventBody['name'], eventBody['parameters_data'], eventBody['review_data']['notification_email']); + const putCommandInput = create_trainspace(trainspaceId, uid, "IMAGE", eventBody['dataset_data'], eventBody['name'], eventBody['parameters_data'], eventBody['review_data']['notification_email']); - if (putQueryBody == null) + if (putCommandInput == null) { return { statusCode: 400, @@ -21,20 +21,22 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { } } - const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); - - const command = new PutItemCommand(putQueryBody); + const command = new PutCommand(putCommandInput); const response = await docClient.send(command); + if (response.$metadata.httpStatusCode != 200) { + client.destroy(); + return { statusCode: 500, body: JSON.stringify({ message: "Internal server error."}) }; } - + + client.destroy(); return { statusCode: 200, body: JSON.stringify({ trainspaceId: trainspaceId, message: "Successfully created a new trainspace."}) diff --git a/serverless/packages/functions/src/trainspace/create_tabular_trainspace.ts b/serverless/packages/functions/src/trainspace/create_tabular_trainspace.ts index 230f4908c..b719cd1df 100644 --- a/serverless/packages/functions/src/trainspace/create_tabular_trainspace.ts +++ b/serverless/packages/functions/src/trainspace/create_tabular_trainspace.ts @@ -1,8 +1,8 @@ import { APIGatewayProxyHandlerV2 } from "aws-lambda"; import parseJwt from "@dlp-sst-app/core/parseJwt"; import { v4 as uuidv4 } from 'uuid'; -import { DynamoDBClient, PutItemCommand } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb'; import { create_trainspace } from '../dbutils/put_trainspace'; export const handler: APIGatewayProxyHandlerV2 = async (event) => { @@ -11,7 +11,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { const eventBody = JSON.parse(event.body? event.body : ""); const trainspaceId = uuidv4(); - const putQueryBody = create_trainspace(trainspaceId, uid, "TABULAR", eventBody['default'], eventBody['name'], + const putCommandInput = create_trainspace(trainspaceId, uid, "TABULAR", eventBody['default'], eventBody['name'], { criterion: eventBody['criterion'], optimizer_name: eventBody['optimizer_name'], @@ -21,7 +21,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { user_arch: eventBody['user_arch'] }, ""); - if (putQueryBody == null) + if (putCommandInput == null) { return { statusCode: 400, @@ -32,10 +32,10 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); - - const command = new PutItemCommand(putQueryBody); + const command = new PutCommand(putCommandInput); const response = await docClient.send(command); + if (response.$metadata.httpStatusCode != 200) { return { statusCode: 500, diff --git a/serverless/packages/functions/src/trainspace/delete_trainspace.ts b/serverless/packages/functions/src/trainspace/delete_trainspace.ts index f5786d80d..483f39179 100644 --- a/serverless/packages/functions/src/trainspace/delete_trainspace.ts +++ b/serverless/packages/functions/src/trainspace/delete_trainspace.ts @@ -1,14 +1,14 @@ import { APIGatewayProxyHandlerV2 } from "aws-lambda"; -import { DynamoDBClient, DeleteItemCommand } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { DynamoDBDocumentClient, DeleteCommand } from '@aws-sdk/lib-dynamodb'; export const handler: APIGatewayProxyHandlerV2 = async (event) => { let queryParams = null; if (event && (queryParams = event['pathParameters']) != null) { - const trainspace_id: string | undefined = queryParams['id']; + const trainspaceId: string | undefined = queryParams['id']; - if (trainspace_id == undefined) { + if (trainspaceId == undefined) { return { statusCode: 400, body: JSON.stringify({ message : "Malformed request content - trainspace ID missing." }), @@ -16,20 +16,17 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { } const client = new DynamoDBClient({}); - const documentClient = DynamoDBDocumentClient.from(client); + const docClient = DynamoDBDocumentClient.from(client); - const command = new DeleteItemCommand({ + const command = new DeleteCommand({ TableName : "trainspace", Key : { - "trainspace_id" : - { - S: trainspace_id - } + trainspace_id: trainspaceId } }); - const response = await documentClient.send(command); + const response = await docClient.send(command); if (response.$metadata.httpStatusCode == undefined || response.$metadata.httpStatusCode != 200) { return { @@ -39,7 +36,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { } return { statusCode: 200, - body: "Successfully deleted trainspace with id " + trainspace_id + body: "Successfully deleted trainspace with id " + trainspaceId } } return { diff --git a/serverless/packages/functions/src/trainspace/get_all_trainspace.ts b/serverless/packages/functions/src/trainspace/get_all_trainspace.ts index 5b1d715bf..916d33ee5 100644 --- a/serverless/packages/functions/src/trainspace/get_all_trainspace.ts +++ b/serverless/packages/functions/src/trainspace/get_all_trainspace.ts @@ -1,7 +1,6 @@ import { APIGatewayProxyHandlerV2 } from "aws-lambda"; import parseJwt from "@dlp-sst-app/core/parseJwt"; import { DynamoDBClient, QueryCommand } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; export const handler: APIGatewayProxyHandlerV2 = async (event) => { if (event) { @@ -10,12 +9,12 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { ]; const client = new DynamoDBClient({}); - const docClient = DynamoDBDocumentClient.from(client); + const fetchedTrainspaceIds: Array = []; let lastEvaluatedKey = undefined; do { - const getCommand = new QueryCommand({ + const getCommand: QueryCommand = new QueryCommand({ TableName: "trainspace", IndexName: "uid", KeyConditionExpression: "uid = :uid", @@ -28,10 +27,14 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => { ExclusiveStartKey: lastEvaluatedKey }); - const results = await docClient.send(getCommand); + const results = await client.send(getCommand); lastEvaluatedKey = results.LastEvaluatedKey; - const page: Array = results['Items']?.map(trainspace => trainspace['trainspace_id'].S); - page.forEach(id => fetchedTrainspaceIds.push(id)); + + if (results['Items']) { + const page: Array = results['Items']?.map(trainspace => trainspace['trainspace_id'].S); + page.forEach(id => { if (id) fetchedTrainspaceIds.push(id); }); + } + } while (lastEvaluatedKey !== undefined); return { diff --git a/serverless/packages/functions/src/trainspace/get_trainspace.ts b/serverless/packages/functions/src/trainspace/get_trainspace.ts index f7d83ce5c..f6eb49357 100644 --- a/serverless/packages/functions/src/trainspace/get_trainspace.ts +++ b/serverless/packages/functions/src/trainspace/get_trainspace.ts @@ -1,14 +1,14 @@ -import { APIGatewayProxyHandlerV2, APIGatewayProxyEvent } from "aws-lambda"; -import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'; -import { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb'; +import { APIGatewayProxyHandlerV2, APIGatewayProxyEventV2 } from "aws-lambda"; +import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; +import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb'; -export const handler: APIGatewayProxyHandlerV2 = async (event : APIGatewayProxyEvent) => { +export const handler: APIGatewayProxyHandlerV2 = async (event : APIGatewayProxyEventV2) => { const queryParams = event['pathParameters']; if (queryParams != null) { - const trainspace_id: string | undefined = queryParams['id']; + const trainspaceId: string | undefined = queryParams['id']; - if (trainspace_id == undefined) { + if (trainspaceId == undefined) { return { statusCode: 400, body: JSON.stringify({message: "Malformed request content - trainspace ID missing."}) @@ -16,20 +16,17 @@ export const handler: APIGatewayProxyHandlerV2 = async (event : APIGatewayProxyE } const client: DynamoDBClient = new DynamoDBClient({}); - const documentClient: DynamoDBDocumentClient = DynamoDBDocumentClient.from(client); - - const command : GetItemCommand = new GetItemCommand({ + const docClient = DynamoDBDocumentClient.from(client); + + const command : GetCommand = new GetCommand({ TableName : "trainspace", Key : { - "trainspace_id" : - { - S: trainspace_id - } + trainspace_id : trainspaceId } }); - const response = await documentClient.send(command); + const response = await docClient.send(command); if (!response.Item) { return { diff --git a/serverless/stacks/AppStack.ts b/serverless/stacks/AppStack.ts index 1c9b791cf..caa87bc40 100644 --- a/serverless/stacks/AppStack.ts +++ b/serverless/stacks/AppStack.ts @@ -37,7 +37,7 @@ export function AppStack({ stack }: StackContext) { "GET /datasets/user/{type}/{filename}/columns": "packages/functions/src/datasets/user/columns.handler", "DELETE /dataset/user/{type}/{filename}" : - "packages/functions/src/datasets/user/delete_user_uploaded_data.handler", + "packages/functions/src/datasets/user/delete_url.handler", "POST /trainspace/tabular": { function: { handler: "packages/functions/src/trainspace/create_tabular_trainspace.handler", @@ -82,6 +82,16 @@ export function AppStack({ stack }: StackContext) { api.getFunction("GET /datasets/user/{type}")?.functionName ?? "", GetUserDatasetColumnsFunctionName: api.getFunction("GET /datasets/user/{type}/{filename}/columns") - ?.functionName ?? "" + ?.functionName ?? "", + PutTabularTrainspaceFunctionName: + api.getFunction("POST /trainspace/tabular")?.functionName ?? "", + PutImageTrainspaceFunctionName: + api.getFunction("POST /trainspace/tabular")?.functionName ?? "", + GetAllTrainspaceIdsFunctionName: + api.getFunction("GET /trainspace")?.functionName ?? "", + GetTrainspaceByIdFunctionName: + api.getFunction("GET /trainspace/{id}")?.functionName ?? "", + DeleteTrainspaceByIdFunctionName: + api.getFunction("DELETE /trainspace/{id}")?.functionName ?? "" }); }