-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
387 lines (327 loc) · 10.3 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const express = require("express");
const moment = require("moment");
const request = require("request");
const fs = require("fs");
const Q = require("q");
const cors = require("cors");
const bunyan = require("bunyan");
const findRemoveSync = require("find-remove");
const prometheus = require("express-prom-bundle");
const metrics = prometheus({ includePath: true, includeMethod: true, promClient: { collectDefaultMetrics: {} } });
const app = express();
const port = process.env.PORT || 7000;
const baseDir = "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.pl";
const logLevel = process.env.LOG_LEVEL || "info";
// create our logger
const log = bunyan.createLogger({
name: "windserver",
level: logLevel
});
const maxAge = 60 * 60 * 24 * 14; // 14 days in seconds
// metrics config
const retrieveCounter = new metrics.promClient.Counter({
name: "windserver_retrievals",
help: "counts whenever we go retrieve data"
});
const retrieveErrorCounter = new metrics.promClient.Counter({
name: "windserver_retrieval_errors",
help: "counts whenever we go retrieve data"
});
const fileRemovalCounter = new metrics.promClient.Counter({
name: "windserver_removals",
help: "counts when we remove an old file"
});
const filesGauge = new metrics.promClient.Gauge({
name: "windserver_data_files",
help: "gauge measuring number of files in data directory"
});
// add metrics middleware
app.use(metrics);
// cors config
var whitelist = process.env.WHITELIST ?
process.env.WHITELIST.split(/\s*,\s*/) : [];
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Forbidden by CORS'));
}
}
};
//app.use(cors(corsOptions));
app.use(cors());
app.listen(port, function () {
log.info({ port: port, corsWhitelist: whitelist }, "starting server");
});
app.get("/", function (req, res) {
res.send("hello wind-js-server.. go to /latest for wind data..");
});
app.get("/pulse", function (req, res) {
function findRecent(targetMoment) {
var stamp = moment(targetMoment).format("YYYYMMDD") +
roundHours(moment(targetMoment).hour(), 6);
var fileName = __dirname + "/json-data/" + stamp + ".json";
res.set("Content-Type", "text/plain");
if (checkPath(fileName, false)) {
res.send("ok");
} else {
if (targetMoment.isBefore(moment().subtract(30, "days"))) {
res.status(500);
res.send("no data");
} else {
findRecent(moment(targetMoment).subtract(6, "hours"));
}
}
}
findRecent(moment().utc());
});
app.get("/latest", function (req, res, next) {
/**
* Find and return the latest available 6 hourly pre-parsed JSON data
*
* @param targetMoment {Object} UTC moment
*/
function sendLatest(targetMoment) {
var stamp = moment(targetMoment).format("YYYYMMDD") +
roundHours(moment(targetMoment).hour(), 6);
var fileName = __dirname + "/json-data/" + stamp + ".json";
log.debug({ fileName: fileName }, "attempt to send file");
res.setHeader("Content-Type", "application/json");
res.sendFile(fileName, {
maxAge: 900000,
immutable: true
}, function (err) {
if (err) {
if (targetMoment.isBefore(moment().subtract(30, "days"))) {
next(err);
} else {
log.debug({ err: err, stamp: stamp },
"does not exist yet, trying previous interval");
sendLatest(moment(targetMoment).subtract(6, "hours"));
}
}
});
}
sendLatest(moment().utc());
});
app.get("/nearest", function (req, res, next) {
var time = req.query.timeIso;
var limit = req.query.searchLimit;
var searchForwards = false;
/**
* Find and return the nearest available 6 hourly pre-parsed JSON data If
* limit provided, searches backwards to limit, then forwards to limit before
* failing.
*
* @param targetMoment {Object} UTC moment
*/
function sendNearestTo(targetMoment) {
if (limit && Math.abs(moment.utc(time).diff(targetMoment, "days")) >= limit) {
if (!searchForwards) {
searchForwards = true;
sendNearestTo(moment(targetMoment).add(limit, "days"));
return;
} else {
return next(new Error("No data within searchLimit"));
}
}
var stamp = moment(targetMoment).format("YYYYMMDD") +
roundHours(moment(targetMoment).hour(), 6);
var fileName = __dirname + "/json-data/" + stamp + ".json";
res.setHeader("Content-Type", "application/json");
res.sendFile(fileName, {
maxAge: 604800000,
immutable: true
}, function (err) {
if (err) {
var nextTarget = searchForwards ? moment(targetMoment).add(6, "hours") :
moment(targetMoment).subtract(6, "hours");
sendNearestTo(nextTarget);
}
});
}
if (time && moment(time).isValid()) {
sendNearestTo(moment.utc(time));
} else {
return next(new Error("Invalid params, expecting: timeIso=ISO_TIME_STRING"));
}
});
/**
*
* Ping for new data every 15 mins
*
*/
setInterval(function () {
run(moment.utc());
}, 900000);
/**
*
* @param targetMoment {Object} moment to check for new data
*/
function run(targetMoment) {
removeOldFiles();
countDataFiles();
getGribData(targetMoment).then(function (response) {
if (response.stamp) {
convertGribToJson(response.stamp, response.targetMoment);
}
});
}
/**
* Uses find-remove module to delete files older than our maxAge seconds.
*/
function removeOldFiles() {
log.debug({ maxAge: maxAge }, "removing old files");
// delete json files older than 2 weeks
var result = findRemoveSync("json-data/", { age: { seconds: maxAge } });
var files = Object.keys(result);
if (files.length > 0) {
fileRemovalCounter.inc(files.length);
log.debug({ numFiles: files.length, age: maxAge }, "deleting old files");
}
}
/**
* Instrumentation that counts the number of data files currently in the
* json-data directory, and sets a gauge accordingly.
*/
function countDataFiles() {
log.debug("counting current data files");
fs.readdir("./json-data", (err, files) => {
if (err) {
log.error(err);
};
filesGauge.set(files.length);
});
}
/**
*
* Finds and returns the latest 6 hourly GRIB2 data from NOAAA
*
* @returns {*|promise}
*/
function getGribData(targetMoment) {
var deferred = Q.defer();
function runQuery(targetMoment) {
// only go 2 weeks deep
if (moment.utc().diff(targetMoment, "days") > 30) {
log.info("reached limit, harvest complete or large gap in data");
return;
}
var localDir = moment(targetMoment).format("YYYYMMDD") + roundHours(moment(targetMoment).hour(), 6);
var requestDir = moment(targetMoment).format("YYYYMMDD") + "/" + roundHours(moment(targetMoment).hour(), 6);
var filePath = "gfs.t" + roundHours(moment(targetMoment).hour(), 6) + "z.pgrb2.1p00.f000";
request.get({
url: baseDir,
qs: {
file: filePath,
lev_10_m_above_ground: "on",
lev_surface: "on",
var_TMP: "on",
var_UGRD: "on",
var_VGRD: "on",
leftlon: 0,
rightlon: 360,
toplat: 90,
bottomlat: -90,
dir: "/gfs." + requestDir
}
}).on("error", function (err) {
retrieveErrorCounter.inc();
log.error({ err: err, stamp: localDir }, "unable to retrieve data");
runQuery(moment(targetMoment).subtract(6, "hours"));
}).on("response", function (response) {
log.debug({ status: response.statusCode, stamp: localDir }, "data retrieved");
if (response.statusCode !== 200) {
retrieveErrorCounter.inc();
runQuery(moment(targetMoment).subtract(6, "hours"));
} else {
retrieveCounter.inc();
// don"t rewrite stamps
if (!checkPath("json-data/" + localDir + ".json", false)) {
log.debug({ stamp: localDir }, "piping data");
// mk sure we"ve got somewhere to put output
checkPath("grib-data", true);
// pipe the file, resolve the valid time stamp
var file = fs.createWriteStream("grib-data/" + localDir + ".f000");
response.pipe(file);
file.on("finish", function () {
file.close();
deferred.resolve({ stamp: localDir, targetMoment: targetMoment });
});
} else {
log.debug({ stamp: localDir }, "end reached, not looking further");
deferred.resolve({ stamp: false, targetMoment: false });
}
}
});
}
runQuery(targetMoment);
return deferred.promise;
}
function convertGribToJson(stamp, targetMoment) {
// mk sure we"ve got somewhere to put output
checkPath("json-data", true);
var exec = require("child_process").exec;
exec("converter/bin/grib2json --data --output json-data/" +
stamp + ".json --names --compact grib-data/" + stamp + ".f000",
{ maxBuffer: 500 * 1024 },
function (error) {
if (error) {
log.error({ err: error });
} else {
log.debug({ stamp: stamp }, "converted file");
// don"t keep raw grib data
exec("rm grib-data/*");
// if we don"t have older stamp, try and harvest one
var prevMoment = moment(targetMoment).subtract(6, "hours");
var prevStamp = prevMoment.format("YYYYMMDD") + roundHours(prevMoment.hour(), 6);
if (!checkPath("json-data/" + prevStamp + ".json", false)) {
log.debug({ stamp: stamp }, "fetching data");
run(prevMoment);
} else {
log.debug({ stamp: stamp }, "end of harvest");
}
}
});
}
/**
*
* Round hours to expected interval, e.g. we"re currently using 6 hourly interval
* i.e. 00 || 06 || 12 || 18
*
* @param hours
* @param interval
* @returns {String}
*/
function roundHours(hours, interval) {
if (interval > 0) {
var result = (Math.floor(hours / interval) * interval);
return result < 10 ? "0" + result.toString() : result;
}
}
/**
* Sync check if path or file exists
*
* @param path {string}
* @param mkdir {boolean} create dir if doesn"t exist
* @returns {boolean}
*/
function checkPath(path, mkdir) {
try {
fs.statSync(path);
return true;
} catch (e) {
if (mkdir) {
fs.mkdirSync(path);
}
return false;
}
}
// add interrupt handler to try and exit cleanly on this signal
process.on("SIGINT", function () {
log.info("shutting down server");
process.exit();
});
// init harvest
run(moment.utc());