SecondPhase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace ConsoleApplication6.SecondPhase
  8. {
  9. class SecondPhase
  10. {
  11. public static void gethoursDiagramSepPortsOnline(String directory, String File16470, String File16471)
  12. {
  13. string line;
  14. List<String> fileEntries = DirSearch(directory);
  15. List<Counter> list16470 = new List<Counter>();
  16. List<Counter> list16471 = new List<Counter>();
  17. foreach (String url in fileEntries)
  18. {
  19. String[] sp = { "/", "_" };
  20. String[] spRes = url.Split(sp, StringSplitOptions.None);
  21. FileInfo fileToDecompress = new FileInfo(url);
  22. Zipper.Decompress(fileToDecompress);
  23. // Read the file and display it line by line.
  24. System.IO.StreamReader file =
  25. new System.IO.StreamReader(url.Replace(".gz", ""));
  26. while ((line = file.ReadLine()) != null)
  27. {
  28. String[] spliters = { "#" };
  29. String[] parts = line.Split(spliters, StringSplitOptions.None);
  30. int y = Convert.ToInt32(spRes[spRes.Length - 4]);
  31. int m = Convert.ToInt32(spRes[spRes.Length - 3]);
  32. int d = Convert.ToInt32(spRes[spRes.Length - 2]);
  33. int h = Convert.ToInt32(spRes[spRes.Length - 1].Replace(".txt.gz", ""));
  34. DateTime dateTime = new DateTime(y, m, d, h, 0, 0);
  35. parts[1] = parts[1].Replace("\t", "");
  36. parts[1] = parts[1].Replace(" ", "");
  37. parts[2] = parts[2].Replace("\t", "");
  38. parts[2] = parts[2].Replace(" ", "");
  39. Boolean found1 = false;
  40. if (parts[1].Contains("16471"))
  41. {
  42. foreach (Counter counter in list16471)
  43. {
  44. if (counter.isTheHour(dateTime))
  45. {
  46. counter.increase(parts[1]);
  47. found1 = true;
  48. }
  49. }
  50. if (!found1)
  51. {
  52. Counter counter1 = new Counter(dateTime);
  53. counter1.increase(parts[1]);
  54. list16471.Add(counter1);
  55. }
  56. }
  57. Boolean found = false;
  58. if (parts[1].Contains("16470"))
  59. {
  60. foreach (Counter counter in list16470)
  61. {
  62. if (counter.isTheHour(dateTime))
  63. {
  64. counter.increase(parts[1]);
  65. found = true;
  66. }
  67. }
  68. if (!found)
  69. {
  70. Counter counter1 = new Counter(dateTime);
  71. counter1.increase(parts[1]);
  72. list16470.Add(counter1);
  73. }
  74. }
  75. }
  76. file.Close();
  77. File.Delete(url.Replace(".gz", ""));
  78. }
  79. System.IO.StreamWriter finalfile = new System.IO.StreamWriter(File16470);
  80. System.IO.StreamWriter Onlinefile = new System.IO.StreamWriter(File16471);
  81. List<Counter> SortedList16470 = list16470.OrderBy(o => o.datetime).ToList();
  82. List<Counter> SortedList16471 = list16471.OrderBy(o => o.datetime).ToList();
  83. foreach (Counter counter in SortedList16470)
  84. {
  85. finalfile.WriteLine(counter.datetime + ".." + counter.count);
  86. }
  87. finalfile.Close();
  88. foreach (Counter counter in SortedList16471)
  89. {
  90. Onlinefile.WriteLine(counter.datetime + ".." + counter.count);
  91. }
  92. Onlinefile.Close();
  93. Console.WriteLine("Reporting DONE !!!!!!");
  94. }
  95. private static List<String> DirSearch(string sDir)
  96. {
  97. List<String> files = new List<String>();
  98. try
  99. {
  100. foreach (string f in Directory.GetFiles(sDir))
  101. {
  102. files.Add(f);
  103. }
  104. foreach (string d in Directory.GetDirectories(sDir))
  105. {
  106. files.AddRange(DirSearch(d));
  107. }
  108. }
  109. catch (System.Exception excpt)
  110. {
  111. }
  112. return files;
  113. }
  114. }
  115. }