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): support timer actions #19949

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comment
yamatatsu committed Aug 2, 2022
commit baaa3d0c69d30d7eed6732fd1bd53d179d399f81
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-iotevents-actions/lib/index.ts
Original file line number Diff line number Diff line change
@@ -3,3 +3,4 @@ export * from './set-variable-action';
export * from './lambda-invoke-action';
export * from './reset-timer-action';
export * from './set-timer-action';
export * from './timer-duration';
62 changes: 7 additions & 55 deletions packages/@aws-cdk/aws-iotevents-actions/lib/set-timer-action.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,27 @@
import * as iotevents from '@aws-cdk/aws-iotevents';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';

/**
* Configuration properties of an action to set a timer.
*/
export interface SetTimerActionProps {
/**
* The duration of the timer, in seconds. One of `duration` or `durationExpression` is required.
*
* The range of the duration is 60-31622400 seconds.
* The evaluated result of the duration expression is rounded down to the nearest whole number.
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds.
*
* @default - none, required if no `durationExpression` is defined.
*/
readonly duration?: cdk.Duration;

/**
* The duration of the timer, in seconds. One of `duration` or `durationExpression` is required.
*
* You can use a string expression that includes numbers, variables ($variable.<variable-name>),
* and input values ($input.<input-name>.<path-to-datum>) as the duration.
*
* The range of the duration is 60-31622400 seconds.
* The evaluated result of the duration expression is rounded down to the nearest whole number.
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds.
*
* @default - none, required if no `duration` is defined.
*/
readonly durationExpression?: iotevents.Expression;
}
import { TimerDuration } from './timer-duration';

/**
* The action to create a timer with duration in seconds.
*/
export class SetTimerAction implements iotevents.IAction {
private readonly durationExpression: string | undefined;

/**
* @param timerName the name of the timer
* @param props the properties to set duration
* @param timerDuration the duration of the timer
*/
constructor(private readonly timerName: string, props: SetTimerActionProps) {
if (!props.duration && !props.durationExpression) {
throw new Error('Either duration or durationExpression must be specified');
}
if (props.duration && props.durationExpression) {
throw new Error('duration and durationExpression cannot be specified at the same time');
}
if (props.duration) {
const seconds = props.duration.toSeconds();
if (seconds < 60) {
throw new Error(`duration cannot be less than 60 seconds, got: ${props.duration.toString()}`);
}
if (seconds > 31622400) {
throw new Error(`duration cannot be greater than 31622400 seconds, got: ${props.duration.toString()}`);
}
this.durationExpression = seconds.toString();
}
if (props.durationExpression) {
this.durationExpression = props.durationExpression.evaluate();
}
constructor(
private readonly timerName: string,
private readonly timerDuration: TimerDuration,
) {
}

bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig {
public _bind(_scope: Construct, _options: iotevents.ActionBindOptions): iotevents.ActionConfig {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be better off as it is I think.
This is implementation of IAction and is following Integration in DESIGN_GUIDELINE as same as aws-events target.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm annoyed with that but it's not in scope here to ask you to fix an old contract I'm mad at. Maybe I'll get neurotic about it and do some excessive refactoring.

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've understood that refactoring is necessary even in light of the current implementations.
I'll refactor it!

return {
configuration: {
setTimer: {
timerName: this.timerName,
durationExpression: this.durationExpression,
durationExpression: this.timerDuration._bind(),
},
},
};
54 changes: 54 additions & 0 deletions packages/@aws-cdk/aws-iotevents-actions/lib/timer-duration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as iotevents from '@aws-cdk/aws-iotevents';
import { Duration } from '@aws-cdk/core';

/**
* The duration of the timer.
*/
export abstract class TimerDuration {
/**
* Create a timer-duration from Duration.
*
* The range of the duration is 60-31622400 seconds.
* The evaluated result of the duration expression is rounded down to the nearest whole number.
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds.
*/
public static fromDuration(duration: Duration): TimerDuration {
const seconds = duration.toSeconds();
if (seconds < 60) {
throw new Error(`duration cannot be less than 60 seconds, got: ${duration.toString()}`);
}
if (seconds > 31622400) {
throw new Error(`duration cannot be greater than 31622400 seconds, got: ${duration.toString()}`);
}
return new TimerDurationImpl(seconds.toString());
}

/**
* Create a timer-duration from Expression.
*
* You can use a string expression that includes numbers, variables ($variable.<variable-name>),
* and input values ($input.<input-name>.<path-to-datum>) as the duration.
*
* The range of the duration is 60-31622400 seconds.
* The evaluated result of the duration expression is rounded down to the nearest whole number.
* For example, if you set the timer to 60.99 seconds, the evaluated result of the duration expression is 60 seconds.
*/
public static fromExpression(expression: iotevents.Expression): TimerDuration {
return new TimerDurationImpl(expression.evaluate());
}

/**
* @internal
*/
public abstract _bind(): string;
}

class TimerDurationImpl extends TimerDuration {
constructor(private readonly durationExpression: string) {
super();
}

public _bind() {
return this.durationExpression;
}
}
Original file line number Diff line number Diff line change
@@ -28,9 +28,7 @@ class TestStack extends cdk.Stack {
eventName: 'enter-event',
condition: iotevents.Expression.currentInput(input),
actions: [
new actions.SetTimerAction('MyTimer', {
duration: cdk.Duration.seconds(60),
}),
new actions.SetTimerAction('MyTimer', actions.TimerDuration.fromDuration(cdk.Duration.seconds(60))),
],
}],
onInput: [{
Original file line number Diff line number Diff line change
@@ -11,8 +11,8 @@ beforeEach(() => {
});

test.each([
['Can set duration', { duration: cdk.Duration.minutes(2) }, '120'],
['Can set durationExpression', { durationExpression: iotevents.Expression.fromString('test-expression') }, 'test-expression'],
['Can set duration', actions.TimerDuration.fromDuration(cdk.Duration.minutes(2)), '120'],
['Can set durationExpression', actions.TimerDuration.fromExpression(iotevents.Expression.fromString('test-expression')), 'test-expression'],
])('%s', (_, durationOption, durationExpression) => {
// WHEN
new iotevents.DetectorModel(stack, 'MyDetectorModel', {
@@ -48,15 +48,10 @@ test.each([
});

test.each([
['neither duration nor durationExpression', {}, 'Either duration or durationExpression must be specified'],
['both duration and durationExpression', {
duration: cdk.Duration.seconds(2),
durationExpression: iotevents.Expression.fromString('test-expression'),
}, 'duration and durationExpression cannot be specified at the same time'],
['duration less than 60 seconds', { duration: cdk.Duration.seconds(59) }, 'duration cannot be less than 60 seconds, got: Duration.seconds(59)'],
['duration greater than 31622400 seconds', { duration: cdk.Duration.seconds(31622401) }, 'duration cannot be greater than 31622400 seconds, got: Duration.seconds(31622401)'],
])('Cannot set %', (_, options, errorMessage) => {
['duration less than 60 seconds', cdk.Duration.seconds(59), 'duration cannot be less than 60 seconds, got: Duration.seconds(59)'],
['duration greater than 31622400 seconds', cdk.Duration.seconds(31622401), 'duration cannot be greater than 31622400 seconds, got: Duration.seconds(31622401)'],
])('Cannot set %', (_, duration, errorMessage) => {
expect(() => {
new actions.SetTimerAction('MyTimer', options);
actions.TimerDuration.fromDuration(duration);
}).toThrow(errorMessage);
});