-
Notifications
You must be signed in to change notification settings - Fork 4
/
lambda-flatten-json-kinesis-records.js
36 lines (31 loc) · 1.39 KB
/
lambda-flatten-json-kinesis-records.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
/* Process the list of records and transform them */
const output = event.records.map((record) => {
const entry = (new Buffer(record.data, 'base64')).toString('utf8');
let ttn_json = JSON.parse(entry);
let ttnDeviceTime = ttn_json.metadata.time
let year = new Date(ttnDeviceTime).getUTCFullYear();
let month = new Date(ttnDeviceTime).getUTCMonth()+1;
let day = new Date(ttnDeviceTime).getUTCDate();
let hours = new Date(ttnDeviceTime).getUTCHours();
let minutes = new Date(ttnDeviceTime).getUTCMinutes();
let seconds = new Date(ttnDeviceTime).getUTCSeconds();
let date = year + "-" + month + "-" + day + " " + hours + ":" + minutes +":" + seconds
let flat_json = {
time: date,
battery: ttn_json.payload_fields.battery,
light: ttn_json.payload_fields.light,
temperature: ttn_json.payload_fields.temperature
}
const payload = (new Buffer(JSON.stringify(flat_json), 'utf8')).toString('base64');
return {
recordId: record.recordId,
result: 'Ok',
data: payload,
};
});
console.log(`Processing completed. Successful records ${output.length}.`);
callback(null, { records: output });
};