<commit_before>/*
 * Copyright 2013 NGDATA nv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ngdata.sep.tools.monitoring;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.joda.time.DateTime;

import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Generate a text report of the replication status.
 */
public class ReplicationStatusReport {

	public static String toJson(ReplicationStatus replicationStatus) throws JsonGenerationException, 
			JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		// ObjectNode json = mapper.createObjectNode();
		// json.put("occVersion", index.getOccVersion());
		// json.put("config", new String(index.getConfiguration(), Charsets.UTF_8));

		ObjectNode root = mapper.createObjectNode();
		ArrayNode peers = root.arrayNode();
		root.put("peers", peers);
		ArrayNode recoveredQueues = root.arrayNode();
		root.put("recoveredQueues", recoveredQueues);

		for (String peerId : sort(replicationStatus.getPeersAndRecoveredQueues())) {
			ArrayNode servers;
			if (replicationStatus.isRecoveredQueue(peerId)) {
				servers = recoveredQueues;
			} else {
				servers = peers;
			}
			for (String server : sort(replicationStatus.getServers(peerId))) {
				ReplicationStatus.Status status = replicationStatus.getStatus(peerId, server);
				ObjectNode serverNode = servers.objectNode();
				serverNode.put("server", server);
				serverNode.put("Hlog count", String.valueOf(status.getHLogCount()));
				serverNode.put("Hlog size", formatAsMB(status.getTotalHLogSize()));
				serverNode.put("Hlog progress", formatProgress(status.getProgressOnCurrentHLog()));
				serverNode.put("Age of last shipped Op", formatDuration(status.ageOfLastShippedOp));
				serverNode.put("Timestamp of last shipped Op", formatTimestamp(status.timestampOfLastShippedOp));
				serverNode.put("Selected peer count", formatInt(status.selectedPeerCount));
				if (status.timestampLastSleep != null) {
					long sleepAge = System.currentTimeMillis() - status.timestampLastSleep;
					serverNode.put("Last slept", "Last slept " + formatDuration(sleepAge) + " ago (muliplier: "
							+ status.sleepMultiplier + "): " + status.sleepReason);
				}
				servers.add(server);
			}
		}
		return mapper.writeValueAsString(root);
	}

	public static void printReport(ReplicationStatus replicationStatus, PrintStream out) {
		if (replicationStatus.getPeersAndRecoveredQueues().size() == 0) {
			System.out.println("There are no peer clusters.");
			return;
		}

		String columnFormat = "  | %1$-50.50s | %2$-15.15s | %3$-15.15s | %4$-15.15s | %5$-15.15s | %6$-30.30s | %7$-5.5s |\n";
		String columnFormatWide = "  | %1$-50.50s | %2$-110.110s |\n";

		out.println();
		out.println();
		out.println("How to interpret the output:");
		out.println(" * this shows the progress from the HBase point of view, i.e. how far");
		out.println("   each regionserver is in shipping its events to the peers.");<commit_msg>replication status - json format - fix2<commit_after>/*
 * Copyright 2013 NGDATA nv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ngdata.sep.tools.monitoring;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ArrayNode;
import org.codehaus.jackson.node.ObjectNode;
import org.joda.time.DateTime;

import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * Generate a text report of the replication status.
 */
public class ReplicationStatusReport {

	public static String toJson(ReplicationStatus replicationStatus) throws JsonGenerationException, 
			JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		// ObjectNode json = mapper.createObjectNode();
		// json.put("occVersion", index.getOccVersion());
		// json.put("config", new String(index.getConfiguration(), Charsets.UTF_8));

		ObjectNode root = mapper.createObjectNode();
		ArrayNode peers = root.arrayNode();
		root.put("peers", peers);
		ArrayNode recoveredQueues = root.arrayNode();
		root.put("recoveredQueues", recoveredQueues);

		for (String peerId : sort(replicationStatus.getPeersAndRecoveredQueues())) {
			ArrayNode servers;
			if (replicationStatus.isRecoveredQueue(peerId)) {
				servers = recoveredQueues;
			} else {
				servers = peers;
			}
			for (String server : sort(replicationStatus.getServers(peerId))) {
				ReplicationStatus.Status status = replicationStatus.getStatus(peerId, server);
				ObjectNode serverNode = servers.objectNode();
				serverNode.put("server", server);
				serverNode.put("Hlog count", String.valueOf(status.getHLogCount()));
				serverNode.put("Hlog size", formatAsMB(status.getTotalHLogSize()));
				serverNode.put("Hlog progress", formatProgress(status.getProgressOnCurrentHLog()));
				serverNode.put("Age of last shipped Op", formatDuration(status.ageOfLastShippedOp));
				serverNode.put("Timestamp of last shipped Op", formatTimestamp(status.timestampOfLastShippedOp));
				serverNode.put("Selected peer count", formatInt(status.selectedPeerCount));
				if (status.timestampLastSleep != null) {
					long sleepAge = System.currentTimeMillis() - status.timestampLastSleep;
					serverNode.put("Last slept", "Last slept " + formatDuration(sleepAge) + " ago (muliplier: "
							+ status.sleepMultiplier + "): " + status.sleepReason);
				}
				servers.add(serverNode);
			}
		}
		return mapper.writeValueAsString(root);
	}

	public static void printReport(ReplicationStatus replicationStatus, PrintStream out) {
		if (replicationStatus.getPeersAndRecoveredQueues().size() == 0) {
			System.out.println("There are no peer clusters.");
			return;
		}

		String columnFormat = "  | %1$-50.50s | %2$-15.15s | %3$-15.15s | %4$-15.15s | %5$-15.15s | %6$-30.30s | %7$-5.5s |\n";
		String columnFormatWide = "  | %1$-50.50s | %2$-110.110s |\n";

		out.println();
		out.println();
		out.println("How to interpret the output:");
		out.println(" * this shows the progress from the HBase point of view, i.e. how far");
		out.println("   each regionserver is in shipping its events to the peers.");
{'fraction_non_alphanumeric': 0.12114076399790685, 'fraction_numerical': 0.014521193092621664, 'mean_word_length': 4.592538405267008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2898573', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2215, 'n_words': 583}