-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (116 loc) · 3.11 KB
/
index.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
'use strict';
const util = require('util');
const pg = require('pg');
const ms = require('ms');
const archive = require('./lib/archive.js');
const report = require('./lib/report.js');
const STATIONS = [
`HWPS1`, `HWPS2`, `HWPS3`, `HWPS4`,
`HWPS5`, `HWPS6`, `HWPS7`, `HWPS8`
];
//--------------------------------------------------------------------------
// aws lambda handler
exports.test = (event, context, callback) => {
function getData(callback) {
const client = new pg.Client();
console.log('==> test');
client.on('drain', () => {
client.end();
});
pg.on('error', function (err) {
console.log('==> database error!', err);
});
client.connect();
client.query(`SELECT date, duid, power FROM dispatch WHERE date BETWEEN '2017-03-25T00:00+10:00' AND '2017-03-25T00:10+10:00'`, (err, result) => {
if (!err) {
callback(result.rows);
} else {
console.log(`==> error: ` + err);
}
});
}
getData((data) => {
console.log('==> done');
callback(null, data);
});
};
exports.archiver = (event, context, callback) => {
const period = process.env.PERIOD || `15m`;
console.log(`==> archiver fetching records for past ${period}…`);
archive.importData(STATIONS, period, (err, records) => {
if (!err) {
console.log(`==> archived ${records.length} entries.`);
callback(err, records);
} else {
console.log(`==> bailing: ${err}`);
callback(err);
}
});
};
let reportCache;
exports.reporter= (event, context, callback) => {
console.log(`==> reporter…`);
let cacheAge;
if (typeof reportCache !== `undefined`) {
cacheAge = new Date() - reportCache.time;
} else {
cacheAge = Infinity;
}
if (cacheAge < (15 * 1000)) {
console.log(`==> cache hit (${ms(cacheAge)} old)`);
callback(null, reportCache.data);
} else {
report.buildReport((err, data) => {
if (!err) {
reportCache = {
time: new Date(),
data: data
};
console.log(`==> built report with ${data.length} records`);
callback(null, data);
} else {
console.log(`==> bailing: ${err}`);
callback(err);
}
});
}
};
//--------------------------------------------------------------------------
// choose mode from environment variable when running in local env
function localTest() {
let handler;
function runHandler(handler, callback) {
const start = process.hrtime();
handler(null, null, (err, data) => {
console.log(`==> output: \n${util.inspect(data)}`);
function getElapsed(start) {
const elapsed = process.hrtime(start);
const val = (elapsed[0] * 1000) + elapsed[1]/1000000
return Math.round(val * 100)/100;
}
console.log("\nelapsed: %d ms", getElapsed(start));
callback();
});
}
function runChained(index, handler) {
if (index > 0) {
runHandler(handler, () => {
--index;
console.log(`--> ${index} execution(s) remaining`);
setTimeout(() => {
runChained(index, handler);
}, 5000);
});
}
}
const handlerName = process.env.FAKE;
if (handlerName) {
handler = exports[handlerName];
if (typeof handler === `undefined`) {
console.log(`no FAKE env set.`);
} else {
runChained(12, handler);
}
}
}
localTest();