SecondPhase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 gethoursDiagram(String directory, String outputFile, String OnlineFile)
  12. {
  13. string line;
  14. List<String> fileEntries = DirSearch(directory);
  15. List<Counter> list1 = new List<Counter>();
  16. List<Counter> listOnline = new List<Counter>();
  17. foreach (String url in fileEntries)
  18. {
  19. String[] sp = { "/", "_" };
  20. Console.WriteLine(url);
  21. String[] spRes = url.Split(sp, StringSplitOptions.None);
  22. FileInfo fileToDecompress = new FileInfo(url);
  23. Zipper.Decompress(fileToDecompress);
  24. // Read the file and display it line by line.
  25. System.IO.StreamReader file =
  26. new System.IO.StreamReader(url.Replace(".gz", ""));
  27. while ((line = file.ReadLine()) != null)
  28. {
  29. String[] spliters = { "#" };
  30. String[] parts = line.Split(spliters, StringSplitOptions.None);
  31. //DateTime dateTime = DateTime.Parse(parts[0]);
  32. int y = Convert.ToInt32(spRes[spRes.Length - 4]);
  33. int m = Convert.ToInt32(spRes[spRes.Length - 3]);
  34. int d = Convert.ToInt32(spRes[spRes.Length - 2]);
  35. int h = Convert.ToInt32(spRes[spRes.Length-1].Replace(".txt.gz", ""));
  36. DateTime dateTime = new DateTime(y, m, d, h, 0, 0);
  37. parts[1] = parts[1].Replace("\t", "");
  38. parts[1] = parts[1].Replace(" ", "");
  39. parts[2] = parts[2].Replace("\t", "");
  40. parts[2] = parts[2].Replace(" ", "");
  41. Boolean found1 = false;
  42. foreach (Counter counter in listOnline)
  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. listOnline.Add(counter1);
  55. }
  56. Boolean found = false;
  57. foreach (Counter counter in list1)
  58. {
  59. if (counter.isTheHour(dateTime))
  60. {
  61. counter.increase(parts[1]);
  62. counter.increase(parts[2]);
  63. found = true;
  64. }
  65. }
  66. if (!found)
  67. {
  68. Counter counter1 = new Counter(dateTime);
  69. counter1.increase(parts[1]);
  70. counter1.increase(parts[2]);
  71. list1.Add(counter1);
  72. }
  73. }
  74. file.Close();
  75. File.Delete(url.Replace(".gz", ""));
  76. }
  77. System.IO.StreamWriter finalfile = new System.IO.StreamWriter(outputFile);
  78. System.IO.StreamWriter Onlinefile = new System.IO.StreamWriter(OnlineFile);
  79. List<Counter> SortedList = list1.OrderBy(o => o.datetime).ToList();
  80. List<Counter> SortedListOnline = listOnline.OrderBy(o => o.datetime).ToList();
  81. foreach (Counter counter in SortedList)
  82. {
  83. finalfile.WriteLine(counter.datetime + ".." + counter.count);
  84. }
  85. finalfile.Close();
  86. foreach (Counter counter in SortedListOnline)
  87. {
  88. Onlinefile.WriteLine(counter.datetime + ".." + counter.count);
  89. }
  90. Onlinefile.Close();
  91. Console.WriteLine("Reporting DONE !!!!!!");
  92. }
  93. private static List<String> DirSearch(string sDir)
  94. {
  95. List<String> files = new List<String>();
  96. try
  97. {
  98. foreach (string f in Directory.GetFiles(sDir))
  99. {
  100. files.Add(f);
  101. }
  102. foreach (string d in Directory.GetDirectories(sDir))
  103. {
  104. files.AddRange(DirSearch(d));
  105. }
  106. }
  107. catch (System.Exception excpt)
  108. {
  109. }
  110. return files;
  111. }
  112. }
  113. }