package com.reyun.check.thread;

import com.reyun.check.dao.HiveDAO;
import com.reyun.check.dao.WarningDAO;
import com.reyun.check.hdfs.HdfsPathSize;
import com.reyun.check.mail.MailUtils;
import com.reyun.check.phonemsg.HttpClientUtil;
import com.reyun.check.phonemsg.PhoneMsgInfo;
import com.reyun.check.util.ConfigLoader;
import com.reyun.check.util.DefaultConfigLoader;
import org.apache.log4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

public class ExecDownloadCheck implements Callable  {
    private static final Logger logger = Logger.getLogger(ExecDownloadCheck.class);

    private DefaultConfigLoader cfg;
    private String tablename;
    private String ds;
    public ExecDownloadCheck(String tablename,String ds) throws ClassNotFoundException
            ,NoSuchMethodException,InstantiationException,IllegalAccessException
            ,InvocationTargetException {
        this.cfg=new ConfigLoader().getConfigInfo();
        this.tablename=tablename;
        this.ds=ds;
    }

    public Boolean call()throws ClassNotFoundException
            ,NoSuchMethodException,InstantiationException,IllegalAccessException
            ,InvocationTargetException {
        logger.info(this.cfg.properties.getProperty(this.tablename));
        String path=this.cfg.properties.getProperty(this.tablename).split(",")[1].replace("s3://","");
        //Map appsmap=new HashMap<String,Long>();
        logger.info(path);
        Map appsmap=new HdfsPathSize().calc(path, this.ds);
        Map<String,StringBuilder> sbmapw=new HiveDAO().downloadCheck(appsmap,this.tablename,this.ds);
        //发送告警邮件
        String title =sbmapw.get("title").toString();
        String content =sbmapw.get("content").toString();
        logger.info(title);
        if(title.length()>0){//异常才发邮件
            MailUtils mailUtils = new MailUtils(new WarningDAO().cfg.properties);
            mailUtils.sendMailContentEvery(title,content);
            try {
                //短信通知
                new HttpClientUtil().sendMsg(title);
                //打电话报警
                new PhoneMsgInfo().sendMsg(title);
            }
            catch (Exception e1) {
                e1.printStackTrace();
            }
        }
        return true;
    }
}
