-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogAnalyzer.java
More file actions
161 lines (135 loc) · 5.74 KB
/
LogAnalyzer.java
File metadata and controls
161 lines (135 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package edu.project3;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;
@CommandLine.Command(name = "nginx log stats",
description = "prints nginx log stats")
@SuppressWarnings({"checkstyle:MultipleStringLiterals", "checkstyle:RegexpSinglelineJava"})
public final class LogAnalyzer implements Runnable {
private LogAnalyzer() {
}
@CommandLine.Option(names = {"-p", "--path"},
description = "Path to logs (local template or URL)",
required = true,
arity = "1..*")
private List<String> path;
@CommandLine.Option(names = {"-s", "--from"}, description = "Start date of logs (ISO8601)")
private String from;
@CommandLine.Option(names = {"-e", "--to"}, description = "End date of logs (ISO8601)")
private String to;
@CommandLine.Option(names = {"-f", "--format"}, description = "Output format (markdown or adoc)")
private Format format;
private enum MetricKeys {
RESOURCE("Ресурс(ы)"),
START_DATE("Начальная дата"),
END_DATE("Конечная дата"),
REQUEST_COUNT("Количество запросов"),
AVG_REQUEST_SIZE("Средний размер запроса");
MetricKeys(String string) {
this.value = string;
}
private final String value;
}
private final Map<String, String> generalMetric = new LinkedHashMap<>() {
{
put(MetricKeys.RESOURCE.value, "-");
put(MetricKeys.START_DATE.value, "-");
put(MetricKeys.END_DATE.value, "-");
put(MetricKeys.REQUEST_COUNT.value, "-");
put(MetricKeys.AVG_REQUEST_SIZE.value, "-");
}
};
private static final int TOP_COUNT = 3;
public enum Format {
markdown,
adoc
}
@SuppressWarnings("checkstyle:UncommentedMain")
public static void main(String[] args) {
CommandLine.run(new LogAnalyzer(), System.err, args);
}
@Override
public void run() {
LocalDateTime startDate = LocalDateTime.MIN;
LocalDateTime endDate = LocalDateTime.MAX;
if (CLIParser.parseDateFromString(from) != null) {
startDate = CLIParser.parseDateFromString(from);
generalMetric.put(MetricKeys.START_DATE.value, startDate.toLocalDate().toString());
}
if (CLIParser.parseDateFromString(to) != null) {
endDate = CLIParser.parseDateFromString(to);
generalMetric.put(MetricKeys.END_DATE.value, endDate.toLocalDate().toString());
}
List<Log> logList = new ArrayList<>();
for (String i : path) {
try {
logList.addAll(LogParser.getLogsFromStrings(new HttpSource(new URI(i)).readStringsFromSource()));
} catch (URISyntaxException e) {
logList.addAll(LogParser.getLogsFromStrings(new FileSource(Path.of(i)).readStringsFromSource()));
}
}
generalMetric.put(MetricKeys.RESOURCE.value, String.join(", ", path));
logList = LogParser.filterLogsForDate(logList, startDate, endDate);
LogStat logStat = new LogStat(logList);
generalMetric.put(MetricKeys.REQUEST_COUNT.value, String.valueOf(logStat.getRequestCount()));
generalMetric.put(MetricKeys.AVG_REQUEST_SIZE.value, String.valueOf(logStat.getAvgServerAnswer()));
StatPrinter statPrinter = new StatPrinter(format);
printGeneralMetric(statPrinter);
printMostRequestedResources(statPrinter, logStat.getMostRequestedResources());
printMostFrequentAgents(statPrinter, logStat.getMostFrequentAgents());
printMostReturnedStatuses(statPrinter, logStat.getMostReturnedStatuses());
printMostRequestedResourcesWithServerError(statPrinter, logStat.getMostRequestedResourcesWithServerError());
}
private void printGeneralMetric(StatPrinter statPrinter) {
System.out.println(statPrinter.printSortedMapFirstKElements(
generalMetric,
generalMetric.size(),
"Общая метрика",
"Метрика",
"Значение"
));
}
private void printMostRequestedResources(StatPrinter statPrinter, Map<String, Long> resources) {
System.out.println(statPrinter.printSortedMapFirstKElements(
resources,
TOP_COUNT,
"Запрашиваемые ресурсы",
"Ресурс",
"Количество"
));
}
private void printMostFrequentAgents(StatPrinter statPrinter, Map<String, Long> agents) {
System.out.println(statPrinter.printSortedMapFirstKElements(
agents,
TOP_COUNT,
"Агенты",
"Агент",
"Количество"
));
}
private void printMostReturnedStatuses(StatPrinter statPrinter, Map<Integer, ?> statuses) {
System.out.println(statPrinter.printServerCodes(
statuses,
TOP_COUNT,
"Коды ответа",
"Код",
"Имя",
"Количество"
));
}
private void printMostRequestedResourcesWithServerError(StatPrinter statPrinter, Map<String, Long> statuses) {
System.out.println(statPrinter.printSortedMapFirstKElements(
statuses,
TOP_COUNT,
"Ресурсы, доступ к которым закончился ошибкой 404",
"Ресурс",
"Количество"
));
}
}