<commit_before>package imperial.modaclouds.monitoring.datacollectors.monitors;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import imperial.modaclouds.monitoring.datacollectors.basic.AbstractMonitor;
import imperial.modaclouds.monitoring.datacollectors.basic.DataCollectorAgent;
import it.polimi.modaclouds.monitoring.dcfactory.DCMetaData;

/**
 * The monitoring collector for availability of Applications.
 */
public class AppAvailabilityMonitor extends AbstractMonitor{

	private Logger logger = LoggerFactory.getLogger(AppAvailabilityMonitor.class);

	/**
	 * Availability monitor thread.
	 */
	private Thread aavmt;

	/**
	 * The unique monitored target.
	 */
	private String monitoredTarget;

	private DataCollectorAgent dcAgent;

	private int samplingTime;

	private int port;

	private String path;

	private int retryPeriod;

	private int retryTimes;


	public AppAvailabilityMonitor(String resourceId, String mode) {
		super(resourceId, mode);

		monitoredTarget = resourceId;

		monitorName = "appavailability";

		dcAgent = DataCollectorAgent.getInstance();
	}

	@Override
	public void run() {
		long startTime = 0;

		while (!aavmt.isInterrupted()) {

			if (mode.equals("kb")) {

				if (System.currentTimeMillis() - startTime > 10000) {
					Collection<DCMetaData> dcConfig = dcAgent.getDataCollectors(resourceId);

					for (DCMetaData dc: dcConfig) {

						if (ModacloudsMonitor.findCollector(dc.getMonitoredMetric()).equals("appavailability")) {			
							Map<String, String> parameters = dc.getParameters();

							samplingTime = Integer.valueOf(parameters.get("samplingTime"))*1000;
							port = Integer.valueOf(parameters.get("port"));
							retryPeriod = Integer.valueOf(parameters.get("retryPeriod"));
							retryTimes = Integer.valueOf(parameters.get("retryTimes"));
							path = parameters.get("path");

							break;
						}
					}

					startTime = System.currentTimeMillis();
				}
			}


			HttpURLConnection connection;
			try {
				String url = "http://localhost:"+port+path;
				logger.info("URL: "+url);
				int count = 0;

				long t0 = System.currentTimeMillis();

				while (true) {

					connection = (HttpURLConnection) new URL(url).openConnection();
					connection.setConnectTimeout(retryPeriod);
					connection.setRequestMethod("HEAD");
					int responseCode = connection.getResponseCode();

					try {
						if (responseCode == 200) {
							dcAgent.sendSyncMonitoringDatum("true", "AppAvailable",monitoredTarget);
							break;
						} 
						else {
							if ( count == retryTimes) {
								dcAgent.sendSyncMonitoringDatum("false", "AppAvailable",monitoredTarget);
								break;
							}
							else {
								count++;
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}


					long t1 = System.currentTimeMillis();

					try {
						Thread.sleep(Math.max(0, samplingTime-t1+t0));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}



	@Override
	public void start() {
		aavmt = new Thread(this, "aavm-mon");
	}

	@Override
	public void init() {
		aavmt.start();
		logger.info("App Availability monitor running!");
	}

	@Override<commit_msg>bug fix<commit_after>package imperial.modaclouds.monitoring.datacollectors.monitors;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collection;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import imperial.modaclouds.monitoring.datacollectors.basic.AbstractMonitor;
import imperial.modaclouds.monitoring.datacollectors.basic.DataCollectorAgent;
import it.polimi.modaclouds.monitoring.dcfactory.DCMetaData;

/**
 * The monitoring collector for availability of Applications.
 */
public class AppAvailabilityMonitor extends AbstractMonitor{

	private Logger logger = LoggerFactory.getLogger(AppAvailabilityMonitor.class);

	/**
	 * Availability monitor thread.
	 */
	private Thread aavmt;

	/**
	 * The unique monitored target.
	 */
	private String monitoredTarget;

	private DataCollectorAgent dcAgent;

	private int samplingTime;

	private int port;

	private String path;

	private int retryPeriod;

	private int retryTimes;


	public AppAvailabilityMonitor(String resourceId, String mode) {
		super(resourceId, mode);

		monitoredTarget = resourceId;

		monitorName = "appavailability";

		dcAgent = DataCollectorAgent.getInstance();
	}

	@Override
	public void run() {
		long startTime = 0;

		while (!aavmt.isInterrupted()) {

			if (mode.equals("kb")) {

				if (System.currentTimeMillis() - startTime > 10000) {
					Collection<DCMetaData> dcConfig = dcAgent.getDataCollectors(resourceId);

					for (DCMetaData dc: dcConfig) {

						if (ModacloudsMonitor.findCollector(dc.getMonitoredMetric()).equals("appavailability")) {			
							Map<String, String> parameters = dc.getParameters();

							samplingTime = Integer.valueOf(parameters.get("samplingTime"))*1000;
							port = Integer.valueOf(parameters.get("port"));
							retryPeriod = Integer.valueOf(parameters.get("retryPeriod"));
							retryTimes = Integer.valueOf(parameters.get("retryTimes"));
							path = parameters.get("path");

							break;
						}
					}

					startTime = System.currentTimeMillis();
				}
			}


			HttpURLConnection connection;
				String url = "http://localhost:"+port+path;
				logger.info("URL: "+url);
				int count = 0;

				long t0 = System.currentTimeMillis();

				while (true) {

					int responseCode = 0;
					
					try {
						connection = (HttpURLConnection) new URL(url).openConnection();
						connection.setConnectTimeout(retryPeriod);
						connection.setRequestMethod("HEAD");
						responseCode = connection.getResponseCode();
					} catch (IOException e1) {
						e1.printStackTrace();
					}

					try {
						if (responseCode == 200) {
							dcAgent.sendSyncMonitoringDatum("true", "AppAvailable",monitoredTarget);
							break;
						} 
						else {
							if ( count == retryTimes) {
								dcAgent.sendSyncMonitoringDatum("false", "AppAvailable",monitoredTarget);
								break;
							}
							else {
								count++;
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
					}


					long t1 = System.currentTimeMillis();

					try {
						Thread.sleep(Math.max(0, samplingTime-t1+t0));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			
		}

	}



	@Override
	public void start() {
		aavmt = new Thread(this, "aavm-mon");
	}

	@Override
	public void init() {
		aavmt.start();
		logger.info("App Availability monitor running!");
	}

	@Override
{'fraction_non_alphanumeric': 0.11671511627906976, 'fraction_numerical': 0.006540697674418605, 'mean_word_length': 3.4108974358974358, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5715850', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2019, 'n_words': 408}