-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.55 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
const { fetch } = require('./fetch');
const Influxdb = require('influxdb-v2');
const config = require('./config.json');
const Profile = require('./Profile');
require('dotenv').config({ path: `.env.${process.env.NODE_ENV}` });
const influx = new Influxdb({
host: process.env.DB_HOST,
token: process.env.DB_TOKEN,
port: process.env.DB_PORT || 9999,
protocol: 'http',
});
const ORG = config.org;
const BUCKET = config.bucket;
let profileArray = [];
//map profiles from config file to array of profiles;
config.profiles.map((profile) => {
const name = profile.name;
profile.systems.map((system) => {
const profileObject = new Profile({ name, system });
profileArray.push(profileObject);
});
});
//wrap fetch in function so that Promise.all can be used.
profileArray.map((profile) => {
profile.fetch = () => fetch(profile);
});
//Promise.all triggers fetch operation and waits until they all resolve. Fetch always resolves, never rejects.
Promise.all(profileArray.map((profile) => profile.fetch())).then((res) => {
const STATS = [];
//if stats isn't null, then add to constant STATS
profileArray.map((profile) => {
if (profile.stats) {
STATS.push(...profile.stats);
}
});
console.log('Writing to database...');
influx
.write(
{
org: ORG,
bucket: BUCKET,
},
STATS
)
.catch((error) => {
console.error(error.message);
console.log('Write to database FAILED!');
process.exit(1);
});
});
process.on('exit', (code) => {
return console.log(`Exiting with code ${code}.`);
});