-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandler.cs
320 lines (276 loc) · 9.62 KB
/
Handler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text.Json;
using SmallTool_MIP.Models;
using System.Linq;
using System.Data.SQLite;
using System.Data;
namespace SmallTool_MIP
{
class Handler
{
private readonly string ProgramLocation = System.AppDomain.CurrentDomain.BaseDirectory;
private string CurrentLogFile;
private static SQLiteConnection db = new SQLiteConnection("DataSource = database.db; Version = 3;");
private readonly static Dictionary<string, string> ColumnFilter = InitializeColumnFilter();
Regex LogSplit = new Regex("(?:^|\t)([\t]\"[^\"\t](?:[^\"])*\"[\t]|[^\t]*)", RegexOptions.Compiled);
Regex TimeSplit = new Regex("(?:^|\t)([\t]\"[^\"\t](?:[^\"])*\\][\t]|[^\t]*)", RegexOptions.Compiled);
public void InitializeLogFile()
{
string LogPath = ProgramLocation + @"\mipLogs\";
Directory.CreateDirectory(LogPath);
string[] LogFiles = Directory.GetFiles(LogPath, "Log-*");
if (LogFiles.Length > 0)
{
List<string> Files = LogFiles.ToList();
int FileNumber = 0;
foreach (string file in Files)
{
Int32.TryParse(file.Split('\\')[^1].Split('-')[^1].Split('.')[0], out int TempFileNumber);
if (TempFileNumber > FileNumber) { FileNumber = TempFileNumber; }
}
FileNumber++;
CurrentLogFile = LogPath + "Log-" + FileNumber + ".log";
File.Create(CurrentLogFile).Close();
}
else
{
CurrentLogFile = LogPath + @"Log-1.log";
File.Create(CurrentLogFile).Close();
}
}
//try to complete the validated MIP folder path
public string LocationValidator(string Location, bool LogOnly)
{
if (!File.Exists(Location) & !Directory.Exists(Location))
{
return "";
}
//check folder
if (!LogOnly)
{
try
{
//check mip\logs folder
if (Directory.Exists(Location + @"\logs\"))
{
Location += @"\logs\";
}
//check logs folder
if (!Directory.EnumerateFiles(Location, "*.miplog").Any())
{
return "";
}
else
{
return (Location);
}
}
catch (Exception e)
{
File.AppendAllText(Location, DateTime.Now.ToString() + e + "\n");
}
}
else
//check file
{
if (!File.Exists(Location))
{
return "";
}
else
{
return Location;
}
}
return "";
}
public string Serialize(Object input)
{
string Output = JsonSerializer.Serialize(input, new JsonSerializerOptions());
return Output;
}
public MIP_Rules DeserializeRules(string ProgramLocation)
{
try
{
return JsonSerializer.Deserialize<MIP_Rules>(File.ReadAllText(ProgramLocation + "rules.json"));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return new MIP_Rules();
}
}
public void TxtLogger(string[] input)
{
try
{
foreach (string item in input)
{
File.AppendAllText(CurrentLogFile, DateTime.Now.ToString() + " " + item + "\n");
//Trace.Flush();
}
}
catch (Exception e)
{
File.AppendAllText(CurrentLogFile, DateTime.Now.ToString() + e);
}
}
public void TxtLogger(string input)
{
try
{
File.AppendAllText(CurrentLogFile, DateTime.Now.ToString() + " " + input + "\n");
//Trace.Flush();
}
catch (Exception e)
{
File.AppendAllText(CurrentLogFile, DateTime.Now.ToString() + e + "\n");
}
}
private static DataTable DBSet()
{
db.Open();
string sql = "select * from mipDB";
SQLiteCommand command = new SQLiteCommand(sql, db);
SQLiteDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable("Filter");
if (reader.HasRows)
{
dt.Load(reader);
}
db.Close();
return dt;
}
private static Dictionary<string,string> InitializeColumnFilter()
{
db.Open();
string sql = "select * from mipDB";
SQLiteCommand command = new SQLiteCommand(sql, db);
SQLiteDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable("Filter");
if (reader.HasRows)
{
dt.Load(reader);
}
db.Close();
Dictionary<string, string> output = new Dictionary<string, string>();
foreach (DataRow row in dt.Rows)
{
output.Add(row["name"].ToString(), row["value"].ToString());
}
return output;
}
public bool IsFiltered(string input, string KeyWord)
{
return input.Contains(ColumnFilter[KeyWord]);
}
public bool EventFilter(string input, string KeyWord)
{
return ColumnFilter[KeyWord].Contains(input);
}
public bool IsFilteredArray(string input, string KeyWord)
{
bool flag = false;
string[] keywords=ColumnFilter[KeyWord].Split(','); ;
foreach(string keyword in keywords)
{
if (input.Contains(keyword))
{
flag = true;
}
}
return flag;
}
public bool IsStart(string input, string KeyWord)
{
return input.StartsWith(ColumnFilter[KeyWord]);
}
public bool IsEqual(string input, string KeyWord)
{
return (input==ColumnFilter[KeyWord]);
}
//split log using Regex
public string[] SplitLog(string item)
{
List<string> list = new List<string>();
string curr = null;
foreach (Match match in LogSplit.Matches(item))
{
curr = match.Value;
if (0 == curr.Length)
{
list.Add("");
}
list.Add(curr.TrimStart(' '));
}
return list.ToArray();
}
public Dictionary<int, string> ParseMIPLog(string[] RawContent)
{
// Add line number to log file
Dictionary<int, string> ParsedContent = new Dictionary<int, string>();
foreach (var Item in RawContent.Select((value, i) => (value, i)))
{
string Line = Item.value;
int Index = Item.i + 1;
if (Line.Length > 0)
{
//string[] LineInfo = { Index.ToString(), Line };
ParsedContent.Add(Index, Line);
}
}
return ParsedContent;
}
public Dictionary<int, string> TimeFilter(Dictionary<int, string> ParsedContent, DateTime start, DateTime end)
{
Dictionary<int, string> output = new Dictionary<int, string>();
DateTime current = new DateTime();
foreach (KeyValuePair<int, string> entry in ParsedContent)
{
String[] list = SplitLog(entry.Value);
if (list.Length > 1) //Try get time from every single line
{
string timeString = list[1].Trim();
DateTime result;
if (DateTime.TryParse(timeString, out result)) //try update current time
{
current = result;
}
}
if (start <= current && current <= end)
{
output.Add(entry.Key, entry.Value);
}
}
return output;
}
public Dictionary<int, string> ConnectionFilter(MIP_TelemetryObject inputTelemetry)
{
Dictionary<int,string> output = new Dictionary<int, string>();
foreach (KeyValuePair<int, string> item in inputTelemetry.TelemetryList)
{
if (IsFilteredArray(item.Value, "MIP_RequestFilter"))
{
output.Add(item.Key,item.Value) ;
}
}
return output;
}
public bool TelemetryHealthy(MIP_TelemetryObject input)
{
foreach (KeyValuePair<int, string> item in input.TelemetryList) // scan error related information
{
if (IsFiltered(item.Value, "MIP_TelemetryError_1") || IsFiltered(item.Value, "MIP_TelemetryError_2"))
{
return false;
}
}
return true;
}
}
}