Esempio n. 1
0
        // Primary method to fill historylist
        // Get All journals matching parameters.
        // if callback set, then each JE is passed back thru callback and not accumulated. Callback is in a thread. Callback can stop the accumulation if it returns false

        static public List <JournalEntry> GetAll(int commander         = -999, DateTime?startdateutc   = null, DateTime?enddateutc = null,
                                                 JournalTypeEnum[] ids = null, DateTime?allidsafterutc = null, Func <JournalEntry, Object, bool> callback = null, Object callbackobj = null,
                                                 int chunksize         = 1000)
        {
            var tluslist = TravelLogUnit.GetAll();
            Dictionary <long, TravelLogUnit> tlus = tluslist.ToDictionary(t => t.ID);

            DbCommand           cmd     = null;
            DbDataReader        reader  = null;
            List <JournalEntry> entries = new List <JournalEntry>();

            try
            {
                cmd    = UserDatabase.Instance.ExecuteWithDatabase(cn => cn.Connection.CreateCommand("select Id,TravelLogId,CommanderId,EventData,Synced from JournalEntries"));
                reader = UserDatabase.Instance.ExecuteWithDatabase(cn =>
                {
                    string cnd = "";
                    if (commander != -999)
                    {
                        cnd = cnd.AppendPrePad("CommanderID = @commander", " and ");
                        cmd.AddParameterWithValue("@commander", commander);
                    }
                    if (startdateutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime >= @after", " and ");
                        cmd.AddParameterWithValue("@after", startdateutc.Value);
                    }
                    if (enddateutc != null)
                    {
                        cnd = cnd.AppendPrePad("EventTime <= @before", " and ");
                        cmd.AddParameterWithValue("@before", enddateutc.Value);
                    }
                    if (ids != null)
                    {
                        int[] array = Array.ConvertAll(ids, x => (int)x);
                        if (allidsafterutc != null)
                        {
                            cmd.AddParameterWithValue("@idafter", allidsafterutc.Value);
                            cnd = cnd.AppendPrePad("(EventTypeId in (" + string.Join(",", array) + ") Or EventTime>=@idafter)", " and ");
                        }
                        else
                        {
                            cnd = cnd.AppendPrePad("EventTypeId in (" + string.Join(",", array) + ")", " and ");
                        }
                    }

                    if (cnd.HasChars())
                    {
                        cmd.CommandText += " where " + cnd;
                    }

                    cmd.CommandText += " Order By EventTime,Id ASC";

                    return(cmd.ExecuteReader());
                });

                List <JournalEntry> retlist = null;

#if TIMESCAN
                Dictionary <string, List <long> > times = new Dictionary <string, List <long> >();
                Stopwatch sw = new Stopwatch();
                sw.Start();
#endif
                do
                {
                    // experiments state that reading the DL is not the time sink, its creating the journal entries

                    retlist = UserDatabase.Instance.ExecuteWithDatabase(cn =>       // split into smaller chunks to allow other things access..
                    {
                        List <JournalEntry> list = new List <JournalEntry>();

                        while (list.Count < chunksize && reader.Read())
                        {
#if TIMESCAN
                            long t = sw.ElapsedTicks;
#endif

                            JournalEntry je = JournalEntry.CreateJournalEntryFixedPos(reader);
                            list.Add(je);

#if TIMESCAN
                            long tw = sw.ElapsedTicks - t;
                            if (!times.TryGetValue(sys.EventTypeStr, out var x))
                            {
                                times[sys.EventTypeStr] = new List <long>();
                            }
                            times[sys.EventTypeStr].Add(tw);
#endif
                        }

                        return(list);
                    });

                    if (callback != null)               // collated, now process them, if callback, feed them thru callback procedure
                    {
                        foreach (var e in retlist)
                        {
                            if (!callback.Invoke(e, callbackobj))     // if indicate stop
                            {
                                retlist = null;
                                break;
                            }
                        }
                    }
                    else
                    {
                        entries.AddRange(retlist);
                    }
                }while (retlist != null && retlist.Count != 0);

#if TIMESCAN
                List <Results> res = new List <Results>();

                foreach (var kvp in times)
                {
                    Results r = new Results();
                    r.name    = kvp.Key;
                    r.avg     = kvp.Value.Average();
                    r.min     = kvp.Value.Min();
                    r.max     = kvp.Value.Max();
                    r.total   = kvp.Value.Sum();
                    r.avgtime = ((double)r.avg / Stopwatch.Frequency * 1000);
                    r.sumtime = ((double)r.total / Stopwatch.Frequency * 1000);
                    r.count   = kvp.Value.Count;
                    res.Add(r);
                }

                //res.Sort(delegate (Results l, Results r) { return l.sumtime.CompareTo(r.sumtime); });
                res.Sort(delegate(Results l, Results r) { return(l.avgtime.CompareTo(r.avgtime)); });

                string rs = "";
                foreach (var r in res)
                {
                    rs = rs + Environment.NewLine + string.Format("Time {0} min {1} max {2} avg {3} ms count {4} totaltime {5} ms", r.name, r.min, r.max, r.avgtime.ToString("#.#########"), r.count, r.sumtime.ToString("#.#######"));
                }

                System.Diagnostics.Trace.WriteLine(rs);
                //File.WriteAllText(@"c:\code\times.txt", rs);
#endif
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Getall Exception " + ex);
            }
            finally
            {
                if (reader != null || cmd != null)
                {
                    UserDatabase.Instance.ExecuteWithDatabase(cn =>
                    {
                        reader?.Close();
                        cmd?.Dispose();
                    });
                }
            }

            return(entries);
        }