-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
369 lines (316 loc) · 11.9 KB
/
Program.cs
File metadata and controls
369 lines (316 loc) · 11.9 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.IO.Compression;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
using System.Globalization;
namespace convertLogs
{
class Program
{
static string Version => "0.9";
static string InputDir = null;
static string OutputDir = null;
static Output OutputType = Output.None;
static void Main(string[] args)
{
Console.WriteLine($"-- convertLogs v{Version} --\n");
//drag and drop
if (args.Length == 1)
args = new string[] { "-f", args[0] };
if (GetArgs(args) && CheckPreconditions())
{
Console.WriteLine($"Extracting to directory {OutputDir}...\n");
DecompressAndConvert(InputDir, OutputDir, OutputType);
Console.WriteLine("\nComplete.");
}
}
static bool GetArgs(string[] args)
{
Dictionary<string, string> options = new Dictionary<string, string>();
if (args.Length == 0)
{
Console.WriteLine($" ARGUMENTS");
Console.WriteLine($" -f <input directory or zip file> (required)");
Console.WriteLine($" -o <output directory> (auto-generated if not included)");
Console.WriteLine(@$" -t <output type> (""none"" or ""json"", defaults to ""none"")");
return false;
}
else if (args.Length % 2 != 0)
{
Console.WriteLine("ERROR: Invalid argument count.");
return false;
}
else
{
for (int i = 0; i < args.Length; i += 2)
options[args[i]] = args[i + 1];
if (!options.ContainsKey("-f"))
{
Console.WriteLine($"ERROR: Input directory cannot be null");
return false;
}
else
{
InputDir = options["-f"];
}
OutputDir = options.ContainsKey("-o") ? options["-o"] : InputDir + "_converted";
OutputDir = OutputDir.Replace(".zip_converted", "_converted");
OutputType = (Output)(options.ContainsKey("-t") ? Enum.Parse(typeof(Output), options["-t"], true) : Output.None);
return true;
}
}
static bool CheckPreconditions()
{
List<string> errors = new List<string>();
try
{
Path.GetFullPath(InputDir);
}
catch
{
Console.WriteLine($@"Invalid input path ""{InputDir}""");
}
try
{
Path.GetFullPath(OutputDir);
}
catch
{
Console.WriteLine($@"Invalid output path ""{OutputDir}""");
}
if (File.Exists(InputDir))
{
if (!InputDir.EndsWith(".zip"))
{
errors.Add($@"Invalid input file ""{InputDir}""");
}
}
else if (!Directory.Exists(InputDir))
{
errors.Add($@"Invalid input directory ""{InputDir}""");
}
OutputDir = GetOutputDirectory(OutputDir);
Directory.CreateDirectory(OutputDir);
if (errors.Count == 0)
return true;
foreach (string error in errors)
Console.WriteLine("ERROR: " + error);
return false;
}
static bool IsCompleteLog(string fileName)
{
return !fileName.Contains("part");
}
static string[] FileList(string path)
{
FileAttributes attr = File.GetAttributes(path);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
return Directory.GetFiles(path, "*", SearchOption.AllDirectories);
else if (path.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
{
string outdir = GetOutputDirectory(path);
ZipFile.ExtractToDirectory(path, outdir, true);
return Directory.GetFiles(outdir, "*", SearchOption.AllDirectories);
}
else
return new string[] { path };
}
static string GetOutputDirectory(string path)
{
string dirname = path.EndsWith(".zip") ? Path.GetFileNameWithoutExtension(path) : path;
int i = 0;
while (!DirectoryIsEmpty(MakeDirName(dirname, i)))
{
i++;
}
dirname = MakeDirName(dirname, i);
return dirname;
}
static bool DirectoryIsEmpty(string dir)
{
if (!Directory.Exists(dir)) return true;
if (Directory.GetFiles(dir, "*", SearchOption.AllDirectories).Length == 0) return true;
return false;
}
static string MakeDirName(string dirname, int i) => i > 0 ? dirname + "_" + i : dirname;
static string[] ToArray(string path)
{
if (File.Exists(path))
{
return new string[] { path };
}
else
{
return Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
}
}
static string[] DecompressLogs(string[] filePaths)
{
List<string> compressedFiles = filePaths.Where(f => f.EndsWith(".gz")).ToList();
List<string> decompressedFiles = filePaths.Where(f => !f.EndsWith(".gz")).ToList();
foreach (string file in compressedFiles.ToArray())
{
string decompFile = Path.GetFileNameWithoutExtension(file);
using (FileStream reader = File.OpenRead(file))
using (GZipStream zip = new GZipStream(reader, CompressionMode.Decompress))
using (StreamReader unzip = new StreamReader(zip))
File.WriteAllText(decompFile, unzip.ReadToEnd());
compressedFiles.Remove(file);
decompressedFiles.Add(decompFile);
}
return decompressedFiles.ToArray();
}
static DateTime? ExtractDate(string path)
{
string fileName = Path.GetFileName(path);
string[] parts = fileName.Split('.');
if (parts.Length < 6)
return DateTime.MinValue;
string dateStr = "";
string formatStr = "";
if (IsCompleteLog(path))
{
dateStr = string.Join(".", parts.Take(4));
formatStr = "yyyy.M.d.H";
}
else
{
dateStr = parts[3] + "." + parts[4];
formatStr = "yyyy.M.d.H.mm";
}
try
{
return DateTime.ParseExact(dateStr, formatStr, CultureInfo.InvariantCulture);
}
catch
{
Console.WriteLine("Unable to extract date from " + path);
return null;
}
}
static void ConvertFile(string filePath, FileWriter writer)
{
string[] lines = File.ReadAllLines(filePath);
string totalLine = "";
foreach (string line in lines)
{
totalLine += line;
if (line.EndsWith("}"))
{
try
{
JObject data = JObject.Parse(totalLine.Replace("\n", ""));
writer.WriteToFile(data);
totalLine = "";
}
catch
{
continue;
// fail silently and try to parse with the next line
}
}
}
}
static void Convert(IEnumerable<string> filePaths, string outputDirectory, Output oType)
{
List<string> newPaths = filePaths.Where(f => ExtractDate(f).HasValue).ToList();
newPaths.Sort((a, b) => DateTime.Compare(ExtractDate(a).Value, ExtractDate(b).Value));
FileWriter writer = new FileWriter(outputDirectory, oType);
foreach (string path in newPaths)
{
Console.WriteLine("Converting " + path);
ConvertFile(path, writer);
}
}
static void DecompressAndConvert(string path, string outputDirectory, Output oType)
{
string[] logs = FileList(path);
string[] readyToConvert = DecompressLogs(logs);
Convert(readyToConvert, outputDirectory, oType);
foreach (string file in readyToConvert)
File.Delete(file);
}
// -----------------------------------------------------------
enum Output { None, Json }
class FileWriter
{
public string OutputDirectory;
public Dictionary<string, StreamWriter> OpenFiles = new Dictionary<string, StreamWriter>();
public FileHelper Helper;
public FileWriter(string outputDir, Output outputType)
{
OutputDirectory = outputDir;
if (outputType == Output.Json)
Helper = new JsonFileHelper(outputDir);
else
Helper = new FlatFileHelper(outputDir);
}
public StreamWriter GetOpenFile(string fileName)
{
if (!OpenFiles.ContainsKey(fileName))
{
string dir = Path.GetDirectoryName(fileName);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
OpenFiles[fileName] = new StreamWriter(Path.GetFullPath(fileName)) { AutoFlush = true };
}
return OpenFiles[fileName];
}
public void WriteToFile(JObject data)
{
string fileName = Helper.GetFileName(data);
StreamWriter learnLog = GetOpenFile(fileName);
Helper.Write(learnLog, data);
}
}
abstract class FileHelper
{
public string OutputDir;
public FileHelper(string outputDir)
{
OutputDir = outputDir;
}
public abstract void Write(StreamWriter fs, JObject data);
public abstract string GetFileName(JObject data);
}
class FlatFileHelper : FileHelper
{
public FlatFileHelper(string outputDir) : base(outputDir) { }
public override void Write(StreamWriter fs, JObject data)
{
fs.WriteLine(data["message"].ToString());
}
public override string GetFileName(JObject data)
{
string oldPath = "/usr/local/blackboard";
string newPath = Path.Combine(Path.GetFullPath(OutputDir), data["host"].ToString());
string path = data["path"].ToString();
if (path.StartsWith(oldPath))
{
return path.Replace(oldPath, newPath);
}
else
{
return Path.Combine(newPath, path);
}
}
}
class JsonFileHelper : FileHelper
{
public JsonFileHelper(string outputDir) : base(outputDir) { }
public override void Write(StreamWriter fs, JObject data)
{
fs.WriteLine(data.ToString(Formatting.None));
fs.Flush();
}
public override string GetFileName(JObject data)
{
return Path.Combine(OutputDir, data["host"].ToString()) + "/logs.json";
}
}
}
}