Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery
/
devops
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
3b9136b1
authored
Sep 13, 2018
by
Amir Aharon
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
change to read multiple files in directory and respond by prefix
parent
945a15fc
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
11 deletions
tools/PollingSimulator/.vscode/launch.json
tools/PollingSimulator/app.js
tools/PollingSimulator/docker-compose.yml
tools/PollingSimulator/.vscode/launch.json
View file @
3b9136b1
...
@@ -11,7 +11,8 @@
...
@@ -11,7 +11,8 @@
"program"
:
"${workspaceFolder}/app.js"
,
"program"
:
"${workspaceFolder}/app.js"
,
"args"
:
[
"args"
:
[
"-p"
,
"8053"
,
"-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"
]
]
}
}
]
]
...
...
tools/PollingSimulator/app.js
View file @
3b9136b1
...
@@ -6,9 +6,11 @@ const request = require('request');
...
@@ -6,9 +6,11 @@ const request = require('request');
const
moment
=
require
(
'moment'
);
const
moment
=
require
(
'moment'
);
var
myArgs
=
require
(
'optimist'
).
argv
,
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
config
=
{};
var
jsonFilesMap
=
[];
//const twoDaysAgoMs = 1000 * 3600 * 47;
//const twoDaysAgoMs = 1000 * 3600 * 47;
/**
/**
* getting configuration either from command args or from env
* getting configuration either from command args or from env
...
@@ -48,15 +50,18 @@ app.listen(config.port)
...
@@ -48,15 +50,18 @@ app.listen(config.port)
console
.
log
(
'Server started! At http://localhost:'
+
config
.
port
);
console
.
log
(
'Server started! At http://localhost:'
+
config
.
port
);
app
.
get
(
"/*"
,
function
(
req
,
res
){
app
.
get
(
"/*"
,
function
(
req
,
res
){
console
.
log
(
"Got Polling request on: "
+
moment
().
format
());
console
.
log
(
"Got Polling request
for: "
+
req
.
path
+
"
on: "
+
moment
().
format
());
jsonResp
=
getCurrentPollingResp
();
jsonResp
=
getCurrentPollingResp
(
req
.
path
);
res
.
send
(
jsonResp
);
res
.
send
(
jsonResp
);
});
});
function
getCurrentPollingResp
(){
function
getCurrentPollingResp
(
path
){
const
jsonFile
=
jsonFilesMap
[
path
];
if
(
jsonFile
){
if
(
Array
.
isArray
(
jsonFile
)){
if
(
Array
.
isArray
(
jsonFile
)){
return
jsonFile
.
map
(
entity
=>
getCurrentEntity
(
entity
));
return
jsonFile
.
map
(
entity
=>
getCurrentEntity
(
entity
));
}
}
}
return
[];
return
[];
}
}
...
@@ -128,9 +133,51 @@ function handleChildren(parent) {
...
@@ -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
;
if
(
err
)
throw
err
;
jsonFile
=
JSON
.
parse
(
data
);
console
.
log
(
'finished reading: '
+
config
.
file
);
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
;
if
(
stats
.
isFile
()){
let
filePrefix
=
getFilePrefix
(
config
.
file
);
handleReadFile
(
config
.
file
,
filePrefix
);
}
else
if
(
stats
.
isDirectory
()){
handleReadDirectory
(
config
.
file
);
}
});
});
tools/PollingSimulator/docker-compose.yml
View file @
3b9136b1
...
@@ -5,6 +5,7 @@ services:
...
@@ -5,6 +5,7 @@ services:
file
:
"
/tmp/polling.json"
file
:
"
/tmp/polling.json"
image
:
municipalitybank.com:5050/mcx/devops/polling-simulator
image
:
municipalitybank.com:5050/mcx/devops/polling-simulator
ports
:
ports
:
-
"
80:8053"
-
"
80
85
:8053"
volumes
:
volumes
:
-
"
/home/amir/git/ipgallery/transportation/doc/chicago-polling.json:/tmp/polling.json"
# - "/home/amir/git/ipgallery/transportation/doc/chicago-polling.json:/tmp/polling.json"
\ No newline at end of file
-
"
/opt/mcx/config/polling-simulator:/tmp"
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment