Skip to content

Commit 95997b4

Browse files
committed
intermediate
1 parent 26186ee commit 95997b4

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

packages/@aws-cdk/tmp-toolkit-helpers/src/api/io/private/span.ts

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as uuid from 'uuid';
33
import type { ActionLessMessage, IoHelper } from './io-helper';
44
import type { IoMessageMaker } from './message-maker';
55
import { formatTime } from '../../../util';
6+
import type { Duration } from '../payloads/types';
67

78
export interface SpanEnd {
89
readonly duration: number;
@@ -55,7 +56,7 @@ type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
5556
/**
5657
* Ending the span returns the observed duration
5758
*/
58-
export interface Duration {
59+
interface ElapsedTime {
5960
readonly asMs: number;
6061
readonly asSec: number;
6162
}
@@ -64,10 +65,31 @@ export interface Duration {
6465
* A message span that can be ended and read times from
6566
*/
6667
export interface IMessageSpan<E extends SpanEnd> {
67-
time(): Duration;
68-
end(payload: VoidWhenEmpty<Omit<E, keyof SpanEnd>>): Promise<Duration>;
69-
end(payload: VoidWhenEmpty<Optional<E, keyof SpanEnd>>): Promise<Duration>;
70-
end(message: string, payload: ForceEmpty<Optional<E, keyof SpanEnd>>): Promise<Duration>;
68+
/**
69+
* Get the time elapsed since the start
70+
*/
71+
elapsedTime(): Promise<ElapsedTime>;
72+
/**
73+
* Sends a simple, generic message with the current timing
74+
* For more complex intermediate messages, get the `elapsedTime` and use `notify`
75+
*/
76+
timing(maker: IoMessageMaker<Duration>, message?: string): Promise<ElapsedTime>;
77+
/**
78+
* Sends an arbitrary intermediate message as part of the span
79+
*/
80+
notify(message: ActionLessMessage<unknown>): Promise<void>;
81+
/**
82+
* End the span with a payload
83+
*/
84+
end(payload: VoidWhenEmpty<Omit<E, keyof SpanEnd>>): Promise<ElapsedTime>;
85+
/**
86+
* End the span with a payload, overwriting
87+
*/
88+
end(payload: VoidWhenEmpty<Optional<E, keyof SpanEnd>>): Promise<ElapsedTime>;
89+
/**
90+
* End the span with a message and payload
91+
*/
92+
end(message: string, payload: ForceEmpty<Optional<E, keyof SpanEnd>>): Promise<ElapsedTime>;
7193
}
7294

7395
/**
@@ -108,6 +130,7 @@ export class SpanMaker<S extends object, E extends SpanEnd> {
108130
startPayload,
109131
));
110132

133+
const timingMsg = '\n✨ %s time: %ds\n';
111134
const time = () => {
112135
const elapsedTime = new Date().getTime() - startTime;
113136
return {
@@ -117,22 +140,36 @@ export class SpanMaker<S extends object, E extends SpanEnd> {
117140
};
118141

119142
return {
120-
time(): Duration {
121-
const elapsedTime = new Date().getTime() - startTime;
122-
return {
123-
asMs: elapsedTime,
124-
asSec: formatTime(elapsedTime),
125-
};
143+
elapsedTime: async (msg?: ActionLessMessage<object>): Promise<ElapsedTime> => {
144+
if (msg) {
145+
await notify({
146+
...msg,
147+
data: msg.data,
148+
});
149+
}
150+
return time();
151+
},
152+
153+
notify: async(msg: ActionLessMessage<unknown>): Promise<void> => {
154+
await notify(msg);
155+
},
156+
157+
timing: async(maker: IoMessageMaker<Duration>, message?: string): Promise<ElapsedTime> => {
158+
const duration = time();
159+
const endMsg = message ? message : timingMsg;
160+
await notify(maker.msg(util.format(endMsg, this.definition.name, duration.asSec), {
161+
duration: duration.asMs,
162+
}));
163+
return duration;
126164
},
127165

128-
end: async (a: any, b?: ForceEmpty<Optional<E, keyof SpanEnd>>): Promise<Duration> => {
166+
end: async (a: any, b?: ForceEmpty<Optional<E, keyof SpanEnd>>): Promise<ElapsedTime> => {
129167
const duration = time();
130-
const endMsg = b ? a : `\n✨ %s time: ${duration.asSec}s\n`;
168+
const endMsg = b ? a : timingMsg;
131169
const endPayload = b ?? a;
132170

133171
await notify(this.definition.end.msg(
134-
util.format(endMsg, this.definition.name), {
135-
marker: spanId,
172+
util.format(endMsg, this.definition.name, duration.asSec), {
136173
duration: duration.asMs,
137174
...endPayload,
138175
} as E));

packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ export class Toolkit extends CloudAssemblySourceBuilder implements AsyncDisposab
497497
: ` ✅ ${stack.displayName}`;
498498

499499
await ioHelper.notify(IO.CDK_TOOLKIT_I5900.msg(chalk.green('\n' + message), deployResult));
500-
deployDuration = await deploySpan.time();
500+
deployDuration = await deploySpan.timing(IO.CDK_TOOLKIT_I5000);
501501

502502
if (Object.keys(deployResult.outputs).length > 0) {
503503
const buffer = ['Outputs:'];

packages/@aws-cdk/toolkit-lib/test/actions/deploy.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ function successfulDeployment() {
285285
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({
286286
action: 'deploy',
287287
level: 'info',
288+
code: 'CDK_TOOLKIT_I5000',
288289
message: expect.stringContaining('Deployment time:'),
289290
}));
290291
}

0 commit comments

Comments
 (0)