Skip to content

Commit d88ada6

Browse files
committed
refactor: pull monitoring events visualization into reusable method
1 parent 9109360 commit d88ada6

File tree

2 files changed

+123
-109
lines changed

2 files changed

+123
-109
lines changed

test/tools/sdam_viz

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
const { MongoClient } = require('../..');
5-
const arrayStrictEqual = require('../../lib/core/utils').arrayStrictEqual;
5+
const visualizeMonitoringEvents = require('./utils').visualizeMonitoringEvents;
66
const chalk = require('chalk');
77
const argv = require('yargs')
88
.usage('Usage: $0 [options] <connection string>')
@@ -32,61 +32,7 @@ async function run() {
3232
)} topology`
3333
);
3434

35-
client.on('serverHeartbeatSucceeded', event =>
36-
print(
37-
`${chalk.yellow('heartbeat')} ${chalk.green('succeeded')} host: '${
38-
event.connectionId
39-
}' ${chalk.gray(`(${event.duration} ms)`)}`
40-
)
41-
);
42-
43-
client.on('serverHeartbeatFailed', event =>
44-
print(
45-
`${chalk.yellow('heartbeat')} ${chalk.red('failed')} host: '${
46-
event.connectionId
47-
}' ${chalk.gray(`(${event.duration} ms)`)}`
48-
)
49-
);
50-
51-
// server information
52-
client.on('serverOpening', event => {
53-
print(
54-
`${chalk.cyan('server')} [${event.address}] ${chalk.bold('opening')} in topology#${
55-
event.topologyId
56-
}`
57-
);
58-
});
59-
60-
client.on('serverClosed', event => {
61-
print(
62-
`${chalk.cyan('server')} [${event.address}] ${chalk.bold('closed')} in topology#${
63-
event.topologyId
64-
}`
65-
);
66-
});
67-
68-
client.on('serverDescriptionChanged', event => {
69-
print(`${chalk.cyan('server')} [${event.address}] changed:`);
70-
console.log(serverDescriptionDiff(event.previousDescription, event.newDescription));
71-
});
72-
73-
// topology information
74-
client.on('topologyOpening', event => {
75-
print(`${chalk.magenta('topology')} adding topology#${event.topologyId}`);
76-
});
77-
78-
client.on('topologyClosed', event => {
79-
print(`${chalk.magenta('topology')} removing topology#${event.topologyId}`);
80-
});
81-
82-
client.on('topologyDescriptionChanged', event => {
83-
const diff = topologyDescriptionDiff(event.previousDescription, event.newDescription);
84-
if (diff !== '') {
85-
print(`${chalk.magenta('topology')} [topology#${event.topologyId}] changed:`);
86-
console.log(diff);
87-
}
88-
});
89-
35+
visualizeMonitoringEvents(client);
9036
await client.connect();
9137

9238
if (argv.workload) {
@@ -111,58 +57,6 @@ async function run() {
11157

11258
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
11359

114-
function diff(lhs, rhs, fields, comparator) {
115-
return fields.reduce((diff, field) => {
116-
if (lhs[field] == null || rhs[field] == null) {
117-
return diff;
118-
}
119-
120-
if (!comparator(lhs[field], rhs[field])) {
121-
diff.push(
122-
` ${field}: ${chalk.green(`[${lhs[field]}]`)} => ${chalk.green(`[${rhs[field]}]`)}`
123-
);
124-
}
125-
126-
return diff;
127-
}, []);
128-
}
129-
130-
function serverDescriptionDiff(lhs, rhs) {
131-
const objectIdFields = ['electionId'];
132-
const arrayFields = ['hosts', 'tags'];
133-
const simpleFields = [
134-
'type',
135-
'minWireVersion',
136-
'me',
137-
'setName',
138-
'setVersion',
139-
'electionId',
140-
'primary',
141-
'logicalSessionTimeoutMinutes'
142-
];
143-
144-
return diff(lhs, rhs, simpleFields, (x, y) => x === y)
145-
.concat(diff(lhs, rhs, arrayFields, (x, y) => arrayStrictEqual(x, y)))
146-
.concat(diff(lhs, rhs, objectIdFields, (x, y) => x.equals(y)))
147-
.join(',\n');
148-
}
149-
150-
function topologyDescriptionDiff(lhs, rhs) {
151-
const simpleFields = [
152-
'type',
153-
'setName',
154-
'maxSetVersion',
155-
'stale',
156-
'compatible',
157-
'compatibilityError',
158-
'logicalSessionTimeoutMinutes',
159-
'error',
160-
'commonWireVersion'
161-
];
162-
163-
return diff(lhs, rhs, simpleFields, (x, y) => x === y).join(',\n');
164-
}
165-
16660
run().catch(error => console.log('Caught', error));
16761
process.on('SIGINT', async function() {
16862
workloadInterrupt = true;

test/tools/utils.js

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const Logger = require('../../lib/core').Logger;
44
const deprecateOptions = require('../../lib/utils').deprecateOptions;
5+
const arrayStrictEqual = require('../../lib/core/utils').arrayStrictEqual;
6+
const chalk = require('chalk');
57
const chai = require('chai');
68
const expect = chai.expect;
79
const sinonChai = require('sinon-chai');
@@ -55,10 +57,128 @@ ClassWithUndefinedLogger.prototype.getLogger = function() {
5557
return undefined;
5658
};
5759

60+
function diff(lhs, rhs, fields, comparator) {
61+
return fields.reduce((diff, field) => {
62+
if (lhs[field] == null || rhs[field] == null) {
63+
return diff;
64+
}
65+
66+
if (!comparator(lhs[field], rhs[field])) {
67+
diff.push(
68+
` ${field}: ${chalk.green(`[${lhs[field]}]`)} => ${chalk.green(`[${rhs[field]}]`)}`
69+
);
70+
}
71+
72+
return diff;
73+
}, []);
74+
}
75+
76+
function serverDescriptionDiff(lhs, rhs) {
77+
const objectIdFields = ['electionId'];
78+
const arrayFields = ['hosts', 'tags'];
79+
const simpleFields = [
80+
'type',
81+
'minWireVersion',
82+
'me',
83+
'setName',
84+
'setVersion',
85+
'electionId',
86+
'primary',
87+
'logicalSessionTimeoutMinutes'
88+
];
89+
90+
return diff(lhs, rhs, simpleFields, (x, y) => x === y)
91+
.concat(diff(lhs, rhs, arrayFields, (x, y) => arrayStrictEqual(x, y)))
92+
.concat(diff(lhs, rhs, objectIdFields, (x, y) => x.equals(y)))
93+
.join(',\n');
94+
}
95+
96+
function topologyDescriptionDiff(lhs, rhs) {
97+
const simpleFields = [
98+
'type',
99+
'setName',
100+
'maxSetVersion',
101+
'stale',
102+
'compatible',
103+
'compatibilityError',
104+
'logicalSessionTimeoutMinutes',
105+
'error',
106+
'commonWireVersion'
107+
];
108+
109+
return diff(lhs, rhs, simpleFields, (x, y) => x === y).join(',\n');
110+
}
111+
112+
function visualizeMonitoringEvents(client) {
113+
function print(msg) {
114+
console.log(`${chalk.white(new Date().toISOString())} ${msg}`);
115+
}
116+
117+
client.on('serverHeartbeatStarted', event =>
118+
print(`${chalk.yellow('heartbeat')} ${chalk.bold('started')} host: '${event.connectionId}`)
119+
);
120+
121+
client.on('serverHeartbeatSucceeded', event =>
122+
print(
123+
`${chalk.yellow('heartbeat')} ${chalk.green('succeeded')} host: '${
124+
event.connectionId
125+
}' ${chalk.gray(`(${event.duration} ms)`)}`
126+
)
127+
);
128+
129+
client.on('serverHeartbeatFailed', event =>
130+
print(
131+
`${chalk.yellow('heartbeat')} ${chalk.red('failed')} host: '${
132+
event.connectionId
133+
}' ${chalk.gray(`(${event.duration} ms)`)}`
134+
)
135+
);
136+
137+
// server information
138+
client.on('serverOpening', event => {
139+
print(
140+
`${chalk.cyan('server')} [${event.address}] ${chalk.bold('opening')} in topology#${
141+
event.topologyId
142+
}`
143+
);
144+
});
145+
146+
client.on('serverClosed', event => {
147+
print(
148+
`${chalk.cyan('server')} [${event.address}] ${chalk.bold('closed')} in topology#${
149+
event.topologyId
150+
}`
151+
);
152+
});
153+
154+
client.on('serverDescriptionChanged', event => {
155+
print(`${chalk.cyan('server')} [${event.address}] changed:`);
156+
console.log(serverDescriptionDiff(event.previousDescription, event.newDescription));
157+
});
158+
159+
// topology information
160+
client.on('topologyOpening', event => {
161+
print(`${chalk.magenta('topology')} adding topology#${event.topologyId}`);
162+
});
163+
164+
client.on('topologyClosed', event => {
165+
print(`${chalk.magenta('topology')} removing topology#${event.topologyId}`);
166+
});
167+
168+
client.on('topologyDescriptionChanged', event => {
169+
const diff = topologyDescriptionDiff(event.previousDescription, event.newDescription);
170+
if (diff !== '') {
171+
print(`${chalk.magenta('topology')} [topology#${event.topologyId}] changed:`);
172+
console.log(diff);
173+
}
174+
});
175+
}
176+
58177
module.exports = {
59178
makeTestFunction,
60179
ensureCalledWith,
61180
ClassWithLogger,
62181
ClassWithoutLogger,
63-
ClassWithUndefinedLogger
182+
ClassWithUndefinedLogger,
183+
visualizeMonitoringEvents
64184
};

0 commit comments

Comments
 (0)