Skip to content

Commit 2eeaebc

Browse files
authored
feat(iotevents): allow setting description, evaluation method and key of DetectorModel (#18644)
This PR is about #17711 (but out of the roadmap). This PR (especially `key` property) make it easier to test the features we will implement. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 162909f commit 2eeaebc

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

packages/@aws-cdk/aws-iotevents/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import * as iotevents from '@aws-cdk/aws-iotevents';
5151

5252
const input = new iotevents.Input(this, 'MyInput', {
5353
inputName: 'my_input', // optional
54-
attributeJsonPaths: ['payload.temperature'],
54+
attributeJsonPaths: ['payload.deviceId', 'payload.temperature'],
5555
});
5656

5757
const onlineState = new iotevents.State({
@@ -64,6 +64,9 @@ const onlineState = new iotevents.State({
6464

6565
new iotevents.DetectorModel(this, 'MyDetectorModel', {
6666
detectorModelName: 'test-detector-model', // optional
67+
description: 'test-detector-model-description', // optional property, default is none
68+
evaluationMethod: iotevents.EventEvaluation.SERIAL, // optional property, default is iotevents.EventEvaluation.BATCH
69+
detectorKey: 'payload.deviceId', // optional property, default is none and single detector instance will be created and all inputs will be routed to it
6770
initialState: onlineState,
6871
});
6972
```

packages/@aws-cdk/aws-iotevents/lib/detector-model.ts

+51
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ export interface IDetectorModel extends IResource {
1616
readonly detectorModelName: string;
1717
}
1818

19+
/**
20+
* Information about the order in which events are evaluated and how actions are executed.
21+
*/
22+
export enum EventEvaluation {
23+
/**
24+
* When setting to SERIAL, variables are updated and event conditions are evaluated in the order
25+
* that the events are defined.
26+
*/
27+
BATCH = 'BATCH',
28+
/**
29+
* When setting to BATCH, variables within a state are updated and events within a state are
30+
* performed only after all event conditions are evaluated.
31+
*/
32+
SERIAL = 'SERIAL',
33+
}
34+
1935
/**
2036
* Properties for defining an AWS IoT Events detector model
2137
*/
@@ -27,6 +43,38 @@ export interface DetectorModelProps {
2743
*/
2844
readonly detectorModelName?: string;
2945

46+
/**
47+
* A brief description of the detector model.
48+
*
49+
* @default none
50+
*/
51+
readonly description?: string;
52+
53+
/**
54+
* Information about the order in which events are evaluated and how actions are executed.
55+
*
56+
* When setting to SERIAL, variables are updated and event conditions are evaluated in the order
57+
* that the events are defined.
58+
* When setting to BATCH, variables within a state are updated and events within a state are
59+
* performed only after all event conditions are evaluated.
60+
*
61+
* @default EventEvaluation.BATCH
62+
*/
63+
readonly evaluationMethod?: EventEvaluation;
64+
65+
/**
66+
* The value used to identify a detector instance. When a device or system sends input, a new
67+
* detector instance with a unique key value is created. AWS IoT Events can continue to route
68+
* input to its corresponding detector instance based on this identifying information.
69+
*
70+
* This parameter uses a JSON-path expression to select the attribute-value pair in the message
71+
* payload that is used for identification. To route the message to the correct detector instance,
72+
* the device must send a message payload that contains the same attribute-value.
73+
*
74+
* @default - none (single detector instance will be created and all inputs will be routed to it)
75+
*/
76+
readonly detectorKey?: string;
77+
3078
/**
3179
* The state that is entered at the creation of each detector.
3280
*/
@@ -70,6 +118,9 @@ export class DetectorModel extends Resource implements IDetectorModel {
70118

71119
const resource = new CfnDetectorModel(this, 'Resource', {
72120
detectorModelName: this.physicalName,
121+
detectorModelDescription: props.description,
122+
evaluationMethod: props.evaluationMethod,
123+
key: props.detectorKey,
73124
detectorModelDefinition: {
74125
initialStateName: props.initialState.stateName,
75126
states: [props.initialState._toStateJson()],

packages/@aws-cdk/aws-iotevents/test/detector-model.test.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ test('can get detector model name', () => {
7676
});
7777
});
7878

79-
test('can set physical name', () => {
79+
test.each([
80+
['physical name', { detectorModelName: 'test-detector-model' }, { DetectorModelName: 'test-detector-model' }],
81+
['description', { description: 'test-detector-model-description' }, { DetectorModelDescription: 'test-detector-model-description' }],
82+
['evaluationMethod', { evaluationMethod: iotevents.EventEvaluation.SERIAL }, { EvaluationMethod: 'SERIAL' }],
83+
['detectorKey', { detectorKey: 'payload.deviceId' }, { Key: 'payload.deviceId' }],
84+
])('can set %s', (_, partialProps, expected) => {
8085
// WHEN
8186
new iotevents.DetectorModel(stack, 'MyDetectorModel', {
82-
detectorModelName: 'test-detector-model',
87+
...partialProps,
8388
initialState: new iotevents.State({
8489
stateName: 'test-state',
8590
onEnter: [{
@@ -90,9 +95,7 @@ test('can set physical name', () => {
9095
});
9196

9297
// THEN
93-
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', {
94-
DetectorModelName: 'test-detector-model',
95-
});
98+
Template.fromStack(stack).hasResourceProperties('AWS::IoTEvents::DetectorModel', expected);
9699
});
97100

98101
test('can set multiple events to State', () => {

packages/@aws-cdk/aws-iotevents/test/integ.detector-model.expected.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
"Properties": {
66
"InputDefinition": {
77
"Attributes": [
8+
{
9+
"JsonPath": "payload.deviceId"
10+
},
811
{
912
"JsonPath": "payload.temperature"
1013
}
@@ -70,7 +73,10 @@
7073
"Arn"
7174
]
7275
},
73-
"DetectorModelName": "test-detector-model"
76+
"DetectorModelDescription": "test-detector-model-description",
77+
"DetectorModelName": "test-detector-model",
78+
"EvaluationMethod": "SERIAL",
79+
"Key": "payload.deviceId"
7480
}
7581
}
7682
}

packages/@aws-cdk/aws-iotevents/test/integ.detector-model.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TestStack extends cdk.Stack {
77

88
const input = new iotevents.Input(this, 'MyInput', {
99
inputName: 'test_input',
10-
attributeJsonPaths: ['payload.temperature'],
10+
attributeJsonPaths: ['payload.deviceId', 'payload.temperature'],
1111
});
1212

1313
const onlineState = new iotevents.State({
@@ -27,6 +27,9 @@ class TestStack extends cdk.Stack {
2727

2828
new iotevents.DetectorModel(this, 'MyDetectorModel', {
2929
detectorModelName: 'test-detector-model',
30+
description: 'test-detector-model-description',
31+
evaluationMethod: iotevents.EventEvaluation.SERIAL,
32+
detectorKey: 'payload.deviceId',
3033
initialState: onlineState,
3134
});
3235
}

0 commit comments

Comments
 (0)