admin
2021-04-07 35a29882f356542dd9c431714c64d277eb7cad40
src/test/java/com/hxh/spring/test/LogTest.java
@@ -1,16 +1,40 @@
package com.hxh.spring.test;
import com.google.gson.Gson;
import com.yeshi.buwan.util.StringUtil;
import com.yeshi.buwan.util.TimeUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
public class LogTest {
    private final static Logger logger = LoggerFactory.getLogger(LogTest.class);
    private static List<String> loadLog(String filePath) {
        List<String> list = new ArrayList<>();
        Scanner scanner = null;
        try {
            scanner = new Scanner(new FileInputStream(filePath));
            while (scanner.hasNextLine()) {
                String text = scanner.nextLine();
                if (!StringUtil.isNullOrEmpty(text)) {
                    list.add(text);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
        return list;
    }
    @Test
    public void testLog() throws FileNotFoundException {
@@ -70,9 +94,6 @@
        }
        int p = 0;
        for (Iterator<Integer> its = ordersMap.keySet().iterator(); its.hasNext(); ) {
            Integer key = its.next();
@@ -84,4 +105,340 @@
        }
    }
    @Test
    public void videoPlay() throws Exception {
        Set<String> sets = new HashSet<>();
        Gson gson = new Gson();
        Scanner scanner = new Scanner(new FileInputStream("C:\\Users\\Administrator\\Desktop\\日志\\布丸播放\\play_2021_03_23.log"));
        Map<String, Integer> countMap = new HashMap<>();
        List<String> videoList = new ArrayList<>();
        while (scanner.hasNextLine()) {
            String text = scanner.nextLine();
            UserVideoPlayLogInfo logInfo = gson.fromJson(text, UserVideoPlayLogInfo.class);
            if ("25".equalsIgnoreCase(logInfo.getResourceId()))
                sets.add(logInfo.getUtdId());
            if (countMap.get(logInfo.getUtdId()) == null) {
                countMap.put(logInfo.getUtdId(), 0);
            }
            if (logInfo.getUtdId() != null && logInfo.getUtdId().equalsIgnoreCase("X8UXEo0HT/0DAMYZJa0w9lWp")) {
                videoList.add(text);
            }
            countMap.put(logInfo.getUtdId(), countMap.get(logInfo.getUtdId()) + 1);
            System.out.println(logInfo);
        }
        System.out.println(sets.size());
        scanner.close();
        for (String st : videoList) {
            System.out.println(st);
        }
    }
    private void printAction(String utdId, List<UserActiveLogInfo> infoList) {
        StringBuilder builder = new StringBuilder();
        builder.append(utdId);
        builder.append("#");
        Date last = null;
        for (UserActiveLogInfo info : infoList) {
            Date thisDate = new Date(TimeUtil.convertGernalTime(info.getTime(), "yyyy-MM-dd HH:mm:ss.SSS"));
            builder.append(info.getType());
            if (last != null) {
                builder.append("(");
                builder.append((thisDate.getTime() - last.getTime()) + "");
                builder.append(")");
            }
            if ("videoDetail".equalsIgnoreCase(info.getType())) {
                builder.append(":" + info.getParams2());
            }
            if ("search".equalsIgnoreCase(info.getType())) {
                builder.append(":" + info.getParams1());
            }
            builder.append("-");
            last = thisDate;
        }
        System.out.println(builder.toString());
    }
    private String getAction(List<UserActiveLogInfo> infoList) {
        List<String> typeList = new ArrayList<>();
        for (UserActiveLogInfo info : infoList) {
            typeList.add(info.getType());
        }
        return org.yeshi.utils.StringUtil.concat(typeList, "-");
    }
    private String getPercent(int total, int count) {
        return new BigDecimal(count).multiply(new BigDecimal(100)).divide(new BigDecimal(total), 2, RoundingMode.FLOOR) + "%";
    }
    @Test
    public void userActive() throws Exception {
        Gson gson = new Gson();
        List<String> list = loadLog("C:\\Users\\Administrator\\Desktop\\日志\\布丸播放\\active_2021_03_31.log");
        Map<String, List<UserActiveLogInfo>> map = new HashMap<>();
        for (String st : list) {
            UserActiveLogInfo activeLogInfo = gson.fromJson(st, UserActiveLogInfo.class);
            if (activeLogInfo != null && !StringUtil.isNullOrEmpty(activeLogInfo.getUtdId())) {
                if (map.get(activeLogInfo.getUtdId()) == null) {
                    map.put(activeLogInfo.getUtdId(), new ArrayList<>());
                }
                map.get(activeLogInfo.getUtdId()).add(activeLogInfo);
            }
        }
        int recommendPlay = 0;
        int playCount = 0;
        int searchCount = 0;
        int searchPlayCount = 0;
        int noSearchAndPlay = 0;
        int searchNoPlay = 0;
        int noComeInCount = 0;
        int comeinAndNoAction = 0;
        for (Iterator<String> its = map.keySet().iterator(); its.hasNext(); ) {
            String key = its.next();
//            printAction(key, map.get(key));
            String actions = getAction(map.get(key));
            if (actions.contains("splash-home-videoDetail")) {
                recommendPlay++;
            }
            if (actions.contains("videoDetail")) {
                playCount++;
            }
            if (actions.contains("search")) {
                searchCount++;
            }
            if (actions.contains("search-videoDetail")) {
                searchPlayCount++;
            }
            //搜索未播放
            if (!actions.contains("search-videoDetail") && actions.contains("search")) {
                searchNoPlay++;
//                printAction(key, map.get(key));
            }
            if (!actions.contains("search") && !actions.contains("videoDetail")) {
                noSearchAndPlay++;
            }
            if (actions.contains("splash") && !actions.contains("home") && !actions.contains("search") && !actions.contains("videoDetail")) {
                noComeInCount++;
                printAction(key, map.get(key));
            }
            if (actions.contains("splash") && actions.contains("home") && !actions.contains("search") && !actions.contains("videoDetail")) {
                comeinAndNoAction++;
            }
        }
        System.out.println("活跃总数:" + map.size());
        System.out.println("播放总数:" + playCount + "比例为:" + getPercent(map.size(), playCount));
        System.out.println("推荐播放:" + recommendPlay + "比例为:" + getPercent(map.size(), recommendPlay));
        System.out.println("搜索总数:" + searchCount + "比例为:" + getPercent(map.size(), searchCount));
        System.out.println("搜索之后播放总数:" + searchPlayCount + "比例为:" + getPercent(map.size(), searchPlayCount));
        System.out.println("搜索之后未播放总数:" + searchNoPlay + "比例为:" + getPercent(map.size(), searchNoPlay));
        System.out.println("未搜索也未播放总数:" + noSearchAndPlay + "比例为:" + getPercent(map.size(), noSearchAndPlay));
        System.out.println("搜索满意度:" +getPercent(searchCount, searchPlayCount));
        System.out.println("未能成功进入首页总数:" + noComeInCount + "比例为:" + getPercent(map.size(), noComeInCount));
        System.out.println("进入首页后无动作的总数:" + comeinAndNoAction + "比例为:" + getPercent(map.size(), comeinAndNoAction));
    }
    class UserVideoPlayLogInfo {
        /**
         * resourceId : 25
         * detailSystemId : 44
         * device : e07f8f33-3c42-3d7f-99f0-c26e2b294117
         * utdId : YFYbx3x95gADACWMYFfQNm5Q
         * time : 2021-03-21 00:00:05.866
         * videoId : 7724416
         * method : getUserVideoDetail
         */
        private String resourceId;
        private String detailSystemId;
        private String device;
        private String utdId;
        private String time;
        private String videoId;
        private String method;
        public String getResourceId() {
            return resourceId;
        }
        public void setResourceId(String resourceId) {
            this.resourceId = resourceId;
        }
        public String getDetailSystemId() {
            return detailSystemId;
        }
        public void setDetailSystemId(String detailSystemId) {
            this.detailSystemId = detailSystemId;
        }
        public String getDevice() {
            return device;
        }
        public void setDevice(String device) {
            this.device = device;
        }
        public String getUtdId() {
            return utdId;
        }
        public void setUtdId(String utdId) {
            this.utdId = utdId;
        }
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time = time;
        }
        public String getVideoId() {
            return videoId;
        }
        public void setVideoId(String videoId) {
            this.videoId = videoId;
        }
        public String getMethod() {
            return method;
        }
        public void setMethod(String method) {
            this.method = method;
        }
    }
    class UserActiveLogInfo {
        /**
         * logName : userActive
         * type : videoDetail
         * device : aaad1d79-78d6-36a1-9bd1-83f489dec3ad
         * time : 2021-03-22 14:04:10.663
         * utdId : X++3kU3tfqIDAP+Ug24skyKv
         * params1 : 1206314
         * params2 : 包青天之开封奇案TV版
         * params3 : search
         * version : 107
         * detailSystemId : 44
         */
        private String logName;
        private String type;
        private String device;
        private String time;
        private String utdId;
        private String params1;
        private String params2;
        private String params3;
        private String version;
        private String detailSystemId;
        public String getLogName() {
            return logName;
        }
        public void setLogName(String logName) {
            this.logName = logName;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getDevice() {
            return device;
        }
        public void setDevice(String device) {
            this.device = device;
        }
        public String getTime() {
            return time;
        }
        public void setTime(String time) {
            this.time = time;
        }
        public String getUtdId() {
            return utdId;
        }
        public void setUtdId(String utdId) {
            this.utdId = utdId;
        }
        public String getParams1() {
            return params1;
        }
        public void setParams1(String params1) {
            this.params1 = params1;
        }
        public String getParams2() {
            return params2;
        }
        public void setParams2(String params2) {
            this.params2 = params2;
        }
        public String getParams3() {
            return params3;
        }
        public void setParams3(String params3) {
            this.params3 = params3;
        }
        public String getVersion() {
            return version;
        }
        public void setVersion(String version) {
            this.version = version;
        }
        public String getDetailSystemId() {
            return detailSystemId;
        }
        public void setDetailSystemId(String detailSystemId) {
            this.detailSystemId = detailSystemId;
        }
    }
}