logsearcherViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.IO;
  6. using System.Runtime.CompilerServices;
  7. using System.Security.AccessControl;
  8. using System.Text.RegularExpressions;
  9. using System.Threading.Tasks;
  10. using System.Windows.Input;
  11. using com.sun.java.swing.plaf.windows;
  12. using Microsoft.Win32;
  13. using Microsoft.Xaml.Behaviors.Core;
  14. using NPOI.XSSF.UserModel.Charts;
  15. namespace PRSServer;
  16. public class OutputData
  17. {
  18. public string Directory { get; set; }
  19. public string DisplayData { get; set; }
  20. public string LineData { get; set; }
  21. public int LineNumber { get; set; }
  22. public ICommand OpenFileCommand { get; set; }
  23. public OutputData()
  24. {
  25. OpenFileCommand = new ActionCommand(OpenFile);
  26. }
  27. public void OpenFile()
  28. {
  29. logsearcherViewModel.LoadFile(LineNumber, Directory);
  30. }
  31. }
  32. public class logsearcherViewModel : INotifyPropertyChanged
  33. {
  34. public string SearchString { get; set; }
  35. public static int LineBuffer { get; set; }
  36. public static string Folder { get; set; }
  37. public ObservableCollection<OutputData> Output { get; set; }
  38. public ICommand SearchCommand { get; set; }
  39. public static String FileName { get; set; }
  40. public static int Line { get; set; }
  41. public static ObservableCollection<String> FileInfo { get; set; }
  42. public logsearcherViewModel()
  43. {
  44. SearchCommand = new ActionCommand(Search);
  45. Output = new ObservableCollection<OutputData>();
  46. FileInfo = new ObservableCollection<String>();
  47. LineBuffer = 10;
  48. }
  49. public static void LoadFile(int LineNumber, string FileLocation)
  50. {
  51. if (FileLocation == "")
  52. {
  53. FileInfo.Clear();
  54. }
  55. else
  56. {
  57. Line = LineNumber;
  58. FileName = FileLocation;
  59. String[] TempFile = System.IO.File.ReadAllLines(FileName);
  60. FileInfo.Clear();
  61. if (LineBuffer < 0)
  62. {
  63. FileInfo.Add("Please Provide a positive line buffer");
  64. }
  65. else
  66. {
  67. for (int i = LineNumber - LineBuffer; i < LineNumber + LineBuffer + 1; i++)
  68. {
  69. if (i > 0 && i < TempFile.Length)
  70. {
  71. FileInfo.Add(TempFile[i]);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. private void Search()
  78. {
  79. Output.Clear();
  80. Task.Run(() => { SearchDir(SearchString, Folder, SendOutput); });
  81. }
  82. private void SendOutput(string text, string fileDirectory, int LineNumber, bool Succeed)
  83. {
  84. App.Current.Dispatcher.Invoke(() =>
  85. {
  86. OutputData data = new OutputData();
  87. data.Directory = fileDirectory;
  88. data.DisplayData = Convert.ToString(LineNumber) + text;
  89. data.LineNumber = LineNumber;
  90. if (Succeed) data.LineData = "O";
  91. else data.LineData = "X";
  92. if (fileDirectory != "") data.LineData = " ";
  93. data.LineData += " ";
  94. Output.Add(data);
  95. });
  96. }
  97. public void SearchDir(string keyword, string folderDirectory, Action<String, String, int, bool> report)
  98. {
  99. if (keyword == "")
  100. {
  101. report("No keyword specified", "", 0, false);
  102. }
  103. else if (keyword == " ")
  104. {
  105. report("Invalid keyword specified", "", 0, false);
  106. }
  107. else
  108. {
  109. if (!Directory.Exists(@folderDirectory))
  110. {
  111. report("Directory does not exist: " + folderDirectory, "", 0, false);
  112. }
  113. else
  114. {
  115. string path = @folderDirectory;
  116. string[] logFiles = Directory.GetFiles(path, "*.log");
  117. for (int i = logFiles.Length - 1; i >= 0; i--)
  118. {
  119. string newPath = logFiles[i];
  120. string fileName = Path.GetFileName(newPath);
  121. List<(string, int)> results = new List<(string, int)>();
  122. string[] CurrentFile = File.ReadAllLines(newPath);
  123. for (int j = 0; j < CurrentFile.Length; j++)
  124. {
  125. string line = CurrentFile[j];
  126. if (Regex.IsMatch(line.ToUpper(), keyword.ToUpper()))
  127. {
  128. results.Add((line, j));
  129. }
  130. }
  131. if (results.Count > 0)
  132. {
  133. report($"{fileName}: found {results.Count} matches", "", 0, true);
  134. for (int j = 0; j < results.Count; j++)
  135. {
  136. (string, int) result = results[j];
  137. report(" " + result.Item1, newPath, result.Item2, false);
  138. }
  139. }
  140. else
  141. {
  142. report($"{fileName}: found no matches", "", 0, false);
  143. }
  144. }
  145. }
  146. ;
  147. }
  148. }
  149. public event PropertyChangedEventHandler? PropertyChanged;
  150. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  151. {
  152. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  153. }
  154. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  155. {
  156. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  157. field = value;
  158. OnPropertyChanged(propertyName);
  159. return true;
  160. }
  161. }