package com.analysis.code;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.URL;
import java.util.*;

/**
 * Created by Zephery on 2016/9/26.
 */
public class ThirdTest {
    public static boolean judgeURL(String line) throws Exception {         //判断来源的主域名
        String lineformat = java.net.URLDecoder.decode(line, "utf-8");
        URL url = new URL(lineformat);
        String host = url.getHost();
        //System.out.println(host);
        if (host != null) {
            if (!host.equals("api.app.happyjuzi.com") && !host.equals("m.happyjuzi.com") && !host.equals("a.happyjuzi.com")) {
                //System.out.println(host);
                return false;
            }
            if (lineformat.contains("&uid=")) {
                //System.out.println(lineformat);
                return false;
            }
        }
        return true;
    }
//
//    public static List<String> getFileList(File file) {
//        List<String> result = new ArrayList<String>();
//        if (!file.isDirectory()) {
//            System.out.println(file.getAbsolutePath());
//            result.add(file.getAbsolutePath());
//        } else {
//            File[] directoryList = file.listFiles(new FileFilter() {
//                public boolean accept(File file) {
//                    if (file.isFile() && file.getName().indexOf("log") > -1) {
//                        return true;
//                    } else {
//                        return false;
//                    }
//                }
//            });
//            for (int i = 0; i < directoryList.length; i++) {
//                result.add(directoryList[i].getPath());
//            }
//        }
//        return result;
//    }

    public static List<String> getFileList(String path, String pathtodate) {     //fu wu qi tiao shi
        List<String> pathlist = new ArrayList<>();
        Iterator it = pathlist.iterator();
        for (int i = 0; i <= 2; i++) {
            for (int j = 0; j <= 9; j++) {
                String hour = i + "" + j;
                pathlist.add(path + "/logstat-" + pathtodate + "-" + hour + ".log");
                if (Integer.parseInt(hour) > 22) {
                    break;
                }
            }
        }
        return pathlist;
    }

    public static void main(String args[]) {
        Long programstarttime = System.currentTimeMillis();
        try {
            int repeatecount = 0;

            String path = args[0];               //fuwuqi
            String pathsplit[] = path.split("/");
            String pathtodate = pathsplit[pathsplit.length - 1];
            List<String> pathlist = getFileList(path, pathtodate);
            String line = null;
//            String path = "src/log/2016-09-26";
//            String pathsplit[] = path.split("/");
//            String pathtodate = "2016-09-26";
//            File file = new File(path);
//            List<String> pathlist = getFileList(file);
//            String line = null;
            int count = 0;
            Map<String, List<Map<String, String>>> originmap = new HashMap<>();          //声明一个全局的map
            Map<String, Long> timemap = new HashMap<>();             //用来存储时间的map
            for (String l : pathlist) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                        new FileInputStream(l)));
                while ((line = bufferedReader.readLine()) != null) {
                    try {
                        String[] data = line.split("&");
                        Map<String, String> tojson = new HashMap<>();
                        //List<Map> list = new ArrayList<>();

                        String iphost = data[0].split("=")[1].split(",")[0];
                        String usi = data[1].split("=")[1];
//                List<Map> list = originmap.get(iphost);
                        for (String da : data) {
                            String source[] = da.split("=");
                            if (source.length < 2) {
                                tojson.put(source[0], null);
                            } else {
                                tojson.put(source[0], source[1].equals("null") ? null : source[1]);
                            }
                        }
                        tojson.put("ip", iphost);
                        tojson.put("comefrom", "");
                        if (!judgeURL(tojson.get("u"))) {
                            continue;
                        }
                        String key = "";
                        if (originmap.get(usi) != null) {
                            key = usi;
                        } else if (originmap.get(iphost) != null) {
                            key = iphost;
                        }
                        if (StringUtils.isNotBlank(key) && originmap.containsKey(key)) {
                            long starttime = Long.parseLong(timemap.get(key).toString().substring(0, 10));
                            long endtime = Long.parseLong(tojson.get("ipt").substring(0, 10));
                            //大于30秒的情况
                            if ((endtime - starttime) < 300) {
                                originmap.get(key).add(tojson);
                            } else {
                                //数据库处理
                                new JudgeFrom(pathtodate).judgeoneip(originmap.get(key));
                                repeatecount++;
                                List<Map<String, String>> list = new ArrayList<>();
                                list.add(tojson);
                                originmap.put(key, list);
                            }
                        } else {
                            //###
                            List<Map<String, String>> list = new ArrayList<>();
                            list.add(tojson);
                            originmap.put(iphost, list);
                        }

                        timemap.put(iphost, Long.parseLong(tojson.get("ipt")));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    //System.out.println(data[0].split("=")[1].split(",")[0]);

                    //只判断100条数据
                    count++;
//                    if (count > 1999) {
//                        break;
//                    }
                    System.out.println(count);
                }
            }

            System.out.println("end");
            JudgeFrom judgeFrom = new JudgeFrom(pathtodate);
            judgeFrom.judgefromwhere(originmap);

            System.out.println(repeatecount);
            //System.out.println("PV:" + (PV + originmap.size()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis() - programstarttime);
    }
}

test

05-08 08:45