package generate_merge;





import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileReader;

import java.io.FileWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.HashMap;

import java.util.Map;

import java.util.Properties;

import java.util.Random;

import java.util.UUID;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;





import com.dao.AvailablePools;



public class LoadTestDB 

{

	



	private static final String dbClassName = "com.mysql.jdbc.Driver";

	private static final String dbClassRedshift = "com.amazon.redshift.jdbc4.Driver";

	private static String host = null;

	private static String port = null;

	//private static final String CONNECTION = "jdbc:mysql://192.168.99.100:3306/";

//	private static final String CONNECTION = "jdbc:mysql://10.0.0.119:3306/";  /*Tunneled MySQL */

//	private static final String CONNECTION_RS = "jdbc:redshift://10.0.0.239:5439/myntra_dw";

	private static final String USER = "ddp_ro";

	private static final String PASSWORD = "Admin@123!";

	

	private static final String RS_USER = "myntrapoc";

	private static final String RS_PASSWORD = "5KuLi523T11k82G";

	private static  Integer file_count = 0;

    private static SimpleDateFormat load_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	private static SimpleDateFormat load_date = new SimpleDateFormat("yyyyMMdd");

	

	private static Runtime runtime = Runtime.getRuntime();

	

	private static long startTimer() 

	{

        return System.currentTimeMillis();

    }

	

	private static long endTimer() 

	{

        return System.currentTimeMillis();

    }



	    

    private static void worker(Integer file_count) throws Exception{

	    try{

		    Properties p = new Properties();

		   	p.put("user", USER);

		   	p.put("password", PASSWORD);

		   	

		   	Properties p_rs = new Properties();

		    p_rs.put("user", RS_USER);

		    p_rs.put("password", RS_PASSWORD);

		    

		    String sql_adhoc_query = "select query_txt from adhoc_query.adhoc_query_log where query_id % 12="+file_count+" and start_time >= '2016-11-16 00:00:00' and substring(query_id,11) >5000000 and query_txt not like '%_table_%' order by start_time limit 2000";

		    //Connection conn= DriverManager.getConnection(CONNECTION, p);

		    Connection conn=null;

		    try

		    {

		    conn= AvailablePools.ADHOC_QUERY_POOL.getConnection();



		    }

		    catch(Exception e)

		    {

		    	e.printStackTrace();

		    }

		    Statement stmt = conn.createStatement();

		    

		    

		    ResultSet rs = stmt.executeQuery(sql_adhoc_query);

		    Connection conn_rs=null;

		    Statement  stmt_rs=null;

		    //Statement stmt_rs = null;

		    while(rs.next()){

			    String sql = rs.getString("query_txt");

			    long start_time,end_time,run_time;

			    start_time = startTimer();

			    try

			    {

				    end_time = endTimer();

				    run_time = (end_time - start_time)/1000 ;

				    System.out.println("Inside Try");

				    System.out.println(sql);



				    conn_rs = AvailablePools.REDSHIFT_QUERY_POOL.getConnection();

				    ResultSet result =conn_rs.createStatement().executeQuery(sql);

					start_time = startTimer();

				    end_time = endTimer();

				    end_time = endTimer();

				    run_time = (end_time - start_time)/1000 ;

			    }catch(Exception e){

			    	System.out.println("Thread is : "+file_count);

			    	System.out.println(e.toString());

			    	if(conn_rs != null){

			    		try{

			    			conn_rs.close();

			    		}catch(Exception connE){e.printStackTrace();}

			    	}

			    	e.printStackTrace();

			    }

	    

		    }  //End While

	 

	    }catch(Exception e) {

	    	System.out.println("Thread is : "+file_count);

	    	e.printStackTrace();

	    } //Outer Catch

   }