Skip to content

Instantly share code, notes, and snippets.

@sbimikesmullin
Last active March 3, 2018 23:41
Show Gist options
  • Save sbimikesmullin/92b3c14904632c4abbff to your computer and use it in GitHub Desktop.
Save sbimikesmullin/92b3c14904632c4abbff to your computer and use it in GitHub Desktop.
interpret stdin and output reports periodically throughout the stream
counters = {}
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on 'data', (chunk) ->
if (matches = `/ \[3\] \[ (.+?) \]: (\w+) cmd=(\w+)/`.exec(''+chunk)) isnt null
[ nil, cla, type, cmd ] = matches
counters["#{cla}:#{type}:#{cmd}"] ||= 0
counters["#{cla}:#{type}:#{cmd}"] += 1
#process.stdout.write chunk
#console.log [cla, type, cmd, counters]
setInterval((->
s = []
for k, v of counters
s.push [ k, v ]
s.sort((a,b) -> a[1] - b[1])
s.reverse()
process.stdout.write "STATS: "
for v in s
process.stdout.write "#{v[1]} #{v[0]} "
process.stdout.write "\n\n"
), 3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment