use of com.orientechnologies.orient.console.OTableFormatter in project orientdb by orientechnologies.
the class ODistributedOutput method formatMessageStats.
public static String formatMessageStats(final OHazelcastPlugin manager, final ODocument distribCfg) {
final List<OIdentifiable> rows = new ArrayList<OIdentifiable>();
final List<ODocument> members = distribCfg.field("members");
final StringBuilder buffer = new StringBuilder();
buffer.append("\nREPLICATION MESSAGE COORDINATOR STATS");
final OTableFormatter table = new OTableFormatter(new OTableFormatter.OTableOutput() {
@Override
public void onMessage(final String text, final Object... args) {
buffer.append(String.format(text, args));
}
});
table.setColumnHidden("#");
if (members != null) {
// BUILD A SORTED SERVER LIST AND OPERATION NAMES
final List<String> orderedServers = new ArrayList<String>(members.size());
final Set<String> operations = new LinkedHashSet<String>();
for (ODocument fromMember : members) {
if (fromMember != null) {
String serverName = fromMember.field("name");
orderedServers.add(serverName);
// INSERT ALL THE FOUND OPERATIONS
final ODocument messages = fromMember.field("messages");
if (messages != null) {
for (String opName : messages.fieldNames()) {
operations.add(opName);
}
}
}
}
Collections.sort(orderedServers);
final ODocument rowTotals = new ODocument();
for (String server : orderedServers) {
// SEARCH FOR THE MEMBER
ODocument member = null;
for (ODocument m : members) {
if (server.equals(m.field("name"))) {
member = m;
break;
}
}
if (member == null)
// SKIP IT
continue;
final ODocument row = new ODocument();
rows.add(row);
row.field("Servers", formatServerName(manager, server));
final ODocument messages = member.field("messages");
if (messages == null)
continue;
long total = 0;
for (String opName : operations) {
final Long counter = messages.field(opName);
if (counter == null) {
row.field(opName, "");
continue;
}
total += counter;
final String value = String.format("%,d", counter);
row.field(opName, value);
// AGGREGATE IN TOTALS
sumTotal(rowTotals, opName, counter);
table.setColumnAlignment(opName, OTableFormatter.ALIGNMENT.RIGHT);
}
row.field("TOTAL", String.format("%,d", total));
sumTotal(rowTotals, "TOTAL", total);
}
// FOOTER WITH THE TOTAL OF ALL THE ROWS
table.setFooter(rowTotals);
rowTotals.field("Servers", "TOTAL");
for (String opName : operations) {
rowTotals.field(opName, String.format("%,d", rowTotals.field(opName)));
}
rowTotals.field("TOTAL", String.format("%,d", rowTotals.field("TOTAL")));
}
table.setColumnAlignment("TOTAL", OTableFormatter.ALIGNMENT.RIGHT);
table.writeRecords(rows, -1);
buffer.append("\n");
return buffer.toString();
}
Aggregations