Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit e430004

Browse files
committed
Add damp scores to status command
1 parent 1b28f89 commit e430004

File tree

10 files changed

+86
-31
lines changed

10 files changed

+86
-31
lines changed

checksums.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// THE SOFTWARE.
2222
'use strict';
2323

24-
var createTable = require('./lib/table.js');
24+
var createTable = require('./lib/table.js').create;
2525
var ClusterManager = require('./lib/cluster.js');
2626
var program = require('commander');
2727

commands.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ function ReuseCommand(tchannelV1, coordinator, member, limit) {
2626
this.limit = limit;
2727
}
2828

29-
function StatusCommand(tchannelV1, coordinator) {
29+
function StatusCommand(tchannelV1, coordinator, quiet) {
3030
this.useTChannelV1 = tchannelV1;
3131
this.coordinator = coordinator;
32+
this.quiet = quiet;
3233
}
3334

3435
function PartitionCommand(tchannelV1, coordinatorOrFile, quiet) {

dist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
var CliColor = require('cli-color');
2525
var AdminClient = require('./lib/admin-client.js');
26-
var createTable = require('./lib/table.js');
26+
var createTable = require('./lib/table.js').create;
2727
var HashRing = require('ringpop/lib/ring/index.js');
2828
var program = require('commander');
2929

lib/table.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
var CliTable = require('cli-table');
2525

26-
module.exports = function createTable(head) {
26+
function create(head) {
2727
return new CliTable({
2828
chars: {
2929
'bottom': '' ,
@@ -44,4 +44,21 @@ module.exports = function createTable(head) {
4444
},
4545
head: head
4646
});
47+
}
48+
49+
function print(command, headers, addRowsFn) {
50+
headers = command.quiet === true ? [] : headers;
51+
52+
var table = create(headers);
53+
54+
if (typeof addRowsFn === 'function') {
55+
addRowsFn(table);
56+
}
57+
58+
console.log(table.toString());
59+
}
60+
61+
module.exports ={
62+
create: create,
63+
print: print
4764
};

lib/util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
// THE SOFTWARE.
2020
'use strict';
2121

22+
function assertTruthy(truthy, message) {
23+
if (!!truthy) return;
24+
console.error(message);
25+
process.exit(1);
26+
}
27+
2228
function assertNoError(err) {
2329
if (err) {
2430
console.error('Error: ' + err.message);
@@ -37,5 +43,6 @@ function safeParse(data) {
3743
module.exports = {
3844
assertNoErr: assertNoError,
3945
assertNoError: assertNoError,
46+
assertTruthy: assertTruthy,
4047
safeParse: safeParse
4148
};

parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ function parseReuseCommand() {
4949
function parseStatusCommand() {
5050
program
5151
.description('Status of members in ring')
52+
.option('-q, --quiet', 'Do not print headers')
5253
.option('--tchannel-v1')
5354
.usage('[options] <hostport or bootstrapfile>');
5455
program.parse(process.argv);
5556
assertPositionArg(program, 0, 'hostport or bootstrapfile');
5657

57-
return new commands.StatusCommand(program.tchannelV1, program.args[0]);
58+
return new commands.StatusCommand(
59+
program.tchannelV1,
60+
program.args[0],
61+
program.quiet);
5862
}
5963

6064
function parsePartitionCommand() {

partitions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// THE SOFTWARE.
2222
'use strict';
2323

24-
var createTable = require('./lib/table.js');
24+
var createTable = require('./lib/table.js').create;
2525
var ClusterManager = require('./lib/cluster.js');
2626
var parsePartitionCommand = require('./parser.js').parsePartitionCommand;
2727

reuse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'use strict';
2323

2424
var Cluster = require('./lib/cluster.js');
25-
var createTable = require('./lib/table.js');
25+
var createTable = require('./lib/table.js').create;
2626
var parseReuseCommand = require('./parser.js').parseReuseCommand;
2727

2828
function main() {

status.js

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,63 @@
2121
// THE SOFTWARE.
2222
'use strict';
2323

24-
var createTable = require('./lib/table.js');
25-
var ClusterManager = require('./lib/cluster.js');
24+
var assertTruthy = require('./lib/util.js').assertTruthy;
25+
var Cluster = require('./lib/cluster.js');
2626
var parseStatusCommand = require('./parser.js').parseStatusCommand;
27+
var printTable = require('./lib/table.js').print;
28+
29+
function getDampScoreRange(allStats, statusMember) {
30+
var lowest = Number.MAX_VALUE;
31+
var highest = 0;
32+
33+
allStats.forEach(function each(stat) {
34+
stat.members.forEach(function each(member) {
35+
if (member.address !== statusMember.address) return;
36+
37+
var dampScore = member.dampScore;
38+
if (typeof dampScore === 'undefined') {
39+
return;
40+
}
41+
42+
if (dampScore < lowest) {
43+
lowest = dampScore;
44+
}
45+
46+
if (dampScore > highest) {
47+
highest = dampScore;
48+
}
49+
});
50+
});
51+
52+
return lowest + '..' + highest;
53+
}
2754

2855
function main() {
2956
var command = parseStatusCommand();
30-
var clusterManager = new ClusterManager({
57+
var cluster = new Cluster({
3158
useTChannelV1: command.useTChannelV1,
3259
coordAddr: command.coordinator
3360
});
34-
clusterManager.fetchStats(function onStats(err) {
35-
if (err) {
36-
console.error('Error: ' + err.message);
37-
process.exit(1);
38-
}
39-
40-
if (clusterManager.getPartitionCount() > 1) {
41-
console.error('Error: cluster is partitioned. An accurate status cannot be provided.');
42-
process.exit(1);
43-
}
44-
45-
var table = createTable([]);
46-
var cluster = clusterManager.getClusterAt(0);
47-
if (!cluster) {
48-
console.error('Error: no members in the cluster could be reached');
49-
process.exit(1);
50-
}
51-
cluster.membership.forEach(function each(member) {
52-
table.push([member.address, member.status]);
61+
cluster.fetchStats(function onStats(err) {
62+
assertTruthy(!err, (err && err.message));
63+
assertTruthy(cluster.getClusterAt(0),
64+
'Error: no members in the cluster could be reached');
65+
assertTruthy(cluster.getPartitionCount() === 1,
66+
'Error: cluster is partitioned. ' +
67+
'An accurate status cannot be provided');
68+
69+
var partition = cluster.getPartitionAt(0);
70+
printTable(command, [
71+
'ADDRESS',
72+
'STATUS',
73+
'DAMPSCORE'
74+
], function addRows(table) {
75+
// Add status information to table
76+
partition.membership.forEach(function each(member) {
77+
table.push([member.address, member.status,
78+
getDampScoreRange(cluster.allStats, member)]);
79+
});
5380
});
54-
console.log(table.toString());
5581
process.exit();
5682
});
5783
}

top.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
var CliColor = require('cli-color');
2525
var program = require('commander');
2626

27-
var createTable = require('./lib/table.js');
27+
var createTable = require('./lib/table.js').create;
2828
var ClusterManager = require('./lib/cluster.js');
2929

3030
var currentRows;

0 commit comments

Comments
 (0)