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 loadLog(String filePath) { List 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 { int count = 0; String path = ""; Scanner scanner = new Scanner(new FileInputStream("C:\\Users\\Administrator\\Desktop\\日志\\布丸播放\\video_play.2021-03-12.log")); while (scanner.hasNextLine()) { String text = scanner.nextLine(); if (text != null && text.contains("getUserVideoDetail:")) { String content = text.split("getUserVideoDetail:")[1]; content = content.replace("##", "# #"); String[] sts = content.split("#"); // device,utdId, loginUid, detailSystemId, videoId, resourceId, from resource(sts[4]); video(sts[3]); count++; } } scanner.close(); System.out.println(count); printMap(resourceMap); printMap(videoMap); } Map resourceMap = new HashMap<>(); private void resource(String resourceId) { Integer r = resourceMap.get(resourceId); if (r == null) { resourceMap.put(resourceId, 0); } resourceMap.put(resourceId, resourceMap.get(resourceId) + 1); } Map videoMap = new HashMap<>(); private void video(String videoId) { Integer r = videoMap.get(videoId); if (r == null) { videoMap.put(videoId, 0); } videoMap.put(videoId, videoMap.get(videoId) + 1); } private void printMap(Map map) { //list排序 TreeMap> ordersMap = new TreeMap<>(Comparator.reverseOrder()); for (Iterator its = map.keySet().iterator(); its.hasNext(); ) { String key = its.next(); Integer value = map.get(key); if (ordersMap.get(value) == null) { ordersMap.put(value, new HashSet<>()); } ordersMap.get(value).add(key); } int p = 0; for (Iterator its = ordersMap.keySet().iterator(); its.hasNext(); ) { Integer key = its.next(); System.out.println(key + ":" + ordersMap.get(key)); p++; if (p > 100) { break; } } } @Test public void videoPlay() throws Exception { Set sets = new HashSet<>(); Set totalSets = new HashSet<>(); Gson gson = new Gson(); Scanner scanner = new Scanner(new FileInputStream("D:\\文件传输\\日志\\2021_04_27.log")); Map countMap = new HashMap<>(); List videoList = new ArrayList<>(); int ppcount = 0; int count = 0; while (scanner.hasNextLine()) { count++; String text = scanner.nextLine(); UserVideoPlayLogInfo logInfo = gson.fromJson(text, UserVideoPlayLogInfo.class); totalSets.add(logInfo.getUtdId()); if ("25".equalsIgnoreCase(logInfo.getResourceId())) { sets.add(logInfo.getUtdId()); ppcount++; } 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("播放UV总数为:" + totalSets.size()); System.out.println("PPTV UV播放总数为:" + sets.size()); System.out.println("PPTV UV比例:" + new BigDecimal(sets.size()).divide(new BigDecimal(totalSets.size()), 2, RoundingMode.FLOOR)); System.out.println("播放总数为:" + count); System.out.println("PPTV 播放总数为:" + ppcount); System.out.println("PPTV 比例:" + new BigDecimal(ppcount).divide(new BigDecimal(count), 2, RoundingMode.FLOOR)); scanner.close(); for (String st : videoList) { System.out.println(st); } } @Test public void searchKey() { Logger logger = LoggerFactory.getLogger("searchKey"); logger.info("{}"); } private void printAction(String utdId, List 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 infoList) { List 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 list = loadLog("D:\\文件传输\\日志\\active_2021_04_27.log"); Map> 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 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; } } }