This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.js
executable file
·130 lines (113 loc) · 3.87 KB
/
cli.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h', 'version', 'v', 'quiet', 'q']
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
record-vbb-delays [command] [options]
Options:
--db -d Path to LevelDB. Default: vbb-delays.ldb
--stations -s Stations to monitor. Default: all
--stations-file JSON file with stations to monitor.
--interval -i In seconds. Default: 30
--quiet -q Don't show progress reports. Default: false
Examples:
record-vbb-delays --db my-custom.leveldb -s 900000100003,900000100001
record-vbb-delays --stations-file stations-to-monitor.json -q
record-vbb-delays export-sql --db my-custom.leveldb >delays.sql
record-vbb-delays export-ndjson --db my-custom.leveldb >delays.ndjson
cat delays.ndjson | record-vbb-delays last-dep-per-stopover >last-dep-delays.ndjson
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`record-vbb-delays v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
console.error(err)
process.exit(1)
}
const cmd = argv._[0]
const dbPath = argv.db || argv.d || 'vbb-delays.ldb'
if (cmd === 'export-sql') {
const {Transform} = require('stream')
const exportSql = require('hafas-record-delays/export-sql')
const pump = require('pump')
const out = new Transform({
transform: (sqlQuery, _, cb) => {
cb(null, Buffer.from(sqlQuery + '\n', 'utf8'))
}
})
exportSql(dbPath, out)
pump(out, process.stdout, showError)
} else if (cmd === 'export-ndjson') {
const exportNdjson = require('hafas-record-delays/export-ndjson')
process.stdout.on('error', showError)
exportNdjson(dbPath, process.stdout)
} else if (cmd === 'last-dep-per-stopover') {
const pump = require('pump')
const {parse, stringify} = require('ndjson')
const lastDepPerStopover = require('hafas-record-delays/last-dep-per-stopover')
pump(
process.stdin,
parse(),
lastDepPerStopover(),
stringify(),
process.stdout,
showError
)
} else if (cmd === 'record' || !cmd) {
const {readFileSync} = require('fs')
const vbbStations = require('vbb-stations')
const {isatty} = require('tty')
const differ = require('ansi-diff-stream')
const esc = require('ansi-escapes')
const record = require('.')
let stations = argv.stations || argv.s
if (stations) {
stations = 'string' === typeof stations ? stations.split(',') : [stations + '']
const nr = /^\d+$/
for (let station of stations) {
if (!nr.test(station)) showError('Every station ID must be a number.')
}
} else if (argv['stations-file']) {
stations = JSON.parse(readFileSync(argv['stations-file']), {encoding: 'utf8'})
} else stations = vbbStations('all').map(s => s.id)
let interval = argv.interval || argv.i
if (interval) {
interval = parseInt(interval) * 1000
if (Number.isNaN(interval)) showError('Interval musst be a number.')
} else interval = 30 * 1000
const recording = record(stations, interval, dbPath)
recording.on('error', (err) => {
if (!err.isHafasError || process.env.NODE_DEBUG === 'record-vbb-delays') {
console.error(err)
} else console.error(err && err.message || (err + ''))
})
process.once('beforeExit', () => recording.stop())
if (!argv.quiet && !argv.q) {
const clearReports = isatty(process.stderr.fd)
let reporter = process.stderr
if (clearReports) {
reporter = differ()
reporter.pipe(process.stderr)
recording.on('error', () => {
process.stderr.write('\n')
reporter.reset()
})
}
const report = ({reqs, departures, avgDuration}) => {
reporter.write([
reqs + (reqs === 1 ? ' request' : ' requests'),
departures + (departures === 1 ? ' departure' : ' departures'),
'~ ' + Math.round(avgDuration) + ' ms/req'
].join(', ') + (clearReports ? '' : '\n'))
}
recording.on('stats', report)
}
} else showError('Missing or invalid command.')