package com.novel.clean;
import java.io.IOException;
import java.util.Map;

import com.novel.clean.util.CleanUtil;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class CleanApi extends Mapper<LongWritable, Text, Text, NullWritable>{
    private Text line = new Text();
    private CleanUtil cleanUtil=new CleanUtil();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] list = value.toString().split("] \\[");
        String logversion = "-999";
        String projectname = "-999";
        String serverip = "-999"; //服务器ip
        String reqbegintime = "-999"; //请求时间
        String reqendtime = "-999";; //响应时间
        String reqspendtime = "-999"; //时间差
        String reqip = "-999"; //客户端ip
        String requrlstr = "-999";//请求地址
        String refer = "-999";
        String method = "-999";;
        String httpcode = "-999";
        String userid = "-999";
        String sessionid = "-999";
        String platformid = "-999";
        String appversion = "-999";
        String channelid = "-999";
        String bookrecommend= "-999";
        String payType = "-999";
        String paynum = "-999";
        String rechargetype = "-999";
        String payamount = "-999";
        String ssid = "-999";
        String isnewuser = "-999";
        String ischannelnewuser = "-999";
        String readwordstype = "-999";
        String readwords = "-999";
        String mobile = "-999";
        String uaplatform = "-999";
        String uastat = "-999";
        String apicode = "-999";
        String xClient = "-999";
        String ua = "-999";
        String reqParam = "-999";
        String processParam = "-999";
        try {
            logversion = list[0].split("\\[")[1];
            projectname = list[1];
            serverip = list[2]; //服务器ip
            reqbegintime = list[3]; //请求时间
            reqendtime = list[4]; //响应时间
            reqspendtime = list[5]; //时间差
            reqip = list[6]; //客户端ip
            requrlstr = list[7];//请求地址
            refer = list[8];
            method = list[9];
            httpcode = list[10];
            userid = list[11];
            sessionid = list[12];
            platformid = list[13];
            appversion = list[14];
            channelid = list[15];
            bookrecommend= list[16];
            payType = list[17];
            paynum = list[18];
            rechargetype = list[19];
            payamount = list[20];
            ssid = list[21];
            isnewuser = list[22];
            ischannelnewuser = list[23];
            readwordstype = list[24];
            readwords = list[25];
            mobile = list[26];
            uaplatform = list[27];
            uastat = list[28];
            apicode = list[29];
            xClient = list[30];
            ua = list[31];
            reqParam = list[32];
            processParam = list[33];
        }catch (Exception e){
        }
        //过滤下载日志
        if((requrlstr.contains("/api/download/")) || (requrlstr.contains("/api/other/jrttBack")) || (requrlstr.contains("/kuaishou/notify")) || (requrlstr.contains("/api/getPublicKey")) || ua.contains("JianKongBao") )
            return;
        else if(requrlstr.contains("type=bbs")){
            if (platformid == "1")
                platformid="100";
            else if (platformid == "2")
                platformid="200";
        }
        //解析国家和地区
        Map<String, String> ips = cleanUtil.getIp(reqip);
        String country = ips.get("country");
        String detail_area = ips.get("detail_area");
        String province=ips.get("province");
        String city=ips.get("city");
        //解析action和action_detail
        Map<String, String> actions = cleanUtil.getAction(requrlstr);
        String action=actions.get("action");
        String action_detail=actions.get("action_detail");
        //解析bookid和chapternum
        Map<String, String> book=cleanUtil.getBook(reqParam);
        String bookid=book.get("bookId");
        String chapternum=book.get("chapterNum");
        //暂时用不到的三个属性
        String referurl="-999";
        String refer_com="-999";
        String referaction="-999";
        //切分Xclient取出相应的数据
        String screensize="-999";
        String type="-999";
        String imsi="-999";
        String version ="-999";
        try {
            screensize = xClient.split("ss=")[1].split(";")[0];
            type = xClient.split("pm=")[1].split(";")[0];
            imsi = xClient.split("imsi=")[1].split(";")[0];
            version = xClient.split("version=")[1].split(";")[0];
        }catch (Exception e){
        }

        String appversion2="-999";
        if (version !=null && version.indexOf(".")>0 ){
            int first_index = version.indexOf('.') + 1;
            int last_index = version.lastIndexOf('.');
            appversion2 = version.substring(first_index,last_index);
        }
        String mac ="-999";
        String device_id ="-999";
        String imei ="-999";
        String idfa ="-999";
        try {
            mac = xClient.split("mac=")[1].split(";")[0];
            device_id = xClient.split("dID=")[1].split(";")[0];
            imei = xClient.split("imei=")[1].split(";")[0];
            idfa = xClient.split("idfa=")[1].split(";")[0];
        }catch (Exception e){
        }

        //暂时用不到的几个属性
        String endbrand = "-999";
        String browserbrand = "-999";
        String browserversion = "-999";
        String primaryurlpath = "-999";
        String others = "-999";
        String cellid = "-999";
        String projectType="-999";
        //解析操作系统版本
        String os_version="-999";
        String platform="api";
        try {
            os_version = ua.split(";")[1].split(" ")[2];
            platform=ua.split(";")[1].split(" ")[1];
        }catch (Exception e){
        }

        //日志时间
        String day=reqbegintime.split(" ")[0];
        line.set(logversion+"\t"+projectname+"\t"+serverip+"\t"+reqbegintime+"\t"+reqendtime+"\t"+reqspendtime+"\t"+reqip+"\t"+country
        +"\t"+detail_area+"\t"+province+"\t"+city+"\t"+requrlstr+"\t"+action+"\t"+action_detail+"\t"+bookid+"\t"+chapternum+"\t"+referurl+"\t"+refer_com+"\t"+
        referaction+"\t"+method+"\t"+httpcode+"\t"+userid+"\t"+sessionid+"\t"+platformid+"\t"+appversion+"\t"+channelid+"\t"+
        bookrecommend+"\t"+payType+"\t"+paynum+"\t"+rechargetype+"\t"+payamount+"\t"+ssid+"\t"+isnewuser+"\t"+ischannelnewuser+"\t"+
        readwordstype+"\t"+readwords+"\t"+mobile+"\t"+uaplatform+"\t"+uastat+"\t"+apicode+"\t"+imei+"\t"+os_version+"\t"+screensize
        +"\t"+type+"\t"+imsi+"\t"+cellid+"\t"+version+"\t"+device_id+"\t"+mac+"\t"+endbrand+"\t"+browserbrand+"\t"+browserversion+"\t"+
        ua+"\t"+primaryurlpath+"\t"+reqParam+"\t"+others+"\t"+processParam+"\t"+projectType+"\t"+idfa+"\t"+appversion2+"\t"+platform+"\t"+day);
        context.write(line, NullWritable.get());
    }
}
