forked from pmuens/serverless-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readings-plot.js
143 lines (129 loc) · 2.83 KB
/
readings-plot.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
'use strict';
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = (event, callback) => {
console.log("Event: " + JSON.stringify(event));
var sensor_id;
var published_at;
var limit;
var has_parameters = false;
var has_sensor_id = false;
var has_published_at = false;
var has_limit = false;
var table = "readings";
var params;
if (event.queryStringParameters !== null &&
event.queryStringParameters !== undefined)
{
has_parameters = true;
if (event.queryStringParameters.sensor_id !== undefined &&
event.queryStringParameters.sensor_id !== null &&
event.queryStringParameters.sensor_id !== "")
{
has_sensor_id = true;
sensor_id = event.queryStringParameters.sensor_id;
}
if (event.queryStringParameters.published_at !== undefined &&
event.queryStringParameters.published_at !== null &&
event.queryStringParameters.published_at !== "")
{
has_published_at = true;
published_at = parseInt(event.queryStringParameters.published_at);
}
if (event.queryStringParameters.limit !== undefined &&
event.queryStringParameters.limit !== null &&
event.queryStringParameters.limit !== "")
{
has_limit = true;
limit = parseInt(event.queryStringParameters.limit);
}
}
// Start set params
if(has_parameters)
{
// has some params
if(has_sensor_id)
{
if(has_published_at)
{
// Take as LastEvaluatedKey
params = {
TableName: table,
ConsistentRead: true,
KeyConditionExpression:"sensor_id = :sensor_id",
ExpressionAttributeValues: {
":sensor_id": sensor_id
},
ExclusiveStartKey: {
"sensor_id": sensor_id,
"published_at": published_at
}
};
}
else
{
// Only has sensor_id, to be used as retrive latest x readings
// read backwards
params = {
TableName: table,
ConsistentRead: true,
KeyConditionExpression:"sensor_id = :sensor_id",
ExpressionAttributeValues: {
":sensor_id": sensor_id
},
ScanIndexForward: false
};
}
}
else
{
// no sensor_id
params = {
TableName: table,
ConsistentRead: true
};
}
}
else
{
// no params
params = {
TableName: table,
ConsistentRead: true
};
}// end of all if-else block
// limit can be combined into any params
if(has_limit)
{
params.Limit = limit;
}
console.log("Params: " + JSON.stringify(params));
// determin call scan or query
if(!has_sensor_id)
{
return dynamoDb.scan(params, (error, data) => {
if (error) {
callback(error);
}
callback(error, data);
});
}
else
{
return dynamoDb.query(params, (error, data) => {
if (error) {
callback(error);
}
else
{
if(!has_published_at)
{
data.Items.sort(function(a, b){
return parseInt(a.published_at) - parseInt(b.published_at);
});
}
}
callback(error, data);
});
}
};