Skip to content
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

Merged
merged 26 commits into from
Jan 22, 2022

Conversation

yamatatsu
Copy link
Contributor

@yamatatsu yamatatsu commented Dec 16, 2021

This PR is proposed by #17711.

The first step of the roadmap in #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


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Dec 16, 2021

@github-actions github-actions bot added the @aws-cdk/aws-iotevents Related to AWS IoT Events label Dec 16, 2021
Copy link
Contributor

@skinny85 skinny85 left a 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.

packages/@aws-cdk/aws-iotevents/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/detector-model.ts Outdated Show resolved Hide resolved
*
* @default None - Defaults to perform the actions always.
*/
readonly condition?: string;
Copy link
Contributor

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.

Copy link
Contributor Author

@yamatatsu yamatatsu Dec 17, 2021

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.)

Copy link
Contributor

@skinny85 skinny85 Dec 17, 2021

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.

Copy link
Contributor Author

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\")"

packages/@aws-cdk/aws-iotevents/package.json Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/state.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/state.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/state.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/event.ts Show resolved Hide resolved
@mergify mergify bot dismissed skinny85’s stale review December 17, 2021 13:01

Pull request has been modified.

@yamatatsu
Copy link
Contributor Author

@skinny85

I'm sorry it took me so long to respond.

I sort out the rest issues.

  1. add validation that the initialState of the DetectorModel has at least one Event with a non-undefined condition

  2. add getCurrentInputCondition() to Input

  3. to present an smart API for conditions as mapping-tenplate.ts in aws-appsync instead of just string

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.

Copy link
Contributor

@skinny85 skinny85 left a 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.

packages/@aws-cdk/aws-iotevents/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/README.md Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/detector-model.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/detector-model.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/event.ts Outdated Show resolved Hide resolved
packages/@aws-cdk/aws-iotevents/lib/state.ts Outdated Show resolved Hide resolved
readonly stateName: string;

/**
* Specifies the actions that are performed when the state is entered and the `condition` is `TRUE`
Copy link
Contributor

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?

Copy link
Contributor Author

@yamatatsu yamatatsu Jan 21, 2022

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!

Copy link
Contributor Author

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 {
Copy link
Contributor

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?

Copy link
Contributor Author

@yamatatsu yamatatsu Jan 21, 2022

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;
  }
}

Copy link
Contributor

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 {
Copy link
Contributor

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();
Copy link
Contributor

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>
@mergify mergify bot dismissed skinny85’s stale review January 21, 2022 00:08

Pull request has been modified.

yamatatsu and others added 11 commits January 21, 2022 09:08
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>
Copy link
Contributor

@skinny85 skinny85 left a 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 {
Copy link
Contributor

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().

Copy link
Contributor

@skinny85 skinny85 left a 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!

@mergify
Copy link
Contributor

mergify bot commented Jan 22, 2022

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-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-LhjRyN9kxr8o
  • Commit ID: 2b4301e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit d0960f1 into aws:master Jan 22, 2022
@mergify
Copy link
Contributor

mergify bot commented Jan 22, 2022

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).

kornicameister added a commit to kornicameister/aws-cdk that referenced this pull request Jan 22, 2022
* 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)
  ...
LukvonStrom pushed a commit to LukvonStrom/aws-cdk that referenced this pull request Jan 26, 2022
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*
mergify bot pushed a commit that referenced this pull request Feb 7, 2022
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*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
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*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this pull request Feb 21, 2022
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*
@yamatatsu yamatatsu deleted the iotevents-dm branch April 6, 2022 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-iotevents Related to AWS IoT Events
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants