<commit_before>package no.uio.master.autoscale.node;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import no.uio.master.autoscale.Autoscale;

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

/**
 * Autoscale JMX operations.<br>
 * Code from org.apache.cassandra.tools#NodeProbe
 * @author toraba
 */
public class NodeData {
	private static Logger LOG = LoggerFactory.getLogger(NodeData.class);
	
	private static final String fmtUrl = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi";
	private static final int defaultPort = 7199;
	final String host;
	final int port;
	private String username;
	private String password;
	
	private JMXConnector jmxc;
	private MBeanServerConnection mbeanServerConn;
	private MemoryMXBean memoryMXBean;
	
	private JMXServiceURL jmxUrl;
	
	public NodeData(String host, int port, String username , String password) throws IOException {
		assert username != null && !username.isEmpty() && password != null && !password.isEmpty()
	               : "neither username nor password can be blank";

	        this.host = host;
	        this.port = port;
	        this.username = username;
	        this.password = password;
	        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public NodeData(String host, int port) throws IOException {
		LOG.debug("Initialize host: " + host + ", port: " + port);
		this.host = host;
        this.port = port;
        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public NodeData(String host) throws IOException {
		LOG.debug("Initialize host: " + host + ", defaultPort: " + defaultPort);
		this.host = host;
        this.port = defaultPort;
        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public void connect() throws IOException  {
			LOG.debug("Connect init...");
			Map<String,Object> env = new HashMap<String,Object>();
			
			if(username != null) {
				String[] creds = { username, password };
				env.put(JMXConnector.CREDENTIALS, env);
			}
			
			jmxc = JMXConnectorFactory.connect(jmxUrl,env);
			mbeanServerConn = jmxc.getMBeanServerConnection();
			
			// MemoryMXBean
			memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn, ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
			
			LOG.debug("Connected");
	}

	public void close() throws IOException {
        jmxc.close();
    }
	
	
	/**
	 * Percentage used of heap memory
	 * @return
	 * @throws IOException 
	 */
	public Double heapMemoryUsed() throws IOException {
		connect();
		Double mem = 0.0;
		
		Long used = memoryMXBean.getHeapMemoryUsage().getUsed();
		Long max = memoryMXBean.getHeapMemoryUsage().getMax();
		
		mem = ((double) used / (double) max) * 100; 
		close();<commit_msg>Removed unused credits-array from NodeData<commit_after>package no.uio.master.autoscale.node;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import no.uio.master.autoscale.Autoscale;

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

/**
 * Autoscale JMX operations.<br>
 * Code from org.apache.cassandra.tools#NodeProbe
 * @author toraba
 */
public class NodeData {
	private static Logger LOG = LoggerFactory.getLogger(NodeData.class);
	
	private static final String fmtUrl = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi";
	private static final int defaultPort = 7199;
	final String host;
	final int port;
	private String username;
	private String password;
	
	private JMXConnector jmxc;
	private MBeanServerConnection mbeanServerConn;
	private MemoryMXBean memoryMXBean;
	
	private JMXServiceURL jmxUrl;
	
	public NodeData(String host, int port, String username , String password) throws IOException {
		assert username != null && !username.isEmpty() && password != null && !password.isEmpty()
	               : "neither username nor password can be blank";

	        this.host = host;
	        this.port = port;
	        this.username = username;
	        this.password = password;
	        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public NodeData(String host, int port) throws IOException {
		LOG.debug("Initialize host: " + host + ", port: " + port);
		this.host = host;
        this.port = port;
        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public NodeData(String host) throws IOException {
		LOG.debug("Initialize host: " + host + ", defaultPort: " + defaultPort);
		this.host = host;
        this.port = defaultPort;
        generateJMXServiceUrl(fmtUrl, host, port);
	}
	
	public void connect() throws IOException  {
			LOG.debug("Connect init...");
			Map<String,Object> env = new HashMap<String,Object>();
			
			if(username != null) {
				//String[] creds = { username, password };
				env.put(JMXConnector.CREDENTIALS, env);
			}
			
			jmxc = JMXConnectorFactory.connect(jmxUrl,env);
			mbeanServerConn = jmxc.getMBeanServerConnection();
			
			// MemoryMXBean
			memoryMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServerConn, ManagementFactory.MEMORY_MXBEAN_NAME, MemoryMXBean.class);
			
			LOG.debug("Connected");
	}

	public void close() throws IOException {
        jmxc.close();
    }
	
	
	/**
	 * Percentage used of heap memory
	 * @return
	 * @throws IOException 
	 */
	public Double heapMemoryUsed() throws IOException {
		connect();
		Double mem = 0.0;
		
		Long used = memoryMXBean.getHeapMemoryUsage().getUsed();
		Long max = memoryMXBean.getHeapMemoryUsage().getMax();
		
		mem = ((double) used / (double) max) * 100; 
		close();
{'fraction_non_alphanumeric': 0.11147811725846409, 'fraction_numerical': 0.003633360858794385, 'mean_word_length': 3.8681672025723475, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3105401', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1805, 'n_words': 437}