Skip to content

Commit

Permalink
#146 - Include createdDate/insertedDate and completedDate to Document…
Browse files Browse the repository at this point in the history
… Action
  • Loading branch information
mfriesen committed Nov 25, 2023
1 parent 7d96f8f commit 0bea990
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 12 deletions.
73 changes: 73 additions & 0 deletions actions/src/main/java/com/formkiq/module/actions/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static com.formkiq.aws.dynamodb.objects.Strings.isEmpty;
import static com.formkiq.module.actions.ActionParameters.PARAMETER_QUEUE_NAME;
import static software.amazon.awssdk.services.dynamodb.model.AttributeValue.fromS;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
Expand All @@ -42,10 +43,14 @@
*/
public class Action implements DynamodbRecord<Action>, DbKeys {

/** Record Completed date. */
private Date completedDate;
/** DocumentId. */
private String documentId;
/** Index. */
private String index = null;
/** Record inserted date. */
private Date insertedDate;
/** Action Metadata. */
private Map<String, String> metadata;
/** Action Parameters. */
Expand All @@ -64,6 +69,26 @@ public Action() {
this.status = ActionStatus.PENDING;
}

/**
* Get Completed Date.
*
* @return {@link Date}
*/
public Date completedDate() {
return this.completedDate;
}

/**
* Set Completed Date.
*
* @param date {@link Date}
* @return {@link Action}
*/
public Action completedDate(final Date date) {
this.completedDate = date;
return this;
}

/**
* Get DocumentId.
*
Expand Down Expand Up @@ -103,6 +128,16 @@ public Map<String, AttributeValue> getAttributes(final String siteId) {
attrs.put(GSI1_SK, fromS(skGsi1));
}

SimpleDateFormat df = DateUtil.getIsoDateFormatter();

if (this.insertedDate != null) {
attrs.put("inserteddate", AttributeValue.fromS(df.format(this.insertedDate)));
}

if (this.completedDate != null) {
attrs.put("completedDate", AttributeValue.fromS(df.format(this.completedDate)));
}

return attrs;
}

Expand Down Expand Up @@ -133,6 +168,24 @@ public Action getFromAttributes(final String siteId, final Map<String, Attribute
record.index(attrs.get(SK).s().split(TAG_DELIMINATOR)[1]);
}

SimpleDateFormat df = DateUtil.getIsoDateFormatter();

if (attrs.containsKey("inserteddate")) {
try {
record = record.insertedDate(df.parse(ss(attrs, "inserteddate")));
} catch (ParseException e) {
// ignore
}
}

if (attrs.containsKey("completedDate")) {
try {
record = record.completedDate(df.parse(ss(attrs, "completedDate")));
} catch (ParseException e) {
// ignore
}
}

return record;
}

Expand All @@ -156,6 +209,26 @@ public Action index(final String idx) {
return this;
}

/**
* Get Inserted Date.
*
* @return {@link Date}
*/
public Date insertedDate() {
return this.insertedDate;
}

/**
* Set Inserted Date.
*
* @param date {@link Date}
* @return {@link Action}
*/
public Action insertedDate(final Date date) {
this.insertedDate = date;
return this;
}

/**
* Get Action Metadata.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -41,6 +42,7 @@
import com.formkiq.aws.dynamodb.QueryResponseToPagination;
import com.formkiq.aws.dynamodb.objects.Objects;
import com.formkiq.module.actions.Action;
import com.formkiq.module.actions.ActionStatus;
import com.formkiq.module.actions.ActionType;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeAction;
Expand Down Expand Up @@ -232,6 +234,10 @@ private List<Action> queryActions(final String siteId, final String documentId,
@Override
public void saveAction(final String siteId, final Action action) {

if (action.insertedDate() == null) {
action.insertedDate(new Date());
}

Map<String, AttributeValue> valueMap = action.getAttributes(siteId);
this.dbClient
.putItem(PutItemRequest.builder().tableName(this.documentTableName).item(valueMap).build());
Expand All @@ -244,6 +250,10 @@ public void saveAction(final String siteId, final String documentId, final Actio
action.documentId(documentId);
action.index("" + index);

if (action.insertedDate() == null) {
action.insertedDate(new Date());
}

Map<String, AttributeValue> valueMap = action.getAttributes(siteId);
this.dbClient
.putItem(PutItemRequest.builder().tableName(this.documentTableName).item(valueMap).build());
Expand All @@ -262,6 +272,10 @@ public List<Map<String, AttributeValue>> saveActions(final String siteId, final
action.documentId(documentId);
action.index("" + idx);

if (action.insertedDate() == null) {
action.insertedDate(new Date());
}

Map<String, AttributeValue> valueMap = action.getAttributes(siteId);

values.add(valueMap);
Expand All @@ -283,11 +297,21 @@ public List<Map<String, AttributeValue>> saveActions(final String siteId, final
public void updateActionStatus(final String siteId, final String documentId,
final Action action) {

if (ActionStatus.COMPLETE.equals(action.status())
|| ActionStatus.FAILED.equals(action.status())) {
action.completedDate(new Date());
}

Map<String, AttributeValue> attrs = action.getAttributes(siteId);

Map<String, AttributeValueUpdate> updates = new HashMap<>();
updates.put("status", AttributeValueUpdate.builder().value(attrs.get("status")).build());

if (action.completedDate() != null) {
updates.put("completedDate",
AttributeValueUpdate.builder().value(attrs.get("completedDate")).build());
}

if (attrs.containsKey(GSI1_PK) && attrs.containsKey(GSI1_SK)) {
updates.put(GSI1_PK, AttributeValueUpdate.builder().value(attrs.get(GSI1_PK)).build());
updates.put(GSI1_SK, AttributeValueUpdate.builder().value(attrs.get(GSI1_SK)).build());
Expand Down
6 changes: 6 additions & 0 deletions docs/openapi/openapi-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4383,6 +4383,12 @@
userId:
type: string
description: User who requested the Action
insertedDate:
type: string
description: Inserted Timestamp
completedDate:
type: string
description: Completed Timestamp
parameters:
type: object
description: Action parameters
Expand Down
6 changes: 6 additions & 0 deletions docs/openapi/openapi-jwt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4383,6 +4383,12 @@
userId:
type: string
description: User who requested the Action
insertedDate:
type: string
description: Inserted Timestamp
completedDate:
type: string
description: Completed Timestamp
parameters:
type: object
description: Action parameters
Expand Down
6 changes: 6 additions & 0 deletions docs/openapi/openapi-key.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4383,6 +4383,12 @@
userId:
type: string
description: User who requested the Action
insertedDate:
type: string
description: Inserted Timestamp
completedDate:
type: string
description: Completed Timestamp
parameters:
type: object
description: Action parameters
Expand Down
Binary file modified dynamodb-documents/documentsTableSchema.numbers
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4385,6 +4385,12 @@ Resources:
userId:
type: "string"
description: "User who requested the Action"
insertedDate:
type: "string"
description: "Inserted Timestamp"
completedDate:
type: "string"
description: "Completed Timestamp"
parameters:
type: "object"
description: Action parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4385,6 +4385,12 @@ Resources:
userId:
type: "string"
description: "User who requested the Action"
insertedDate:
type: "string"
description: "Inserted Timestamp"
completedDate:
type: "string"
description: "Completed Timestamp"
parameters:
type: "object"
description: Action parameters
Expand Down
6 changes: 6 additions & 0 deletions lambda-api-graalvm/src/main/resources/cloudformation/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4385,6 +4385,12 @@ Resources:
userId:
type: "string"
description: "User who requested the Action"
insertedDate:
type: "string"
description: "Inserted Timestamp"
completedDate:
type: "string"
description: "Completed Timestamp"
parameters:
type: "object"
description: Action parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public class DocumentActionsProcessor implements RequestHandler<Map<String, Obje
private S3Service s3Service;
/** {@link AwsServiceCache}. */
private static AwsServiceCache preInitServiceCache;

static {

if (System.getenv().containsKey("AWS_REGION")) {
Expand Down Expand Up @@ -529,7 +530,6 @@ public void processEvent(final LambdaLogger logger, final DocumentEvent event)
this.actionsService.updateActionStatus(siteId, documentId, action);
}


} else {
logger
.log(String.format("NO ACTIONS found for SiteId %s Document %s", siteId, documentId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,10 @@ public void testHandle01() throws IOException, URISyntaxException {
assertEquals("[TEXT]", resultmap.get("parseTypes").toString());
assertEquals("true", resultmap.get("addPdfDetectedCharactersAsText").toString());

assertEquals(ActionStatus.RUNNING,
actionsService.getActions(siteId, documentId).get(0).status());
Action action = actionsService.getActions(siteId, documentId).get(0);
assertEquals(ActionStatus.RUNNING, action.status());
assertNotNull(action.insertedDate());
assertNull(action.completedDate());
}
}

Expand Down Expand Up @@ -779,8 +781,10 @@ public void testHandle02() throws IOException, URISyntaxException {
Map<String, Object> resultmap = gson.fromJson(lastRequest.getBodyAsString(), Map.class);
assertNotNull(resultmap.get("contentUrls").toString());

assertEquals(ActionStatus.COMPLETE,
actionsService.getActions(siteId, documentId).get(0).status());
Action action = actionsService.getActions(siteId, documentId).get(0);
assertEquals(ActionStatus.COMPLETE, action.status());
assertNotNull(action.insertedDate());
assertNotNull(action.completedDate());
}
}

Expand Down Expand Up @@ -819,8 +823,10 @@ public void testHandle03() throws IOException, URISyntaxException {
Map<String, Object> resultmap = gson.fromJson(lastRequest.getBodyAsString(), Map.class);
assertNotNull(resultmap.get("contentUrls").toString());

assertEquals(ActionStatus.COMPLETE,
actionsService.getActions(siteId, documentId).get(0).status());
Action action = actionsService.getActions(siteId, documentId).get(0);
assertEquals(ActionStatus.COMPLETE, action.status());
assertNotNull(action.insertedDate());
assertNotNull(action.completedDate());
}
}

Expand Down Expand Up @@ -848,7 +854,10 @@ public void testHandle04() throws IOException, URISyntaxException {
// then
actions = actionsService.getActions(siteId, documentId);
assertEquals(1, actions.size());
assertEquals(ActionStatus.FAILED, actions.get(0).status());
Action action = actions.get(0);
assertEquals(ActionStatus.FAILED, action.status());
assertNotNull(action.insertedDate());
assertNotNull(action.completedDate());
}
}

Expand Down Expand Up @@ -906,8 +915,10 @@ public void testHandle05() throws IOException, URISyntaxException {
assertNotNull(documents.get(0).get("lastModifiedDate"));
assertNotNull(documents.get(0).get("url"));

assertEquals(ActionStatus.COMPLETE,
actionsService.getActions(siteId, documentId).get(0).status());
Action action = actionsService.getActions(siteId, documentId).get(0);
assertEquals(ActionStatus.COMPLETE, action.status());
assertNotNull(action.insertedDate());
assertNotNull(action.completedDate());
}
}
}
Expand Down Expand Up @@ -970,8 +981,10 @@ public void testHandle06() throws IOException, URISyntaxException {
assertEquals("CLEAN", documents.get(0).get("status"));
assertEquals("2022-01-01", documents.get(0).get("timestamp"));

assertEquals(ActionStatus.COMPLETE,
actionsService.getActions(siteId, documentId).get(0).status());
Action action = actionsService.getActions(siteId, documentId).get(1);
assertEquals(ActionStatus.COMPLETE, action.status());
assertNotNull(action.insertedDate());
assertNotNull(action.completedDate());
}
}
}
Expand Down

0 comments on commit 0bea990

Please sign in to comment.