public async Task <IEnumerable <AnalogyLogMessage> > Process(IAnalogyOfflineDataProvider fileDataProvider, string filename, CancellationToken token) { FileName = filename; if (string.IsNullOrEmpty(FileName)) { return(new List <AnalogyLogMessage>()); } if (!DataWindow.ForceNoFileCaching && FileProcessingManager.Instance.AlreadyProcessed(FileName) && Settings.EnableFileCaching) //get it from the cache { var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName); DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName)); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(cachedMessages); } if (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName)) { while (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName)) { await Task.Delay(1000); } var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName); DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName)); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(cachedMessages); } //otherwise read file: try { FileProcessingManager.Instance.AddProcessingFile(FileName); if (!DataWindow.DoNotAddToRecentHistory) { Settings.AddToRecentFiles(fileDataProvider.ID, FileName); } var messages = (await fileDataProvider.Process(filename, token, DataWindow).ConfigureAwait(false)).ToList(); FileProcessingManager.Instance.DoneProcessingFile(messages.ToList(), FileName); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(messages); } catch (Exception e) { AnalogyLogger.Intance.LogCritical("Analogy", $"Error parsing file: {e}"); AnalogyLogMessage error = new AnalogyLogMessage($"Error reading file {filename}: Error: {e.Message}", AnalogyLogLevel.Error, AnalogyLogClass.General, "Analogy", "None"); error.Source = nameof(FileProcessor); error.Module = "Analogy"; DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName); return(new List <AnalogyLogMessage> { error }); } }
public async Task <IEnumerable <AnalogyLogMessage> > Process(IAnalogyOfflineDataProvider fileDataProvider, string filename, CancellationToken token, bool isReload = false) { //TODO in case of zip recursive call on all extracted files FileName = filename; if (string.IsNullOrEmpty(FileName)) { return(new List <AnalogyLogMessage>()); } if (!isReload && !DataWindow.ForceNoFileCaching && FileProcessingManager.Instance.AlreadyProcessed(FileName) && Settings.EnableFileCaching) //get it from the cache { var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName); DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName)); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(cachedMessages); } if (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName)) { while (FileProcessingManager.Instance.IsFileCurrentlyBeingProcessed(FileName)) { await Task.Delay(1000); } var cachedMessages = FileProcessingManager.Instance.GetMessages(FileName); DataWindow.AppendMessages(cachedMessages, Utils.GetFileNameAsDataSource(FileName)); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(cachedMessages); } //otherwise read file: try { if (fileDataProvider.CanOpenFile(filename)) //if can open natively: add to processing and process { FileProcessingManager.Instance.AddProcessingFile(FileName); if (!DataWindow.DoNotAddToRecentHistory) { Settings.AddToRecentFiles(fileDataProvider.ID, FileName); } var messages = (await fileDataProvider.Process(filename, token, DataWindow).ConfigureAwait(false)) .ToList(); FileProcessingManager.Instance.DoneProcessingFile(messages.ToList(), FileName); if (messages.Any()) { lastNewestMessage = messages.Select(m => m.Date).Max(); } OnFileReadingFinished?.Invoke(this, EventArgs.Empty); if (LogWindow != null) { Interlocked.Decrement(ref LogWindow.fileLoadingCount); } return(messages); } else //cannot open natively. is it compressed file? { if (Utils.IsCompressedArchive(filename)) { var compressedMessages = new List <AnalogyLogMessage>(); string extractedPath = UnzipFilesIntoTempFolder(filename, fileDataProvider); if (string.IsNullOrEmpty(extractedPath)) { string msg = $"File is not supported by {fileDataProvider}. File: {filename}"; AnalogyLogger.Instance.LogCritical("Analogy", msg); AnalogyLogMessage error = new AnalogyErrorMessage(msg, "Analogy"); error.Source = nameof(FileProcessor); error.Module = "Analogy"; DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName); return(new List <AnalogyLogMessage> { error }); } CleanupManager.Instance.AddFolder(extractedPath); var files = Directory.GetFiles(extractedPath); files.ForEach(async file => { var messages = await Process(fileDataProvider, file, token, isReload); compressedMessages.AddRange(messages); }); return(compressedMessages); } else { string msg = $"File is not supported by {fileDataProvider}. File: {filename}"; AnalogyLogger.Instance.LogCritical("Analogy", msg); AnalogyLogMessage error = new AnalogyErrorMessage(msg, "Analogy"); error.Source = nameof(FileProcessor); error.Module = "Analogy"; DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName); return(new List <AnalogyLogMessage> { error }); } } } catch (Exception e) { AnalogyLogger.Instance.LogCritical("Analogy", $"Error parsing file: {e}"); var error = new AnalogyErrorMessage($"Error reading file {filename}: Error: {e.Message}", "Analogy"); error.Source = nameof(FileProcessor); error.Module = "Analogy"; DataWindow.AppendMessage(error, fileDataProvider.GetType().FullName); return(new List <AnalogyLogMessage> { error }); } }