博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Pocket Gem OA: Log Parser
阅读量:4518 次
发布时间:2019-06-08

本文共 5614 字,大约阅读时间需要 18 分钟。

time a given player spends actually connected to thenetwork.We keep console logs of various game subsystems for each play session. Each log messagehas the following format:(MM/dd/yyyy-hh:mm:ss) :: [message logged]Sample log lines:(11/12/2015-02:34:56) :: START(01/02/1990-13:10:00) :: DISCONNECTED(03/13/2018-21:01:01) :: ERROR - File "close.png" not found.Log messages that pertain to network connectivity are as follows:START : Logged when the game starts up.CONNECTED : Logged when a network connection is established.DISCONNECTED : Logged when network connection is lost.SHUTDOWN : Logged when the player is quitting the game.A player's session length is the amount of time between the START and SHUTDOWNmessages.A player's connected time is the amount of time they spend in a connected state. A player startsthe game disconnected, becomes connected when we log a CONNECTED message, andbecomes disconnected when we log a DISCONNECTED message.You can make the following assumptions:All logs will be properly formattedAll dates will be validNo log message will stretch across multiple linesAll log files will be ordered chronologicallyThere will always be exactly one START and exactly one SHUTDOWN event loggedInputA file containing lines of log messages.OutputThe connectivity percentage as a string, rounded down to an integer.Examples(01/01/2000-01:00:00) :: START(01/01/2000-01:01:00) :: CONNECTED(01/01/2000-01:21:00) :: DISCONNECTED(01/01/2000-01:50:00) :: SHUTDOWNThe player spent 20 minutes out of 50 connected, 20 / 50 = 0.4, output should be "40%"(02/03/2002-14:00:00) :: START(02/03/2002-14:00:00) :: CONNECTED(02/03/2002-14:08:00) :: DISCONNECTED(02/03/2002-14:10:00) :: CONNECTED(02/03/2002-14:15:00) :: SHUTDOWNThe player spent 13 minutes out of 15 connected, 13 / 15 = 0.8667, output should be "86%"More sample input can be found in the text files input_1.txt, input_2.txt, and input_3.txt
有事游戏中有Log用于记录各种状态,想知道究竟用户连接的时间是多少。状态除了START, CONNECTED, DISCONNECTED, SHUTDONW外还有ERROR啥的,但是只需要关注前四个。输入形式: vector
lines(11/01/2015-04:00:00) :: START(11/01/2015-04:00:00) :: CONNECTED(11/01/2015-04:30:00) :: DISCONNECTED(11/01/2015-04:45:00) :: CONNECTED(11/01/2015-05:00:00) :: SHUTDOWN

 

涉及到处理Date, 以及STDIN from a file

处理Date可以用SimpleDateFormat这个class

static Date parseTime(String timeStr) {

  Date time = new Date();
  DateFormat dft = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ss");
  try {
    time = dft.parse(timeStr);
  }catch (ParseException ignored) {}
  return time;
}

1 package pocketGems; 2  3 import java.io.*; 4 import java.util.*; 5 import java.text.DateFormat; 6 import java.text.ParseException; 7 import java.text.SimpleDateFormat; 8  9 public class LogParser2 {10     public static void main(String[] args)11             throws FileNotFoundException, IOException {12         String filename = "C:/Users/yang liu/workspace/Interview2017/src/pocketGems/test1.txt";13         if (args.length > 0) {14             filename = args[0];15         }16 17         18         String answer = parseFile(filename);19         System.out.println(answer);20     }21     22     static String parseFile(String filename)23             throws FileNotFoundException, IOException{24         BufferedReader input = new BufferedReader(new FileReader(filename));25         List
allLines = new ArrayList
();26 String line = "";27 while ((line = input.readLine()) != null) {28 allLines.add(line);29 }30 input.close();31 return parseLines(allLines.toArray(new String[allLines.size()]));32 }33 34 static String parseLines(String[] lines) {35 Map
status = new HashMap
();36 37 status.put("START", 0);38 status.put("CONNECTED", 1);39 status.put("DISCONNECTED", -1);40 status.put("SHUTDOWN", -2);41 42 long totalTime = 0;43 long connectTime = 0;44 boolean isConnected = false;45 Date lastConnectMoment = new Date();46 Date startMoment = new Date();47 Date shutMoment = new Date();48 49 for (String line : lines) {50 String[] lineSplit = line.split(" :: ");51 String event = lineSplit[1];52 if (!status.containsKey(event)) continue;53 54 String cur = lineSplit[0];55 Date currentTime = parseTime(cur.substring(1, cur.length()-1));56 57 58 int eventID = status.get(event);59 if (eventID > 0) {60 if (!isConnected)61 lastConnectMoment = currentTime;62 isConnected = true;63 }64 else if (eventID < 0) {65 if (isConnected)66 connectTime += currentTime.getTime() - lastConnectMoment.getTime();67 isConnected = false;68 }69 if (eventID == 0) startMoment = currentTime;70 if (eventID == -2) shutMoment = currentTime;71 }72 totalTime = shutMoment.getTime() - startMoment.getTime();73 74 double ratio = (double)connectTime/totalTime * 100;75 return String.format("%d%s", (int)ratio, "%");76 }77 78 static Date parseTime(String timeStr) {79 Date time = new Date();80 DateFormat dft = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ss");81 try {82 time = dft.parse(timeStr);83 }catch (ParseException ignored) {}84 return time;85 }86 }

 

转载于:https://www.cnblogs.com/EdwardLiu/p/6354194.html

你可能感兴趣的文章
awk多模式匹配
查看>>
线段树
查看>>
[javascript]实现登陆界面拖动窗口
查看>>
a span等行内元素加margin属性后无效果解决方案
查看>>
傻瓜式硬盘重装win7系统图文加视频教程
查看>>
BZOJ 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头:统计 + 筛法【调和级数】
查看>>
如果一个人请优雅的活着。
查看>>
验证码
查看>>
Django缓存配置
查看>>
Ubuntu下安装 Mysql
查看>>
LeetCode--Reverse Integer
查看>>
PHP_APC+Ajax实现的监视进度条的文件上传
查看>>
计算机网络课堂笔记3.29
查看>>
word2vec----CBOW
查看>>
衰减学习率真的有用吗?
查看>>
ORACLE 建库过程总结
查看>>
Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用...
查看>>
c# Winform 开发分屏显示应用程序
查看>>
canvas刮奖
查看>>