package com.intel.alex.Utils;

import java.io.*;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * Created by root on 3/2/16.
 */
public class HadoopConfUtil {
    private static final String[] confArray={"yarn.scheduler.maximum-allocation-mb","yarn.scheduler.minimum-allocation-mb",
            "yarn.resourcemanager.scheduler.class","yarn.nodemanager.resource.memory-mb",
            "yarn.nodemanager.resource.cpu-vcores","yarn.app.mapreduce.am.resource.mb",
            "mapreduce.map.memory.mb","mapreduce.reduce.memory.mb",
            "mapreduce.map.java.opts","mapreduce.reduce.java.opts","mapreduce.job.reduce.slowstart.completedmaps"};
    private static final String[] paramArray={"schedulermax","schedulermin","schedulerclass","containermemory","cpuvcores","mapreduceamresource","mapmemory",
            "reducememory","mapopts","reduceopts","slowstart"};
    private static final File yarnFile=new File("yarn-site.xml");
    private static final File mapFile=new File("/etc/hadoop/conf/mapred-site.xml");
    private static final File hadoopEnvFile=new File("/etc/hadoop/conf/hadoop-env.sh");
    private static final File hiveEnvFile=new File("/etc/hive/conf/hive-env.sh");

    public void getHadoopConfiguration(Map<String, Object> dataMap){
        org.apache.hadoop.conf.Configuration conf=new org.apache.hadoop.conf.Configuration(true);
        InputStream yarnIn=null;
        InputStream mapredIn=null;
        BufferedReader hadoopEnvReader=null;
        BufferedReader hiveEnvReader=null;
        String hadoopEnv=null,hiveEnv=null;
        try{
            //get configurations from yarn-site.xml, mapred-site.xml
            yarnIn=new FileInputStream(yarnFile);
            mapredIn=new FileInputStream(mapFile);

            conf.addResource(yarnIn);
            conf.addResource(mapredIn);

            for(int i=0;i<confArray.length;i++)
                if(i==2||i==4||i==10)
                    dataMap.put(paramArray[i],conf.get(confArray[i]));
                else if(i==8||i==9){
                    String[] res=conf.get(confArray[i]).split(" ");
                    for(String s:res){
                        if(s.contains("-Xmx")){
                            dataMap.put(paramArray[i],getOptsGval(s.substring(4)));
                        }
                    }
                }
                else{
                    dataMap.put(paramArray[i],getmbGval(conf.get(confArray[i])));

                }
            //get configurations from hadoop-env.sh, hive-env.sh
            hadoopEnvReader=new BufferedReader(new FileReader(hadoopEnvFile));
            hiveEnvReader=new BufferedReader(new FileReader(hiveEnvFile));
            String hadoopline,hiveline;
            while((hadoopline=hadoopEnvReader.readLine())!=null){
                if(hadoopline.contains("export YARN_OPTS=")){
                    hadoopEnv=hadoopline;
                    break;
                }

            }
            while((hiveline=hiveEnvReader.readLine())!=null){
                if(hiveline.contains("export HADOOP_CLIENT_OPTS=")){
                    hiveEnv=hiveline;
                    break;
                }

            }
            dataMap.put("yarnopts",getOptsGval(hadoopEnv.split(" ")[2].substring(4)));
            dataMap.put("hiveopts",getOptsGval(hiveEnv.split(" ")[2].substring(4)));
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        finally {
            try{
                yarnIn.close();
                mapredIn.close();
                hadoopEnvReader.close();
                hiveEnvReader.close();

            } catch (IOException e){
                e.printStackTrace();
            }
        }


    }
    private  String getOptsGval(String value){
        DecimalFormat df = new DecimalFormat("##.##");
        Double d=Double.parseDouble(value)/1024.0/1024.0/1024.0;
        return df.format(d)+"G";
    }
    private  String getmbGval(String value){
        DecimalFormat df = new DecimalFormat("##.##");
        Double d=Double.parseDouble(value)/1024.0;
        return df.format(d)+"G";
    }
}

@@ -2790,16 +2790,44 @@
      if(
+hadoopline.charAt(0)!='#'&& 
 hadoopli
@@ -2853,32 +2853,32 @@
  YARN_OPTS=%22))%7B%0A
-
                 
@@ -3044,16 +3044,42 @@
      if(
+hiveline.charAt(0)!='#'&& 
 hiveline
@@ -3269,43 +3269,34 @@
 val(
-hadoopEnv.split(%22 %22)%5B2%5D.substring(4
+getXmxVal(hadoopEnv,%22-Xmx%22
 )));
@@ -3347,41 +3347,32 @@
 val(
-hiveEnv.split(%22 %22)%5B2%5D.substring(4
+getXmxVal(hiveEnv,%22-Xmx%22
 )));
@@ -3804,16 +3804,369 @@
     %7D%0A%0A%0A
+    %7D%0A    private String getXmxVal(String line,String parttern)%7B%0A        String%5B%5D res=line.split(%22=%22);%0A        String vals=res%5B1%5D.substring(1,res%5B1%5D.length()-1);%0A        String%5B%5D param=vals.split(%22 %22);%0A        for(String s:param)%7B%0A            if(s.contains(parttern))%7B%0A                return s.substring(4);%0A            %7D%0A        %7D%0A        return null;%0A
     %7D%0A  

fix the env.sh files' parse bug