private void LogFileWatcher_Created(object sender, FileSystemEventArgs e) { if (ProgramHelper.IsSupportedFormat(e.FullPath)) { AddDelayed(e.FullPath); } }
private void LogFileWatcher_Renamed(object sender, RenamedEventArgs e) { if (ProgramHelper.IsTemporaryFormat(e.OldFullPath) && ProgramHelper.IsCompressedFormat(e.FullPath)) { AddDelayed(e.FullPath); } }
/// <summary> /// Invoked when a BackgroundWorker begins working. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BgWorkerDoWork(object sender, DoWorkEventArgs e) { var rowData = e.Argument as GridRow; e.Result = rowData; _runningCount++; ProgramHelper.DoWork(rowData); }
/// <summary> /// Runs the next operation, if one is available /// </summary> private void _RunNextOperation() { if (_logQueue.Count > 0 && (ProgramHelper.ParseMultipleLogs() || !_anyRunning)) { _RunOperation(_logQueue.Dequeue()); } else { if (!_anyRunning) { BtnParse.Enabled = true; BtnClearAll.Enabled = true; BtnCancelAll.Enabled = false; BtnDiscordBatch.Enabled = true; ChkAutoDiscordBatch.Enabled = true; _settingsForm.ConditionalSettingDisable(_anyRunning); AutoUpdateDiscordBatch(); } } }
public ConsoleProgram(IEnumerable <string> logFiles) { if (ProgramHelper.ParseMultipleLogs()) { var splitLogFiles = new List <List <string> >(); var sizeSortedLogFiles = new List <string>(logFiles); for (int i = 0; i < ProgramHelper.GetMaxParallelRunning(); i++) { splitLogFiles.Add(new List <string>()); } sizeSortedLogFiles.Sort((x, y) => { var fInfoX = new FileInfo(x); long xValue = fInfoX.Exists ? fInfoX.Length : 0; var fInfoY = new FileInfo(y); long yValue = fInfoY.Exists ? fInfoY.Length : 0; return(xValue.CompareTo(yValue)); }); int index = 0; foreach (string file in sizeSortedLogFiles) { splitLogFiles[index].Add(file); index = (index + 1) % ProgramHelper.GetMaxParallelRunning(); } Parallel.ForEach(splitLogFiles, files => { foreach (string file in files) { ParseLog(file); } }); } else { foreach (string file in logFiles) { ParseLog(file); } } }
/// <summary> /// Queues an operation. If the 'MultipleLogs' setting is true, operations are run asynchronously /// </summary> /// <param name="operation"></param> private void QueueOrRunOperation(FormOperationController operation) { btnClearAll.Enabled = false; btnParse.Enabled = false; btnCancelAll.Enabled = true; if (Properties.Settings.Default.ParseMultipleLogs && _runningCount < ProgramHelper.GetMaxParallelRunning()) { _RunOperation(operation); } else { if (_anyRunning) { _logQueue.Enqueue(operation); operation.ToPendingState(); } else { _RunOperation(operation); } } }
private void ParseLog(object logFile) { var row = new GridRow(logFile as string, "Ready to parse") { BgWorker = new System.ComponentModel.BackgroundWorker() { WorkerReportsProgress = true } }; try { ProgramHelper.DoWork(row); } catch (CancellationException ex) { Console.WriteLine(ex.InnerException != null ? ex.InnerException.Message : ex.Message); } catch (Exception) { Console.WriteLine("Something terrible has happened"); } }
private static void ParseLog(string logFile) { var operation = new ConsoleOperationController(logFile); try { ProgramHelper.DoWork(operation); operation.FinalizeStatus("Parsing Successful - "); } catch (ProgramException ex) { operation.UpdateProgress(ex.InnerException.Message); operation.FinalizeStatus("Parsing Failure - "); } catch (Exception) { operation.UpdateProgress("Something terrible has happened"); operation.FinalizeStatus("Parsing Failure - "); } finally { ProgramHelper.GenerateTraceFile(operation); } }
private void BtnPopulateFromDirectory(object sender, EventArgs e) { string path = null; using (var fbd = new FolderBrowserDialog()) { fbd.ShowNewFolderButton = false; DialogResult result = fbd.ShowDialog(); if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath)) { path = fbd.SelectedPath; } } if (path != null) { var toAdd = new List <string>(); foreach (string format in ProgramHelper.GetSupportedFormats()) { toAdd.AddRange(Directory.EnumerateFiles(path, "*" + format, SearchOption.AllDirectories)); } AddLogFiles(toAdd); } }
private void ParseLog(string logFile) { var operation = new ConsoleOperationController(logFile, "Ready to parse"); try { ProgramHelper.DoWork(operation); operation.FinalizeStatus("Parsing Successful - "); } catch (ExceptionEncompass ex) { operation.UpdateProgress(ex.GetFinalException().Message); operation.FinalizeStatus("Parsing Failure - "); } catch (Exception) { operation.UpdateProgress("Something terrible has happened"); operation.FinalizeStatus("Parsing Failure - "); } finally { ProgramHelper.GenerateLogFile(operation); } }
/// <summary> /// Queues an operation. If the 'MultipleLogs' setting is true, operations are run asynchronously /// </summary> /// <param name="operation"></param> private void QueueOrRunOperation(FormOperationController operation) { BtnClearAll.Enabled = false; BtnParse.Enabled = false; BtnCancelAll.Enabled = true; BtnDiscordBatch.Enabled = false; ChkAutoDiscordBatch.Enabled = false; if (ProgramHelper.ParseMultipleLogs() && _runningCount < ProgramHelper.GetMaxParallelRunning()) { _RunOperation(operation); } else { if (_anyRunning) { _logQueue.Enqueue(operation); operation.ToPendingState(); } else { _RunOperation(operation); } } }
private void _RunOperation(FormOperationController operation) { _runningCount++; _settingsForm.ConditionalSettingDisable(_anyRunning); operation.ToQueuedState(); var cancelTokenSource = new CancellationTokenSource();// Prepare task Task task = Task.Run(() => { operation.ToRunState(); ProgramHelper.DoWork(operation); }, cancelTokenSource.Token).ContinueWith(t => { cancelTokenSource.Dispose(); _runningCount--; // Exception management if (t.IsFaulted) { if (t.Exception != null) { if (t.Exception.InnerExceptions.Count > 1) { operation.UpdateProgress("Something terrible has happened"); } else { Exception ex = t.Exception.InnerExceptions[0]; if (!(ex is EncompassException)) { operation.UpdateProgress("Something terrible has happened"); } if (!(ex.InnerException is OperationCanceledException)) { operation.UpdateProgress(ex.GetFinalException().Message); } else { operation.UpdateProgress("Operation Aborted"); } } } else { operation.UpdateProgress("Something terrible has happened"); } } if (operation.State == OperationState.ClearOnCancel) { operatorBindingSource.Remove(operation); } else { if (t.IsFaulted) { operation.ToUnCompleteState(); } else if (t.IsCanceled) { operation.UpdateProgress("Operation Aborted"); operation.ToUnCompleteState(); } else if (t.IsCompleted) { operation.ToCompleteState(); } else { operation.UpdateProgress("Something terrible has happened"); operation.ToUnCompleteState(); } } ProgramHelper.GenerateTraceFile(operation); RunNextOperation(); }, TaskScheduler.FromCurrentSynchronizationContext()); operation.SetContext(cancelTokenSource, task); }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnDiscordBatchClick(object sender, EventArgs e) { AddTraceMessage("Sending batch to Discord"); if (Properties.Settings.Default.WebhookURL == null) { MessageBox.Show("Set a discord webhook url in settings first"); return; } var fullDpsReportLogs = new List <FormOperationController>(); foreach (FormOperationController operation in OperatorBindingSource) { if (operation.DPSReportLink != null && operation.DPSReportLink.Contains("https")) { fullDpsReportLogs.Add(operation); } } if (!fullDpsReportLogs.Any()) { MessageBox.Show("Nothing to send"); return; } BtnDiscordBatch.Enabled = false; BtnParse.Enabled = false; // first sort by time fullDpsReportLogs.Sort((x, y) => { return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart))); }); var fullDpsReportsLogsByDate = fullDpsReportLogs.GroupBy(x => DateTime.Parse(x.BasicMetaData.LogStart).Date).ToDictionary(x => x.Key, x => x.ToList()); // split the logs so that a single embed does not reach the discord embed limit and also keep a reasonable size by embed string message = ""; bool start = true; foreach (KeyValuePair <DateTime, List <FormOperationController> > pair in fullDpsReportsLogsByDate) { if (!start) { message += "\r\n"; } start = false; var splitDpsReportLogs = new List <List <FormOperationController> >() { new List <FormOperationController>() }; message += pair.Key.ToString("yyyy-MM-dd") + " - "; List <FormOperationController> curListToFill = splitDpsReportLogs.First(); foreach (FormOperationController controller in pair.Value) { if (curListToFill.Count < 40) { curListToFill.Add(controller); } else { curListToFill = new List <FormOperationController>() { controller }; splitDpsReportLogs.Add(curListToFill); } } foreach (List <FormOperationController> dpsReportLogs in splitDpsReportLogs) { EmbedBuilder embedBuilder = ProgramHelper.GetEmbedBuilder(); embedBuilder.WithCurrentTimestamp(); embedBuilder.WithFooter(pair.Key.ToString("yyyy-MM-dd")); dpsReportLogs.Sort((x, y) => { var categoryCompare = x.BasicMetaData.FightCategory.CompareTo(y.BasicMetaData.FightCategory); if (categoryCompare == 0) { return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart))); } return(categoryCompare); }); string currentSubCategory = ""; var embedFieldBuilder = new EmbedFieldBuilder(); var fieldValue = "I can not be empty"; foreach (FormOperationController controller in dpsReportLogs) { string subCategory = controller.BasicMetaData.FightCategory.GetSubCategoryName(); string toAdd = "[" + controller.BasicMetaData.FightName + "](" + controller.DPSReportLink + ") " + (controller.BasicMetaData.FightSuccess ? " :white_check_mark: " : " :x: ") + ": " + controller.BasicMetaData.FightDuration; if (subCategory != currentSubCategory) { embedFieldBuilder.WithValue(fieldValue); embedFieldBuilder = new EmbedFieldBuilder(); fieldValue = ""; embedBuilder.AddField(embedFieldBuilder); embedFieldBuilder.WithName(subCategory); currentSubCategory = subCategory; } else if (fieldValue.Length + toAdd.Length > 1024) { embedFieldBuilder.WithValue(fieldValue); embedFieldBuilder = new EmbedFieldBuilder(); fieldValue = ""; embedBuilder.AddField(embedFieldBuilder); } else { fieldValue += "\r\n"; } fieldValue += toAdd; } embedFieldBuilder.WithValue(fieldValue); message += new WebhookController(Properties.Settings.Default.WebhookURL, embedBuilder.Build()).SendMessage() + " - "; } } MessageBox.Show(message); // BtnDiscordBatch.Enabled = !_anyRunning; BtnParse.Enabled = !_anyRunning; }
private string DiscordBatch(out List <ulong> ids) { ids = new List <ulong>(); AddTraceMessage("Discord: Sending batch to Discord"); if (Properties.Settings.Default.WebhookURL == null) { AddTraceMessage("Discord: No webhook url given"); return("Set a discord webhook url in settings first"); } var fullDpsReportLogs = new List <FormOperationController>(); foreach (FormOperationController operation in OperatorBindingSource) { if (operation.DPSReportLink != null && operation.DPSReportLink.Contains("https")) { fullDpsReportLogs.Add(operation); } } if (!fullDpsReportLogs.Any()) { AddTraceMessage("Discord: Nothing to send"); return("Nothing to send"); } // first sort by time AddTraceMessage("Discord: Sorting logs by time"); fullDpsReportLogs.Sort((x, y) => { return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart))); }); AddTraceMessage("Discord: Splitting logs by start day"); var fullDpsReportsLogsByDate = fullDpsReportLogs.GroupBy(x => DateTime.Parse(x.BasicMetaData.LogStart).Date).ToDictionary(x => x.Key, x => x.ToList()); // split the logs so that a single embed does not reach the discord embed limit and also keep a reasonable size by embed string message = ""; bool start = true; foreach (KeyValuePair <DateTime, List <FormOperationController> > pair in fullDpsReportsLogsByDate) { if (!start) { message += "\r\n"; } start = false; var splitDpsReportLogs = new List <List <FormOperationController> >() { new List <FormOperationController>() }; message += pair.Key.ToString("yyyy-MM-dd") + " - "; List <FormOperationController> curListToFill = splitDpsReportLogs.First(); AddTraceMessage("Discord: Splitting message to avoid reaching discord's character limit"); foreach (FormOperationController controller in pair.Value) { if (curListToFill.Count < 40) { curListToFill.Add(controller); } else { curListToFill = new List <FormOperationController>() { controller }; splitDpsReportLogs.Add(curListToFill); } } foreach (List <FormOperationController> dpsReportLogs in splitDpsReportLogs) { EmbedBuilder embedBuilder = ProgramHelper.GetEmbedBuilder(); AddTraceMessage("Discord: Creating embed for " + dpsReportLogs.Count + " logs"); var first = DateTime.Parse(dpsReportLogs.First().BasicMetaData.LogStart); var last = DateTime.Parse(dpsReportLogs.Last().BasicMetaData.LogEnd); embedBuilder.WithFooter(pair.Key.ToString("dd/MM/yyyy") + " - " + first.ToString("T") + " - " + last.ToString("T")); AddTraceMessage("Discord: Sorting logs by category"); dpsReportLogs.Sort((x, y) => { int categoryCompare = x.BasicMetaData.FightCategory.CompareTo(y.BasicMetaData.FightCategory); if (categoryCompare == 0) { return(DateTime.Parse(x.BasicMetaData.LogStart).CompareTo(DateTime.Parse(y.BasicMetaData.LogStart))); } return(categoryCompare); }); string currentSubCategory = ""; var embedFieldBuilder = new EmbedFieldBuilder(); string fieldValue = "I can not be empty"; AddTraceMessage("Discord: Building embed body"); foreach (FormOperationController controller in dpsReportLogs) { string subCategory = controller.BasicMetaData.FightCategory.GetSubCategoryName(); string toAdd = "[" + controller.BasicMetaData.FightName + "](" + controller.DPSReportLink + ") " + (controller.BasicMetaData.FightSuccess ? " :white_check_mark: " : " :x: ") + ": " + controller.BasicMetaData.FightDuration; if (subCategory != currentSubCategory) { embedFieldBuilder.WithValue(fieldValue); embedFieldBuilder = new EmbedFieldBuilder(); fieldValue = ""; embedBuilder.AddField(embedFieldBuilder); embedFieldBuilder.WithName(subCategory); currentSubCategory = subCategory; } else if (fieldValue.Length + toAdd.Length > 1024) { embedFieldBuilder.WithValue(fieldValue); embedFieldBuilder = new EmbedFieldBuilder(); fieldValue = ""; embedBuilder.AddField(embedFieldBuilder); embedFieldBuilder.WithName(subCategory); } else { fieldValue += "\r\n"; } fieldValue += toAdd; } embedFieldBuilder.WithValue(fieldValue); AddTraceMessage("Discord: Sending embed"); try { ids.Add(WebhookController.SendMessage(Properties.Settings.Default.WebhookURL, embedBuilder.Build(), out string curMessage)); AddTraceMessage("Discord: embed sent " + curMessage); message += curMessage + " - "; } catch (Exception ex) { AddTraceMessage("Discord: couldn't send embed " + ex.Message); message += ex.Message + " - "; } } } return(message); }