-
Notifications
You must be signed in to change notification settings - Fork 0
/
alarm-function.js
114 lines (110 loc) · 3.4 KB
/
alarm-function.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
const fli = require('fli-webtask');
const wt = require('webtask-tools');
const bodyParser = require('body-parser');
const moment = require('moment');
const express = fli.npm.express;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const responseHandler = fli.lib.responseHandler;
const app = express();
const router = express.Router();
const validateMiddleware = (req, res, next) => {
if(req.webtaskContext.secrets.token !== req.query.token) {
const errMsgToken = 'No token.';
responseHandler(errMsgToken, res);
return next(errMsgToken);
}
const market = _.get(req, 'params.market', '').toUpperCase();
const db = req.webtaskContext.secrets[market] || market.toLowerCase();
if(!db) {
const errMsgMarketDB = 'No db name provided for market.';
responseHandler(errMsgMarketDB, res);
return next(errMsgMarketDB);
}
req.db = db;
return next();
};
const triggerAlarm = (req) => (alarmBody) => {
return loader({
method: 'post',
url: req.webtaskContext.secrets.gmailFunction,
qs: {token: req.webtaskContext.secrets.token},
json: {
to: req.webtaskContext.secrets.admin,
subject: 'Alarm!',
body: alarmBody
}
}, () => {});
};
router
.all('/test', function (req, res) {
console.log(`-- alarm test flow`);
const yesterday = moment().subtract(1, 'days').format('YYYY-MM-DD');
as.waterfall([
// 1 - get db update for yesterday
// 2 - send email if alarm is not ok
(next) => loader({
method: 'get',
url: `${req.webtaskContext.secrets.storeFunction}/${req.db}/query/updated/${yesterday}`,
qs: {token: req.webtaskContext.secrets.token}
}, (err, data) => next(null, data || [])),
(data, next) => {
if(data && !data.length) {
const alarmBody = `Alarm: No deals data. Date: ${yesterday}. Market DB: ${req.db}.`;
triggerAlarm(req)(alarmBody);
return next(null, alarmBody);
}
return next(null, 'ok');
}
],
(err, status) => {
responseHandler(err, res, {status: status});
});
})
.all('/trigger', function (req, res) {
console.log(`-- alarm trigger flow`);
as.waterfall([
(next) => {
const alarmBody = _.get(req, 'query.msg') || _.get(req, 'body.msg');
if(!!alarmBody) {
triggerAlarm(req)(alarmBody);
return next(null, alarmBody);
}
return next(null, 'No alarm msg.');
}
],
(err, status) => {
responseHandler(err, res, {status: status});
});
})
.all('/record', function (req, res) {
console.log(`-- alarm record flow`);
const alarmName = _.get(req, 'query.name') || _.get(req, 'body.name');
let context = req.webtaskContext;
as.waterfall([
(next) => {
if(!!alarmName) {
return context.storage.get(next);
}
return next('No alarm name.');
},
(storage, next) => {
storage[req.db] = storage[req.db] || {};
storage[req.db][alarmName] = storage[req.db][alarmName] || 0;
let alarmValue = storage[req.db][alarmName] + 1;
storage[req.db][alarmName] = alarmValue;
if(alarmValue % 33 === 0) {
triggerAlarm(req)(`Take a look on alarm ${alarmName} in ${req.db}`);
}
context.storage.set(storage, () => next(null, alarmValue));
}
],
(err, status) => {
responseHandler(err, res, {status: err || status});
});
});
app
.use(bodyParser.json())
.use('/:market', validateMiddleware, router);
module.exports = wt.fromExpress(app);