// Called by ScanForNewEntries (from EDJournalClass Scan Tick Worker) to scan a NFI for new entries private void ScanReader(EDJournalReader nfi, List <JournalEntry> entries, List <UIEvent> uientries) { int netlogpos = 0; try { if (nfi.TravelLogUnit.id == 0) { nfi.TravelLogUnit.type = TravelLogUnit.JournalType; nfi.TravelLogUnit.Add(); } netlogpos = nfi.TravelLogUnit.Size; bool readanything = nfi.ReadJournal(out List <JournalReaderEntry> ents, out List <UIEvent> uie, historyrefreshparsing: false, resetOnError: false); uientries.AddRange(uie); if (readanything) // if we read, we must update the travel log pos { //System.Diagnostics.Debug.WriteLine("ScanReader " + Path.GetFileName(nfi.FileName) + " read " + ents.Count + " ui " +uientries.Count + " size " + netlogpos); using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { using (DbTransaction txn = cn.BeginTransaction()) { ents = ents.Where(jre => JournalEntry.FindEntry(jre.JournalEntry, jre.Json).Count == 0).ToList(); foreach (JournalReaderEntry jre in ents) { entries.Add(jre.JournalEntry); jre.JournalEntry.Add(jre.Json, cn, txn); } // System.Diagnostics.Debug.WriteLine("Wrote " + ents.Count() + " to db and updated TLU"); nfi.TravelLogUnit.Update(cn); txn.Commit(); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception " + ex.Message); // Revert and re-read the failed entries if (nfi != null && nfi.TravelLogUnit != null) { nfi.TravelLogUnit.Size = netlogpos; } throw; } }
// Called by ScanForNewEntries (from EDJournalClass Scan Tick Worker) to scan a NFI for new entries private void ScanReader(EDJournalReader nfi, List <JournalEntry> entries) { int netlogpos = 0; try { if (nfi.TravelLogUnit.id == 0) { nfi.TravelLogUnit.type = 3; nfi.TravelLogUnit.Add(); } netlogpos = nfi.TravelLogUnit.Size; List <JournalReaderEntry> ents = nfi.ReadJournalLog().ToList(); //System.Diagnostics.Debug.WriteLine("ScanReader " + Path.GetFileName(nfi.FileName) + " read " + ents.Count + " size " + netlogpos); if (ents.Count > 0) { using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { using (DbTransaction txn = cn.BeginTransaction()) { ents = ents.Where(jre => JournalEntry.FindEntry(jre.JournalEntry, jre.Json).Count == 0).ToList(); foreach (JournalReaderEntry jre in ents) { entries.Add(jre.JournalEntry); jre.JournalEntry.Add(jre.Json, cn, txn); ticksNoActivity = 0; } nfi.TravelLogUnit.Update(cn); txn.Commit(); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception " + ex.Message); // Revert and re-read the failed entries if (nfi != null && nfi.TravelLogUnit != null) { nfi.TravelLogUnit.Size = netlogpos; } throw; } }
private void ScanReader(EDJournalReader nfi, List <JournalEntry> entries) { int netlogpos = 0; try { if (nfi.TravelLogUnit.id == 0) { nfi.TravelLogUnit.type = 3; nfi.TravelLogUnit.Add(); } netlogpos = nfi.TravelLogUnit.Size; List <JournalReaderEntry> ents = nfi.ReadJournalLog().ToList(); if (ents.Count > 0) { using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { using (DbTransaction txn = cn.BeginTransaction()) { ents = ents.Where(jre => JournalEntry.FindEntry(jre.JournalEntry, jre.Json).Count == 0).ToList(); foreach (JournalReaderEntry jre in ents) { entries.Add(jre.JournalEntry); jre.JournalEntry.Add(jre.Json, cn, txn); ticksNoActivity = 0; } nfi.TravelLogUnit.Update(cn); txn.Commit(); } } } } catch { // Revert and re-read the failed entries if (nfi != null && nfi.TravelLogUnit != null) { nfi.TravelLogUnit.Size = netlogpos; } throw; } }
public void ParseJournalFiles(Func <bool> cancelRequested, Action <int, string> updateProgress, bool forceReload = false) { System.Diagnostics.Trace.WriteLine("Scanned " + WatcherFolder); Dictionary <string, TravelLogUnit> m_travelogUnits = TravelLogUnit.GetAll().Where(t => (t.type & 0xFF) == 3).GroupBy(t => t.Name).Select(g => g.First()).ToDictionary(t => t.Name); // order by file write time so we end up on the last one written FileInfo[] allFiles = Directory.EnumerateFiles(WatcherFolder, journalfilematch, SearchOption.AllDirectories).Select(f => new FileInfo(f)).OrderBy(p => p.LastWriteTime).ToArray(); List <EDJournalReader> readersToUpdate = new List <EDJournalReader>(); for (int i = 0; i < allFiles.Length; i++) { FileInfo fi = allFiles[i]; var reader = OpenFileReader(fi, m_travelogUnits); // open it if (!m_travelogUnits.ContainsKey(reader.TravelLogUnit.Name)) { m_travelogUnits[reader.TravelLogUnit.Name] = reader.TravelLogUnit; reader.TravelLogUnit.type = 3; reader.TravelLogUnit.Add(); } if (!netlogreaders.ContainsKey(reader.TravelLogUnit.Name)) { netlogreaders[reader.TravelLogUnit.Name] = reader; } if (forceReload) { // Force a reload of the travel log reader.TravelLogUnit.Size = 0; } if (reader.filePos != fi.Length || i == allFiles.Length - 1) // File not already in DB, or is the last one { readersToUpdate.Add(reader); } } for (int i = 0; i < readersToUpdate.Count; i++) { using (SQLiteConnectionUser cn = new SQLiteConnectionUser(utc: true)) { EDJournalReader reader = readersToUpdate[i]; updateProgress(i * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name); List <JournalReaderEntry> entries = reader.ReadJournalLog(true).ToList(); // this may create new commanders, and may write to the TLU db ILookup <DateTime, JournalEntry> existing = JournalEntry.GetAllByTLU(reader.TravelLogUnit.id).ToLookup(e => e.EventTimeUTC); using (DbTransaction tn = cn.BeginTransaction()) { foreach (JournalReaderEntry jre in entries) { if (!existing[jre.JournalEntry.EventTimeUTC].Any(e => JournalEntry.AreSameEntry(jre.JournalEntry, e, ent1jo: jre.Json))) { jre.JournalEntry.Add(jre.Json, cn, tn); System.Diagnostics.Trace.WriteLine(string.Format("Write Journal to db {0} {1}", jre.JournalEntry.EventTimeUTC, jre.JournalEntry.EventTypeStr)); } } tn.Commit(); } reader.TravelLogUnit.Update(cn); updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name); lastnfi = reader; } } updateProgress(-1, ""); }
// Called by EDJournalClass periodically to scan for journal entries public List <JournalEntry> ScanForNewEntries() { var entries = new List <JournalEntry>(); try { string filename = null; if (lastnfi != null) // always give old file another go, even if we are going to change { if (!File.Exists(lastnfi.FileName)) // if its been removed, null { lastnfi = null; } else { ScanReader(lastnfi, entries); if (entries.Count > 0) { ticksNoActivity = 0; return(entries); // feed back now don't change file } } } if (m_netLogFileQueue.TryDequeue(out filename)) // if a new one queued, we swap to using it { lastnfi = OpenFileReader(new FileInfo(filename)); System.Diagnostics.Debug.WriteLine(string.Format("Change to scan {0}", lastnfi.FileName)); if (lastnfi != null) { ScanReader(lastnfi, entries); // scan new one } } // every few goes, if its not there or filepos is greater equal to length (so only done when fully up to date) else if (ticksNoActivity >= 30 && (lastnfi == null || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length)) { HashSet <string> tlunames = new HashSet <string>(TravelLogUnit.GetAllNames()); string[] filenames = Directory.EnumerateFiles(WatcherFolder, journalfilematch, SearchOption.AllDirectories) .Select(s => new { name = Path.GetFileName(s), fullname = s }) .Where(s => !tlunames.Contains(s.name)) // find any new ones.. .OrderBy(s => s.name) .Select(s => s.fullname) .ToArray(); foreach (var name in filenames) // for any new filenames.. { System.Diagnostics.Debug.WriteLine("No Activity but found new file " + name); lastnfi = OpenFileReader(new FileInfo(name)); break; // stop on first found } if (lastnfi != null) { ScanReader(lastnfi, entries); // scan new one } ticksNoActivity = 0; } ticksNoActivity++; return(entries); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); return(new List <JournalEntry>()); } }
// given a list of files to reparse, read them and store to db or fire them back (and set firebacklastn to make it work) public void ProcessDetectedNewFiles(List <EDJournalReader> readersToUpdate, Action <int, string> updateProgress, Action <JournalEntry, int, int, int, int> fireback = null, int firebacklastn = 0) { for (int i = 0; i < readersToUpdate.Count; i++) { EDJournalReader reader = readersToUpdate[i]; List <JournalEntry> entries = new List <JournalEntry>(); List <UIEvent> uievents = new List <UIEvent>(); bool readanything = reader.ReadJournal(entries, uievents, historyrefreshparsing: true); // this may create new commanders, and may write to the TLU if (fireback != null) { if (readanything) // need to update TLU pos if read anything { reader.TravelLogUnit.Update(); } if (i >= readersToUpdate.Count - firebacklastn) // if within fireback window { for (int e = 0; e < entries.Count; e++) { //System.Diagnostics.Debug.WriteLine("Fire {0} {1} {2} {3} {4} {5}", entries[e].CommanderId, i, readersToUpdate.Count, e, entries.Count, entries[e].EventTypeStr ); fireback(entries[e], i, readersToUpdate.Count, e, entries.Count); } } } else { UserDatabase.Instance.ExecuteWithDatabase(cn => { // only lookup TLUs if there is actually anything to compare against ILookup <DateTime, JournalEntry> existing = entries.Count > 0 ? JournalEntry.GetAllByTLU(reader.ID, cn.Connection).ToLookup(e => e.EventTimeUTC) : null; //System.Diagnostics.Trace.WriteLine(BaseUtils.AppTicks.TickCountLap("PJF"), i + " into db"); using (DbTransaction tn = cn.Connection.BeginTransaction()) { foreach (JournalEntry jre in entries) { //System.Diagnostics.Trace.WriteLine(string.Format("--- Check {0} {1} Existing {2} : {3}", jre.EventTimeUTC, jre.EventTypeStr, existing[jre.EventTimeUTC].Count(), jre.GetJson().ToString())); if (!existing[jre.EventTimeUTC].Any(e => JournalEntry.AreSameEntry(jre, e, cn.Connection, ent1jo: jre.GetJson(cn.Connection, tn)))) { //foreach (var x in existing[jre.EventTimeUTC]) { System.Diagnostics.Trace.WriteLine(string.Format(" passed vs {0} Deepequals {1}", x.GetJson().ToString(), x.GetJson().DeepEquals(jre.GetJson()))); } BaseUtils.JSON.JObject jo = jre.GetJson(cn.Connection, tn); jre.Add(jo, cn.Connection, tn); //System.Diagnostics.Trace.WriteLine(string.Format("Write Journal to db {0} {1}", jre.EventTimeUTC, jre.EventTypeStr)); } else { //System.Diagnostics.Trace.WriteLine(string.Format("Duplicate Journal to db {0} {1}", jre.EventTimeUTC, jre.EventTypeStr)); } } if (readanything) { reader.TravelLogUnit.Update(cn.Connection, tn); } tn.Commit(); } }); } updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.FullName); lastnfi = reader; } updateProgress(-1, ""); }
// Called by EDScanner periodically to scan for journal entries public Tuple <List <JournalEntry>, List <UIEvent> > ScanForNewEntries() { var entries = new List <JournalEntry>(); var uientries = new List <UIEvent>(); string filename = null; if (lastnfi != null) // always give old file another go, even if we are going to change { if (!File.Exists(lastnfi.FullName)) // if its been removed, null { lastnfi = null; } else { //var notdone = new FileInfo(lastnfi.FullName).Length != lastnfi.Pos ? "**********" : ""; System.Diagnostics.Debug.WriteLine($"Scan last nfi {lastnfi.FullName} from {lastnfi.Pos} Length file is {new FileInfo(lastnfi.FullName).Length} {notdone} "); ScanReader(entries, uientries); if (entries.Count > 0 || uientries.Count > 0) { // System.Diagnostics.Debug.WriteLine("ScanFornew read " + entries.Count() + " ui " + uientries.Count()); ticksNoActivity = 0; return(new Tuple <List <JournalEntry>, List <UIEvent> >(entries, uientries)); // feed back now don't change file } } } if (m_netLogFileQueue.TryDequeue(out filename)) // if a new one queued, we swap to using it { lastnfi = OpenFileReader(filename); System.Diagnostics.Debug.WriteLine(string.Format("Change to scan {0}", lastnfi.FullName)); ScanReader(entries, uientries); } else if (ticksNoActivity >= 30 && (lastnfi == null || lastnfi.Pos >= new FileInfo(lastnfi.FullName).Length)) { // every few goes, if its not there or filepos is greater equal to length (so only done when fully up to date) // scan all files in the folder, pick out any new logs, and process the first that is found. try { HashSet <string> tlunames = new HashSet <string>(TravelLogUnit.GetAllNames()); string[] filenames = Directory.EnumerateFiles(WatcherFolder, journalfilematch, SearchOption.AllDirectories) .Select(s => new { name = Path.GetFileName(s), fullname = s }) .Where(s => !tlunames.Contains(s.fullname)) // find any new ones.. .Where(g => new FileInfo(g.fullname).LastWriteTime >= mindateutc) .OrderBy(s => s.name) .Select(s => s.fullname) .ToArray(); if (filenames.Length > 0) { lastnfi = OpenFileReader(filenames[0]); // open first one System.Diagnostics.Debug.WriteLine(string.Format("Found new file {0}", lastnfi.FullName)); ScanReader(entries, uientries); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception during monitor watch file found " + ex); } finally { ticksNoActivity = 0; } } ticksNoActivity++; return(new Tuple <List <JournalEntry>, List <UIEvent> >(entries, uientries)); // feed back now don't change file }
public List <JournalEntry> ScanForNewEntries() { var entries = new List <JournalEntry>(); EDJournalReader nfi = null; try { string filename = null; if (lastnfi != null) { ScanReader(lastnfi, entries); } if (entries.Count != 0) { return(entries); } if (m_netLogFileQueue.TryDequeue(out filename)) // if a new one queued, we swap to using it { nfi = OpenFileReader(new FileInfo(filename)); lastnfi = nfi; System.Diagnostics.Trace.WriteLine(string.Format("Change in file, scan {0}", lastnfi.FileName)); } else if (ticksNoActivity >= 30 && (lastnfi == null || (!File.Exists(lastnfi.FileName) || lastnfi.filePos >= new FileInfo(lastnfi.FileName).Length))) { if (lastnfi == null) { Trace.Write($"No last file - scanning for journals"); } else if (!File.Exists(lastnfi.FileName)) { Trace.WriteLine($"File {lastnfi.FileName} not found - scanning for journals"); } else { // Trace.WriteLine($"No activity on {lastnfi.FileName} for 60 seconds ({lastnfi.filePos} >= {new FileInfo(lastnfi.FileName).Length} - scanning for new journals"); } HashSet <string> tlunames = new HashSet <string>(TravelLogUnit.GetAllNames()); string[] filenames = Directory.EnumerateFiles(m_watcherfolder, "Journal.*.log", SearchOption.AllDirectories) .Select(s => new { name = Path.GetFileName(s), fullname = s }) .Where(s => !tlunames.Contains(s.name)) .OrderBy(s => s.name) .Select(s => s.fullname) .ToArray(); ticksNoActivity = 0; foreach (var name in filenames) { nfi = OpenFileReader(new FileInfo(name)); lastnfi = nfi; break; } } else { nfi = lastnfi; } ticksNoActivity++; if (nfi != null) { ScanReader(nfi, entries); } return(entries); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Net tick exception : " + ex.Message); System.Diagnostics.Trace.WriteLine(ex.StackTrace); return(new List <JournalEntry>()); } }