1
|
import java.io.BufferedReader;
|
2
|
import java.io.FileNotFoundException;
|
3
|
import java.io.FileReader;
|
4
|
import java.io.IOException;
|
5
|
import java.io.PrintWriter;
|
6
|
import java.io.UnsupportedEncodingException;
|
7
|
import java.util.Collections;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.Map;
|
10
|
import java.util.regex.Matcher;
|
11
|
import java.util.regex.Pattern;
|
12
|
|
13
|
public class QueryAnalyzer
|
14
|
{
|
15
|
private static Map<String, Integer> queries = new HashMap<>();
|
16
|
|
17
|
private static String slowestQuery = null;
|
18
|
private static double slowestQueryTime = 0;
|
19
|
|
20
|
private static String mostUsedQuery = null;
|
21
|
private static int mostUsedQueryCount = 0;
|
22
|
|
23
|
public static void main(String[] args)
|
24
|
{
|
25
|
try (BufferedReader br = new BufferedReader(new FileReader(args[0])))
|
26
|
{
|
27
|
Pattern queryTypePattern = Pattern.compile("Query type: (.*?);");
|
28
|
Pattern timePattern = Pattern.compile("Processing time until now: (.*?) ms;");
|
29
|
Pattern mostUsedPattern = Pattern.compile("Use count: (.*?);");
|
30
|
|
31
|
for (String line; (line = br.readLine()) != null; )
|
32
|
{
|
33
|
|
34
|
Matcher queryTypeMatch = queryTypePattern.matcher(line);
|
35
|
if (queryTypeMatch.find())
|
36
|
{
|
37
|
String queryType = queryTypeMatch.group(1);
|
38
|
Integer occurences = queries.get(queryType);
|
39
|
if (occurences != null)
|
40
|
{
|
41
|
queries.put(queryType, occurences + 1);
|
42
|
}
|
43
|
else
|
44
|
{
|
45
|
queries.put(queryType, 1);
|
46
|
}
|
47
|
}
|
48
|
|
49
|
|
50
|
Matcher timeMatch = timePattern.matcher(line);
|
51
|
if (timeMatch.find())
|
52
|
{
|
53
|
double time = Double.parseDouble(timeMatch.group(1));
|
54
|
if (time > slowestQueryTime)
|
55
|
{
|
56
|
slowestQueryTime = time;
|
57
|
slowestQuery = line;
|
58
|
}
|
59
|
}
|
60
|
|
61
|
|
62
|
Matcher mostUsedMatch = mostUsedPattern.matcher(line);
|
63
|
if (mostUsedMatch.find())
|
64
|
{
|
65
|
int count = Integer.parseInt(mostUsedMatch.group(1));
|
66
|
if (count > mostUsedQueryCount)
|
67
|
{
|
68
|
mostUsedQueryCount = count;
|
69
|
mostUsedQuery = line;
|
70
|
}
|
71
|
}
|
72
|
}
|
73
|
}
|
74
|
catch (FileNotFoundException e)
|
75
|
{
|
76
|
|
77
|
e.printStackTrace();
|
78
|
}
|
79
|
catch (IOException e)
|
80
|
{
|
81
|
|
82
|
e.printStackTrace();
|
83
|
}
|
84
|
|
85
|
StringBuilder sb = new StringBuilder();
|
86
|
|
87
|
sb.append("Number of each query: \n");
|
88
|
queries.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).forEach((x) -> {
|
89
|
sb.append(x + "\n");
|
90
|
});
|
91
|
|
92
|
sb.append("\nSlowest query: \n")
|
93
|
.append(slowestQuery)
|
94
|
.append("\n\nMost used query: \n")
|
95
|
.append(mostUsedQuery);
|
96
|
|
97
|
PrintWriter writer;
|
98
|
try
|
99
|
{
|
100
|
String fileName = args[0].replace(".log", "-analyzed.txt");
|
101
|
writer = new PrintWriter(fileName);
|
102
|
writer.println(sb.toString());
|
103
|
writer.close();
|
104
|
}
|
105
|
catch (FileNotFoundException e)
|
106
|
{
|
107
|
|
108
|
e.printStackTrace();
|
109
|
}
|
110
|
}
|
111
|
}
|