<commit_before>package eu.choreos.monitoring.platform.daemon;

import it.cnr.isti.labse.glimpse.event.GlimpseBaseEvent;

import java.util.List;
import java.util.Properties;

import eu.choreos.monitoring.platform.daemon.notifier.GlimpseMessageHandler;
import eu.choreos.monitoring.platform.utils.GmondDataReader;
import eu.choreos.monitoring.platform.utils.HostnameHandler;

public class ThresholdEvalDaemon {

	private static final int ALIVE_INTERVAL = 120000;
	private static final int NOTIFICATION_INTERVAL = 6000;
	private GlimpseMessageHandler messageHandler;
	private int nonSentMessagesIterationsCounter = 0;

	private AnomalyAnalyser analyser;

	public ThresholdEvalDaemon(Properties settings, String host, int port) {
		this(settings, host, port, (new GlimpseMessageHandler(settings)),
				(new GmondDataReader(host, port)));
	}

	public ThresholdEvalDaemon(Properties settings, String host, int port,
			GlimpseMessageHandler msgHandler, GmondDataReader dataReader) {
		messageHandler = msgHandler;
		analyser = new AnomalyAnalyser(dataReader);
	}

	public void addThreshold(Threshold threshold) {
		analyser.addThreshold(threshold);
	}

	public void continuouslyEvaluateThresholdsAndSendMessages(
			GlimpseBaseEvent<String> message) {

		while (true) {
			evaluateThresholdsSendMessagesAndSleep(message,
					NOTIFICATION_INTERVAL);
		}
	}

	public void evaluateThresholdsSendMessagesAndSleep(
			GlimpseBaseEvent<String> message, int sleepingTime) {

		if (thereAreSurpassedThresholds()) {
			sendAllSurpassedThresholdMessages(message);
		} else {
			nonSentMessagesIterationsCounter++;

			int timeSinceLastMessage = nonSentMessagesIterationsCounter
					* NOTIFICATION_INTERVAL;

			if (timeSinceLastMessage >= ALIVE_INTERVAL) {
				sendHeartbeat(message);
			}
		}

		sleep(sleepingTime);
	}

	public boolean thereAreSurpassedThresholds() {
		return !getSurpassedThresholds().isEmpty();
	}

	public List<Threshold> getSurpassedThresholds() {

		List<Threshold> evaluateAllThresholds = analyser
				.getAllSurpassedThresholds();
		for (Threshold threshold : evaluateAllThresholds) {
			System.out.println(threshold);
		}
		return evaluateAllThresholds;
	}

	public void sendHeartbeat(GlimpseBaseEvent<String> message) {
		message.setNetworkedSystemSource(HostnameHandler.getHostName());
		message.setData("Alive");
		sendMessage(message);
	}

	private void sendMessage(GlimpseBaseEvent<String> message) {
		messageHandler.sendMessage(message);
		nonSentMessagesIterationsCounter = 0;
	}

	private void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void sendAllSurpassedThresholdMessages(
			GlimpseBaseEvent<String> message) {
		for (Threshold surpassed : getSurpassedThresholds()) {
			message.setData(surpassed.toString());
			sendMessage(message);
		}
	}

}
<commit_msg>Added a Sysout to tell that a message was sent.<commit_after>package eu.choreos.monitoring.platform.daemon;

import it.cnr.isti.labse.glimpse.event.GlimpseBaseEvent;

import java.util.List;
import java.util.Properties;

import eu.choreos.monitoring.platform.daemon.notifier.GlimpseMessageHandler;
import eu.choreos.monitoring.platform.utils.GmondDataReader;
import eu.choreos.monitoring.platform.utils.HostnameHandler;

public class ThresholdEvalDaemon {

	private static final int ALIVE_INTERVAL = 120000;
	private static final int NOTIFICATION_INTERVAL = 6000;
	private GlimpseMessageHandler messageHandler;
	private int nonSentMessagesIterationsCounter = 0;

	private AnomalyAnalyser analyser;

	public ThresholdEvalDaemon(Properties settings, String host, int port) {
		this(settings, host, port, (new GlimpseMessageHandler(settings)),
				(new GmondDataReader(host, port)));
	}

	public ThresholdEvalDaemon(Properties settings, String host, int port,
			GlimpseMessageHandler msgHandler, GmondDataReader dataReader) {
		messageHandler = msgHandler;
		analyser = new AnomalyAnalyser(dataReader);
	}

	public void addThreshold(Threshold threshold) {
		analyser.addThreshold(threshold);
	}

	public void continuouslyEvaluateThresholdsAndSendMessages(
			GlimpseBaseEvent<String> message) {

		while (true) {
			evaluateThresholdsSendMessagesAndSleep(message,
					NOTIFICATION_INTERVAL);
		}
	}

	public void evaluateThresholdsSendMessagesAndSleep(
			GlimpseBaseEvent<String> message, int sleepingTime) {

		if (thereAreSurpassedThresholds()) {
			sendAllSurpassedThresholdMessages(message);
		} else {
			nonSentMessagesIterationsCounter++;

			int timeSinceLastMessage = nonSentMessagesIterationsCounter
					* NOTIFICATION_INTERVAL;

			if (timeSinceLastMessage >= ALIVE_INTERVAL) {
				sendHeartbeat(message);
			}
		}

		sleep(sleepingTime);
	}

	public boolean thereAreSurpassedThresholds() {
		return !getSurpassedThresholds().isEmpty();
	}

	public List<Threshold> getSurpassedThresholds() {

		List<Threshold> evaluateAllThresholds = analyser
				.getAllSurpassedThresholds();
		for (Threshold threshold : evaluateAllThresholds) {
			System.out.println(threshold);
		}
		return evaluateAllThresholds;
	}

	public void sendHeartbeat(GlimpseBaseEvent<String> message) {
		message.setNetworkedSystemSource(HostnameHandler.getHostName());
		message.setData("Alive");
		sendMessage(message);
	}

	private void sendMessage(GlimpseBaseEvent<String> message) {
		System.out.println("Sent a message: " + message.getData());
		messageHandler.sendMessage(message);
		nonSentMessagesIterationsCounter = 0;
	}

	private void sleep(int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private void sendAllSurpassedThresholdMessages(
			GlimpseBaseEvent<String> message) {
		for (Threshold surpassed : getSurpassedThresholds()) {
			message.setData(surpassed.toString());
			sendMessage(message);
		}
	}

}

{'fraction_non_alphanumeric': 0.08933470507544582, 'fraction_numerical': 0.00411522633744856, 'mean_word_length': 5.735565819861431, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '539706', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1755, 'n_words': 331}