Skip to content

Commit

Permalink
Add dump_mesh option (fixes rjrodger#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Waters committed Mar 31, 2017
1 parent 295c833 commit 1a3a8d5
Showing 1 changed file with 107 additions and 121 deletions.
228 changes: 107 additions & 121 deletions sneeze.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var Joi = Optioner.Joi
var DEFAULT_HOST = module.exports.DEFAULT_HOST = '127.0.0.1'
var DEFAULT_PORT = module.exports.DEFAULT_PORT = 39999

var monitor_start = Date.now()
var monitor_data = {}

var optioner = Optioner({
isbase: false,
dump_mesh: false,
host: DEFAULT_HOST,
bases: Joi.array().default([DEFAULT_HOST+':'+DEFAULT_PORT]),
retry_attempts: 22,
Expand Down Expand Up @@ -183,6 +186,10 @@ function Sneeze (options) {

_.each( swim.members(), updateinfo )

if (options.dump_mesh) {
render(monitor_data).forEach(entry => console.log(entry))
}

swim.on(Swim.EventType.Update, function onUpdate(info) {
updateinfo(info)
})
Expand Down Expand Up @@ -212,11 +219,13 @@ function Sneeze (options) {

if( 0 === m.state ) {
add_node( m )
monitor_node ( 'add', m )
}

// Note: trigger happy
else if( 2 === m.state ) {
remove_node( m )
monitor_node ( 'rem', m )
}
}
}
Expand All @@ -236,6 +245,49 @@ function Sneeze (options) {
return self
}

function monitor_node ( kind, member ) {
var meta = member.meta
var m = monitor_data[meta.identifier$]

if (!m) {
m = monitor_data[meta.identifier$] = {
add: 0,
rem: 0
}
}

m[kind] += 1
m.id = meta.identifier$
m.tag = meta.tag$ || ''
m.host = member.host
m.meta = meta
m.state = member.state
m.meta = parse_meta(meta, options)
m.when = Date.now()

if (options.monitor.active) {

var w = process.stdout.write.bind(process.stdout)
var entries = Object.keys(monitor_data).reduce((memo, item) => {
var member = monitor_data[item]
if (member.id !== meta.identifier$) {
memo.push(member)
}
return memo
}, [])

var nm = entries.length

w(AE.clearScreen)
w(AE.cursorHide)
w(AE.cursorUp(nm+2))
w(AE.eraseDown)


render(entries).forEach(entry => console.log(entry))
}

}

function add_node( member ) {
var meta = member.meta
Expand Down Expand Up @@ -284,146 +336,80 @@ Util.inherits(Sneeze, Events.EventEmitter)

function make_monitor (sneeze, options) {

var start = Date.now()
var allmembers = {}
var sortedmembers = []

function update (kind, member) {
var meta = member.meta
var host = member.host

var m = allmembers[meta.identifier$]
Keypress(process.stdin)

if (m) {
m[kind] += 1
m.host = host
m.meta = meta
m.state = member.state
m.meta = parse_meta(meta,options)
m.tag = meta.tag$ || ''
}
else {
m = {
id: meta.identifier$,
tag: meta.tag$ || '',
host: host,
meta: meta,
state: member.state,
add: 0,
rem: 0,
meta: parse_meta(meta,options)
}
m[kind] += 1
process.stdin.on('keypress', function (ch, key) {
if (key && key.ctrl && key.name == 'c') {
process.exit()
}

m.when = Date.now()

allmembers[meta.identifier$] = m

sortedmembers = sortedmembers.filter(function (m) {
return m.id != meta.identifier$
})

sortedmembers.unshift(m)

//console.log(allmembers)
}


sneeze.on('add', function (meta, member) {
update('add', member)
//console.log('add',member)
render()
// prune failed members
if ('p' === ch) {
monitor_data = monitor_data.filter(m => m.state !== 2)
render(monitor_data)
}
})

sneeze.on('remove', function (meta, member) {
update('rem', member)
//console.log('rem',member.meta)
render()
})
process.stdin.setRawMode(true)
process.stdin.resume()
}

function render (items) {

var w = process.stdout.write.bind(process.stdout)
var states = {
0: 'A', 1: 'S', 2: 'F'
}
var head = Chalk.bold

function render () {
var size_host = 4
var size_meta = 4
var size_tag = 3

sortedmembers.forEach(function (m) {
size_host = Math.max(size_host,m.host.length)
size_meta = Math.max(size_meta,m.meta.length)
size_tag = Math.max(size_tag,m.tag.length)
})

var nm = sortedmembers.length

w(AE.clearScreen)
w(AE.cursorHide)
w(AE.cursorUp(nm+2))
w(AE.eraseDown)

console.log(head([
Pad('host',size_host),
Pad(2,'a'),
Pad(2,'r'),
Pad(2,'s'),
Pad(8,'time'),
Pad('tag',size_tag),
Pad('meta',size_meta),
'id'
].join(' ')))

sortedmembers.forEach(function (m,i) {
var memline = [
Pad(m.host||'',size_host),
Pad(2,''+(m.add||0)),
Pad(2,''+(m.rem||0)),
Pad(2,''+(states[m.state]||'U')),
Pad(8,''+(m.when-start)),
Pad(m.tag||'',size_tag),
Pad(m.meta||'',size_meta),
m.id
]

var lt = memline.join(' ')

lt = 2 === m.state ? Chalk.red(lt) : lt

console.log(lt)
})
}


Keypress(process.stdin)
var size_host = 4
var size_meta = 4
var size_tag = 3

process.stdin.on('keypress', function (ch, key) {
if (key && key.ctrl && key.name == 'c') {
process.exit()
}
Object.keys(items).forEach(key => {
var m = items[key]
size_host = Math.max(size_host,m.host.length)
size_meta = Math.max(size_meta,m.meta.length)
size_tag = Math.max(size_tag,m.tag.length)
})

// prune failed members
if ('p' === ch) {
sortedmembers = sortedmembers.filter(function (m) {
if (2 === m.state) {
delete allmembers[m.id]
return false
}
return true
})
render()
}
var nm = Object.keys(items).length

var ret = []

ret.push(head([
Pad('host',size_host),
Pad(2,'a'),
Pad(2,'r'),
Pad(2,'s'),
Pad(8,'time'),
Pad('tag',size_tag),
Pad('meta',size_meta),
'id'
].join(' ')))

Object.keys(items).forEach(key => {
var m = items[key]
var memline = [
Pad(m.host||'',size_host),
Pad(2,''+(m.add||0)),
Pad(2,''+(m.rem||0)),
Pad(2,''+(states[m.state]||'U')),
Pad(8,''+(m.when-monitor_start)),
Pad(m.meta||'',size_meta),
m.id
]

var lt = memline.join(' ')

lt = 2 === m.state ? Chalk.red(lt) : lt

ret.push(lt)
})

process.stdin.setRawMode(true)
process.stdin.resume()
return ret;
}


function parse_meta (meta, options) {
var out = []
options.monitor.meta.forEach(function (mf) {
Expand Down

0 comments on commit 1a3a8d5

Please sign in to comment.