ServerLogParser.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package analyzer;
  2. import analyzer.models.Hashtag;
  3. import analyzer.models.Round;
  4. import analyzer.models.User;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.*;
  9. /**
  10. * This class reads the server logs, creates objects and organizes them into data structures.
  11. * An instance of this class must run cooperatively with an instance of class ClientLogParser
  12. */
  13. public class ServerLogParser {
  14. private static long startRoundTime = 0;
  15. public static volatile int serverRoundNo = 0;
  16. public static volatile boolean DONE = false;
  17. //public static volatile List<Integer> roundLineCounters = new ArrayList<>();
  18. public static volatile int serverLineCounter = 0;
  19. public static volatile int lineTotal = 0;
  20. public static volatile Map<Long, User> users = new Hashtable<>(Analyzer.clientNo, 1);
  21. public static volatile Map<Integer, Round> rounds = new Hashtable<>(1000);
  22. public static volatile Map<String, Hashtag> hashtags = new Hashtable<>(Analyzer.clientNo);
  23. public static void run() throws IOException {
  24. parse();
  25. }
  26. private static void traceUser() {
  27. //parseUserID(path);
  28. }
  29. private static void countLines() throws IOException {
  30. FileReader fr = new FileReader(Analyzer.serverLogPath);
  31. BufferedReader br= new BufferedReader(fr);
  32. while(br.readLine() != null) {
  33. lineTotal++;
  34. }
  35. br.close();
  36. }
  37. private static void parse() throws IOException {
  38. countLines();
  39. FileReader fr = new FileReader(Analyzer.serverLogPath);
  40. BufferedReader br= new BufferedReader(fr);
  41. String line;
  42. int roundLinesCount = 0;
  43. while((line = br.readLine()) != null) {
  44. serverLineCounter++;
  45. roundLinesCount++;
  46. String[] elements = line.trim().split("\t");//timestamp user_id hashtags
  47. long timestamp = Long.parseLong(elements[0]);
  48. //get log start time
  49. if(serverLineCounter == 1){
  50. startRoundTime = timestamp;
  51. serverRoundNo = 1;
  52. }
  53. //next round
  54. if(timestamp >= startRoundTime + Analyzer.roundLength) {
  55. //System.out.println(serverRoundNo+ " " + roundLinesCount);
  56. startRoundTime += Analyzer.roundLength;
  57. //roundLineCounters.add(roundLinesCount - 1);
  58. serverRoundNo++;
  59. roundLinesCount = 1;
  60. }
  61. parseData(elements, serverRoundNo);
  62. int progress = (int) (((float) serverLineCounter /(float) lineTotal) * 100F);
  63. System.out.print("\r");
  64. System.out.print(progress + " %");
  65. }
  66. //roundLineCounters.add(roundLinesCount);
  67. System.out.println();
  68. System.out.println("No of Rounds " + serverRoundNo);
  69. System.out.println(users.size() + " users found");
  70. System.out.println(hashtags.size() + " hashtags found");
  71. //printOut();
  72. System.out.println();
  73. br.close();
  74. DONE = true;
  75. }
  76. private static void parseData(String[] elements, int onRound) {
  77. Round r = rounds.get(onRound);
  78. if(r == null) {
  79. r = new Round(onRound);
  80. rounds.put(onRound, r);
  81. }
  82. long id = Long.parseLong(elements[1]);
  83. User u = users.get(id);
  84. if(u == null) {
  85. u = new User(id);
  86. users.put(id, u);
  87. }
  88. String[] hashtagNames = elements[2].split(" ");
  89. Hashtag[] parsedHashtags = new Hashtag[hashtagNames.length];
  90. for(int i = 0; i < hashtagNames.length; i++) {
  91. parsedHashtags[i] = hashtags.get(hashtagNames[i]);
  92. if(parsedHashtags[i] == null) {
  93. parsedHashtags[i] = new Hashtag(hashtagNames[i]);
  94. hashtags.put(hashtagNames[i], parsedHashtags[i]);
  95. }
  96. }
  97. r.addUser(u);
  98. u.addRound(r);
  99. for(Hashtag h : parsedHashtags) {
  100. r.addHashtag(h);
  101. u.addHashtag(h);
  102. h.addUser(u);
  103. h.addRound(r);
  104. }
  105. }
  106. }