Skip to content

Commit

Permalink
Implemented Daniel Wu's PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alantao912 committed Oct 29, 2023
1 parent 4bd534e commit fd4939a
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 88 deletions.
51 changes: 12 additions & 39 deletions serverless/packages/functions/src/dbutils/put_trainspace.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion serverless/packages/functions/src/trainspace/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

export enum TrainStatus {
QUEUED = "QUEUE",
QUEUED = "QUEUED",
STARTING="STARTING",
UPLOADING="UPLOADING",
TRAINING="TRAINING",
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -11,30 +11,32 @@ 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,
body: JSON.stringify({ message: "Invalid request body" })
}
}


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."})
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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'],
Expand All @@ -21,7 +21,7 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
user_arch: eventBody['user_arch']
}, "");

if (putQueryBody == null)
if (putCommandInput == null)
{
return {
statusCode: 400,
Expand All @@ -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,
Expand Down
21 changes: 9 additions & 12 deletions serverless/packages/functions/src/trainspace/delete_trainspace.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
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." }),
};
}

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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -10,12 +9,12 @@ export const handler: APIGatewayProxyHandlerV2 = async (event) => {
];

const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

const fetchedTrainspaceIds: Array<string> = [];
let lastEvaluatedKey = undefined;

do {
const getCommand = new QueryCommand({
const getCommand: QueryCommand = new QueryCommand({
TableName: "trainspace",
IndexName: "uid",
KeyConditionExpression: "uid = :uid",
Expand All @@ -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<string> = results['Items']?.map(trainspace => trainspace['trainspace_id'].S);
page.forEach(id => fetchedTrainspaceIds.push(id));

if (results['Items']) {
const page: Array<string | undefined> = results['Items']?.map(trainspace => trainspace['trainspace_id'].S);
page.forEach(id => { if (id) fetchedTrainspaceIds.push(id); });
}


} while (lastEvaluatedKey !== undefined);
return {
Expand Down
25 changes: 11 additions & 14 deletions serverless/packages/functions/src/trainspace/get_trainspace.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
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."})
};
}

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 {
Expand Down
14 changes: 12 additions & 2 deletions serverless/stacks/AppStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 ?? ""
});
}

0 comments on commit fd4939a

Please sign in to comment.