Created
December 15, 2019 03:55
-
-
Save imlucas/9f83388e68921221771a4df0c5d65edc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const log = require ('ololog').configure ({ | |
locate: false | |
}); | |
const { green, yellow, red, cyan, magenta, lightGray, darkGray, inverse, bgLightCyan, underline, dim } = require ('ansicolor') | |
const stringify = require ('string.ify') | |
const arrayStrictEqual = require('strict-array-equals'); | |
function diff(lhs, rhs, fields, comparator) { | |
return fields.reduce((diff, field) => { | |
if (lhs[field] == null || rhs[field] == null) { | |
return diff; | |
} | |
if (!comparator(lhs[field], rhs[field])) { | |
diff.push( | |
`${field}: ${yellow(`[${lhs[field]}]`)} => ${green(`[${rhs[field]}]`)}` | |
); | |
} | |
return diff; | |
}, []); | |
} | |
function serverDescriptionDiff(lhs, rhs) { | |
const objectIdFields = ['electionId']; | |
const arrayFields = ['hosts', 'tags']; | |
const simpleFields = [ | |
'type', | |
'minWireVersion', | |
'me', | |
'setName', | |
'setVersion', | |
'electionId', | |
'primary', | |
'logicalSessionTimeoutMinutes' | |
]; | |
return diff(lhs, rhs, simpleFields, (x, y) => x === y) | |
.concat(diff(lhs, rhs, arrayFields, (x, y) => arrayStrictEqual(x, y))) | |
.concat(diff(lhs, rhs, objectIdFields, (x, y) => x.equals(y))); | |
} | |
function topologyDescriptionDiff(lhs, rhs) { | |
const simpleFields = [ | |
'type', | |
'setName', | |
'maxSetVersion', | |
'stale', | |
'compatible', | |
'compatibilityError', | |
'logicalSessionTimeoutMinutes', | |
'error', | |
'commonWireVersion' | |
]; | |
return diff(lhs, rhs, simpleFields, (x, y) => x === y); | |
} | |
class DataService{ | |
setupListeners(client) { | |
this.client = client; | |
log(`${underline('Connecting')}`); | |
log(`${client.s.url}`); | |
log(client.s.options); | |
if (client) { | |
client.on('serverDescriptionChanged', event => { | |
const diff = serverDescriptionDiff(event.previousDescription, event.newDescription); | |
log(`${cyan('server')} [${event.address}] changed:`); | |
diff.map(line => log(' ' + line)); | |
this.emit('serverDescriptionChanged', event); | |
}); | |
client.on('serverOpening', event => { | |
log( | |
`${cyan('server')} [${event.address}] ${underline('opening')} in topology#${ | |
event.topologyId | |
}` | |
); | |
this.emit('serverOpening', event); | |
}); | |
client.on('serverClosed', event => { | |
log( | |
`${cyan('server')} [${event.address}] ${underline('closed')} in topology#${ | |
event.topologyId | |
}` | |
); | |
this.emit('serverClosed', event); | |
}); | |
client.on('topologyOpening', event => { | |
log(`${magenta('topology')} adding topology#${event.topologyId}`); | |
this.emit('topologyOpening', event); | |
}); | |
client.on('topologyClosed', event => { | |
log(`${magenta('topology')} removing topology#${event.topologyId}`); | |
this.emit('topologyClosed', event); | |
}); | |
client.on('topologyDescriptionChanged', event => { | |
client.isWritable = this._isWritable(event); | |
client.isMongos = this._isMongos(event); | |
const diff = topologyDescriptionDiff(event.previousDescription, event.newDescription); | |
if (diff.length > 0) { | |
log(`${magenta('topology')} [topology#${event.topologyId}] changed:`); | |
diff.map(line => log(' ' + line)); | |
} | |
this.emit('topologyDescriptionChanged', event); | |
}); | |
client.on('serverHeartbeatSucceeded', event => { | |
log( | |
`${darkGray('heartbeat')} ${green('succeeded')} host: '${ | |
event.connectionId | |
}' ${lightGray(`(${event.duration} ms)`)}` | |
) | |
this.emit('serverHeartbeatSucceeded', event); | |
}); | |
client.on('serverHeartbeatFailed', event => { | |
debug('serverHeartbeatFailed'); | |
log( | |
`${darkGray('heartbeat')} ${red('failed')} host: '${ | |
event.connectionId | |
}' ${lightGray(`(${event.duration} ms)`)}` | |
) | |
this.emit('serverHeartbeatFailed', event); | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment