import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* @author Administrator
*
*/
/**
* @author Administrator
*
*/
public class DataAnalyze {
public static void main(String[] args) {
// TODO Auto-generated method stub
String TimeSortedLogFile = "/home/wdl/test/result.txt";
List<String> record = new ArrayList<String>();
List<List<String>> typeGroupRecord = new ArrayList<List<String>>();
List<Double> gropCount = new ArrayList<Double>();
double count = 0;
double totalCount = 0;
// 扫描文件,将记录存储到List中
try {
File File = new File(TimeSortedLogFile);
BufferedReader wReader = new BufferedReader(new InputStreamReader(
new FileInputStream(TimeSortedLogFile), "UTF-8"));
String line = null;
while ((line = wReader.readLine()) != null) {
if ("".equals(line.trim())) { // 遇空行,略过继续执行
continue;
}
record.add(line);
}
wReader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
// 找出所有类型,如(a,f)
Map<List<String>, Integer> typePair = new HashMap<List<String>, Integer>();
List<String> satisfiedEqualIPEvent = new ArrayList<String>();
for (int i = 0; i < record.size(); i++) {
List<String> type = new ArrayList<String>(); // 记录不包含IP的日志类型
String[] logArr = record.get(i).split("\t");
for (int j = 0; j < logArr.length - 1; j++) {
String[] sublogArr = logArr[j - 1].split("_"); // sublogArr大小为2
type.add(sublogArr[1]);
}
typePair.put(type, 0);
}
// 遍历每一种类型
for (Entry<List<String>, Integer> entry : typePair.entrySet()) {
count = 0;
totalCount = 0;
satisfiedEqualIPEvent = resolvePair(entry.getKey(), record);
typeGroupRecord.add(satisfiedEqualIPEvent);
for (String tempStr : satisfiedEqualIPEvent) {
String[] str = tempStr.split("\t");
count = Double.parseDouble(str[str.length - 1]);
totalCount = totalCount + count;
}
gropCount.add(totalCount);
}
}
/**
* 对于每一对类型,找到同一台ip出现此故障的记录
*
* @param type
* @param record
* @return
*/
public static List<String> resolvePair(List<String> type,
List<String> record) {
Boolean ipEqualFlag = false;
Boolean typeEqualFlag = true;
List<String> satisfiedEqualIPEvent = new ArrayList<String>();
for (int i = 0; i < record.size(); i++) {
typeEqualFlag = true;
ipEqualFlag = false;
String[] logArr = record.get(i).split("\t");
List<String> singleType = new ArrayList<String>();
List<String> ip = new ArrayList<String>();
for (int j = 0; j < logArr.length - 1; j++) {
String[] sublogArr = logArr[j - 1].split("_"); // sublogArr大小为2
ip.add(sublogArr[0]);
singleType.add(sublogArr[1]);
}
Set s = new HashSet(ip); // 观察每一项的ip是否相同
if (s.size() > 1) {
ipEqualFlag = false;
} else {
ipEqualFlag = true;
}
if (type.size() == singleType.size()) { // 在所有记录中提取所有相同类型
for (int k = 0; k < type.size(); k++)
if (type.get(k).equals(singleType.get(k)) == false) {
ipEqualFlag = false;
break;
}
}
if (ipEqualFlag == true && typeEqualFlag == true) { // 若结果中同一条规则中事件的ip相同
satisfiedEqualIPEvent.add(record.get(i));
}
}
return satisfiedEqualIPEvent;
}
}