<commit_before>package fi.csc.chipster.web.adminweb.data;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;

import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import fi.csc.chipster.web.adminweb.ChipsterConfiguration;
import fi.csc.microarray.config.ConfigurationLoader.IllegalConfigurationException;
import fi.csc.microarray.exception.MicroarrayException;
import fi.csc.microarray.messaging.JMSMessagingEndpoint;
import fi.csc.microarray.messaging.MessagingEndpoint;
import fi.csc.microarray.messaging.MessagingTopic;
import fi.csc.microarray.messaging.MessagingTopic.AccessMode;
import fi.csc.microarray.messaging.NodeBase;
import fi.csc.microarray.messaging.TempTopicMessagingListenerBase;
import fi.csc.microarray.messaging.Topics;
import fi.csc.microarray.messaging.message.ChipsterMessage;
import fi.csc.microarray.messaging.message.CommandMessage;
import fi.csc.microarray.messaging.message.ParameterMessage;

/**
 * This class uses JMS messages to send data queries and converts result messages to
 * Java objects. The methods wait for the results, turning asynchronous messages to 
 * blocking method calls.
 * 
 * @author klemela
 */
public class StorageAdminAPI {
	
	private static final Logger logger = Logger.getLogger(StorageAdminAPI.class);

	public interface StorageEntryListener {
		public void process(List<StorageEntry> entries);
	}

	NodeBase nodeSupport = new NodeBase() {
		public String getName() {
			return "admin";
		}
	};
	
	private static final long TIMEOUT = 30;
	private final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;

	private MessagingTopic filebrokerAdminTopic;

	private MessagingEndpoint messagingEndpoint;

	public StorageAdminAPI() throws IOException, IllegalConfigurationException, MicroarrayException, JMSException {

		ChipsterConfiguration.init();
		messagingEndpoint = new JMSMessagingEndpoint(nodeSupport);
		filebrokerAdminTopic = messagingEndpoint.createTopic(Topics.Name.FILEBROKER_ADMIN_TOPIC, AccessMode.WRITE);
	}

	public List<StorageEntry> listStorageUsageOfSessions(String username) throws JMSException, InterruptedException {
		
		StorageEntryMessageListener listener = new StorageEntryMessageListener();
		return listener.query(username);
	}

	public class StorageEntryMessageListener extends TempTopicMessagingListenerBase {

		private List<StorageEntry> entries;
		private CountDownLatch latch;
		
		public List<StorageEntry> query(String username) throws JMSException, InterruptedException {
			
			latch = new CountDownLatch(1);
			
			CommandMessage request = new CommandMessage(CommandMessage.COMMAND_LIST_STORAGE_USAGE_OF_SESSIONS);
			request.addNamedParameter("username", username);

			filebrokerAdminTopic.sendReplyableMessage(request, this);			
			latch.await(TIMEOUT, TIMEOUT_UNIT);
			
			return entries;
		}

		public void onChipsterMessage(ChipsterMessage msg) {
			ParameterMessage resultMessage = (ParameterMessage) msg;

			String usernamesString =  resultMessage.getNamedParameter(ParameterMessage.PARAMETER_USERNAME_LIST);
			String namesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_SESSION_NAME_LIST);
			String sizesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_SIZE_LIST);
			String datesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_DATE_LIST);

			String[] usernames = usernamesString.split("\t");
			String[] names = namesString.split("\t");
			String[] sizes = sizesString.split("\t");
			String[] dates = datesString.split("\t");

			
			DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
			entries = new LinkedList<StorageEntry>();
			for (int i = 0; i < names.length; i++) {

				StorageEntry entry = new StorageEntry();
				entry.setDate(dateTimeFormatter.parseDateTime(dates[i]).toDate());
				entry.setUsername(usernames[i]);
				entry.setSize(Long.parseLong(sizes[i]));
				entry.setName(names[i]);
				entries.add(entry);
			}

			latch.countDown();
		}
	}	
	
	public List<StorageAggregate> listStorageUsageOfUsers() throws JMSException, InterruptedException {
		
		StorageAggregateMessageListener listener = new StorageAggregateMessageListener();
		return listener.query();
	}


	public class StorageAggregateMessageListener extends TempTopicMessagingListenerBase {

		private CountDownLatch latch;
		private List<StorageAggregate> entries;

		public List<StorageAggregate> query() throws JMSException, InterruptedException {

			latch = new CountDownLatch(1);<commit_msg>Organize imports<commit_after>package fi.csc.chipster.web.adminweb.data;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.jms.JMSException;

import org.apache.log4j.Logger;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;

import fi.csc.chipster.web.adminweb.ChipsterConfiguration;
import fi.csc.microarray.config.ConfigurationLoader.IllegalConfigurationException;
import fi.csc.microarray.exception.MicroarrayException;
import fi.csc.microarray.messaging.JMSMessagingEndpoint;
import fi.csc.microarray.messaging.MessagingEndpoint;
import fi.csc.microarray.messaging.MessagingTopic;
import fi.csc.microarray.messaging.MessagingTopic.AccessMode;
import fi.csc.microarray.messaging.NodeBase;
import fi.csc.microarray.messaging.TempTopicMessagingListenerBase;
import fi.csc.microarray.messaging.Topics;
import fi.csc.microarray.messaging.message.ChipsterMessage;
import fi.csc.microarray.messaging.message.CommandMessage;
import fi.csc.microarray.messaging.message.ParameterMessage;

/**
 * This class uses JMS messages to send data queries and converts result messages to
 * Java objects. The methods wait for the results, turning asynchronous messages to 
 * blocking method calls.
 * 
 * @author klemela
 */
public class StorageAdminAPI {
	
	private static final Logger logger = Logger.getLogger(StorageAdminAPI.class);

	public interface StorageEntryListener {
		public void process(List<StorageEntry> entries);
	}

	NodeBase nodeSupport = new NodeBase() {
		public String getName() {
			return "admin";
		}
	};
	
	private static final long TIMEOUT = 30;
	private final TimeUnit TIMEOUT_UNIT = TimeUnit.SECONDS;

	private MessagingTopic filebrokerAdminTopic;

	private MessagingEndpoint messagingEndpoint;

	public StorageAdminAPI() throws IOException, IllegalConfigurationException, MicroarrayException, JMSException {

		ChipsterConfiguration.init();
		messagingEndpoint = new JMSMessagingEndpoint(nodeSupport);
		filebrokerAdminTopic = messagingEndpoint.createTopic(Topics.Name.FILEBROKER_ADMIN_TOPIC, AccessMode.WRITE);
	}

	public List<StorageEntry> listStorageUsageOfSessions(String username) throws JMSException, InterruptedException {
		
		StorageEntryMessageListener listener = new StorageEntryMessageListener();
		return listener.query(username);
	}

	public List<StorageAggregate> listStorageUsageOfUsers() throws JMSException, InterruptedException {
		
		StorageAggregateMessageListener listener = new StorageAggregateMessageListener();
		return listener.query();
	}
	
	private class StorageEntryMessageListener extends TempTopicMessagingListenerBase {

		private List<StorageEntry> entries;
		private CountDownLatch latch;
		
		public List<StorageEntry> query(String username) throws JMSException, InterruptedException {
			
			latch = new CountDownLatch(1);
			
			CommandMessage request = new CommandMessage(CommandMessage.COMMAND_LIST_STORAGE_USAGE_OF_SESSIONS);
			request.addNamedParameter("username", username);

			filebrokerAdminTopic.sendReplyableMessage(request, this);			
			latch.await(TIMEOUT, TIMEOUT_UNIT);
			
			return entries;
		}

		public void onChipsterMessage(ChipsterMessage msg) {
			ParameterMessage resultMessage = (ParameterMessage) msg;

			String usernamesString =  resultMessage.getNamedParameter(ParameterMessage.PARAMETER_USERNAME_LIST);
			String namesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_SESSION_NAME_LIST);
			String sizesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_SIZE_LIST);
			String datesString = resultMessage.getNamedParameter(ParameterMessage.PARAMETER_DATE_LIST);

			String[] usernames = usernamesString.split("\t");
			String[] names = namesString.split("\t");
			String[] sizes = sizesString.split("\t");
			String[] dates = datesString.split("\t");

			
			DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime();
			entries = new LinkedList<StorageEntry>();
			for (int i = 0; i < names.length; i++) {

				StorageEntry entry = new StorageEntry();
				entry.setDate(dateTimeFormatter.parseDateTime(dates[i]).toDate());
				entry.setUsername(usernames[i]);
				entry.setSize(Long.parseLong(sizes[i]));
				entry.setName(names[i]);
				entries.add(entry);
			}

			latch.countDown();
		}
	}	

	private class StorageAggregateMessageListener extends TempTopicMessagingListenerBase {

		private CountDownLatch latch;
		private List<StorageAggregate> entries;

		public List<StorageAggregate> query() throws JMSException, InterruptedException {

			latch = new CountDownLatch(1);
{'fraction_non_alphanumeric': 0.08778989727840729, 'fraction_numerical': 0.0012707825902785132, 'mean_word_length': 5.990377498149519, 'pattern_counts': {'":': 0, '<': 21, '<?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': '3319741', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2636, 'n_words': 538}