public void log時差計算() {
long line = 0;
List<String> lineList = new ArrayList<String>();
try {
Map<String, List<Long>> map = new HashMap<String, List<Long>>();
File file = new File("C:\\work\\sifaccess.log20150130");
BufferedReader reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 読込
String date1 = null;
String id1 = null;
String ip1 = null;
String date2 = null;
String id2 = null;
String ip2 = null;
while ((tempString = reader.readLine()) != null) {
lineList.add(tempString);
}
reader.close();
for (int i = 0; i < lineList.size(); i ++) {
String lineStr = lineList.get(i);
line ++;
if (lineStr.indexOf(" UP IF-ID") > 0) {
date1 = lineStr.substring(0, 23);
ip1 = lineStr.substring(24, lineStr.indexOf(", "));
id1 = lineStr.substring(lineStr.indexOf(" /") + 1, lineStr.indexOf(".htm") + 4);
// 次の行
boolean isFind = false;
for (int j = i + 1 ; j < lineList.size(); j ++) {
lineStr = lineList.get(j);
if (lineStr.indexOf(" UP ") > 0) {
continue;
}
ip2 = lineStr.substring(24, lineStr.indexOf(", "));
date2 = lineStr.substring(0, 23);
id2 = lineStr.substring(lineStr.indexOf(" /") + 1, lineStr.indexOf(".htm") + 4);
if (ip1.equals(ip2) && id1.equals(id2)) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
Date dateUP = formatter.parse(date1);
Date dateDOWN = formatter.parse(date2);
long time = dateDOWN.getTime() - dateUP.getTime();
if (map.containsKey(id1)) {
List<Long> list = (List<Long>)map.get(id1);
list.add(time);
map.put(id1, list);
}
else {
List<Long> list = new ArrayList<Long>();
list.add(time);
map.put(id1, list);
}
isFind = true;
break;
}
}
if (!isFind) {
System.out.println("error:" + line + "行");
}
}
if (lineStr.indexOf(" DOWN method:") > 0) {
long time = Long.parseLong(lineStr.substring(lineStr.indexOf(" out:") + 5).replace("ms", ""));
id1 = lineStr.substring(lineStr.indexOf(" /") + 1, lineStr.indexOf(".htm") + 4);
if (map.containsKey(id1)) {
List<Long> list = (List<Long>)map.get(id1);
list.add(time);
map.put(id1, list);
}
else {
List<Long> list = new ArrayList<Long>();
list.add(time);
map.put(id1, list);
}
}
}
System.out.println("読込終了:" + line + "行");
// 書き込み
File outFile = new File("C:\\work\\access.txt");
if(!outFile.exists()) {
try {
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(outFile), "Shift_JIS");
BufferedWriter bw = new BufferedWriter(osw);
Set<String> set = map.keySet();
for (String key : set) {
List<Long> list = map.get(key);
long min=99999999L;
long max=0;
long ave = 0;
long sum = 0;
for (Long second : list) {
long temp = second.longValue();
sum = sum + temp;
if (min > temp) {
min = temp;
}
if (max < temp) {
max = temp;
}
}
ave = sum / list.size();
bw.write(key + " " + min + " " + max + " " + ave + " " + list.size() + "\r\n");
}
bw.close();
osw.close();
System.out.println("書込終了");
} catch (Exception e) {
System.out.println("error line:" + line);
e.printStackTrace();
}
}