String sizesStr = properties.getProperty("top.sizes");
Set<String> sizes = new HashSet<String>(Arrays.asList(resolveShortNames(sizesStr.split(","), properties)));
Set<String> keys = new HashSet<String>(metricNames.values());
ConsoleReader reader = this.getConsoleReader();
if (reader != null) {
Terminal terminal = reader.getTerminal();
_height = terminal.getHeight() - 2;
_width = terminal.getWidth() - 2;
try {
reader.setPrompt("");
reader.clearScreen();
} catch (IOException e) {
if (Main.debug) {
e.printStackTrace();
}
}
startCommandWatcher(reader, quit, help, this);
}
List<String> shardServerList = new ArrayList<String>(client.shardServerList(cluster));
Collections.sort(shardServerList);
Map<String, AtomicReference<Client>> shardClients = setupClients(shardServerList);
String shardServerLabel = properties.getProperty(TOP_SHARD_SERVER_SHORTNAME);
int longestServerName = Math.max(getSizeOfLongestKey(shardClients), shardServerLabel.length());
StringBuilder header = new StringBuilder("%" + longestServerName + "s");
for (int i = 1; i < labels.length; i++) {
header.append(" %10s");
}
do {
int lineCount = 0;
StringBuilder output = new StringBuilder();
if (quit.get()) {
return;
} else if (help.get()) {
showHelp(output, labels, helpMap);
} else {
output.append(truncate(String.format(header.toString(), (Object[]) labels)) + "\n");
lineCount++;
SERVER: for (Entry<String, AtomicReference<Client>> e : new TreeMap<String, AtomicReference<Client>>(
shardClients).entrySet()) {
String shardServer = e.getKey();
AtomicReference<Client> ref = e.getValue();
Map<String, Metric> metrics = getMetrics(shardServer, ref, keys);
if (metrics == null) {
String line = String.format("%" + longestServerName + "s*%n", shardServer);
output.append(line);
lineCount++;
if (tooLong(lineCount)) {
break SERVER;
}
} else {
Object[] cols = new Object[labels.length];
int c = 0;
cols[c++] = shardServer;
StringBuilder sb = new StringBuilder("%" + longestServerName + "s");
for (int i = 1; i < labels.length; i++) {
String mn = metricNames.get(labels[i]);
Metric metric = metrics.get(mn);
Double value;
if (metric == null) {
value = null;
} else {
Map<String, Double> doubleMap = metric.getDoubleMap();
value = doubleMap.get("oneMinuteRate");
if (value == null) {
value = doubleMap.get("value");
}
}
if (value == null) {
value = 0.0;
}
cols[c++] = humanize(value, sizes.contains(mn));
sb.append(" %10s");
}
output.append(truncate(String.format(sb.toString(), cols)) + "\n");
lineCount++;
if (tooLong(lineCount)) {
break SERVER;
}
}
}
}
if (reader != null) {
try {
reader.clearScreen();
} catch (IOException e) {
if (Main.debug) {
e.printStackTrace();
}
}
}
out.print(output.toString());
out.flush();
if (reader != null) {
try {
synchronized (this) {
wait(3000);
}
} catch (InterruptedException e) {
return;
}
Terminal terminal = reader.getTerminal();
_height = terminal.getHeight() - 2;
_width = terminal.getWidth() - 2;
List<String> currentShardServerList = new ArrayList<String>(client.shardServerList(cluster));
Collections.sort(currentShardServerList);