Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright / License:
- * All Rights Reserved. Exceptions require written permission of the author.
- * Author: Janmm14
- */
- package de.janmm14.misc;
- import java.io.File;
- import java.nio.file.Files;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public final class LogOnlineCounter {
- private LogOnlineCounter() {
- throw new UnsupportedOperationException();
- }
- @SuppressWarnings("UnnecessaryReturnStatement")
- public static void main(String[] args) throws Exception {
- if (args == null || args.length < 1) {
- System.out.println("Usage: java -jar LogOnlinecounter.jar <path_to_file>");
- System.out.println("(without < and >)");
- System.exit(0);
- return;
- }
- StringBuilder sb = new StringBuilder();
- for (String arg : args) {
- sb.append(arg).append(' ');
- }
- String logFilePath = sb.substring(0, sb.length() - 1);
- String absolutePath = new File(".").getAbsolutePath();
- System.out.println("Running in folder: " + absolutePath.substring(0, absolutePath.length() - 1));
- System.out.println("Log file: " + logFilePath);
- File logFile = new File(logFilePath);
- if (!logFile.isFile()) {
- System.out.println("Usage: java -jar LogOnlinecounter.jar <path_to_file>");
- System.out.println("(without < and >)");
- System.out.println();
- System.out.println("The given log file path is not poining to a file!");
- System.out.println("Aborting!");
- System.exit(0);
- return;
- }
- System.out.println("Starting...");
- Pattern loggedInPattern =
- Pattern.compile("\\[(?:.+)\\] \\[Server thread/INFO[^\\]]*\\]: (.+)\\[[^\\]]+\\] logged in", Pattern.CASE_INSENSITIVE);//\[(?:.+)\] \[S[^\]]*\]: (.+)\[\.\] logged in
- Pattern lostConnectionPattern =
- Pattern.compile("\\[(?:.+)\\] \\[Server thread/INFO[^\\]]*\\]: (.+) lost connection", Pattern.CASE_INSENSITIVE); //\[(?:.+)\] \[S\]: (.+) lost connection
- Map<String, AtomicInteger> counts = new HashMap<>();
- System.out.println("Reading file...");
- List<String> toMatchList = Files.readAllLines(logFile.toPath());
- System.out.println("Lines read: " + toMatchList.size());
- System.out.println("Start searching...");
- for (String toMatch : toMatchList) {
- {
- Matcher matcher = loggedInPattern.matcher(toMatch);
- while (matcher.find()) {
- String name = matcher.group(1).toLowerCase().trim().intern();
- if (counts.containsKey(name)) {
- counts.get(name).incrementAndGet();
- } else {
- counts.put(name, new AtomicInteger(1));
- }
- }
- }
- {
- Matcher matcher = lostConnectionPattern.matcher(toMatch);
- while (matcher.find()) {
- String name = matcher.group(1).toLowerCase().trim().intern();
- if (counts.containsKey(name)) {
- counts.get(name).decrementAndGet();
- } else {
- counts.put(name, new AtomicInteger(-1));
- }
- }
- }
- }
- System.out.println("Search ended!");
- System.out.println();
- System.out.println();
- List<Map.Entry<String, AtomicInteger>> entries = counts.entrySet().stream()
- .filter(entry -> entry.getValue().get() == 1).collect(Collectors.toList());
- System.out.println("People online at end of log: " + entries.size());
- System.out.println();
- entries.sort((first, second) -> {
- Objects.requireNonNull(first, "first object in comparision is null!");
- Objects.requireNonNull(second, "second object in comparision is null!");
- return first.getKey().compareTo(second.getKey());
- });
- entries.forEach(entry -> System.out.println(entry.getKey()));
- System.out.println();
- System.out.println();
- List<Map.Entry<String, AtomicInteger>> incorrect = counts.entrySet().stream().filter(entry -> entry.getValue().get() > 1 || entry.getValue().get() < 0)
- .collect(Collectors.toList());
- System.out.println("Incorrect amount of joins and leaves: " + incorrect.size());
- if (!incorrect.isEmpty()) {
- System.out.println();
- incorrect.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
- }
- System.exit(0);
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement