Skip to content

Commit

Permalink
chore: convert Lambda handlers to vanilla JS (#24)
Browse files Browse the repository at this point in the history
Partially addresses #18

We don't necessarily need JSII (and therefore TS) to process these.
  • Loading branch information
echeung-amzn authored Feb 25, 2022
1 parent f43b4f7 commit 5f9bf6b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// Ignoring some checks since the function is in pure JS

/* eslint-disable */
const aws = require('aws-sdk');

const DOCS = `
## Display a CloudWatch bitmap graph
Displays CloudWatch metrics as a bitmap, for faster display of metrics.
### Widget parameters
Param | Description
---|---
**graph** | The graph definition. Use the parameters from the **Source** tab in CloudWatch Console's **Metrics** page.
**useMonitorPortal** (default = true) | Flag indicating whether we want to use MonitorPortal to render the graph. False will switch to CloudWatch API.
### Example parameters
\`\`\` yaml
useMonitorPortal: boolean
Expand All @@ -24,11 +21,11 @@ const DOCS = `
\`\`\`
`;

exports.handler = async (event: any) => {
async function renderUsingCloudWatch(graph: any, width: number, height: number) {
exports.handler = async (event) => {
async function renderUsingCloudWatch(graph, width, height) {
const params = {MetricWidget: JSON.stringify(graph)};
const region = graph.region;
const customBackoff = (retryCount: number) => {
const customBackoff = (retryCount) => {
// Keep retrying with a random delay, long enough to overcome throttling from CW
const delay = 300 + Math.floor(Math.random() * 500);
console.log(`retry number ${retryCount} with a delay of ${delay} ms`);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import aws = require("aws-sdk");
const aws = require("aws-sdk");

const region = process.env.AWS_REGION;
const millisPerDay = 1000 * 60 * 60 * 24;
Expand All @@ -15,13 +15,13 @@ const clientOptions = {
const cloudwatch = new aws.CloudWatch(clientOptions);
const sm = new aws.SecretsManager(clientOptions);

function daysSince(date: any, now = Date.now()) {
function daysSince(date, now = Date.now()) {
const millis = now - date.getTime();
const days = millis / millisPerDay;
return Math.floor(days);
}

exports.handler = async (event: any, context: any) => {
exports.handler = async (event, context) => {
console.debug("event:", JSON.stringify(event));
console.debug("context:", JSON.stringify(context));

Expand Down
18 changes: 9 additions & 9 deletions test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f9bf6b

Please sign in to comment.