-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
66 lines (61 loc) · 1.97 KB
/
client.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
'use strict';
require('dotenv').config();
if (typeof process.env.PING_REMOTE === 'undefined' || typeof process.env.TARGET_FILE === 'undefined' || typeof process.env.MACHINE_NAME === 'undefined') {
console.warn('PING_REMOTE, TARGET_FILE and MACHINE_NAME must all be environment variables or defined in .env');
process.exit();
}
if (typeof process.env.ODDS_OF_RUNNING !== 'undefined') {
var oddsOfRunning = parseFloat(process.env.ODDS_OF_RUNNING);
if (oddsOfRunning < 0 || oddsOfRunning > 1 || isNaN(oddsOfRunning)) {
console.warn('ODDS_OF_RUNNING must be >=0 and <= 1');
process.exit();
}
var runScore = process.env.ODDS_OF_RUNNING - Math.random();
if (runScore < 0) {
console.log('Did not satisfy required odds of running');
process.exit();
}
}
var async = require('async');
var request = require('request');
var Datastore = require('nedb');
var bandwidthTest = require('./lib/bandwidth');
var pingTest = require('./lib/ping');
async.series({
bandwidth: bandwidthTest,
ping: pingTest
}, function (err, results) {
if (err) console.warn(err);
typeof request;
console.log(results);
var doc = {
machine: process.env.MACHINE_NAME,
date: (new Date()).toISOString(),
data: results
};
if (typeof process.env.LOCAL_HISTORY !== 'undefined') {
var db = new Datastore({ filename: process.env.LOCAL_HISTORY, autoload: true });
db.insert(doc, function (err) {
if (err) console.warn(err);
else console.log('Wrote to local datastore');
});
}
if (typeof process.env.REMOTE_SERVER !== 'undefined') {
request.post(
{ url: process.env.REMOTE_SERVER,
headers: {
'api-key': process.env.REMOTE_API_KEY
},
form: {
machine: doc.machine,
datetime: doc.date,
data: JSON.stringify(doc.data)
}
},
function (err, httpResponse, body) {
if (err) console.warn(err);
if (typeof httpResponse !== 'undefined') console.log(body);
}
);
}
});