From 0063b7fade9d7071be466f956413eda8a3615d6c Mon Sep 17 00:00:00 2001 From: kqian5 Date: Fri, 8 Jun 2018 13:55:54 -0400 Subject: [PATCH 1/5] Documentation ofr MonitoringRunner Class --- src/main/java/monitor/MonitoringRunner.java | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/monitor/MonitoringRunner.java b/src/main/java/monitor/MonitoringRunner.java index 1f67028..92532dc 100644 --- a/src/main/java/monitor/MonitoringRunner.java +++ b/src/main/java/monitor/MonitoringRunner.java @@ -11,16 +11,30 @@ import java.util.List; /** - * Created by ahmetkucuk on 3/8/18. + * Class that acts as an embeddable HTTP server + * that can return a list of JobRecord objects from + * the database formatted in json + * @author - ahmetkucuk + * @author - kqian5 */ public class MonitoringRunner extends NanoHTTPD{ + /** + * Constructor for a MonitoringRunner object + * Starts the server to port 1278 and waits for a maximum + * of 5000 milliseconds for socket to read + * @throws IOException - thrown if error starting server + */ public MonitoringRunner() throws IOException { super(1278); start(NanoHTTPD.SOCKET_READ_TIMEOUT, false); System.out.println("\nRunning! Point your browsers to http://localhost:1278/ \n"); } + /** + * Method that constructs a new MonitoringRunner object + * and catches an exception if server fails to start + */ public static void startServer() { try { new MonitoringRunner(); @@ -29,6 +43,11 @@ public static void startServer() { } } + /** + * Method that looks up a list of JobRecord objects in the DB + * for a specific date and formats an appropriate response in + * json + */ @Override public Response serve(IHTTPSession session) { DateTime fromDate = DateTime.now().minusDays(10); From 631fd288296b171d33a57051d471128017de8c7e Mon Sep 17 00:00:00 2001 From: kqian5 Date: Thu, 21 Jun 2018 10:27:28 -0400 Subject: [PATCH 2/5] revised monitor doc --- src/main/java/monitor/MonitoringRunner.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/monitor/MonitoringRunner.java b/src/main/java/monitor/MonitoringRunner.java index 92532dc..d8761ba 100644 --- a/src/main/java/monitor/MonitoringRunner.java +++ b/src/main/java/monitor/MonitoringRunner.java @@ -45,8 +45,8 @@ public static void startServer() { /** * Method that looks up a list of JobRecord objects in the DB - * for a specific date and formats an appropriate response in - * json + * for a specific date(10 days prior to request time) and + * formats an appropriate response in json */ @Override public Response serve(IHTTPSession session) { From a5c67f67522732d7e06b901c686f99564e888b66 Mon Sep 17 00:00:00 2001 From: kqian5 Date: Tue, 26 Jun 2018 16:57:25 -0400 Subject: [PATCH 3/5] documentation for DBPrefs --- src/main/java/common/db/DBPrefs.java | 36 +++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/common/db/DBPrefs.java b/src/main/java/common/db/DBPrefs.java index b06cea3..3e07629 100644 --- a/src/main/java/common/db/DBPrefs.java +++ b/src/main/java/common/db/DBPrefs.java @@ -13,7 +13,9 @@ import java.util.concurrent.TimeoutException; /** - * Created by ahmetkucuk on 28/12/15. + * Class that sets up database and rabbitmq configurations and connections + * @author - ahmetkucuk + * @author - kqian5 */ public class DBPrefs { @@ -28,6 +30,10 @@ public class DBPrefs { public static final int DB_PORT = 5432; + /** + * Sets the POSTGRE database configurations in a BasicDataSource object and returns it + * @return - the BasicDataSource object to return + */ public static BasicDataSource getDataSource() { Map env = System.getenv(); @@ -48,6 +54,11 @@ public static BasicDataSource getDataSource() { return dbPoolSourc; } + /** + * Method that creates a map of all the necessary + * configuration settings for a postgres db + * @return - map of setting to its value + */ public static Map getTestEnv(){ Map env = new HashMap<>(); env.put("POSTGRES_USER", "postgres"); @@ -57,6 +68,9 @@ public static Map getTestEnv(){ return env; } + /** + * Method that pauses the thread for 2000 milliseconds + */ public static void pause() { try { Thread.sleep(2000); @@ -65,6 +79,11 @@ public static void pause() { } } + /** + * Method that attempts to connect to database + * until a connection is successful and checks connection + * by perform sql command "SELECT 1" + */ public static void waitUntilConnected() { boolean connected = false; @@ -98,6 +117,13 @@ public static void waitUntilConnected() { } } + /** + * Method that attempts to connect to rabbitmq until a + * connection is successful, pausing for 2000 milliseconds + * between request + * @param host - the host to connect to + * @param port - the port to connect to + */ public static void waitUntilRabbitMqReady(String host, int port) { System.out.println("Sending Task"); ConnectionFactory factory = new ConnectionFactory(); @@ -120,6 +146,11 @@ public static void waitUntilRabbitMqReady(String host, int port) { } + /** + * Method that sets up configurations for the connection + * to the Image database + * @return + */ public static BasicDataSource getImageDataSource() { Map env = System.getenv(); @@ -139,6 +170,9 @@ public static BasicDataSource getImageDataSource() { return dbPoolSourc; } + /** + * Method that waits until the database and rabbitmq are connected to + */ public static void waitDefaultDBConnections() { DBPrefs.waitUntilConnected(); DBPrefs.waitUntilRabbitMqReady(DEFAULT_RABBIT_MQ_HOST, DEFAULT_RABBIT_MQ_PORT); From ed3c13a7b59928eb9a64a61f7aa1c40ebbf36855 Mon Sep 17 00:00:00 2001 From: kqian5 Date: Tue, 26 Jun 2018 17:00:33 -0400 Subject: [PATCH 4/5] DBPrefs documentation --- src/main/java/common/db/DBPrefs.java | 300 +++++++++++++-------------- 1 file changed, 148 insertions(+), 152 deletions(-) diff --git a/src/main/java/common/db/DBPrefs.java b/src/main/java/common/db/DBPrefs.java index 3e07629..eeab90a 100644 --- a/src/main/java/common/db/DBPrefs.java +++ b/src/main/java/common/db/DBPrefs.java @@ -19,164 +19,160 @@ */ public class DBPrefs { -// public static final String DB_HOST = "postgres-exposed"; -// public static final String DB_NAME = "isd"; -// public static String DB_USERNAME = "postgres"; -// public static String DB_USER_PASSWORD = "r3mot3p8sswo4d"; + final static String DEFAULT_RABBIT_MQ_HOST = "rabbitmq"; + final static int DEFAULT_RABBIT_MQ_PORT = 5672; - final static String DEFAULT_RABBIT_MQ_HOST = "rabbitmq"; - final static int DEFAULT_RABBIT_MQ_PORT = 5672; + public static final int DB_PORT = 5432; - public static final int DB_PORT = 5432; + /** + * Sets the POSTGRE database configurations in a BasicDataSource object and + * returns it + * @return - the BasicDataSource object to return + */ + public static BasicDataSource getDataSource() { + Map env = System.getenv(); - /** - * Sets the POSTGRE database configurations in a BasicDataSource object and returns it - * @return - the BasicDataSource object to return - */ - public static BasicDataSource getDataSource() { - - Map env = System.getenv(); - - BasicDataSource dbPoolSourc = new BasicDataSource(); + BasicDataSource dbPoolSourc = new BasicDataSource(); + dbPoolSourc.setSoftMinEvictableIdleTimeMillis(6500); + dbPoolSourc.setDefaultAutoCommit(true); + dbPoolSourc.setPoolPreparedStatements(false); + dbPoolSourc.setDefaultQueryTimeout(60); + dbPoolSourc.setMinIdle(1); + dbPoolSourc.setMaxIdle(10); + dbPoolSourc.setMaxTotal(100); + dbPoolSourc.setUsername(env.get("POSTGRES_USER")); + dbPoolSourc.setPassword(env.get("POSTGRES_PASSWORD")); + dbPoolSourc.setValidationQuery("SELECT 1;"); + dbPoolSourc.setDriverClassName("org.postgresql.Driver"); + dbPoolSourc.setUrl("jdbc:postgresql://" + env.get("POSTGRES_DB_HOST") + "/" + env.get("POSTGRES_DB")); + return dbPoolSourc; + } + + /** + * Method that creates a map of all the necessary configuration settings for a + * postgres db + * + * @return - map of setting to its value + */ + public static Map getTestEnv() { + Map env = new HashMap<>(); + env.put("POSTGRES_USER", "postgres"); + env.put("POSTGRES_PASSWORD", ""); + env.put("POSTGRES_DB", "postgres"); + env.put("POSTGRES_DB_HOST", "localhost"); + return env; + } + + /** + * Method that pauses the thread for 2000 milliseconds + */ + public static void pause() { + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + /** + * Method that attempts to connect to database until a connection is successful + * and checks connection by perform sql command "SELECT 1" + */ + public static void waitUntilConnected() { + + boolean connected = false; + Connection ex = null; + while (!connected) { + try { + DataSource dsourc = getDataSource(); + try { + ex = dsourc.getConnection(); + ex.setAutoCommit(true); + Statement ex1 = ex.createStatement(); + connected = ex1.execute("SELECT 1;"); + } catch (SQLException var7) { + System.out.println("Failed to Connect. Will retry"); + } finally { + if (ex != null) { + ex.close(); + } + + } + } catch (SQLException var9) { + System.out.println("Connection error 1"); + } + pause(); + System.out.println("Waiting for DB Connection"); + } + try { + ex.close(); + } catch (SQLException e) { + System.out.println("Connection error 2"); + } + } + + /** + * Method that attempts to connect to rabbitmq until a connection is successful, + * pausing for 2000 milliseconds between request + * + * @param host + * - the host to connect to + * @param port + * - the port to connect to + */ + public static void waitUntilRabbitMqReady(String host, int port) { + System.out.println("Sending Task"); + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost(host); + factory.setPort(port); + + boolean connected = false; + while (!connected) { + try { + com.rabbitmq.client.Connection e = factory.newConnection(); + e.close(); + connected = true; + } catch (TimeoutException | IOException var5) { + var5.printStackTrace(); + } + + pause(); + System.out.println("Waiting for Rabbit MQ Connection"); + } + + } + + /** + * Method that sets up configurations for the connection to the Image database + * and returns a BasicDataSource object with these configurations + * @return - the BasicDataSource object + */ + public static BasicDataSource getImageDataSource() { + + Map env = System.getenv(); + + BasicDataSource dbPoolSourc = new BasicDataSource(); dbPoolSourc.setSoftMinEvictableIdleTimeMillis(6500); dbPoolSourc.setDefaultAutoCommit(true); dbPoolSourc.setPoolPreparedStatements(false); - dbPoolSourc.setDefaultQueryTimeout(60); - dbPoolSourc.setMinIdle(1); - dbPoolSourc.setMaxIdle(10); - dbPoolSourc.setMaxTotal(100); - dbPoolSourc.setUsername(env.get("POSTGRES_USER")); - dbPoolSourc.setPassword(env.get("POSTGRES_PASSWORD")); - dbPoolSourc.setValidationQuery("SELECT 1;"); - dbPoolSourc.setDriverClassName("org.postgresql.Driver"); - dbPoolSourc.setUrl("jdbc:postgresql://" + env.get("POSTGRES_DB_HOST") + "/" + env.get("POSTGRES_DB")); - return dbPoolSourc; - } - - /** - * Method that creates a map of all the necessary - * configuration settings for a postgres db - * @return - map of setting to its value - */ - public static Map getTestEnv(){ - Map env = new HashMap<>(); - env.put("POSTGRES_USER", "postgres"); - env.put("POSTGRES_PASSWORD", ""); - env.put("POSTGRES_DB", "postgres"); - env.put("POSTGRES_DB_HOST", "localhost"); - return env; - } - - /** - * Method that pauses the thread for 2000 milliseconds - */ - public static void pause() { - try { - Thread.sleep(2000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - /** - * Method that attempts to connect to database - * until a connection is successful and checks connection - * by perform sql command "SELECT 1" - */ - public static void waitUntilConnected() { - - boolean connected = false; - Connection ex = null; - while(!connected) { - try { - DataSource dsourc = getDataSource(); - try { - ex = dsourc.getConnection(); - ex.setAutoCommit(true); - Statement ex1 = ex.createStatement(); - connected = ex1.execute("SELECT 1;"); - } catch (SQLException var7) { - System.out.println("Failed to Connect. Will retry"); - } finally { - if(ex != null) { - ex.close(); - } - - } - } catch (SQLException var9) { - System.out.println("Connection error 1"); - } - pause(); - System.out.println("Waiting for DB Connection"); - } - try { - ex.close(); - } catch (SQLException e) { - System.out.println("Connection error 2"); - } - } - - /** - * Method that attempts to connect to rabbitmq until a - * connection is successful, pausing for 2000 milliseconds - * between request - * @param host - the host to connect to - * @param port - the port to connect to - */ - public static void waitUntilRabbitMqReady(String host, int port) { - System.out.println("Sending Task"); - ConnectionFactory factory = new ConnectionFactory(); - factory.setHost(host); - factory.setPort(port); - - boolean connected = false; - while(!connected) { - try { - com.rabbitmq.client.Connection e = factory.newConnection(); - e.close(); - connected = true; - } catch (TimeoutException | IOException var5) { - var5.printStackTrace(); - } - - pause(); - System.out.println("Waiting for Rabbit MQ Connection"); - } - - } - - /** - * Method that sets up configurations for the connection - * to the Image database - * @return - */ - public static BasicDataSource getImageDataSource() { - - Map env = System.getenv(); - - BasicDataSource dbPoolSourc = new BasicDataSource(); - dbPoolSourc.setSoftMinEvictableIdleTimeMillis(6500); - dbPoolSourc.setDefaultAutoCommit(true); - dbPoolSourc.setPoolPreparedStatements(false); - dbPoolSourc.setDefaultQueryTimeout(60); - dbPoolSourc.setMinIdle(2); - dbPoolSourc.setMaxIdle(10); - dbPoolSourc.setMaxTotal(100); - dbPoolSourc.setUsername(env.get("MYSQL_USER")); - dbPoolSourc.setPassword(env.get("MYSQL_PASSWORD")); - dbPoolSourc.setDriverClassName("com.mysql.jdbc.Driver"); - dbPoolSourc.setUrl("jdbc:mysql://" + env.get("MYSQL_DB_HOST") + "/" + env.get("MYSQL_DB")); - return dbPoolSourc; - } - - /** - * Method that waits until the database and rabbitmq are connected to - */ - public static void waitDefaultDBConnections() { - DBPrefs.waitUntilConnected(); - DBPrefs.waitUntilRabbitMqReady(DEFAULT_RABBIT_MQ_HOST, DEFAULT_RABBIT_MQ_PORT); - } + dbPoolSourc.setDefaultQueryTimeout(60); + dbPoolSourc.setMinIdle(2); + dbPoolSourc.setMaxIdle(10); + dbPoolSourc.setMaxTotal(100); + dbPoolSourc.setUsername(env.get("MYSQL_USER")); + dbPoolSourc.setPassword(env.get("MYSQL_PASSWORD")); + dbPoolSourc.setDriverClassName("com.mysql.jdbc.Driver"); + dbPoolSourc.setUrl("jdbc:mysql://" + env.get("MYSQL_DB_HOST") + "/" + env.get("MYSQL_DB")); + return dbPoolSourc; + } + + /** + * Method that waits until the database and rabbitmq are connected to + */ + public static void waitDefaultDBConnections() { + DBPrefs.waitUntilConnected(); + DBPrefs.waitUntilRabbitMqReady(DEFAULT_RABBIT_MQ_HOST, DEFAULT_RABBIT_MQ_PORT); + } } - From 9c9ef8076cfd01e9250a6695c900d3a0b9f672e5 Mon Sep 17 00:00:00 2001 From: kqian5 Date: Wed, 27 Jun 2018 20:56:47 -0400 Subject: [PATCH 5/5] revised dbprefs doc --- src/main/java/common/db/DBPrefs.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/main/java/common/db/DBPrefs.java b/src/main/java/common/db/DBPrefs.java index eeab90a..8111537 100644 --- a/src/main/java/common/db/DBPrefs.java +++ b/src/main/java/common/db/DBPrefs.java @@ -92,7 +92,7 @@ public static void waitUntilConnected() { Statement ex1 = ex.createStatement(); connected = ex1.execute("SELECT 1;"); } catch (SQLException var7) { - System.out.println("Failed to Connect. Will retry"); + System.out.println("Failed to Connect. Will retry in 2000 milliseconds."); } finally { if (ex != null) { ex.close(); @@ -100,26 +100,23 @@ public static void waitUntilConnected() { } } catch (SQLException var9) { - System.out.println("Connection error 1"); + System.out.println("Connection error 1. Will retry in 2000 milliseconds."); } pause(); - System.out.println("Waiting for DB Connection"); + System.out.println("Waiting for DB Connection. Will retry in 2000 milliseconds."); } try { ex.close(); } catch (SQLException e) { - System.out.println("Connection error 2"); + System.out.println("Error closing connection from data source."); } } /** * Method that attempts to connect to rabbitmq until a connection is successful, * pausing for 2000 milliseconds between request - * - * @param host - * - the host to connect to - * @param port - * - the port to connect to + * @param host - the host to connect to + * @param port - the port to connect to */ public static void waitUntilRabbitMqReady(String host, int port) { System.out.println("Sending Task"); @@ -138,7 +135,7 @@ public static void waitUntilRabbitMqReady(String host, int port) { } pause(); - System.out.println("Waiting for Rabbit MQ Connection"); + System.out.println("Waiting for Rabbit MQ Connection. Will retry in 2000 milliseconds."); } }