Skip to content

Commit 6ca29b2

Browse files
committed
Improve documentation
1 parent 0fcd6ed commit 6ca29b2

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

alertmanager/index.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ var sns = new AWS.SNS();
1212

1313
var dynamodb = new AWS.DynamoDB();
1414

15-
function flatten(kv) {
15+
// DynamoDB records are typed by this horrible convention:
16+
// { "name": { S: "a string value" }, "age": { N: "123" } }
17+
// that actually means
18+
// { "name": "a string value", "age": 123 }
19+
// read more @ https://www.npmjs.com/package/dynamodb-data-types
20+
function unwrapDynamoDBTypedObject(kv) {
1621
var ret = {};
1722

1823
if (typeof (kv) !== 'object') {
19-
throw new Error('Can only flatten key-value objects');
24+
throw new Error('Can only unwrapDynamoDBTypedObject key-value objects');
2025
}
2126

2227
for (var key in kv) {
@@ -44,6 +49,8 @@ function httpSucceedAndLog(context, succeedResult) {
4449
function failAndLog(context, failResult) {
4550
console.log(failResult);
4651
context.fail(failResult);
52+
// TODO: do we have to respond to failures with the HTTP statusCode
53+
// wrapper when using Lambda-proxy in ApiGateway?
4754
/*
4855
context.fail({
4956
statusCode: 500,
@@ -64,10 +71,10 @@ var apis = {
6471
return;
6572
}
6673

67-
// httpSucceedAndLog(context, data.Items.map(flatten));
74+
// httpSucceedAndLog(context, data.Items.map(unwrapDynamoDBTypedObject));
6875
context.succeed({
6976
statusCode: 200,
70-
body: JSON.stringify(data.Items.map(flatten))
77+
body: JSON.stringify(data.Items.map(unwrapDynamoDBTypedObject))
7178
});
7279
});
7380
},
@@ -118,7 +125,7 @@ var apis = {
118125
return;
119126
}
120127

121-
var items = data.Items.map(flatten);
128+
var items = data.Items.map(unwrapDynamoDBTypedObject);
122129

123130
var largestNumber = 0;
124131

@@ -202,7 +209,7 @@ var apis = {
202209
},
203210

204211
'DynamoDB: alertmanager_alerts': function (event, context) {
205-
if (event.Records.length !== 1) { // should not happen, as trigger config: BatchSize=0
212+
if (event.Records.length !== 1) { // should not happen, as trigger config: BatchSize=1
206213
failAndLog(context, new Error("Record count must be 1"));
207214
return;
208215
}
@@ -212,7 +219,7 @@ var apis = {
212219
return;
213220
}
214221

215-
var record = flatten(event.Records[0].dynamodb.NewImage);
222+
var record = unwrapDynamoDBTypedObject(event.Records[0].dynamodb.NewImage);
216223

217224
sns.publish({
218225
Message: record.subject + "\n\n" + record.details,

0 commit comments

Comments
 (0)