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

(iotevents): add aws-iotevents DetectorModel L2 Construct #17711

Closed
1 of 2 tasks
yamatatsu opened this issue Nov 25, 2021 · 13 comments
Closed
1 of 2 tasks

(iotevents): add aws-iotevents DetectorModel L2 Construct #17711

yamatatsu opened this issue Nov 25, 2021 · 13 comments
Labels
@aws-cdk/aws-iotevents Related to AWS IoT Events closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/large Large work item – several weeks of effort feature/new-construct A request for a new L2 construct feature-request A feature should be added or improved. in-progress This issue is being actively worked on. p2

Comments

@yamatatsu
Copy link
Contributor

yamatatsu commented Nov 25, 2021

Description

Now, @aws-cdk/aws-iotevents has no L2 Construct. I will implements L2 Constructs.

Use Case

When users create IoT Events DetectorModel, This Cunstruct will support it.

Proposed Solution

We can create L2 constructs for aws-iotevents.

Other information

I'm starting to design.

And in first PR, I'm going to commit DetectorModel and State with only required properties.

Acknowledge

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Design

ref: CloudFormation

usage:

const timer = new iotevents.Timer("heartbeatTimer", 60);

// Define nodes of the state machine
const onlineState = new iotevents.State({
  stateName: "online",
  onEnterEvents: [
    {
      eventName: "setTimer",
      actions: [timer.set()], // `timer.set()` return `SetTimerAction`
    },
  ],
  onInputEvents: [
    {
      eventName: "resetTimer",
      condition: 'currentInput("HeartbeatInputData")',
      actions: [timer.reset()], // `timer.reset()` return `ResetTimerAction`
    },
  ],
});
const offlineState = new iotevents.State({
  stateName: "offline",
});

// Define edges of the state machine
onlineState.transitionTo({
  eventName: "to_offline",
  condition: timer.timeout(), // `timer.timeout()` return just string
  nextState: offlineState,
});
offlineState.transitionTo({
  eventName: "to_online",
  condition: 'currentInput("HeartbeatInputData")',
  nextState: onlineState,
});

// Define the state machine
new iotevents.DetectorModel(this, "DetectorModel", {
  initialState: onlineState,
});

DetectorModel:

class DetectorModel {
  constructor(private readonly initialState: State) {}

  private getDefinition() {
    const stateSet = this.initialState.getGraphStates();

    return {
      initialState: this.initialState.stateName,
      states: Array.from(stateSet).map((state) => state.toCfn()),
    };
  }
}

State:

class State {
  constructor(private readonly props: StateProps) {}

  /**
   * get states recursively
   */
  getGraphStates(stateSet: Set<State> = new Set<State>()): Set<State> {
    if (stateSet.has(this)) {
      return stateSet;
    }
    stateSet.add(this);

    const nextStates = initialState.transitionEvents.forEach((te) => {
      te.nextState.getGraphStates(stateSet);
    });

    return stateSet;
  }
}

graph:

image

image

Roadmap

  1. implement DetectorModel and State with only required properties
    • It will not be able to have multiple states yet.
  2. implement state.transitionTo()
    • It will be able to have multiple states and to transit.
    • It will not be able to have events that is without transition.
    • It will not be able to perform actions.
  3. implement IAction
  4. create new repository aws-iotevents-actions
  5. implement onInput and onExit
  6. implement rest Expressions
  7. implement some actions (separate PRs for each actions)
    • It will be able to perform actions.
@yamatatsu yamatatsu added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Nov 25, 2021
@github-actions github-actions bot added the @aws-cdk/aws-iotevents Related to AWS IoT Events label Nov 25, 2021
@yamatatsu
Copy link
Contributor Author

I will contribute!

@peterwoodworth peterwoodworth added effort/large Large work item – several weeks of effort feature/new-construct A request for a new L2 construct and removed needs-triage This issue or PR still needs to be triaged. labels Nov 26, 2021
@peterwoodworth
Copy link
Contributor

Thanks for the feature request for an l2 @yamatatsu! Excited to see what comes from this :)

@skinny85 will you be able to help review the design here?

@yamatatsu
Copy link
Contributor Author

yamatatsu commented Nov 27, 2021

@skinny85
Sorry. above diagram is yet draft, so I'll ask for a review after a more thorough examination.

@yamatatsu
Copy link
Contributor Author

@skinny85
I've edited above description!
I've written a lot, but it might be better to just agree on USAGE this issue and discuss the others in PR😉

@skinny85
Copy link
Contributor

Thanks @yamatatsu. My comments:

  1. Why the double array here (array of events with an array of actions)?

      onEnterEvents: [{
      eventName: 'setTimer',
      actions: [timer.set()], // `timer.set()` return `SetTimerAction`
    }],
  2. I don't know what Timer is - it's not created with the iotevents module prefix...

  3. I don't understand what condition is timer.timeout() here:

    onlineState.addTransition({
      eventName: 'to_offline',
      condition: timer.timeout(), // `timer.timeout()` return just string
      nextState: offlineState,
    });
  4. I don't like the name State.addTransition(). I would prefer something like State.transitionTo(otherState, options).

  5. I don't see what's the point of Definition here:

    new iotevents.DetectorModel(this, 'DetectorModel', {
      definition: new iotevents.Definition(onlineState),
    });

    Seems purely like boilerplate that DetectorModel can do for you.
    I would change to:

    new iotevents.DetectorModel(this, 'DetectorModel', {
      initialState: onlineState,
    });

Just my quick thoughts based on the first reading of the proposal 🙂.

@yamatatsu
Copy link
Contributor Author

@skinny85

  1. Why the double array here (array of events with an array of actions)?

My sample code is too simple. 😅 An event can be specified a condition which the event to be triggerd or not.
For example, following case:

  • (A) On every enter the state, set timer.
  • (B) On every enter the state, increment count of enter the state.
  • (C) On enter the state at first time, notify something.

The code will be written as following:

onEnterEvents: [
  {
    eventName: 'setTimer',
    actions: [
      timer.set(), // (A)
      { // (B)
        variableName: 'countOfEnterThisState',
        value: 'isUndefined($variable.countOfEnterThisState) && 1 || $variable.countOfEnterThisState + 1',
      },
    ],
  },
  {
    eventName: 'firstEnter',
    condition: '$variable.countOfEnterThisState == 1',
    actions: [
      { // (C)
        lambda: { // CFn page: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-iotevents-detectormodel-lambda.html
          functionArn: 'notification-lambda-arn',
        },
      }
    ],
  },
],
  1. I don't know what Timer is - it's not created with the iotevents module prefix...

Oh.. sorry!

const timer = new iotevents.Timer('heartbeatTimer', 60);

And I've fix also above description code.

  1. I don't understand what condition is timer.timeout() here:

I intend to implement Timer as just generater for SetTimer, ResetTimer, ClearTimer and timeout() Function. It will generate these from same timerName.

const timer = new iotevents.Timer('heartbeatTimer', 60);
timer.set()
// => {
//   SetTimer: {
//     TimerName: "heartbeatTimer",
//     Seconds: 60,
//   }
// }
timer.reset()
// => {
//   ResetTimer: {
//     TimerName: "heartbeatTimer",
//   }
// }
timer.clear()
// => {
//   ClearTimer: {
//     TimerName: "heartbeatTimer",
//   }
// }
timer.timeout()
// => 'timeout("heartbeatTimer")'
  1. I don't like the name State.addTransition(). I would prefer something like State.transitionTo(otherState, options).

Sounds good! Thank you!

  1. I don't see what's the point of Definition here:

Sounds good! 👍

@skinny85
Copy link
Contributor

OK, makes sense 🙂. Looks good to me, feel free to start working on the implementation based on that design.

@yamatatsu
Copy link
Contributor Author

I've craete an PR that I implemented Inputin, before creating an PR that I implement DetectorModel.
Because CloudFormation threw an error on deploying DetectorModel:

Detector Model Definition must use at least one Input in a condition.

Following refference that refer to Input is needed:

const input = new iotevents.Input(this, 'input', {
  attributeJsonPaths: ['payload.temperature'],
});

const onlineState = new iotevents.State({
  stateName: 'online',
  onEnterEvents: [{
    eventName: 'test-event',
    condition: `currentInput("${input.inputName}")`, // this reference is needed for deploying
  }],
});

new iotevents.DetectorModel(this, 'MyDetectorModel', {
  initialState: onlineState,
});

mergify bot pushed a commit that referenced this issue Dec 11, 2021
This is proposed by #17711.

This PR was created for implemeting `Input` L2 Construct. Implementing it is needed before `DetectorModel`. The reason is described in here: #17711 (comment)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@skinny85 skinny85 added the in-progress This issue is being actively worked on. label Dec 15, 2021
@skinny85 skinny85 removed their assignment Dec 15, 2021
mergify bot pushed a commit that referenced this issue Jan 22, 2022
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](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*
LukvonStrom pushed a commit to LukvonStrom/aws-cdk that referenced this issue 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 issue Jan 27, 2022
… 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*
@skinny85
Copy link
Contributor

skinny85 commented Feb 1, 2022

@yamatatsu I believe this was done in #18049 - can we close this issue?

@yamatatsu
Copy link
Contributor Author

@skinny85 This issue have the roadmap and we have rest works.
But if it is better that this issue is splited to some small issues, I will close this issue and create small issues 👍🏻 WDYT?

@yamatatsu
Copy link
Contributor Author

yamatatsu commented Feb 2, 2022

The Roadmap is here, and I've reach to No.2 implement state.transitionTo() with #18768 .

Roadmap

  1. implement DetectorModel and State with only required properties
    • It will not be able to have multiple states yet.
  2. implement state.transitionTo()
    • It will be able to have multiple states and to transit.
    • It will not be able to have events that is without transition.
    • It will not be able to perform actions.
  3. implement IAction
  4. create new repository aws-iotevents-actions
  5. implement some actions (separate PRs for each actions)
    • It will be able to perform actions.

@skinny85
Copy link
Contributor

skinny85 commented Feb 2, 2022

Nah, let's just keep this open then, no need to create new issues for this 🙂.

@skinny85 skinny85 removed their assignment Feb 2, 2022
mergify bot pushed a commit that referenced this issue 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 issue Feb 21, 2022
This is proposed by aws#17711.

This PR was created for implemeting `Input` L2 Construct. Implementing it is needed before `DetectorModel`. The reason is described in here: aws#17711 (comment)

----

*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 issue 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 issue Feb 21, 2022
… of DetectorModel (aws#18644)

This PR is about aws#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*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue 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*
mergify bot pushed a commit that referenced this issue Mar 4, 2022
This PR allow IoT Events detector model to perform actions as [this documentation](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-supported-actions.html).
This PR is in roadmap of #17711.

![スクリーンショット 2022-02-08 22 43 33](https://user-images.githubusercontent.com/11013683/152999288-81721f15-fefb-4108-b34b-aab3f88a7ab8.png)

With this fix, all the interfaces of the DetectorModel are now implemented! And next works is implementing expressions and actions.

The exapmle in readme became not simple, so also this PR has sorted explanation of readme.

----

*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 issue Mar 8, 2022
…19249)

This PR allow states in IoT Events detector model to set event on input and exit.
This PR is in roadmap of #17711.
<img width="530" alt="スクリーンショット 2022-03-05 13 40 57" src="https://user-images.githubusercontent.com/11013683/156868196-a37f5926-05e2-4d3b-a881-17520b465518.png">


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TheRealAmazonKendra pushed a commit to TheRealAmazonKendra/aws-cdk that referenced this issue Mar 11, 2022
This PR allow IoT Events detector model to perform actions as [this documentation](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-supported-actions.html).
This PR is in roadmap of aws#17711.

![スクリーンショット 2022-02-08 22 43 33](https://user-images.githubusercontent.com/11013683/152999288-81721f15-fefb-4108-b34b-aab3f88a7ab8.png)

With this fix, all the interfaces of the DetectorModel are now implemented! And next works is implementing expressions and actions.

The exapmle in readme became not simple, so also this PR has sorted explanation of readme.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TheRealAmazonKendra pushed a commit to TheRealAmazonKendra/aws-cdk that referenced this issue Mar 11, 2022
…ws#19249)

This PR allow states in IoT Events detector model to set event on input and exit.
This PR is in roadmap of aws#17711.
<img width="530" alt="スクリーンショット 2022-03-05 13 40 57" src="https://user-images.githubusercontent.com/11013683/156868196-a37f5926-05e2-4d3b-a881-17520b465518.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 issue Aug 3, 2022
This PR is a part of roadmap in #17711. And if this PR is merged, I will close this issue and create some good first issues to implement rest actions and expressions.

This PR supports the timer actions (`SetTimerAction`, `ResetTimerAction` and `ClearTimerAction`) and `timeout()` expression.

These allow to embed a timer to the state machine of the detector model.
Below figure illustrate the state machine [Device Heartbeat](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-examples-dhb.html). This detector model is used to alert if the message is interrupted for a certain period of time. The integ-test included in this PR is example of creating Device Heartbeat detector model.

```mermaid
stateDiagram-v2
  [*] --> Online: set a timer on input message
  Online --> Online: reset the timer\non input messages
  Online --> Offline: timeout the timer
  Offline --> Online: input messages
```

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
josephedward pushed a commit to josephedward/aws-cdk that referenced this issue Aug 30, 2022
This PR is a part of roadmap in aws#17711. And if this PR is merged, I will close this issue and create some good first issues to implement rest actions and expressions.

This PR supports the timer actions (`SetTimerAction`, `ResetTimerAction` and `ClearTimerAction`) and `timeout()` expression.

These allow to embed a timer to the state machine of the detector model.
Below figure illustrate the state machine [Device Heartbeat](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-examples-dhb.html). This detector model is used to alert if the message is interrupted for a certain period of time. The integ-test included in this PR is example of creating Device Heartbeat detector model.

```mermaid
stateDiagram-v2
  [*] --> Online: set a timer on input message
  Online --> Online: reset the timer\non input messages
  Online --> Offline: timeout the timer
  Offline --> Online: input messages
```

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

github-actions bot commented Mar 5, 2023

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels Mar 5, 2023
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 closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. effort/large Large work item – several weeks of effort feature/new-construct A request for a new L2 construct feature-request A feature should be added or improved. in-progress This issue is being actively worked on. p2
Projects
None yet
Development

No branches or pull requests

3 participants