Skip to content

Commit

Permalink
#192 - Add Endpoint to retry failed document actions
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Nov 30, 2023
1 parent dc8f367 commit 01bf675
Show file tree
Hide file tree
Showing 19 changed files with 618 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public enum ActionStatus {
COMPLETE,
/** FAILED. */
FAILED,
/** Failed Retry. */
FAILED_RETRY,
/** In Queue. */
IN_QUEUE,
/** Pending. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ public interface ActionsService {
Action findActionInQueue(String siteId, String documentId, String queueId);

/**
* Find Documents with FAILED status.
* Find Documents in Queue.
*
* @param siteId {@link String}
* @param status {@link ActionStatus}
* @param queueId {@link String}
* @param exclusiveStartKey {@link Map}
* @param limit int
* @return {@link PaginationResults}
* @return {@link PaginationResults} {@link Action}
*/
PaginationResults<String> findDocumentsWithStatus(String siteId, ActionStatus status,
PaginationResults<Action> findDocumentsInQueue(String siteId, String queueId,
Map<String, AttributeValue> exclusiveStartKey, int limit);

/**
* Find Documents in Queue.
* Find Documents with FAILED status.
*
* @param siteId {@link String}
* @param queueId {@link String}
* @param status {@link ActionStatus}
* @param exclusiveStartKey {@link Map}
* @param limit int
* @return {@link PaginationResults} {@link Action}
* @return {@link PaginationResults}
*/
PaginationResults<Action> findDocumentsInQueue(String siteId, String queueId,
PaginationResults<String> findDocumentsWithStatus(String siteId, ActionStatus status,
Map<String, AttributeValue> exclusiveStartKey, int limit);

/**
Expand Down Expand Up @@ -127,6 +127,14 @@ void insertBeforeAction(String siteId, String documentId, List<Action> actions,
*/
void saveAction(String siteId, String documentId, Action action, int index);

/**
* Save {@link Action}.
*
* @param siteId {@link String}
* @param actions {@link List} {@link Action}
*/
void saveActions(String siteId, List<Action> actions);

/**
* Save {@link List} {@link Action}.
*
Expand All @@ -135,7 +143,7 @@ void insertBeforeAction(String siteId, String documentId, List<Action> actions,
* @param actions {@link List} {@link Action}
* @return {@link List} {@link Map}
*/
List<Map<String, AttributeValue>> saveActions(String siteId, String documentId,
List<Map<String, AttributeValue>> saveNewActions(String siteId, String documentId,
List<Action> actions);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ public Action findActionInQueue(final String siteId, final String documentId,
return action;
}

@Override
public PaginationResults<Action> findDocumentsInQueue(final String siteId, final String queueName,
final Map<String, AttributeValue> exclusiveStartKey, final int limit) {

BatchGetConfig batchConfig = new BatchGetConfig();
String pk = createDatabaseKey(siteId, "action#" + ActionType.QUEUE + "#" + queueName);
String sk = "action#";

QueryConfig config = new QueryConfig().indexName(GSI1).scanIndexForward(Boolean.TRUE);
QueryResponse response =
this.db.queryBeginsWith(config, fromS(pk), fromS(sk), exclusiveStartKey, limit);

List<Map<String, AttributeValue>> keys = response.items().stream()
.map(i -> Map.of(PK, i.get(PK), SK, i.get(SK))).collect(Collectors.toList());

List<Action> list = this.db.getBatch(batchConfig, keys).stream()
.map(a -> new Action().getFromAttributes(siteId, a)).collect(Collectors.toList());

PaginationMapToken pagination = new QueryResponseToPagination().apply(response);
return new PaginationResults<>(list, pagination);
}

@Override
public PaginationResults<String> findDocumentsWithStatus(final String siteId,
final ActionStatus status, final Map<String, AttributeValue> exclusiveStartKey,
Expand Down Expand Up @@ -163,28 +185,6 @@ public PaginationResults<String> findDocumentsWithStatus(final String siteId,

}

@Override
public PaginationResults<Action> findDocumentsInQueue(final String siteId, final String queueName,
final Map<String, AttributeValue> exclusiveStartKey, final int limit) {

BatchGetConfig batchConfig = new BatchGetConfig();
String pk = createDatabaseKey(siteId, "action#" + ActionType.QUEUE + "#" + queueName);
String sk = "action#";

QueryConfig config = new QueryConfig().indexName(GSI1).scanIndexForward(Boolean.TRUE);
QueryResponse response =
this.db.queryBeginsWith(config, fromS(pk), fromS(sk), exclusiveStartKey, limit);

List<Map<String, AttributeValue>> keys = response.items().stream()
.map(i -> Map.of(PK, i.get(PK), SK, i.get(SK))).collect(Collectors.toList());

List<Action> list = this.db.getBatch(batchConfig, keys).stream()
.map(a -> new Action().getFromAttributes(siteId, a)).collect(Collectors.toList());

PaginationMapToken pagination = new QueryResponseToPagination().apply(response);
return new PaginationResults<>(list, pagination);
}

@Override
public List<Action> getActions(final String siteId, final String documentId) {
return queryActions(siteId, documentId, null, null);
Expand Down Expand Up @@ -283,8 +283,28 @@ public void saveAction(final String siteId, final String documentId, final Actio
}

@Override
public List<Map<String, AttributeValue>> saveActions(final String siteId, final String documentId,
final List<Action> actions) {
public void saveActions(final String siteId, final List<Action> actions) {

List<Map<String, AttributeValue>> values = new ArrayList<>();

for (Action action : actions) {

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

if (!values.isEmpty()) {
Map<String, Collection<WriteRequest>> items =
new AttributeValuesToWriteRequests(this.documentTableName).apply(values);

BatchWriteItemRequest batch = BatchWriteItemRequest.builder().requestItems(items).build();
this.dbClient.batchWriteItem(batch);
}
}

@Override
public List<Map<String, AttributeValue>> saveNewActions(final String siteId,
final String documentId, final List<Action> actions) {

int idx = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void hasActions01() {
Action action0 = new Action().type(ActionType.OCR).userId(userId0);

// when
service.saveActions(siteId, documentId0, Arrays.asList(action0));
service.saveNewActions(siteId, documentId0, Arrays.asList(action0));

// then
assertTrue(service.hasActions(siteId, documentId0));
Expand All @@ -112,7 +112,7 @@ public void testDeleteDocument() {

Action action0 =
new Action().type(ActionType.OCR).userId("joe").parameters(Map.of("test", "1234"));
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));

// when
documentService.deleteDocument(siteId, documentId, false);
Expand All @@ -136,7 +136,7 @@ public void testDeleteDocumentActions() {

Action action0 =
new Action().type(ActionType.OCR).userId("joe").parameters(Map.of("test", "1234"));
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));

// when
service.deleteActions(siteId, documentId);
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testInsertAction01() {
Action insertedAction = new Action().type(ActionType.OCR).userId(user);

List<Action> actions = Arrays.asList(action0, action1);
service.saveActions(siteId, documentId, actions);
service.saveNewActions(siteId, documentId, actions);
assertEquals(2, service.getActions(siteId, documentId).size());

// when
Expand Down Expand Up @@ -209,8 +209,8 @@ public void testSave01() throws Exception {

// when
final List<Map<String, AttributeValue>> list =
service.saveActions(siteId, documentId0, Arrays.asList(action0));
service.saveActions(siteId, documentId1, Arrays.asList(action1));
service.saveNewActions(siteId, documentId0, Arrays.asList(action0));
service.saveNewActions(siteId, documentId1, Arrays.asList(action1));

// then
assertEquals(1, list.size());
Expand Down Expand Up @@ -265,7 +265,7 @@ public void testSave02() {
}

// when
service.saveActions(siteId, documentId, actions);
service.saveNewActions(siteId, documentId, actions);

// then
int i = 0;
Expand Down Expand Up @@ -294,7 +294,7 @@ public void testSave03() throws Exception {
Action action0 = new Action().type(ActionType.QUEUE).userId(userId0).queueId(name);

// when
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));

// then
List<Action> results = service.getActions(siteId, documentId);
Expand All @@ -320,7 +320,7 @@ public void testUpdateActionStatus01() {
String userId0 = "joe";
Action action0 =
new Action().type(ActionType.OCR).userId(userId0).parameters(Map.of("test", "1234"));
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));
assertEquals(ActionStatus.PENDING, service.getActions(siteId, documentId).get(0).status());

action0.status(ActionStatus.COMPLETE);
Expand All @@ -347,7 +347,7 @@ public void testUpdateActionStatus02() {
String documentId = UUID.randomUUID().toString();
String userId0 = "joe";
Action action0 = new Action().type(ActionType.QUEUE).userId(userId0).queueId(queueId);
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));
assertEquals(ActionStatus.PENDING, service.getActions(siteId, documentId).get(0).status());

action0.status(ActionStatus.IN_QUEUE);
Expand Down Expand Up @@ -395,7 +395,7 @@ public void testUpdateActionStatus03() {
String documentId = UUID.randomUUID().toString();
String userId0 = "joe";
Action action0 = new Action().type(ActionType.QUEUE).userId(userId0).queueId(name);
service.saveActions(siteId, documentId, Arrays.asList(action0));
service.saveNewActions(siteId, documentId, Arrays.asList(action0));
assertEquals(ActionStatus.PENDING, service.getActions(siteId, documentId).get(0).status());

action0.status(ActionStatus.FAILED);
Expand Down
42 changes: 42 additions & 0 deletions docs/openapi/openapi-iam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
- PENDING
- RUNNING
- SKIPPED
- FAILED_RETRY
- $ref: '#/components/parameters/documentSearchDeleted'
- name: date
in: query
Expand Down Expand Up @@ -1326,6 +1327,40 @@
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/actions/retry:
post:
operationId: AddDocumentRetryAction
summary: Retries failed document action(s)
description: Retries all failed document action(s). Failed action status changes to "FAILED_RETRY" and a new "PENDING" action is created.
tags:
- Document Actions
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
responses:
"200":
description: 200 OK
headers:
Access-Control-Allow-Origin:
$ref: '#/components/headers/AccessControlAllowOrigin'
Access-Control-Allow-Methods:
$ref: '#/components/headers/AccessControlAllowMethods'
Access-Control-Allow-Headers:
$ref: '#/components/headers/AccessControlAllowHeaders'
content:
application/json:
schema:
$ref: '#/components/schemas/AddDocumentActionsRetryResponse'
"400":
description: 400 OK
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationErrorsResponse'
security:
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/ocr:
get:
operationId: GetDocumentOcr
Expand Down Expand Up @@ -4922,6 +4957,7 @@
- PENDING
- RUNNING
- SKIPPED
- FAILED_RETRY
type:
type: string
description: Type of Document Action
Expand Down Expand Up @@ -4997,6 +5033,12 @@
message:
type: string
description: Document Action message
AddDocumentActionsRetryResponse:
type: object
properties:
message:
type: string
description: Document Action message
AddDocumentOcrResponse:
type: object
properties:
Expand Down
42 changes: 42 additions & 0 deletions docs/openapi/openapi-jwt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@
- PENDING
- RUNNING
- SKIPPED
- FAILED_RETRY
- $ref: '#/components/parameters/documentSearchDeleted'
- name: date
in: query
Expand Down Expand Up @@ -1326,6 +1327,40 @@
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/actions/retry:
post:
operationId: AddDocumentRetryAction
summary: Retries failed document action(s)
description: Retries all failed document action(s). Failed action status changes to "FAILED_RETRY" and a new "PENDING" action is created.
tags:
- Document Actions
parameters:
- $ref: '#/components/parameters/siteIdParam'
- $ref: '#/components/parameters/documentIdParam'
responses:
"200":
description: 200 OK
headers:
Access-Control-Allow-Origin:
$ref: '#/components/headers/AccessControlAllowOrigin'
Access-Control-Allow-Methods:
$ref: '#/components/headers/AccessControlAllowMethods'
Access-Control-Allow-Headers:
$ref: '#/components/headers/AccessControlAllowHeaders'
content:
application/json:
schema:
$ref: '#/components/schemas/AddDocumentActionsRetryResponse'
"400":
description: 400 OK
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationErrorsResponse'
security:
- ApiAuthorization: []
x-amazon-apigateway-integration:
$ref: '#/components/x-amazon-apigateway-integrations/lambdaApi200'
/documents/{documentId}/ocr:
get:
operationId: GetDocumentOcr
Expand Down Expand Up @@ -4922,6 +4957,7 @@
- PENDING
- RUNNING
- SKIPPED
- FAILED_RETRY
type:
type: string
description: Type of Document Action
Expand Down Expand Up @@ -4997,6 +5033,12 @@
message:
type: string
description: Document Action message
AddDocumentActionsRetryResponse:
type: object
properties:
message:
type: string
description: Document Action message
AddDocumentOcrResponse:
type: object
properties:
Expand Down
Loading

0 comments on commit 01bf675

Please sign in to comment.