-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
148 lines (120 loc) · 3.56 KB
/
handler.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
const AWS = require("aws-sdk");
module.exports.status = async (event, context, callback) => {
const sensorId =
event.queryStringParameters && event.queryStringParameters["sensorId"];
let result = undefined;
if (sensorId) {
const dynamoDb = new AWS.DynamoDB.DocumentClient();
const params = {
TableName: process.env.DYNAMODB_TABLE,
ScanIndexForward: false,
Limit: 1,
KeyConditionExpression: "sensorId = :s",
ExpressionAttributeValues: {
":s": sensorId
}
};
result = await dynamoDb.query(params).promise();
}
console.log(`items for ${sensorId}`, result ? result.Items : null);
const latestItem = result && result.Items[0] ? { ...result.Items[0] } : null;
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
},
body: JSON.stringify({
latestItem: latestItem,
config: {
maxAddBatchSize: 1500
}
})
};
callback(null, response);
};
// module.exports.all = async (event, context, callback) => {
// const dynamoDb = new AWS.DynamoDB.DocumentClient();
// const params = {
// TableName: process.env.DYNAMODB_TABLE,
// };
// const result = await dynamoDb.scan(params).promise();
// const response = {
// statusCode: 200,
// headers: {
// 'Access-Control-Allow-Origin': '*',
// 'Access-Control-Allow-Credentials': true,
// },
// body: JSON.stringify({
// items: result.Items,
// }),
// };
// callback(null, response);
// };
const chunkArray = (myArray, chunkSize) => {
var results = [];
while (myArray.length) {
results.push(myArray.splice(0, chunkSize));
}
return results;
};
module.exports.add = async (event, context, callback) => {
console.log("Received event:", JSON.stringify(event, null, 2));
const dynamoDb = new AWS.DynamoDB();
const dataIn = JSON.parse(event.body);
// Write oldest first because if writing fails, we have have continuous data from
// oldest to newest and the status endpoint will return correct results
const itemsSortedOldestFirst = dataIn.items.sort((a, b) =>
a.ts.localeCompare(b.ts)
);
console.log(`Received ${itemsSortedOldestFirst.length} items.`);
const chunkSize = 25; // AWS limit for batchWriteItem
for (const arr of chunkArray(itemsSortedOldestFirst, chunkSize)) {
const items = arr.map(e => ({
PutRequest: {
Item: {
sensorId: { S: dataIn.sensorId },
ts: { S: e.ts },
temperature: { S: e.temperature }
}
}
}));
const params = {
RequestItems: {
[process.env.DYNAMODB_TABLE]: items
}
};
await dynamoDb.batchWriteItem(params).promise();
console.log(`Added ${items.length} items to ${dataIn.sensorId}.`);
}
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
}
};
callback(null, response);
};
module.exports.addOne = async (event, context, callback) => {
console.log("Received event:", JSON.stringify(event, null, 2));
const dynamoDb = new AWS.DynamoDB();
const dataIn = JSON.parse(event.body);
const params = {
Item: {
...dataIn
},
TableName: process.env.DYNAMODB_TABLE
};
await dynamoDb.putItem(params).promise();
console.log("Added item:", params);
const response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true
}
};
callback(null, response);
};