Skip to content

Commit

Permalink
JavaScript (v3): libs - Move Prompter.logSeparator to Logger.logSepar…
Browse files Browse the repository at this point in the history
…ator.
  • Loading branch information
cpyle0819 authored and ford-at-aws committed Oct 13, 2023
1 parent bf45b05 commit 2a4257a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class TopicsQueuesWkflw {
* @param {import('@aws-sdk/client-sns').SNSClient} snsClient
* @param {import('@aws-sdk/client-sqs').SQSClient} sqsClient
* @param {import('../../libs/prompter.js').Prompter} prompter
* @param {import('../../libs/slow-logger.js').Logger} logger
* @param {import('../../libs/logger.js').Logger} logger
*/
constructor(snsClient, sqsClient, prompter, logger) {
this.snsClient = snsClient;
Expand All @@ -72,7 +72,7 @@ export class TopicsQueuesWkflw {
});

if (this.isFifo) {
this.prompter.logSeparator(MESSAGES.headerDedup);
this.logger.logSeparator(MESSAGES.headerDedup);
await this.logger.log(MESSAGES.deduplicationNotice);
await this.logger.log(MESSAGES.deduplicationDescription);
this.autoDedup = await this.prompter.confirm({
Expand All @@ -88,7 +88,7 @@ export class TopicsQueuesWkflw {
});
if (this.isFifo) {
this.topicName += ".fifo";
this.prompter.logSeparator(MESSAGES.headerFifoNaming);
this.logger.logSeparator(MESSAGES.headerFifoNaming);
await this.logger.log(MESSAGES.appendFifoNotice);
}

Expand Down Expand Up @@ -184,7 +184,7 @@ export class TopicsQueuesWkflw {
);

if (index !== 0) {
this.prompter.logSeparator();
this.logger.logSeparator();
}

await this.logger.log(MESSAGES.attachPolicyNotice);
Expand Down Expand Up @@ -399,21 +399,21 @@ export class TopicsQueuesWkflw {
console.clear();

try {
this.prompter.logSeparator(MESSAGES.headerWelcome);
this.logger.logSeparator(MESSAGES.headerWelcome);
await this.welcome();
this.prompter.logSeparator(MESSAGES.headerFifo);
this.logger.logSeparator(MESSAGES.headerFifo);
await this.confirmFifo();
this.prompter.logSeparator(MESSAGES.headerCreateTopic);
this.logger.logSeparator(MESSAGES.headerCreateTopic);
await this.createTopic();
this.prompter.logSeparator(MESSAGES.headerCreateQueues);
this.logger.logSeparator(MESSAGES.headerCreateQueues);
await this.createQueues();
this.prompter.logSeparator(MESSAGES.headerAttachPolicy);
this.logger.logSeparator(MESSAGES.headerAttachPolicy);
await this.attachQueueIamPolicies();
this.prompter.logSeparator(MESSAGES.headerSubscribeQueues);
this.logger.logSeparator(MESSAGES.headerSubscribeQueues);
await this.subscribeQueuesToTopic();
this.prompter.logSeparator(MESSAGES.headerPublishMessage);
this.logger.logSeparator(MESSAGES.headerPublishMessage);
await this.publishMessages();
this.prompter.logSeparator(MESSAGES.headerReceiveMessages);
this.logger.logSeparator(MESSAGES.headerReceiveMessages);
await this.receiveAndDeleteMessages();
} catch (err) {
console.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const SNSClientMock = {

const LoggerMock = {
log: vi.fn(),
logSeparator: vi.fn(),
};

describe("TopicsQueuesWkflw", () => {
Expand Down Expand Up @@ -111,7 +112,6 @@ describe("TopicsQueuesWkflw", () => {
new SQSClient({}),
{
confirm: () => Promise.resolve(true),
logSeparator: vi.fn(),
},
LoggerMock,
);
Expand Down Expand Up @@ -151,7 +151,6 @@ describe("TopicsQueuesWkflw", () => {
{
confirm: () => Promise.resolve(true),
input: () => Promise.resolve("user-input"),
logSeparator: vi.fn(),
},
LoggerMock,
);
Expand Down Expand Up @@ -197,7 +196,6 @@ describe("TopicsQueuesWkflw", () => {
it("should attach a policy to each of the SQS queues", async () => {
const PrompterMock = {
confirm: vi.fn(() => Promise.resolve(true)),
logSeparator: vi.fn(() => {}),
};

const topicsQueuesWkflw = new TopicsQueuesWkflw(
Expand Down
37 changes: 37 additions & 0 deletions javascriptv3/example_code/libs/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

export class Logger {
/**
* @param {string} message
*/
log(message) {
console.log(message);
return Promise.resolve();
}

/**
* Log a horizontal rule to the console. If a message is provided,
* log a section header.
* @param {string?} message
*/
logSeparator(message) {
if (!message) {
console.log("\n", "*".repeat(80), "\n");
} else {
console.log(
"\n",
"*".repeat(80),
"\n",
"** ",
message,
" ".repeat(80 - message.length - 8),
"**\n",
"*".repeat(80),
"\n",
);
}
}
}
25 changes: 1 addition & 24 deletions javascriptv3/example_code/libs/prompter.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Prompter {
checkContinue = async (prompt = "") => {
const prefix = prompt && prompt + " ";
let ok = await this.confirm({
message: `${prefix}Continue?"}`,
message: `${prefix}Continue?`,
});
if (!ok) throw new Error("Exiting...");
};
Expand All @@ -46,29 +46,6 @@ export class Prompter {
return confirm(options);
}

/**
* Log a horizontal rule to the console. If a message is provided,
* log a section header.
* @param {string?} message
*/
logSeparator(message) {
if (!message) {
console.log("\n", "*".repeat(80), "\n");
} else {
console.log(
"\n",
"*".repeat(80),
"\n",
"** ",
message,
" ".repeat(80 - message.length - 8),
"**\n",
"*".repeat(80),
"\n",
);
}
}

/**
* @param {{ message: string, choices: { name: string, value: string }[]}} options
*/
Expand Down
22 changes: 7 additions & 15 deletions javascriptv3/example_code/libs/slow-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.wkflw.topicsandqueues.logger]
export class Logger {
/**
* @param {string} message
*/
log(message) {
console.log(message);
return Promise.resolve();
}
}
import { Logger } from "./logger.js";

// snippet-start:[javascript.v3.wkflw.topicsandqueues.logger]
export class SlowLogger extends Logger {
constructor(delayInMs) {
super();
this.delay = delayInMs;
}

sleep() {
_sleep() {
return new Promise((resolve) => setTimeout(resolve, this.delay));
}

/**
* @param {string} message
*/
async logSlow(message) {
async _logSlow(message) {
const chars = message.split("");
for (const c of chars) {
process.stdout.write(c);
await this.sleep();
await this._sleep();
}
process.stderr.write("\n");
}
Expand All @@ -49,12 +41,12 @@ export class SlowLogger extends Logger {
let line = "";
for (const word of words) {
if (line.length + word.length > maxWidth) {
await this.logSlow(line);
await this._logSlow(line);
line = "";
}
line += word + " ";
}
await this.logSlow(line);
await this._logSlow(line);
}
}
// snippet-end:[javascript.v3.wkflw.topicsandqueues.logger]
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class SageMakerPipelinesWkflw {

/**
* @param {import("libs/prompter.js").Prompter} prompter
* @param {import("libs/slow-logger.js").Logger} logger
* @param {import("libs/logger.js").Logger} logger
* @param {{ IAM: import("@aws-sdk/client-iam").IAMClient, Lambda: import("@aws-sdk/client-lambda").LambdaClient, SageMaker: import("@aws-sdk/client-sagemaker").SageMakerClient, S3: import("@aws-sdk/client-s3").S3Client, SQS: import("@aws-sdk/client-sqs").SQSClient }} clients
*/
constructor(prompter, logger, clients) {
Expand All @@ -61,7 +61,7 @@ export class SageMakerPipelinesWkflw {
} finally {
// Run all of the clean up functions. If any fail, we log the error and continue.
// This ensures all clean up functions are run.
this.prompter.logSeparator();
this.logger.logSeparator();
const doCleanUp = await this.prompter.confirm({
message: "Clean up resources?",
});
Expand All @@ -77,10 +77,10 @@ export class SageMakerPipelinesWkflw {
}

async startWorkflow() {
this.prompter.logSeparator(MESSAGES.greetingHeader);
this.logger.logSeparator(MESSAGES.greetingHeader);
await this.logger.log(MESSAGES.greeting);

this.prompter.logSeparator();
this.logger.logSeparator();
await this.logger.log(
MESSAGES.creatingRole.replace(
"${ROLE_NAME}",
Expand All @@ -105,7 +105,7 @@ export class SageMakerPipelinesWkflw {
),
);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.creatingRole.replace(
Expand All @@ -132,7 +132,7 @@ export class SageMakerPipelinesWkflw {
),
);

this.prompter.logSeparator();
this.logger.logSeparator();

// Create an IAM policy that allows the AWS Lambda function to invoke SageMaker APIs.
const {
Expand Down Expand Up @@ -167,7 +167,7 @@ export class SageMakerPipelinesWkflw {

await this.logger.log(MESSAGES.policyAttached);

this.prompter.logSeparator();
this.logger.logSeparator();

// Create Lambda layer for SageMaker packages.
const { versionArn: layerVersionArn, cleanUp: lambdaLayerCleanUp } =
Expand Down Expand Up @@ -201,7 +201,7 @@ export class SageMakerPipelinesWkflw {
),
);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.creatingSQSQueue.replace("${QUEUE_NAME}", this.names.SQS_QUEUE),
Expand All @@ -222,7 +222,7 @@ export class SageMakerPipelinesWkflw {
MESSAGES.sqsQueueCreated.replace("${QUEUE_NAME}", this.names.SQS_QUEUE),
);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.configuringLambdaSQSEventSource
Expand All @@ -247,7 +247,7 @@ export class SageMakerPipelinesWkflw {
.replace("${QUEUE_NAME}", this.names.SQS_QUEUE),
);

this.prompter.logSeparator();
this.logger.logSeparator();

// Create an IAM policy that allows the SageMaker pipeline to invoke AWS Lambda
// and send messages to the Amazon SQS queue.
Expand Down Expand Up @@ -287,7 +287,7 @@ export class SageMakerPipelinesWkflw {

await this.logger.log(MESSAGES.policyAttached);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.creatingPipeline.replace(
Expand All @@ -312,7 +312,7 @@ export class SageMakerPipelinesWkflw {
),
);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.creatingS3Bucket.replace("${BUCKET_NAME}", this.names.S3_BUCKET),
Expand All @@ -329,7 +329,7 @@ export class SageMakerPipelinesWkflw {
MESSAGES.s3BucketCreated.replace("${BUCKET_NAME}", this.names.S3_BUCKET),
);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(
MESSAGES.uploadingInputData.replace(
Expand All @@ -346,7 +346,7 @@ export class SageMakerPipelinesWkflw {

await this.logger.log(MESSAGES.inputDataUploaded);

this.prompter.logSeparator();
this.logger.logSeparator();

await this.prompter.checkContinue(MESSAGES.executePipeline);

Expand All @@ -365,7 +365,7 @@ export class SageMakerPipelinesWkflw {
sagemakerClient: this.clients.SageMaker,
});

this.prompter.logSeparator();
this.logger.logSeparator();

await this.logger.log(MESSAGES.outputDelay);

Expand All @@ -379,7 +379,7 @@ export class SageMakerPipelinesWkflw {
}),
);

this.prompter.logSeparator();
this.logger.logSeparator();
await this.logger.log(MESSAGES.outputDataRetrieved);
console.log(output.split("\n").slice(0, 6).join("\n"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { describe, test } from "vitest";
import { SageMakerPipelinesWkflw } from "../scenarios/wkflw-sagemaker-geospatial-pipeline/SageMakerPipelinesWkflw.js";
import { Logger } from "libs/slow-logger.js";
import { Logger } from "libs/logger.js";
import { IAMClient } from "@aws-sdk/client-iam";
import { SageMakerClient } from "@aws-sdk/client-sagemaker";
import { S3Client } from "@aws-sdk/client-s3";
Expand Down

0 comments on commit 2a4257a

Please sign in to comment.