Skip to content

Instantly share code, notes, and snippets.

@frah
Created Apr 17, 2016
Embed
What would you like to do?
'use strict';
console.log('Loading function');
let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
function date_desc(a, b) {
let aa = new Date(a.date);
let bb = new Date(b.date);
if (aa < bb) return -1;
if (aa > bb) return 1;
return 0;
}
exports.handler = (event, context, callback) => {
dynamo.scan({"TableName": "kinesis_sensor_datas"}, (err, data) => {
if (err) {
callback(err,data);
} else {
let watt1 = data.Items.filter((d)=>{return d.type === "watt"});
let temp1 = data.Items.filter((d)=>{return d.type === "temperature"});
let humi1 = data.Items.filter((d)=>{return d.type === "humidity"});
let watt = watt1.sort(date_desc).map((d)=>{return d.value});
let temp = temp1.sort(date_desc).map((d)=>{return d.value});
let humi = humi1.sort(date_desc).map((d)=>{return d.value});
callback(err,{device: data.Items[0].device, watt: watt, temperature: temp, humidity: humi});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment