-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Weigel
committed
Jan 13, 2019
1 parent
79fdbba
commit c0d0fe4
Showing
8 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
var fs = require('fs'); | ||
var http = require('http'); | ||
var readline = require('readline'); | ||
var yargs = require('yargs'); | ||
|
||
let argv = yargs | ||
.default({ | ||
'columns': '', | ||
'start': '0001-01-01T00:00:00.000000000Z', | ||
'stop': '9999-12-31T23:59:59.999999999Z', | ||
'file': '', | ||
'url': '', | ||
'format': 'csv' | ||
}) | ||
.option('columns', {type:'string'}) | ||
.option('start', {type:'string'}) | ||
.option('stop', {type:'string'}) | ||
.argv; | ||
|
||
function range(start, end) { | ||
var arr = []; | ||
for (var i = start; i <= end; i += 1) { | ||
arr.push(i); | ||
} | ||
return arr; | ||
} | ||
|
||
let columnsarr = []; | ||
if (argv.columns !== '') { | ||
// Convert, e.g, "1,2,4-6" to [1,2,4,5,6] - 1 | ||
let tmp = argv.columns.split(","); | ||
for (i = 0; i < tmp.length; i++) { | ||
if (/-/.test(tmp[i])) { | ||
tmp2 = tmp[i].split("-"); | ||
let expanded = range(parseInt(tmp2[0])-1,parseInt(tmp2[1])-1); | ||
columnsarr = columnsarr.concat(expanded); | ||
} else { | ||
columnsarr.push(tmp[i]-1); | ||
} | ||
} | ||
} else { | ||
columnsarr = null; | ||
} | ||
|
||
if (argv.file !== '') { | ||
if (argv.format !== 'csv') { | ||
console.log('Only --format=csv is implemented'); | ||
process.exit(1); | ||
} | ||
readlines(fs.createReadStream(argv.file)); | ||
} else if (argv.url !== '') { | ||
http.get(argv.url, res => readlines(res)); | ||
} else { | ||
readlines(process.stdin); | ||
} | ||
|
||
function readlines(readableStream) { | ||
var rl = readline.createInterface({ | ||
input: readableStream, | ||
output: process.stdout, | ||
terminal: false | ||
}) | ||
.on('line', function(line){ | ||
let linea = line.split(','); | ||
let time = linea[0]; | ||
let l = time.length; | ||
let m = Math.min(l,argv.start.length,argv.stop.length); | ||
let start = argv.start.slice(0,m); | ||
let stop = argv.stop.slice(0,m); | ||
time = time.slice(0,m); | ||
if (time >= start && time < stop) { | ||
if (columnsarr == null) { | ||
console.log(line); | ||
} else { | ||
line = ""; | ||
for (let i = 0;i < columnsarr.length;i++) { | ||
line = line + linea[columnsarr[i]] + ","; | ||
} | ||
console.log(line.slice(0,-1)); | ||
} | ||
} | ||
if (time >= stop) { | ||
process.exit(0); | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var test = require('../lib/test.js'); | ||
var testa = [ | ||
{ | ||
"command": "node lib/subset.js --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1¶meters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07", | ||
"Nlines": "7", | ||
"Ncommas": "7" | ||
}, | ||
{ | ||
"command": "node lib/subset.js --columns 1 --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1¶meters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07", | ||
"Nlines": "7", | ||
"Ncommas": "0" | ||
}, | ||
{ | ||
"command": "node lib/subset.js --columns 1-2 --url 'http://hapi-server.org/servers/TestData/hapi/data?id=dataset1¶meters=scalar&time.min=1970-01-01Z&time.max=1970-01-01T00:00:11Z&attach=false' --stop 1970-01-01T00:00:07", | ||
"Nlines": "7", | ||
"Ncommas": "7" | ||
} | ||
] | ||
test.commands(testa,"subset.js"); |