-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
268 lines (255 loc) · 8.4 KB
/
script.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const path = require('path');
require('dotenv').config({ path: path.dirname(process.argv[1]) + '/.env' });
const oracledb = require('oracledb');
const axios = require('axios');
const qs = require('qs');
const datetime = require('node-datetime');
const { program } = require('commander');
const Datastore = require('nedb');
const logsDb = new Datastore({ filename: path.dirname(process.argv[1]) + '/logs.db', autoload: true, timestampData: true });
logsDb.ensureIndex({ fieldName: "createdAt" });
const yesterday = yesterdayDateString();
const trafsysUrl = 'https://portal.trafnet.com/rest/';
/**
* A log object that collects info related to a run of this program.
* @typedef {Object} RunInfo
* @property {string} AccessToken - The access token used to get Trafsys data. Will be reused until it expires.
* @property {Date} AccessTokenExpiresAt - The time at which the access token expires.
* @property {string} FromDate - The From date used for this run, in YYYY-MM-DD format.
* @property {string} ToDate - The To date used for this run, in YYYY-MM-DD format.
* @property {number?} Records - The number of records written to the Oracle db.
*/
/**
* Generates a RunInfo object for the current run.
* @returns {Promise<RunInfo>}
*/
async function getRunInfo() {
// helper function to call nedb cursor methods using async/await syntax
let execAsync = cursor => new Promise(
(resolve, reject) => cursor.exec((err, result) => err ? reject(err) : resolve(result))
);
// use sort and limit to get most recently saved log object
let previousRun = await execAsync(logsDb.findOne({}).sort({ createdAt: -1 }).limit(1));
let currentRun = {};
if (previousRun) {
let expiresAt = datetime.create(previousRun.AccessTokenExpiresAt);
let nowish = datetime.create();
// Offset by 5 minutes to give some wiggle room (technical term)
nowish.offsetInHours(-1/12);
// .getTime() converts the object to a timestamp for comparison
if (expiresAt.getTime() > nowish.getTime()) {
currentRun.AccessToken = previousRun.AccessToken;
currentRun.AccessTokenExpiresAt = previousRun.AccessTokenExpiresAt;
}
}
if (!currentRun.AccessToken) {
let tokenData = await getAccessToken();
currentRun.AccessToken = tokenData.access_token;
currentRun.AccessTokenExpiresAt = new Date(tokenData[".expires"]);
}
program
.option('-f, --from <date>', 'From Date (YYYY-MM-DD)', previousRun?.ToDate || yesterday)
.option('-t, --to <date>', 'To Date (YYYY-MM-DD)', yesterday);
program.parse();
let opts = program.opts();
currentRun.FromDate = opts.from;
currentRun.ToDate = opts.to;
return currentRun;
}
/**
* Gets a fresh access token from TrafSys.
*
* @returns {Promise<{access_token: string, ".expires": string}>}
*/
async function getAccessToken() {
let tokenResponse = await axios.post(trafsysUrl + 'token', qs.stringify({
username: process.env.TRAFSYS_USER,
password: process.env.TRAFSYS_PASSWORD,
grant_type: 'password'
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
return tokenResponse.data;
}
/**
* Checks if all the required environment variables are present.
* If they are not, display an error and quit.
*/
function checkEnv() {
let keys = [
'ORACLE_USER',
'ORACLE_PASSWORD',
'ORACLE_CONNECTION_STRING',
'TRAFSYS_USER',
'TRAFSYS_PASSWORD'
];
let missingKeys = keys.filter(key => !(key in process.env));
if (missingKeys.length === 0) return;
console.error('Missing required environment variables: ' + missingKeys.join(', '));
process.exit();
}
/**
* Creates the ULS_TRAFSYS_DATA table if it does not exist.
* @param {oracledb.Connection} connection - The database connection.
*/
async function ensureTableExists(connection) {
let result = await connection.execute(
`select table_name
from user_tables
where table_name = 'ULS_TRAFSYS_DATA'`
);
if (result.rows.length === 0) {
await connection.execute(
`create table ULS_TRAFSYS_DATA
(
SiteCode varchar2(100),
Location varchar2(100),
IsInternal number(1),
PeriodEnding date,
Ins number,
Outs number,
primary key(SiteCode, Location, PeriodEnding)
)`
);
}
}
/**
* A TrafSys data record.
* @typedef {Object} DataRecord
* @property {string} SiteCode - The alphanumeric code that identifies the site within the organization.
* @property {string} Location - The name of the location where the sensors are counting.
* @property {number} IsInternal - Indicates (using 0 or 1) whether this is an internal location.
* @property {string} PeriodEnding - The end of the hour-long time period this record corresponds to.
* @property {number} Ins - The in counts for that time period and location.
* @property {number} Outs - The out counts for that time period and location.
*/
/**
* Retrieves TrafSys data from the REST API.
* @param {RunInfo} runInfo - The run information for the current run.
* @returns {Promise<DataRecord[]>} Data pulled from the api and given a RecordId.
*/
async function getTrafsysData(runInfo) {
let dataResponse = await axios.get(trafsysUrl + 'api/traffic', {
params: {
SiteCode: '',
IncludeInternalLocations: true,
DataSummedByDay: false,
DateFrom: runInfo.FromDate,
DateTo: runInfo.ToDate,
},
headers: {
'Authorization': 'Bearer ' + runInfo.AccessToken
}
});
let data = dataResponse.data;
// check that the response actually contains data
if (!data?.[Symbol.iterator])
{
throw new Error("Bad response from Trafsys: " + toString(dataResponse));
}
for (let record of data) {
// Oracle has no boolean datatype for columns, so cast it to a number
record.IsInternal = +record.IsInternal;
}
runInfo.Records = data.length;
return data;
}
/**
* @returns {string} Yesterday's date formatted as a YYYY-MM-DD string.
*/
function yesterdayDateString() {
var date = datetime.create();
date.offsetInDays(-1);
return date.format('Y-m-d');
}
/**
* Inserts the TrafSys data into the database.
* @param {oracledb.Connection} connection - The database connection.
* @param {DataRecord[]} data
*/
async function insertData(connection, data) {
if (data.length === 0) return;
let result = await connection.executeMany(
`begin
insert into ULS_TRAFSYS_DATA values
(
:SiteCode,
:Location,
:IsInternal,
TO_DATE(:PeriodEnding, 'YYYY-MM-DD"T"HH24:MI:SS'),
:Ins,
:Outs
);
exception when dup_val_on_index then
update ULS_TRAFSYS_DATA
set Ins = :Ins, Outs = :Outs
where SiteCode = :SiteCode
and Location = :Location
and PeriodEnding = TO_DATE(:PeriodEnding, 'YYYY-MM-DD"T"HH24:MI:SS');
end;`,
data,
{
autoCommit: true,
bindDefs: {
SiteCode: { type: oracledb.STRING, maxSize: 100 },
Location: { type: oracledb.STRING, maxSize: 100 },
IsInternal: { type: oracledb.NUMBER },
PeriodEnding: { type: oracledb.STRING, maxSize: 100 },
Ins: { type: oracledb.NUMBER },
Outs: { type: oracledb.NUMBER }
}
}
);
}
/**
* Returns a promise that resolves after one second.
* @returns {Promise<void>}
*/
function waitASecond() {
return new Promise(resolve => setTimeout(resolve, 1000));
}
/**
* Run the program, pulling data from TrafSys and inserting it into the database.
*/
async function run() {
checkEnv();
let connection;
try {
connection = await oracledb.getConnection({
user: process.env.ORACLE_USER,
password: process.env.ORACLE_PASSWORD,
connectString: process.env.ORACLE_CONNECTION_STRING
});
await ensureTableExists(connection);
let runInfo = await getRunInfo();
let trafsysData;
try {
trafsysData = await getTrafsysData(runInfo);
}
catch (e) {
if (e.isAxiosError && e.response.status == 401) {
// wait a second to prevent "429 Too Many Requests"
await waitASecond();
let tokenData = await getAccessToken();
runInfo.AccessToken = tokenData.access_token;
runInfo.AccessTokenExpiresAt = new Date(tokenData[".expires"]);
trafsysData = await getTrafsysData(runInfo);
} else {
throw e;
}
}
await insertData(connection, trafsysData);
logsDb.insert(runInfo);
}
catch (e) {
console.error(e.toString());
}
finally {
if (connection) {
await connection.close();
}
}
}
run();