// 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]; reader.ReadJournal(out List <JournalEntry> entries, out List <UIEvent> uievents, historyrefreshparsing: true, resetOnError: true); // this may create new commanders, and may write to the TLU db UserDatabase.Instance.ExecuteWithDatabase(cn => { if (entries.Count > 0) { if (fireback != null) { 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 { ILookup <DateTime, JournalEntry> existing = JournalEntry.GetAllByTLU(reader.TravelLogUnit.id, cn.Connection).ToLookup(e => e.EventTimeUTC); //System.Diagnostics.Trace.WriteLine(BaseUtils.AppTicks.TickCountLap("PJF"), i + " into db"); using (DbTransaction tn = cn.Connection.BeginTransaction()) { foreach (JournalEntry jre in entries) { if (!existing[jre.EventTimeUTC].Any(e => JournalEntry.AreSameEntry(jre, e, cn.Connection, ent1jo: jre.GetJson()))) { jre.Add(jre.GetJson(), cn.Connection, 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.Connection); updateProgress((i + 1) * 100 / readersToUpdate.Count, reader.TravelLogUnit.Name); lastnfi = reader; }); } updateProgress(-1, ""); }
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, ""); }
// 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, ""); }