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;
}

 package pocketGems;

 import java.io.*;
import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat; public class LogParser2 {
public static void main(String[] args)
throws FileNotFoundException, IOException {
String filename = "C:/Users/yang liu/workspace/Interview2017/src/pocketGems/test1.txt";
if (args.length > 0) {
filename = args[0];
} String answer = parseFile(filename);
System.out.println(answer);
} static String parseFile(String filename)
throws FileNotFoundException, IOException{
BufferedReader input = new BufferedReader(new FileReader(filename));
List<String> allLines = new ArrayList<String>();
String line = "";
while ((line = input.readLine()) != null) {
allLines.add(line);
}
input.close();
return parseLines(allLines.toArray(new String[allLines.size()]));
} static String parseLines(String[] lines) {
Map<String, Integer> status = new HashMap<String, Integer>(); status.put("START", 0);
status.put("CONNECTED", 1);
status.put("DISCONNECTED", -1);
status.put("SHUTDOWN", -2); long totalTime = 0;
long connectTime = 0;
boolean isConnected = false;
Date lastConnectMoment = new Date();
Date startMoment = new Date();
Date shutMoment = new Date(); for (String line : lines) {
String[] lineSplit = line.split(" :: ");
String event = lineSplit[1];
if (!status.containsKey(event)) continue; String cur = lineSplit[0];
Date currentTime = parseTime(cur.substring(1, cur.length()-1)); int eventID = status.get(event);
if (eventID > 0) {
if (!isConnected)
lastConnectMoment = currentTime;
isConnected = true;
}
else if (eventID < 0) {
if (isConnected)
connectTime += currentTime.getTime() - lastConnectMoment.getTime();
isConnected = false;
}
if (eventID == 0) startMoment = currentTime;
if (eventID == -2) shutMoment = currentTime;
}
totalTime = shutMoment.getTime() - startMoment.getTime(); double ratio = (double)connectTime/totalTime * 100;
return String.format("%d%s", (int)ratio, "%");
} 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;
}
}

Pocket Gem OA: Log Parser的更多相关文章

  1. Pocket Gem OA: Path Finder

    1. 有向图 找所有start node到end node之间的路径 输入是一个txt 形式如下: A E A : B C D. B : C C : E D : B. 输出一个List<Stri ...

  2. Log Parser 2.2 分析 IIS 日志

    1,安装Log Parser 2.2 https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 ...

  3. 用Log Parser Studio分析IIS日志

    发现一个强大的图形化IIS日志分析工具——Log Parser Studio,下面分享一个实际操作案例. 1. 安装Log Parser Studio a) 需要先安装Log Parser,下载地址: ...

  4. 云计算之路-阿里云上:借助IIS Log Parser Studio分析“黑色30秒”问题

    今天下午15:11-15:13间出现了类似“黑色30秒”的状况,我们用强大的IIS日志分析工具——Log Parser Studio进行了进一步的分析. 分析情况如下—— 先看一下Windows性能监 ...

  5. Log Parser 2.2

    Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 A ...

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

    Log Parser(微软网站下载)是微软公司出品的日志分析工具,它功能强大,使用简单,可以分析基于文本的日志文件.XML 文件.CSV(逗号分隔符)文件,以及操作系统的事件日志.注册表.文件系统.A ...

  7. 日志分析工具Log Parser介绍

    摘要: 微软动态CRM专家罗勇 ,回复321或者20190322可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me! 分析Dynamics 365 Customer Enga ...

  8. Log Parser Studio 分析 IIS 日志

    Log Parser Studio 分析 IIS 日志 来源 https://www.cnblogs.com/lonelyxmas/p/8671336.html 软件下载地址: Log Parser ...

  9. IIS 日志分析工具:Log Parser Studio

    1.安装Log Parser,下载地址:http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 ...

随机推荐

  1. 视频转码成mp4格式,添加关键帧,添加元数据,把元数据放在第一帧,可拖动

    作者测试是在windows下使用,所以下载的页面地址是: http://ffmpeg.zeranoe.com/builds/点击页面上的Download FFmpeg git-738ebb4 64-b ...

  2. 第四次java实验

    java实验 实验一: 要求 Android Stuidio的安装测试: 参考<Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd)>第 ...

  3. 理解WidowManager

    --摘自<Android进阶解密> *WindowManger的关联类* 1.Window是一个抽象类,具体的实现类为PhoneWindow,它对View进行管理.WindowManage ...

  4. Loadrunner乱码问题解决方案(录制&&运行)

    在使用Loadrunner录制和回放时有时会出现乱码,从而导致脚本运行失败,这让我们很难定位脚本问题所在. 1.乱码产生的原因 1)loadrunner工具使用的是UTF-8编码,但被测系统使用的是G ...

  5. docker备份mongodb数据,导入导出

    场景:服务器要升级,之前在linux部署的mongodb没有用docker,升级后,mongodb要用docker部署,并将原有的mongodb数据导入到docker部署的mongodb中. 1.在l ...

  6. c#学习个人总结

    c#一门程序语言,我的专业课.有了一学期的接触对它的了解还只是皮毛,里面有许多的知识都有待了解学习和提高,熟练掌握程序的语法,能够熟练的调用,对于一些理论性的知识还需要今后更多的揣摩和研究,讲书本中的 ...

  7. linux学习:特殊符号,数学运算,图像与数组与部分终端命令用法整理

    指令:let.expr.array.convert.tput.date.read.md5.ln.apt.系统信息 一:特殊符号用法整理 系统变量 $# 是传给脚本的参数个数 $0 是脚本本身的名字 $ ...

  8. SPFA找最大比例环

    SPFA找最大比例环 ans=Σ点权/Σ边权 所以 可以变式为 Σ边权*ans-Σ点权=0 要找出最大的ans 可以二分 边权值变为 目的地点权-ans*边权 检查是否有负环 有则可以更优 #incl ...

  9. 泡泡一分钟:Cooperative Object Transportation by Multiple Ground and Aerial Vehicles: Modeling and Planning

    张宁 Cooperative Object Transportation by Multiple Ground and Aerial Vehicles: Modeling and Planning 多 ...

  10. lcr电桥浅谈

    此图告知,现代的lcr电桥使用的都是虚地,阻抗很高. 系统采用的鉴相器比较简单,是检半个正弦基波(RMS有效值积分),可以有效抵御偶次基波. 倒不如直接使用运放电容移相,比较采样法直接采样最高点. 此 ...