Commit 26ea3cbd by Amir Aharon

add polling simulator

parent 90ef581b
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "run app",
"program": "${workspaceFolder}/app.js",
"args": [
"-p","8053",
"-f","/home/amir/git/ipgallery/transportation/doc/chicago-polling.json"
]
}
]
}
\ No newline at end of file
FROM node:8-alpine
# Create app directory
WORKDIR /usr/src/app
## NODE PART
# Install app dependencies
COPY package.json .
# install dependencies
RUN npm install
# Bundle app source
COPY app.js .
CMD [ "npm", "start"]
\ No newline at end of file
const express = require('express');
const http = require('http');
const url = require('url');
const fs = require('fs');
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]';
var config = {};
//const twoDaysAgoMs = 1000 * 3600 * 47;
/**
* getting configuration either from command args or from env
*/
var getConfiguration = function(){
myArgs.f ?
config.file = myArgs.f : config.file = process.env.file;
myArgs.p ?
config.port = myArgs.p : config.port = 8053; // default port
console.log("Configuration is: " + JSON.stringify(config));
};
//private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy' 'HH:mm:ss:S");
/**
* some validation
*/
if ((myArgs.h)||(myArgs.help)) {
console.log(help);
process.exit(0);
}
getConfiguration();
if (config.file == null){
console.log('please insert file path');
process.exit(0);
}
/**
* start app
// */
const app = express();
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();
res.send(jsonResp);
});
function getCurrentPollingResp(){
if (Array.isArray(jsonFile)){
return jsonFile.map(entity => getCurrentEntity(entity));
}
return [];
}
function getCurrentEntity(entity){
currentEntity = {...entity}; // ES6 for Object.assign({},entity);
handleChildren(currentEntity);
return currentEntity;
}
function randomFloatBetween(minValue,maxValue,precision){
if(typeof(precision) == 'undefined'){
precision = 2;
}
return parseFloat(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue).toFixed(precision));
}
function randomIntBetween(minValue,maxValue){
return parseInt(Math.min(minValue + (Math.random() * (maxValue - minValue)),maxValue));
}
function randomStringBetween(values){
return values[parseInt(Math.random() * values.length)]
}
function randomDatetimeBetween(unit,value){
var randomValue = randomIntBetween(0,value);
var randomDate = moment().subtract(randomValue, unit.toLowerCase());
return randomDate.format('YYYY-MM-DD HH:mm:ss.S');
}
function handleChild(child, parent) {
var obj = parent[child];
switch (typeof obj){
case 'object':
handleChildren(obj);
break;
case 'string':
var args = obj.split(":");
if (args.length >= 4){
if (args[0] == 'R'){
var startRange = args[2];
var endRange = args[3];
var type = args[1];
switch (type){
case 'I':
parent[child] = randomIntBetween(startRange,endRange);
break;
case 'F':
parent[child] = randomFloatBetween(startRange,endRange);
break;
case 'S':
parent[child] = randomStringBetween(args.slice(2,args.length));
break;
case 'T':
parent[child] = randomDatetimeBetween(args[2],args[3]);
break;
}
}
}
break;
case 'number':
if (child == 'timestamp')
parent[child] = Date.now();
break;
}
}
function handleChildren(parent) {
for (var child in parent) {
handleChild(child, parent);
}
}
fs.readFile(config.file, (err, data) => {
if (err) throw err;
jsonFile = JSON.parse(data);
console.log('finished reading: ' + config.file);
});
version: '2'
services:
polling-simulator:
environment:
file: "/tmp/polling.json"
image: municipalitybank.com:5050/mcx/devops/polling-simulator
ports:
- "8053:8053"
volumes:
- "/home/amir/git/ipgallery/transportation/doc/chicago-polling.json:/tmp/polling.json"
\ No newline at end of file
{
"name": "ws-event-emulator",
"version": "1.0.0",
"description": "emulate events in websocket",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"keywords": [
"event",
"emit"
],
"author": "amir aharon <amir.aharon@ipgallery.com>",
"license": "ISC",
"dependencies": {
"express": "^4.16.2",
"fs": "0.0.1-security",
"http": "0.0.0",
"moment": "^2.22.2",
"optimist": "^0.6.1",
"request": "^2.83.0"
},
"bin": {
"ws-event-emulator": "./app.js"
}
}
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