<commit_before>/*
 * Copyright (C) 2002 EBI, GRL
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.ensembl.healthcheck;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ensembl.healthcheck.util.DBUtils;
import org.ensembl.healthcheck.util.LogFormatter;
import org.ensembl.healthcheck.util.MyStreamHandler;
import org.ensembl.healthcheck.util.Utils;

/**
 * Pull healthcheck results from a database and write to an HTML file.
 */
public class DatabaseToHTML {

	private boolean debug = false;

	private String PROPERTIES_FILE = "database.properties";

	private String outputDir = ".";

	private long sessionID = -1;

	protected static Logger logger = Logger.getLogger("HealthCheckLogger");

	/**
	 * Command-line entry point.
	 * 
	 * @param args
	 *          Command line args.
	 */
	public static void main(String[] args) {

		new DatabaseToHTML().run(args);

	}

	// ---------------------------------------------------------------------
	/**
	 * Main run method.
	 * 
	 * @param args
	 *          Command-line arguments.
	 */
	private void run(String[] args) {

		parseCommandLine(args);

		setupLogging();

		Utils.readPropertiesFileIntoSystem(PROPERTIES_FILE);

		parseProperties();

		Connection con = connectToOutputDatabase();

		sessionID = getSessionID(sessionID, con);

		printOutput(con);

	} // run

	// ---------------------------------------------------------------------

	private void parseCommandLine(String[] args) {

		for (int i = 0; i < args.length; i++) {

			if (args[i].equals("-h")) {

				printUsage();
				System.exit(0);

			} else if (args[i].equals("-output")) {

				i++;
				outputDir = args[i];
				logger.finest("Will write output to " + outputDir);

			} else if (args[i].equals("-session")) {

				i++;
				sessionID = Long.parseLong(args[i]);
				logger.finest("Will use session ID " + sessionID);

			} else if (args[i].equals("-debug")) {

				debug = true;

			}

		}

	} // parseCommandLine

	// -------------------------------------------------------------------------

	private void printUsage() {

		System.out.println("\nUsage: DatabaseToHTML {options} \n");
		System.out.println("Options:");
		System.out.println("  -output         Specify output directory. Default is current directory.");
		System.out.println("  -h              This message.");
		System.out.println("  -debug          Print debugging info");
		System.out.println();
		System.out.println("All other configuration information is read from the file database.properties. ");
		System.out.println("See the comments in that file for information on which options to set.");

	}

	// ---------------------------------------------------------------------

	private void setupLogging() {

		// stop parent logger getting the message
		logger.setUseParentHandlers(false);

		Handler myHandler = new MyStreamHandler(System.out, new LogFormatter());

		logger.addHandler(myHandler);
		logger.setLevel(Level.WARNING);

		if (debug) {

			logger.setLevel(Level.FINEST);

		}

	} // setupLogging

	// ---------------------------------------------------------------------

	private void parseProperties() {

	}

	// ---------------------------------------------------------------------

	private Connection connectToOutputDatabase() {

		Connection con = DBUtils.openConnection(System.getProperty("output.driver"), System.getProperty("output.databaseURL")
				+ System.getProperty("output.database"), System.getProperty("output.user"), System.getProperty("output.password"));

		logger.fine("Connecting to " + System.getProperty("output.databaseURL") + System.getProperty("output.database") + " as "
				+ System.getProperty("output.user") + " password " + System.getProperty("output.password"));

		return con;

	}

	// ---------------------------------------------------------------------
	/**
	 * Get the most recent session ID from the database, or use the one defined on
	 * the command line.
	 */
	private long getSessionID(long s, Connection con) {

		long sess = -1;

		String sql = "SELECT MAX(session_id) FROM session";

		if (s > 0) {

			sess = s;

		} else {

			try {

				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);

				if (rs.next()) {
					sess = rs.getLong(1);
					logger.finest("Maximum session ID from database: " + sess);
				}

			} catch (SQLException e) {

				System.err.println("Error executing:\n" + sql);
				e.printStackTrace();

			}

		}

		if (sess == 1) {
			logger.severe("Can't get session ID from command-line or database.");
		}

		return sess;

	}

	// ---------------------------------------------------------------------

	/**
	 * Print formatted output.
	 */
	private void printOutput(Connection con) {

		try {

			PrintWriter pwIntro = new PrintWriter(new FileOutputStream(outputDir + File.separator + "healthcheck_summary.html"));

			printIntroPage(pwIntro, con);

			// now loop over each species and print the detailed output
			String sql = "SELECT DISTINCT(species) FROM report ORDER BY species WHERE session_id=" + sessionID;

			try {

				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);

				while (rs.next()) {
					String species = rs.getString("species");

					PrintWriter pw = new PrintWriter(new FileOutputStream(outputDir + File.separator + species + ".html"));

					printHeader(pw, species, con);

					printNavigation(pw, species, con);

					// printExecutiveSummary(pw);

					printSummaryByDatabase(pw, species, con);

					printSummaryByTest(pw, species, con);

					printReportsByDatabase(pw, species, con);<commit_msg>SQL fix.<commit_after>/*
 * Copyright (C) 2002 EBI, GRL
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.ensembl.healthcheck;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.ensembl.healthcheck.util.DBUtils;
import org.ensembl.healthcheck.util.LogFormatter;
import org.ensembl.healthcheck.util.MyStreamHandler;
import org.ensembl.healthcheck.util.Utils;

/**
 * Pull healthcheck results from a database and write to an HTML file.
 */
public class DatabaseToHTML {

	private boolean debug = false;

	private String PROPERTIES_FILE = "database.properties";

	private String outputDir = ".";

	private long sessionID = -1;

	protected static Logger logger = Logger.getLogger("HealthCheckLogger");

	/**
	 * Command-line entry point.
	 * 
	 * @param args
	 *          Command line args.
	 */
	public static void main(String[] args) {

		new DatabaseToHTML().run(args);

	}

	// ---------------------------------------------------------------------
	/**
	 * Main run method.
	 * 
	 * @param args
	 *          Command-line arguments.
	 */
	private void run(String[] args) {

		parseCommandLine(args);

		setupLogging();

		Utils.readPropertiesFileIntoSystem(PROPERTIES_FILE);

		parseProperties();

		Connection con = connectToOutputDatabase();

		sessionID = getSessionID(sessionID, con);

		printOutput(con);

	} // run

	// ---------------------------------------------------------------------

	private void parseCommandLine(String[] args) {

		for (int i = 0; i < args.length; i++) {

			if (args[i].equals("-h")) {

				printUsage();
				System.exit(0);

			} else if (args[i].equals("-output")) {

				i++;
				outputDir = args[i];
				logger.finest("Will write output to " + outputDir);

			} else if (args[i].equals("-session")) {

				i++;
				sessionID = Long.parseLong(args[i]);
				logger.finest("Will use session ID " + sessionID);

			} else if (args[i].equals("-debug")) {

				debug = true;

			}

		}

	} // parseCommandLine

	// -------------------------------------------------------------------------

	private void printUsage() {

		System.out.println("\nUsage: DatabaseToHTML {options} \n");
		System.out.println("Options:");
		System.out.println("  -output         Specify output directory. Default is current directory.");
		System.out.println("  -h              This message.");
		System.out.println("  -debug          Print debugging info");
		System.out.println();
		System.out.println("All other configuration information is read from the file database.properties. ");
		System.out.println("See the comments in that file for information on which options to set.");

	}

	// ---------------------------------------------------------------------

	private void setupLogging() {

		// stop parent logger getting the message
		logger.setUseParentHandlers(false);

		Handler myHandler = new MyStreamHandler(System.out, new LogFormatter());

		logger.addHandler(myHandler);
		logger.setLevel(Level.WARNING);

		if (debug) {

			logger.setLevel(Level.FINEST);

		}

	} // setupLogging

	// ---------------------------------------------------------------------

	private void parseProperties() {

	}

	// ---------------------------------------------------------------------

	private Connection connectToOutputDatabase() {

		Connection con = DBUtils.openConnection(System.getProperty("output.driver"), System.getProperty("output.databaseURL")
				+ System.getProperty("output.database"), System.getProperty("output.user"), System.getProperty("output.password"));

		logger.fine("Connecting to " + System.getProperty("output.databaseURL") + System.getProperty("output.database") + " as "
				+ System.getProperty("output.user") + " password " + System.getProperty("output.password"));

		return con;

	}

	// ---------------------------------------------------------------------
	/**
	 * Get the most recent session ID from the database, or use the one defined on
	 * the command line.
	 */
	private long getSessionID(long s, Connection con) {

		long sess = -1;

		String sql = "SELECT MAX(session_id) FROM session";

		if (s > 0) {

			sess = s;

		} else {

			try {

				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);

				if (rs.next()) {
					sess = rs.getLong(1);
					logger.finest("Maximum session ID from database: " + sess);
				}

			} catch (SQLException e) {

				System.err.println("Error executing:\n" + sql);
				e.printStackTrace();

			}

		}

		if (sess == 1) {
			logger.severe("Can't get session ID from command-line or database.");
		}

		return sess;

	}

	// ---------------------------------------------------------------------

	/**
	 * Print formatted output.
	 */
	private void printOutput(Connection con) {

		try {

			PrintWriter pwIntro = new PrintWriter(new FileOutputStream(outputDir + File.separator + "healthcheck_summary.html"));

			printIntroPage(pwIntro, con);

			// now loop over each species and print the detailed output
			String sql = "SELECT DISTINCT(species) FROM report WHERE session_id=" + sessionID + " ORDER BY species ";

			try {

				Statement stmt = con.createStatement();
				ResultSet rs = stmt.executeQuery(sql);

				while (rs.next()) {
					String species = rs.getString("species");

					PrintWriter pw = new PrintWriter(new FileOutputStream(outputDir + File.separator + species + ".html"));

					printHeader(pw, species, con);

					printNavigation(pw, species, con);

					// printExecutiveSummary(pw);

					printSummaryByDatabase(pw, species, con);

					printSummaryByTest(pw, species, con);

					printReportsByDatabase(pw, species, con);
{'fraction_non_alphanumeric': 0.19663568630463168, 'fraction_numerical': 0.004147784007988325, 'mean_word_length': 2.440803382663848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5518981', 'n_tokens_mistral': 4022, 'n_tokens_neox': 3429, 'n_words': 1028}