This repository was archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (63 loc) · 1.83 KB
/
index.js
File metadata and controls
72 lines (63 loc) · 1.83 KB
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
const axios = require('axios');
const timerModel = require('./timer')
const mongoose = require('mongoose');
const mongoURI = process.env.MONGO_URI ;
mongoose.connect(mongoURI, {
auth: {
user: process.env.MONGO_USER ,
password: process.env.MONGO_PASS
},
useNewUrlParser: true,
useUnifiedTopology: true});
const callAndSave = async(options) => {
let start = process.hrtime()
await axios(options);
let end = process.hrtime(start)
const serviceTime = Math.round((end[0] * 1000) + (end[1] / 1000000))
const date = new Date().toISOString()
saveData({timestamp: date, serviceTime})
}
const saveData = async(data) => {
serviceTimer = new timerModel(data)
serviceTimer.save();
}
const generateRats = (number, options) => {
const p = []
for(let i = 0; i < number; ++i) {
p.push(callAndSave(options))
}
return p
}
const ratQueen = (req, res) => {
const n = req.query.ratsNumber;
let loop = req.query.loop;
let interval = req.query.interval
let options = req.query.options;
if(!loop) {
loop = 1;
}
loop = parseInt(loop)
if(options) {
options = Buffer.from(options, 'base64').toString('ascii');
options = JSON.parse(options)
}
let count = 0;
let intervalVar = setInterval(async() => {
++count;
try {
if(count === loop) {
await Promise.all(generateRats(n,options));
clearInterval(intervalVar);
res.status(200).json({'status': 'ok'})
}else {
Promise.all(generateRats(n, options));
}
}
catch(error) {
console.log("error", error)
clearInterval(intervalVar);
res.status(200).json({'error': error})
}
}, interval);
}
exports.ratQueen = ratQueen