Skip to content

Commit

Permalink
Created icav2 statechange handler construct
Browse files Browse the repository at this point in the history
Logs and raises an internal event
  • Loading branch information
alexiswl committed May 7, 2024
1 parent e15e70f commit 354d13c
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Construct } from 'constructs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import * as path from 'path';
import * as events from 'aws-cdk-lib/aws-events';
import * as events_targets from 'aws-cdk-lib/aws-events-targets';

export interface Icav2AnalysisEventHandlerConstructProps {
/* Names of objects to get */
tableName: string; // Name of the table to get / update / query

/* Names of objects to create */
stateMachineName: string; // Name of the state machine to create

/* Event configurations to push to */
detailType: string; // Detail type of the event to raise
eventBusName: string; // Detail of the eventbus to push the event to
source: string; // Source of the event we push

/* Event filter rule */
pipelineId: string;
}

export class Icav2AnalysisEventHandlerConstruct extends Construct {
public readonly stateMachineObj: sfn.StateMachine;

constructor(scope: Construct, id: string, props: Icav2AnalysisEventHandlerConstructProps) {
super(scope, id);

// Get table object
const table_obj = dynamodb.TableV2.fromTableName(this, 'table_obj', props.tableName);

// Get the event bus object
const eventbus_obj = events.EventBus.fromEventBusName(
this,
'orcabus_eventbus_obj',
props.eventBusName
);

// Build state machine object
this.stateMachineObj = new sfn.StateMachine(this, 'state_machine', {
stateMachineName: props.stateMachineName,
definitionBody: sfn.DefinitionBody.fromFile(
path.join(
__dirname,
'step_functions_templates/icav2_get_workflow_status_and_raise_internal_event.asl.json'
)
),
definitionSubstitutions: {
/* Table object */
__table_name__: table_obj.tableName,
/* Event metadata */
__detail_type__: props.detailType,
__eventbus_name__: props.eventBusName,
__eventsource__: props.source,
},
});

/* Grant the state machine read and write access to the table */
table_obj.grantReadWriteData(this.stateMachineObj);

// Create a rule for this state machine
const rule = new events.Rule(this, 'rule', {
eventBus: eventbus_obj,
ruleName: `${props.stateMachineName}-rule`,
eventPattern: {
source: [props.source],
detailType: [props.detailType],
detail: {
'pipeline.id': props.pipelineId,
},
},
});

/* Add rule as a target to the state machine */
rule.addTarget(
new events_targets.SfnStateMachine(this.stateMachineObj, {
input: events.RuleTargetInput.fromEventPath('$.detail'),
})
);

/* Grant the state machine the ability to submit events to the event bus */
eventbus_obj.grantPutEventsTo(this.stateMachineObj.role);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"Comment": "Handle icav2 state change",
"StartAt": "Move event detail",
"States": {
"Move event detail": {
"Type": "Pass",
"Next": "DynamoDB Get UUID from ICAv2 Analysis ID",
"Parameters": {
"event_detail.$": "$"
}
},
"DynamoDB Get UUID from ICAv2 Analysis ID": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": "${__table_name__}",
"Key": {
"id.$": "$.event_detail.id",
"id_type": "icav2_analysis_id"
}
},
"Next": "Check Analysis ID in DataBase",
"ResultPath": "$.get_analysis_id_in_db_step",
"ResultSelector": {
"db_response.$": "$"
}
},
"Check Analysis ID in DataBase": {
"Type": "Choice",
"Choices": [
{
"Not": {
"Variable": "$.get_analysis_id_in_db_step.db_response.Item",
"IsPresent": true
},
"Next": "Success"
}
],
"Default": "DynamoDB Update Status"
},
"DynamoDB Update Status": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:updateItem",
"Parameters": {
"TableName": "${__table_name__}",
"Key": {
"id.$": "$.get_analysis_id_in_db_step.db_response.db_uuid",
"id_type": "db_uuid"
},
"UpdateExpression": "SET status = :status",
"ExpressionAttributeValues": {
":status": {
"S.$": "$.get_analysis_id_in_db_step.db_response.status"
}
}
},
"Next": "Wait 1 Second",
"ResultPath": "$.update_analysis_id_in_db_step"
},
"Wait 1 Second": {
"Type": "Wait",
"Seconds": 1,
"Next": "DynamoDB Get UUID Row"
},
"DynamoDB Get UUID Row": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:getItem",
"Parameters": {
"TableName": "${__table_name__}",
"Key": {
"id.$": "$.get_analysis_id_in_db_step.db_response.db_uuid",
"id_type": "db_uuid"
}
},
"Next": "PutEvent",
"ResultPath": "$.update_analysis_id_in_db_step"
},
"PutEvent": {
"Type": "Task",
"Resource": "arn:aws:states:::events:putEvents",
"Parameters": {
"Entries": [
{
"Detail.$": "$.update_analysis_id_in_db_step",
"DetailType": "${__detail_type__}",
"EventBusName": "${__eventbus_name__}",
"Source": "${__eventsource__}"
}
]
},
"Next": "Success"
},
"Success": {
"Type": "Succeed"
}
}
}
12 changes: 6 additions & 6 deletions lib/workload/components/dynamodb-icav2-table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { Construct } from 'constructs';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';

export interface DynamodbIcav2PipelineConstructProps {
table_name: string;
tableName: string;
}

export class DynamodbIcav2PipelineConstruct extends Construct {
public readonly table_obj: dynamodb.ITableV2;
public readonly table_name_arn: string;
public readonly tableObj: dynamodb.ITableV2;
public readonly tableNameArn: string;

constructor(scope: Construct, id: string, props: DynamodbIcav2PipelineConstructProps) {
super(scope, id);

this.table_obj = new dynamodb.TableV2(this, 'dynamodb_icav2_pipeline_table', {
this.tableObj = new dynamodb.TableV2(this, 'dynamodb_icav2_pipeline_table', {
/* Either a db_uuid or an icav2 analysis id or a portal run id */
partitionKey: {
name: 'id',
Expand All @@ -23,10 +23,10 @@ export class DynamodbIcav2PipelineConstruct extends Construct {
name: 'id_type',
type: dynamodb.AttributeType.STRING,
},
tableName: props.table_name,
tableName: props.tableName,
});

// Set outputs
this.table_name_arn = this.table_obj.tableArn;
this.tableNameArn = this.tableObj.tableArn;
}
}

0 comments on commit 354d13c

Please sign in to comment.