-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (115 loc) · 3.45 KB
/
app.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
var fs = require('fs');
var http = require('http');
var xml2js = require('xml2js');
var monk = require('monk');
var moment = require('moment');
var cron = require('cron');
var QMON_CRON_SCHEDULE = process.env.QMON_CRON_SCHEDULE;
var QMON_CRON_TIMEZONE = process.env.QMON_CRON_TIMEZONE;
var QMON_USSCONTENT_WS_HOST = process.env.QMON_USSCONTENT_WS_HOST;
var QMON_USSCONTENT_WS_PATH = process.env.QMON_USSCONTENT_WS_PATH;
var QMON_USSCONTENT_WS_USERAGENT = process.env.QMON_USSCONTENT_WS_USERAGENT;
var QMON_DB_URL = process.env.QMON_DB_URL;
var QMON_DB_COLL_QUEUETIME = process.env.QMON_DB_COLL_QUEUETIME
if (!QMON_CRON_SCHEDULE && !QMON_CRON_TIMEZONE && !QMON_USSCONTENT_WS_HOST &&
!QMON_USSCONTENT_WS_HOST && !QMON_USSCONTENT_WS_PATH && !QMON_DB_URL &&
!QMON_DB_COLL_QUEUETIME) {
throw Error("One or more environment variables missing. Please check your configuration.");
}
var db = monk(QMON_DB_URL);
var CronJob = cron.CronJob;
var job = new CronJob({
cronTime: QMON_CRON_SCHEDULE,
onTick: monitorQueue,
start: false,
timeZone: QMON_CRON_TIMEZONE
});
job.start();
function monitorQueue() {
console.log(new Date() + ' job triggered');
getUSSContent(function handleResponse(err, response) {
if (err) {
console.log(err);
return;
}
saveXml(response);
parseXml(response, saveData);
});
}
function getUSSContent(cb) {
return http.get({
host: QMON_USSCONTENT_WS_HOST,
path: QMON_USSCONTENT_WS_PATH,
headers: {
'User-Agent': QMON_USSCONTENT_WS_USERAGENT,
}
}, function(response) {
var completedResponse = '';
response.on('data', function(d) {
completedResponse += d;
});
response.on('error', function(err) {
cb(err);
});
response.on('end', function() {
cb(null, completedResponse);
});
})
.on('error', function(err) {
cb(err);
})
}
function saveXml(data) {
fs.writeFile('qmon-' + moment().format('YYYYMMDDHHmmSSz') + '.xml', data, function logResponse(err) {
if (err) {
console.log('error writing xml: ' + err);
return;
}
console.log('xml saved');
})
}
function parseXml(xmlString, cb) {
var results = [];
xml2js.parseString(xmlString, {explicitArray: false}, function buildJsonData(err, result) {
if (err) {
cb(err);
return;
}
if (!result || !result.ResponseOfUSS ||
!result.ResponseOfUSS.Result || !result.ResponseOfUSS.Result.USSZoneList ||
!result.ResponseOfUSS.Result.USSZoneList.USSZone) {
cb(new Error('Invalid XML data'));
return;
}
var zones = result.ResponseOfUSS.Result.USSZoneList.USSZone;
if (!Array.isArray(zones)) zones = [zones];
for (x in zones) {
var contents = zones[x].Content.USSContent;
if (!Array.isArray(contents)) {
contents = [contents];
}
for (y in contents) {
var ride = {};
ride['date'] = new Date();
ride['contentId'] = contents[y].USSContentID
ride['name'] = contents[y].Name
ride['queueTime'] = Math.floor(contents[y].QueueTime)
ride['zoneId'] = contents[y].ZoneID
ride['zoneName'] = contents[y].ZoneName
console.log(JSON.stringify(ride));
results.push(ride);
}
}
cb(null, results);
});
}
function saveData(err, data) {
if (err) {
console.log(err);
return;
}
db.get(QMON_DB_COLL_QUEUETIME).insert(data, function logResponse(err, doc) {
if (err) console.log('save error. ' + err);
else console.log('save success.')
})
}