Pocket Gem OA: Log Parser

time a given player spends actually connected to the
network.
We keep console logs of various game subsystems for each play session. Each log message
has 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 SHUTDOWN
messages.
A player‘s connected time is the amount of time they spend in a connected state. A player starts
the game disconnected, becomes connected when we log a CONNECTED message, and
becomes disconnected when we log a DISCONNECTED message.
You can make the following assumptions:
All logs will be properly formatted
All dates will be valid
No log message will stretch across multiple lines
All log files will be ordered chronologically
There will always be exactly one START and exactly one SHUTDOWN event logged
Input
A file containing lines of log messages.
Output
The 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) :: SHUTDOWN
The 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) :: SHUTDOWN
The 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<string> 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<String> allLines = new ArrayList<String>();
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<String, Integer> status = new HashMap<String, Integer>();
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 }
时间: 2024-08-18 21:08:07

Pocket Gem OA: Log Parser的相关文章

IIS 日志分析工具:Log Parser Studio

1.安装Log Parser,下载地址:http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 2.安装Log Parser Studio,下载地址:http://gallery.technet.microsoft.com/Log-Parser-Studio-cd458765,下载之后解压即可. 3. 运行Log Parser Studio:在解压的LPSV2.D1文件夹中运行LPS.exe

Log Parser 2.2 + Log Parser Lizard GUI 分析IIS日志示例

Log Parser 日志分析工具,用命令行操作,可以分析 IIS logs,event logs,active directory,log4net,file system,t-sql Log Parser Lizard 以可视化界面操作,使用类似sql的语法查询 下载地址: Log Parser 2.2 :http://www.microsoft.com/en-us/download/details.aspx?id=24659 Log Parser Lizard GUI :http://www

log parser 微软iis 日志分析

Log Parser 2.2 您可以从 Microsoft 下载中心下载 Log Parser. Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 Active Directory)进行通用查询.只要告诉 Log Parser 您所需的信息以及您希望如何处理这些信息,它就能很好地完成任务.查询结果可以是基于文本的自定义格式输出,也可以针对更特定的目标

Log Parser 2.2

Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 Active Directory)进行通用查询.只要告诉 Log Parser 您所需的信息以及您希望如何处理这些信息,它就能很好地完成任务.查询结果可以是基于文本的自定义格式输出,也可以针对更特定的目标(如 SQL.SYSLOG 或图表)进行保存.大多数软件都是为完成有限几个特定任务而设计的.Log

用Log Parser Studio分析IIS日志

发现一个强大的图形化IIS日志分析工具——Log Parser Studio,下面分享一个实际操作案例. 1. 安装Log Parser Studio a) 需要先安装Log Parser,下载地址:http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 b) 安装Log Parser Studio,下载地址:http://gallery.technet.microsoft.com/Log-Pars

Jenkins Log Parser Plugin使用说明

官方wiki:https://wiki.jenkins-ci.org/display/JENKINS/Log+Parser+Plugin 插件概要信息: 解析控制台输出,高亮显示error/warning/info行. 描述: log-parser插件解析Jenkins构建生成的控制台日志. 解析控制台日志提供了如下功能: 高亮感兴趣的日志(error,warning,info)行 将日志切分成块 在日志和它的区块中显示errors/warnings/infomations行的总数的概要 链接e

Log Parser 2.2 分析 IIS 日志

1,安装Log Parser 2.2 https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 https://gallery.technet.microsoft.com/Log-Parser-Studio-cd458765  下载之后解压即可 2. 运行Log Parser Studio 在之前解压的LPSV2.D1文件夹中运行LPS.exe. 3. 指定IIS日志文件路径 新建查询 帮助文档 查

Log Parser 微软强大的日志分析工具

Log Parser(微软网站下载)是微软公司出品的日志分析工具,它功能强大,使用简单,可以分析基于文本的日志文件.XML 文件.CSV(逗号分隔符)文件,以及操作系统的事件日志.注册表.文件系统.Active Directory.它可以像使用 SQL 语句一样查询分析这些数据,甚至可以把分析结果以各种图表的形式展现出来. Log Parser 的安装很简单,没有什么特别的,安装之后,可以在安装目录下找到 LogParser.chm 这个文件,该文件是 LogParser 的帮助文件,为英语语言

log parser分析windows日志

首先将windows安全日志导出,步骤如下: 运行eventvwr.msc命令,打开windows日志,如下图,将所有事件另存为: 保存完之后是一个.evtx格式的文件,将使用log parser分析这个导出的日志: 分析命令如下: LogParser.exe -i:EVT "SELECT TimeGenerated,EXTRACT_TOKEN(Strings,0,'|') AS USERNAME,EXTRACT_TOKEN(Strings,2,'|') AS SERVICE\_NAME,EXT