-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(iotevents): add DetectorModel L2 Construct #18049
Conversation
bb17fa9
to
7873d17
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good initial attempt @yamatatsu! But we need to iterate on the API a little more before we can merge this in.
* | ||
* @default None - Defaults to perform the actions always. | ||
*/ | ||
readonly condition?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like we need to present an API for these conditions - just writing expressions in strings won't cut it I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's sounds great! 😍
Recently, I knew mapping-tenplate.ts
in aws-appsync
. I wanna try to design like it! (Or is there any other code I can refer to?)
(And I wanna try to design IoT Rules expressions that we discussed this earlier. I couldn't imagine implementing it at all before but.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's the closest similar thing we have for something like this in the CDK currently, yes.
Can you link me to the AWS docs for this functionality? I want to make sure I have a good understanding of the language that's permitted here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! But, as far as I can find, the only document that explains "what a condition is" is the CloudFormation document.
This CFn document is described that condition
is expression returning boolean. The refferences of Expressions are here.
And bellow is examples of condition
from Detector model examples
.
"condition": "!$variable.noDelay && $variable.enteringNewState"
"condition": "(((($variable.averageTemperature * ($variable.sensorCount - 1)) + $input.temperatureInput.sensorData.temperature) / $variable.sensorCount) < ($variable.desiredTemperature - $variable.allowedError))"
"condition": "currentInput(\"AWS_IoTEvents_Blueprints_Heartbeat_Input\")"
"condition": "timeout(\"awake\")"
Pull request has been modified.
I'm sorry it took me so long to respond. I sort out the rest issues.
These issues is related each I think. That we should add are smart API and validation for it. I will try to implement these features in this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work @yamatatsu! I love the Expression
API, looks much cleaner than just mucking around with string expressions.
I have a few notes, but nothing major, mainly decreasing the surface area of the public API of this module.
readonly stateName: string; | ||
|
||
/** | ||
* Specifies the actions that are performed when the state is entered and the `condition` is `TRUE` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to say, I find the constant name changes here super confusing 😕. This is a good example. We are writing documentation for a property called onEnterEvents
, its type is Event[]
, and the description says "Specifies the actions that are performed". Doesn't even mention the word "event" anywhere.
Are we missing something here? Should what in this PR is called an Event
actually be some sort of Action?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Naming
In CloudFormation, State
has OnEnter
and OnEnter
has only events
(typed as Event[]
). I easily concated onEnter
and events
...
If I named it just onEnter
, would it be less uncomfortable?
JSDoc
I should fix it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fix it in 78fc88c
(#18049).
/** | ||
* returns true if this state has at least one condition via events | ||
*/ | ||
public hasCondition(): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't like this method. This is actually a DetectorModel
concern, not a State
concern, that this validation has to be performed.
Can we remove this method, and perform this validation in DetectorModel
by calling toStateJson()
on its initialState
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we implement this validation with toStateJson()
, It will be as following I think:
const stateJson = props.initialState._toStateJson();
if (!stateJson.onEnter?.events.some(event => event.condition)) {
throw new Error('Detector Model must have at least one Input with a condition');
}
But we can't get onEnter?.events
because onEnter
can be cdk.IResolvable
, same for events.some()
and event.condition
.
Should toStateJson()
return more exact type that is compatible with CfnDetectorModel.StateProperty
instead of CfnDetectorModel.StateProperty
itself?
Following is the type CfnDetectorModel.StateProperty
that toStateJson()
return:
namespace CfnDetectorModel {
export interface StateProperty {
readonly onEnter?: CfnDetectorModel.OnEnterProperty | cdk.IResolvable;
readonly onExit?: CfnDetectorModel.OnExitProperty | cdk.IResolvable;
readonly onInput?: CfnDetectorModel.OnInputProperty | cdk.IResolvable;
readonly stateName: string;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see the problem. I think we could just check whether onEnter
is a CfnDetectorModel.OnEnterProperty
, and skip the validation if it's an IResolvable
. But I don't want you to write all of that code if a simpler alternative is possible here.
So, let's keep _toStateJson()
as it is now, but let's make this method @internal
, and rename it to _onEnterEventsHaveAtLeastOneCondition()
.
/** | ||
* Return the state property JSON | ||
*/ | ||
public toStateJson(): CfnDetectorModel.StateProperty { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this method @internal
? I don't think there's a need to expose it in the public API of this module.
It will require renaming it to _toStateJson()
.
}); | ||
|
||
test('cannot create without condition', () => { | ||
const stack = new cdk.Stack(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend putting this in a beforeEach()
- it's repeated in every test.
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Pull request has been modified.
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks awesome @yamatatsu. Let's rename that one method in State
, and make it @internal
, and this will be ready to get merged.
/** | ||
* returns true if this state has at least one condition via events | ||
*/ | ||
public hasCondition(): boolean { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I see the problem. I think we could just check whether onEnter
is a CfnDetectorModel.OnEnterProperty
, and skip the validation if it's an IResolvable
. But I don't want you to write all of that code if a simpler alternative is possible here.
So, let's keep _toStateJson()
as it is now, but let's make this method @internal
, and rename it to _onEnterEventsHaveAtLeastOneCondition()
.
Co-authored-by: Adam Ruka <adamruka85@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution @yamatatsu!
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
AWS CodeBuild CI Report
Powered by github-codebuild-logs, available on the AWS Serverless Application Repository |
Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork). |
* origin/master: (31 commits) feat(iotevents): add DetectorModel L2 Construct (aws#18049) feat(ecs): add `BaseService.fromServiceArnWithCluster()` for use in CodePipeline (aws#18530) docs(s3): remove vestigial documentation (aws#18604) chore(cli): LogMonitor test fails randomly due to Date.now() (aws#18601) chore(codedeploy): migrate tests to use the Assertions module (aws#18585) docs(stepfunctions): fix typo (aws#18583) chore(eks-legacy): migrate tests to `assertions` (aws#18596) fix(cli): hotswap should wait for lambda's `updateFunctionCode` to complete (aws#18536) fix(apigatewayv2): websocket api: allow all methods in grant manage connections (aws#18544) chore(dynamodb): migrate tests to assertions (aws#18560) fix(aws-apigateway): cross region authorizer ref (aws#18444) feat(lambda-nodejs): Allow setting mainFields for esbuild (aws#18569) docs(cfnspec): update CloudFormation documentation (aws#18587) feat(assertions): support for conditions (aws#18577) fix(ecs-patterns): Fix Network Load Balancer Port assignments in ECS Patterns (aws#18157) chore(region-info): ap-southeast-3 (Jakarta) ELBV2_ACCOUNT (aws#18300) fix(pipelines): CodeBuild projects are hard to tell apart (aws#18492) fix(ecs): only works in 'aws' partition (aws#18496) chore(app-delivery): migrate unit tests to Assertions (aws#18574) chore: migrate kaizen3031593's modules to assertions (aws#18205) ...
This PR is proposed by aws#17711. The first step of the roadmap in aws#17711 is implemented in this PR. > 1. implement DetectorModel and State with only required properties > - It will not be able to have multiple states yet. If this PR is merged, the simplest detector model can be created as following: ![image](https://user-images.githubusercontent.com/11013683/146365658-248bba67-743c-4ba3-a195-56223146525f.png) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This PR allow IoT Events detector model to transit to multiple states. This PR is in roadmap of #17711. <img width="561" alt="スクリーンショット 2022-02-02 0 38 10" src="https://user-images.githubusercontent.com/11013683/151999891-45afa8e8-57ed-4264-a323-16b84ed35348.png"> Following image is the graph displayed on AWS console when this integ test deployed. [Compared to the previous version](#18049), you can see that the state transitions are now represented. ![image](https://user-images.githubusercontent.com/11013683/151999116-5b3b36b0-d2b9-4e3a-9483-824dc0618f4b.png) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This PR is proposed by aws#17711. The first step of the roadmap in aws#17711 is implemented in this PR. > 1. implement DetectorModel and State with only required properties > - It will not be able to have multiple states yet. If this PR is merged, the simplest detector model can be created as following: ![image](https://user-images.githubusercontent.com/11013683/146365658-248bba67-743c-4ba3-a195-56223146525f.png) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This PR allow IoT Events detector model to transit to multiple states. This PR is in roadmap of aws#17711. <img width="561" alt="スクリーンショット 2022-02-02 0 38 10" src="https://user-images.githubusercontent.com/11013683/151999891-45afa8e8-57ed-4264-a323-16b84ed35348.png"> Following image is the graph displayed on AWS console when this integ test deployed. [Compared to the previous version](aws#18049), you can see that the state transitions are now represented. ![image](https://user-images.githubusercontent.com/11013683/151999116-5b3b36b0-d2b9-4e3a-9483-824dc0618f4b.png) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
This PR is proposed by #17711.
The first step of the roadmap in #17711 is implemented in this PR.
If this PR is merged, the simplest detector model can be created as following:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license