Advertisement
Janmm14

LogOnlineCounter source

Jul 24th, 2015 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. /* Copyright / License:
  2.  * All Rights Reserved. Exceptions require written permission of the author.
  3.  * Author: Janmm14
  4.  */
  5.  
  6. package de.janmm14.misc;
  7.  
  8. import java.io.File;
  9. import java.nio.file.Files;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Objects;
  14. import java.util.concurrent.atomic.AtomicInteger;
  15. import java.util.regex.Matcher;
  16. import java.util.regex.Pattern;
  17. import java.util.stream.Collectors;
  18.  
  19. public final class LogOnlineCounter {
  20.  
  21.     private LogOnlineCounter() {
  22.         throw new UnsupportedOperationException();
  23.     }
  24.  
  25.     @SuppressWarnings("UnnecessaryReturnStatement")
  26.     public static void main(String[] args) throws Exception {
  27.         if (args == null || args.length < 1) {
  28.             System.out.println("Usage: java -jar LogOnlinecounter.jar <path_to_file>");
  29.             System.out.println("(without < and >)");
  30.             System.exit(0);
  31.             return;
  32.         }
  33.         StringBuilder sb = new StringBuilder();
  34.         for (String arg : args) {
  35.             sb.append(arg).append(' ');
  36.         }
  37.         String logFilePath = sb.substring(0, sb.length() - 1);
  38.         String absolutePath = new File(".").getAbsolutePath();
  39.         System.out.println("Running in folder: " + absolutePath.substring(0, absolutePath.length() - 1));
  40.         System.out.println("Log file: " + logFilePath);
  41.         File logFile = new File(logFilePath);
  42.         if (!logFile.isFile()) {
  43.             System.out.println("Usage: java -jar LogOnlinecounter.jar <path_to_file>");
  44.             System.out.println("(without < and >)");
  45.             System.out.println();
  46.             System.out.println("The given log file path is not poining to a file!");
  47.             System.out.println("Aborting!");
  48.             System.exit(0);
  49.             return;
  50.         }
  51.         System.out.println("Starting...");
  52.         Pattern loggedInPattern =
  53.                 Pattern.compile("\\[(?:.+)\\] \\[Server thread/INFO[^\\]]*\\]: (.+)\\[[^\\]]+\\] logged in", Pattern.CASE_INSENSITIVE);//\[(?:.+)\] \[S[^\]]*\]: (.+)\[\.\] logged in
  54.         Pattern lostConnectionPattern =
  55.                 Pattern.compile("\\[(?:.+)\\] \\[Server thread/INFO[^\\]]*\\]: (.+) lost connection", Pattern.CASE_INSENSITIVE); //\[(?:.+)\] \[S\]: (.+) lost connection
  56.  
  57.  
  58.         Map<String, AtomicInteger> counts = new HashMap<>();
  59.  
  60.         System.out.println("Reading file...");
  61.         List<String> toMatchList = Files.readAllLines(logFile.toPath());
  62.         System.out.println("Lines read: " + toMatchList.size());
  63.         System.out.println("Start searching...");
  64.         for (String toMatch : toMatchList) {
  65.             {
  66.                 Matcher matcher = loggedInPattern.matcher(toMatch);
  67.                 while (matcher.find()) {
  68.                     String name = matcher.group(1).toLowerCase().trim().intern();
  69.                     if (counts.containsKey(name)) {
  70.                         counts.get(name).incrementAndGet();
  71.                     } else {
  72.                         counts.put(name, new AtomicInteger(1));
  73.                     }
  74.                 }
  75.             }
  76.             {
  77.                 Matcher matcher = lostConnectionPattern.matcher(toMatch);
  78.                 while (matcher.find()) {
  79.                     String name = matcher.group(1).toLowerCase().trim().intern();
  80.                     if (counts.containsKey(name)) {
  81.                         counts.get(name).decrementAndGet();
  82.                     } else {
  83.                         counts.put(name, new AtomicInteger(-1));
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.         System.out.println("Search ended!");
  89.         System.out.println();
  90.         System.out.println();
  91.  
  92.         List<Map.Entry<String, AtomicInteger>> entries = counts.entrySet().stream()
  93.                 .filter(entry -> entry.getValue().get() == 1).collect(Collectors.toList());
  94.         System.out.println("People online at end of log: " + entries.size());
  95.         System.out.println();
  96.         entries.sort((first, second) -> {
  97.             Objects.requireNonNull(first, "first object in comparision is null!");
  98.             Objects.requireNonNull(second, "second object in comparision is null!");
  99.             return first.getKey().compareTo(second.getKey());
  100.         });
  101.         entries.forEach(entry -> System.out.println(entry.getKey()));
  102.  
  103.         System.out.println();
  104.         System.out.println();
  105.  
  106.         List<Map.Entry<String, AtomicInteger>> incorrect = counts.entrySet().stream().filter(entry -> entry.getValue().get() > 1 || entry.getValue().get() < 0)
  107.                 .collect(Collectors.toList());
  108.         System.out.println("Incorrect amount of joins and leaves: " + incorrect.size());
  109.         if (!incorrect.isEmpty()) {
  110.             System.out.println();
  111.             incorrect.forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
  112.         }
  113.         System.exit(0);
  114.         return;
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement