Commit 3b9136b1 by Amir Aharon

change to read multiple files in directory and respond by prefix

parent 945a15fc
......@@ -11,7 +11,8 @@
"program": "${workspaceFolder}/app.js",
"args": [
"-p","8053",
"-f","/home/amir/git/ipgallery/transportation/doc/chicago-polling.json"
//"-f","/home/amir/git/ipgallery/transportation/doc/chicago-polling.json"
"-f","/opt/mcx/config/polling-simulator"
]
}
]
......
......@@ -6,9 +6,11 @@ const request = require('request');
const moment = require('moment');
var myArgs = require('optimist').argv,
help = 'node app.js -p port -f file-path [-d delay-between-samples --postUrl http url to post instead of websocket]';
help = 'node app.js -p port -f file-path/directory [-d delay-between-samples --postUrl http url to post instead of websocket]';
var config = {};
var jsonFilesMap = [];
//const twoDaysAgoMs = 1000 * 3600 * 47;
/**
* getting configuration either from command args or from env
......@@ -48,14 +50,17 @@ app.listen(config.port)
console.log('Server started! At http://localhost:' + config.port);
app.get("/*", function(req,res){
console.log("Got Polling request on: " + moment().format());
jsonResp = getCurrentPollingResp();
console.log("Got Polling request for: " + req.path + "on: " + moment().format());
jsonResp = getCurrentPollingResp(req.path);
res.send(jsonResp);
});
function getCurrentPollingResp(){
if (Array.isArray(jsonFile)){
return jsonFile.map(entity => getCurrentEntity(entity));
function getCurrentPollingResp(path){
const jsonFile = jsonFilesMap[path];
if (jsonFile){
if (Array.isArray(jsonFile)){
return jsonFile.map(entity => getCurrentEntity(entity));
}
}
return [];
}
......@@ -128,9 +133,51 @@ function handleChildren(parent) {
}
}
fs.readFile(config.file, (err, data) => {
function handleReadFile(filePath,filePrefix){
fs.readFile(filePath, (err, data) => {
if (err) throw err;
jsonFilesMap['/' + filePrefix] = JSON.parse(data);
//jsonFile = JSON.parse(data);
console.log('finished reading: ' + filePath);
});
}
function getFilePrefix(fileName){
let filePrefixOffset = fileName.lastIndexOf('.');
let fileStartPrefix = fileName.lastIndexOf('/');
if (fileStartPrefix != -1){
fileStartPrefix++;
} else {
fileStartPrefix = 0;
}
let filePrefix = filePrefixOffset != -1 ? fileName.slice(fileStartPrefix,filePrefixOffset) : fileName;
return filePrefix;
}
function handleReadDirectory(dirPath){
fs.readdir(dirPath, function(err, items) {
console.log(items);
/**
* getting the file prefix name
* and reading the file
*/
items.forEach(item => {
let filePrefix = getFilePrefix(item);
handleReadFile(dirPath + '/' + item, filePrefix);
});
});
}
fs.stat(config.file, (err, stats) => {
if (err) throw err;
jsonFile = JSON.parse(data);
console.log('finished reading: ' + config.file);
if (stats.isFile()){
let filePrefix = getFilePrefix(config.file);
handleReadFile(config.file,filePrefix);
} else if (stats.isDirectory()){
handleReadDirectory(config.file);
}
});
......@@ -5,6 +5,7 @@ services:
file: "/tmp/polling.json"
image: municipalitybank.com:5050/mcx/devops/polling-simulator
ports:
- "80:8053"
- "8085:8053"
volumes:
- "/home/amir/git/ipgallery/transportation/doc/chicago-polling.json:/tmp/polling.json"
\ No newline at end of file
# - "/home/amir/git/ipgallery/transportation/doc/chicago-polling.json:/tmp/polling.json"
- "/opt/mcx/config/polling-simulator:/tmp"
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment