<commit_before>package org.ohmage.reporting;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.ohmage.domain.AuditInformation;
import org.ohmage.request.FailedRequest;
import org.ohmage.request.InputKeys;
import org.ohmage.request.RequestBuilder;
import org.ohmage.service.AuditServices;
import org.ohmage.service.ServiceException;

/**
 * Begins on server startup and creates a daily snapshot of some information
 * from the audit table.
 * 
 * @author <NAME>
 */
public final class AuditReporter {
	private static final Logger LOGGER = Logger.getLogger(AuditReporter.class);
	
	// The number of idle threads ready to generate the report. Two reports 
	// should never be generated at the same time, so we only need one thread
	// to handle this.
	private static final int THREAD_POOL_SIZE = 1;
	
	// The key to use to get the location of where to save the audit reports 
	// from the constructor's map.
	private static final String KEY_SAVE_LOCATION = "audit.location";
	
	// Formatter for creating the entries in the audit folder.
	private static final DateFormat AUDIT_FILE_OUTPUT_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	
	// Where we will save the audit reports.
	private static String saveLocation;
	
	/**
	 * The class that runs as its own thread to generate the report.
	 * 
	 * @author <NAME>
	 */
	private final class GenerateReport implements Runnable {
		@Override
		public void run() {
			// Generate a dummy, failed request in case something goes wrong in
			// the service.
			FailedRequest failedRequest = new FailedRequest();
			
			// Generate the start date.
			Calendar startDate = Calendar.getInstance();
			startDate.add(Calendar.DAY_OF_YEAR, -1);
			startDate.set(Calendar.HOUR_OF_DAY, 0);
			startDate.set(Calendar.MINUTE, 0);
			startDate.set(Calendar.SECOND, 0);
			startDate.set(Calendar.MILLISECOND, 0);
			
			// Generate the end date.
			Calendar endDate = Calendar.getInstance();
			endDate.set(Calendar.HOUR_OF_DAY, 0);
			endDate.set(Calendar.MINUTE, 0);
			endDate.set(Calendar.SECOND, 0);
			endDate.set(Calendar.MILLISECOND, 0);
			
			try {
				Thread.sleep(2000);
			}
			catch(InterruptedException e) {
				LOGGER.debug("Interrupted.", e);
			}
			
			// Use the service to aggregate the results.
			List<AuditInformation> audits;
			try {
				audits = AuditServices.getAuditInformation(
						failedRequest, 
						null, 
						null, <commit_msg>Cleanup and removal of debugging code.<commit_after>package org.ohmage.reporting;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.ohmage.domain.AuditInformation;
import org.ohmage.request.FailedRequest;
import org.ohmage.request.InputKeys;
import org.ohmage.request.RequestBuilder;
import org.ohmage.service.AuditServices;
import org.ohmage.service.ServiceException;

/**
 * Begins on server startup and creates a daily snapshot of some information
 * from the audit table.
 * 
 * @author <NAME>
 */
public final class AuditReporter {
	private static final Logger LOGGER = Logger.getLogger(AuditReporter.class);
	
	// The number of idle threads ready to generate the report. Two reports 
	// should never be generated at the same time, so we only need one thread
	// to handle this.
	private static final int THREAD_POOL_SIZE = 1;
	
	// The key to use to get the location of where to save the audit reports 
	// from the constructor's map.
	private static final String KEY_SAVE_LOCATION = "audit.location";
	
	// Formatter for creating the entries in the audit folder.
	private static final DateFormat AUDIT_FILE_OUTPUT_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	
	// Where we will save the audit reports.
	private static String saveLocation;
	
	/**
	 * The class that runs as its own thread to generate the report.
	 * 
	 * @author <NAME>
	 */
	private final class GenerateReport implements Runnable {
		@Override
		public void run() {
			// Generate a dummy, failed request in case something goes wrong in
			// the service.
			FailedRequest failedRequest = new FailedRequest();
			
			// Generate the start date.
			Calendar startDate = Calendar.getInstance();
			startDate.add(Calendar.DAY_OF_YEAR, -1);
			startDate.set(Calendar.HOUR_OF_DAY, 0);
			startDate.set(Calendar.MINUTE, 0);
			startDate.set(Calendar.SECOND, 0);
			startDate.set(Calendar.MILLISECOND, 0);
			
			// Generate the end date.
			Calendar endDate = Calendar.getInstance();
			endDate.set(Calendar.HOUR_OF_DAY, 0);
			endDate.set(Calendar.MINUTE, 0);
			endDate.set(Calendar.SECOND, 0);
			endDate.set(Calendar.MILLISECOND, 0);
			
			// Use the service to aggregate the results.
			List<AuditInformation> audits;
			try {
				audits = AuditServices.getAuditInformation(
						failedRequest, 
						null, 
						null, 
{'fraction_non_alphanumeric': 0.09318673395818312, 'fraction_numerical': 0.004686373467916366, 'mean_word_length': 3.8846830985915495, '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': '6976614', 'n_tokens_mistral': 1868, 'n_tokens_neox': 1641, 'n_words': 509}