public int CreateBatchInterval(string fileName, CaptureState marked)
        {
            //bool success = false;
            int newCaptureBatchId = 0;

            //using (var context = new PacketCaptureContext())
            using (var context = new PacketAnalysisEntity())
            {
                // Check to see if the batch already exists (it shouldn't - but we will use this for testing)
                var captureBatchId = (from c in context.CaptureBatches
                             where c.FileName == fileName
                             select c.CaptureBatchId).FirstOrDefault();

                if (captureBatchId > 0)
                {
                    // CaptureBatch already exists - just use the current CaptureBatchId
                    newCaptureBatchId = captureBatchId;
                }
                else
                {
                    // We need to add a new capture batch and get the CaptureBatchId

                    var newBatch = context.CaptureBatches.Add(new CaptureBatch());
                    newBatch.FileName = fileName;
                    newBatch.Marked = marked == CaptureState.Marked? true : false;
                    //newCaptureBatchId = context.SaveChanges();
                    context.SaveChanges();
                    newCaptureBatchId = context.CaptureBatches.Select(c => c.CaptureBatchId).Max();
                }
            }

            // success;
            return newCaptureBatchId;
        }
 public void DeleteCumulativeMarkedDisplayStatitics()
 {
     using (var context = new PacketAnalysisEntity())
     {
         context.DisplayStatisticsDelete(true, 2);
     }
 }
        public BindingList<BatchIntervalMarked> GetMarkedBatchIntervals(int captureBatchId)
        {
            BindingList<BatchIntervalMarked> intervals = new BindingList<BatchIntervalMarked>();

            using (var context = new PacketAnalysisEntity())
            {
                var batchIntervals = from b in context.BatchIntervals
                                     where b.CaptureBatchId == captureBatchId
                                     select new
                                     {
                                         b.BatchIntervalId,
                                         b.CaptureBatchId,
                                         b.IntervalNumber,
                                         b.PacketCount,
                                         b.CaptureBatch.Marked
                                     };

                foreach (var bi in batchIntervals)
                {
                    BatchIntervalMarked bim = new BatchIntervalMarked();
                    bim.BatchIntervalId = bi.BatchIntervalId;
                    bim.CaptureBatchId = bi.CaptureBatchId;
                    bim.IntervalNumber = bi.IntervalNumber;
                    bim.PacketCount = bi.PacketCount;
                    bim.Marked = bi.Marked == true ? CaptureState.Marked : CaptureState.Unmarked;
                    intervals.Add(bim);
                }
            }
            return intervals;
        }
 public void DeleteSingleUnmarkedDisplayStatitics()
 {
     using (var context = new PacketAnalysisEntity())
     {
         context.DisplayStatisticsDelete(false, 1);
     }
 }
 public void InsertCumulativeProbabilityDistribution(BindingList<CumulativeProbabilityDistribution> cumulativeProbabilityDistribution)
 {
     using (var context = new PacketAnalysisEntity())
     {
         foreach (CumulativeProbabilityDistribution cpd in cumulativeProbabilityDistribution)
         {
             context.CumulativeProbabilityDistributions.Add(cpd);
         }
         context.SaveChanges();
     }
 }
        public void InsertSingleHistogramData()
        {
            using (var context = new PacketAnalysisEntity())
            {
                foreach (SingleHistogram h in this._SingleHistograms)
                {
                    context.SingleHistograms.Add(h);
                    //context.SaveChanges();
                }

                context.SaveChanges();
            }
        }
        public void InsertCumulativeHistogramData()
        {
            using (var context = new PacketAnalysisEntity())
            {
                foreach (CumulativeHistogram h in this._CumulativeHistogram)
                {
                    context.CumulativeHistograms.Add(h);
                    //context.SaveChanges();
                }

                context.SaveChanges();
            }
        }
        public DisplayStatistic GetCumulativeMarkedDisplayStatistics()
        {
            DisplayStatistic statistics = new DisplayStatistic();

            using (var context = new PacketAnalysisEntity())
            {
                var data = (from d in context.DisplayStatistics
                            where d.Marked == true && d.BatchType == 2
                            select d).FirstOrDefault();

                statistics = data;
            }
            return statistics;
        }
        public void DeleteCumulativeProbabilityDistribution(CaptureState captureState)
        {
            using (var context = new PacketAnalysisEntity())
            {
                var distribution = from d in context.CumulativeProbabilityDistributions
                                   where d.CaptureState == (int)captureState
                                   select d;

                foreach (CumulativeProbabilityDistribution cpd in distribution)
                {
                    context.CumulativeProbabilityDistributions.Remove(cpd);
                }

                context.SaveChanges();
            }
        }
        //public SingleHistogramData(int captureBatchId, CaptureState captureState)
        //{
        //    this._CaptureBatchId = captureBatchId;
        //    this._CaptureState = captureState;
        //}
        public BindingList<SingleHistogram> GetSingleHistogramData(int captureBatchId)
        {
            BindingList<SingleHistogram> SingleHistogramData = new BindingList<SingleHistogram>();

            using (var context = new PacketAnalysisEntity())
            {
                var histograms = from p in context.SingleHistograms
                                 where p.CaptureBatchId == captureBatchId
                                 select p;

                foreach (SingleHistogram h in histograms)
                {
                    SingleHistogramData.Add(h);
                }
            }
            return SingleHistogramData;
        }
        // need to create insert and delete functions
        public BindingList<CumulativeProbabilityDistribution> GetCumulativeProbabilityDistribution(CaptureState captureState)
        {
            BindingList<CumulativeProbabilityDistribution> probabilities = new BindingList<CumulativeProbabilityDistribution>();

               using (var context = new PacketAnalysisEntity())
               {
               var cpd = from c in context.CumulativeProbabilityDistributions
                         where c.CaptureState == (int)captureState
                         select c;

               foreach (CumulativeProbabilityDistribution dist in cpd)
               {
                   probabilities.Add(dist);
               }
               }
               return probabilities;
        }
        public BindingList<CumulativeHistogram> GetCumulativeHistogramData()
        {
            BindingList<CumulativeHistogram> CumulativeHistogramData = new BindingList<CumulativeHistogram>();

            using (var context = new PacketAnalysisEntity())
            {
                var histograms = from p in context.CumulativeHistograms
                                 where p.CaptureState == (int)_CaptureState
                                 select p;

                foreach (CumulativeHistogram h in histograms)
                {
                    CumulativeHistogramData.Add(h);
                }
            }
            return CumulativeHistogramData;
        }
        public void DeleteCumulativeHistogramData()
        {
            int captureState = Convert.ToInt32(this._CaptureState);

            using (var context = new PacketAnalysisEntity())
            {
                var deleteHistogram = (from h in context.CumulativeHistograms
                                       where h.CaptureState == captureState
                                       select h).ToList();

                foreach (CumulativeHistogram h in deleteHistogram)
                {
                    context.CumulativeHistograms.Remove(h);
                }

                context.SaveChanges();
            }
        }
        public void DeleteHypothesisTestResults()
        {
            using (var context = new PacketAnalysisEntity())
            {
                // Check to see if the table is empty
                var isEmpty = (from e in context.HypothesisTests select e).Count();

                // If not empty delete any rows
                if (isEmpty > 0)
                {
                    var deleteResults = (from t in context.HypothesisTests select t).ToList();

                    foreach (HypothesisTest ht in deleteResults)
                    {
                        context.HypothesisTests.Remove(ht);
                    }
                    context.SaveChanges();
                }
            }
        }
        public BindingList<BatchIntervalMarked> GetMarkedBatchIntervals()
        {
            BindingList<BatchIntervalMarked> intervals = new BindingList<BatchIntervalMarked>();

            using (var context = new PacketAnalysisEntity())
            {
                //var batchIntervals = (from i in context.BatchIntervals
                //                     orderby i.IntervalNumber
                //                     select i).ToList();

                //// This one causes VS to blow up!!
                ////var batchIntervals = context.BatchIntervals.Include(c => c.CaptureBatchId).ToList();
                //var batchIntervals = context.BatchIntervals
                //    .Where(c => c.CaptureBatch.Marked)
                //    //.Include(c => c.CaptureBatch)
                //    .ToList();

                //var batchIntervals = context.BatchIntervals.Include(c => c.CaptureBatchId).ToList();
                var batchIntervals = from b in context.BatchIntervals
                                     select new
                                     {
                                         b.BatchIntervalId,
                                         b.CaptureBatchId,
                                         b.IntervalNumber,
                                         b.PacketCount,
                                         b.CaptureBatch.Marked
                                     };

                foreach (var bi in batchIntervals)
                {
                    BatchIntervalMarked bim = new BatchIntervalMarked();
                    bim.BatchIntervalId = bi.BatchIntervalId;
                    bim.CaptureBatchId = bi.CaptureBatchId;
                    bim.IntervalNumber = bi.IntervalNumber;
                    bim.PacketCount = bi.PacketCount;
                    bim.Marked = bi.Marked;
                    intervals.Add(bim);
                }
            }
            return intervals;
        }
        public bool DeleteCumulativeIntervals(CaptureState marked)
        {
            bool success = false;

            bool Marked = marked == CaptureState.Marked ? true : false;

            // Delete the cumulative interval counts for a marked type
            using(var context = new PacketAnalysisEntity())
            {
                var rowCount = (from c in context.CumulativeIntervals
                                where c.Marked == Marked
                                select c.CumulativeIntervalNumber).Count();

                if (rowCount > 0)
                {
                    var deleteParameter = new SqlParameter("@Marked", Marked);
                    var retval = new SqlParameter("@RowsDeleted", SqlDbType.Int);
                    retval.Value = 0;
                    retval.Direction = ParameterDirection.Output;

                    var result = context.Database
                        .SqlQuery<CumulativeInterval>("[COWE].[CumulativeIntervalDelete] @Marked, @RowsDeleted out", deleteParameter, retval);
                    //context.Database.ExecuteSqlCommand("exec @retval = [COWE].[CumulativeIntervalDelete] @Marked", retval, deleteParameter);

                    //var rows = result.Single();

                    ////if (result.Count() == rowCount)
                    //if ((int)retval.Value == (int)rowCount)
                    //{
                    //    success = true;
                    //}
                }
            }

            return success;
        }
        public HypothesisTest GetHypothesisTestResults()
        {
            HypothesisTest ht = new HypothesisTest();

            using (var context = new PacketAnalysisEntity())
            {
                var testResults = (from t in context.HypothesisTests select t).FirstOrDefault();
                ht = testResults as HypothesisTest;
            }
            return ht;
        }
        public BindingList<RawPacket> LoadPackets(string fileName)
        {
            BindingList<RawPacket> packets = new BindingList<RawPacket>();

            try
            {
                using (var context = new PacketAnalysisEntity())
                {
                    var captureBatchId = (from b in context.CaptureBatches
                                          where b.FileName == fileName
                                          select b.CaptureBatchId).FirstOrDefault();

                    var capturePackets = (from c in context.CapturePackets
                                          where c.CaptureBatchId == captureBatchId
                                          select c).ToList();

                    foreach (var pkt in capturePackets)
                    {
                        RawPacket packet = new RawPacket();
                        packet.CapturePacketId = pkt.CapturePacketId;
                        //packet.FileName = pkt.FileName;
                        packet.CaptureBatchId = pkt.CaptureBatchId;
                        packet.PacketNumber = pkt.PacketNumber;
                        packet.TimeStamp = pkt.TimeStamp;
                        packet.Marked = pkt.Marked == true? CaptureState.Marked : CaptureState.Unmarked;
                        packets.Add(packet);
                    }
                }
                return packets;
            }
            catch (Exception ex)
            {
                throw new Exception("Error retrieving capture packet data for file [" + fileName + "]: " + ex.Message);
            }
        }
        public BindingList<CumulativeInterval> GetMarkedCumulativeIntervals(CaptureState marked)
        {
            // Get the cumulative intervals for the passed in marked type
            BindingList<CumulativeInterval> intervals = new BindingList<CumulativeInterval>();

            bool Marked = marked == CaptureState.Marked? true : false;

            using (var context = new PacketAnalysisEntity())
            {
                var cumulativeIntervals = (from i in context.CumulativeIntervals
                                           orderby i.CumulativeIntervalNumber
                                           where i.Marked == Marked
                                           select i).ToList();

                if (cumulativeIntervals.Count > 0)
                {
                    foreach (CumulativeInterval ci in cumulativeIntervals)
                    {
                        intervals.Add(ci);
                    }
                }
            }

            return intervals;
        }
 public void InsertSingleUnarkedDisplayStatitics(BatchStatistics batchStatistics)
 {
     using (var context = new PacketAnalysisEntity())
     {
         context.DisplayStatisticsInsert(
             batchStatistics.IntervalCount,
             batchStatistics.IntervalCountTrimmed,
             batchStatistics.PacketCountMean,
             batchStatistics.PacketCountStandardDeviation,
             batchStatistics.PacketCountMinimum,
             batchStatistics.PacketCountMaximum,
             batchStatistics.MeanOfMeans,
             batchStatistics.MeanOfMeansStandardDeviation,
             false,   // Marked
             Convert.ToInt32(BatchType.Single)
         );
         context.SaveChanges();
     }
 }
        public BindingList<DisplayStatistic> GetSingleUnmarkedDisplayStatistics()
        {
            BindingList<DisplayStatistic> statistics = new BindingList<DisplayStatistic>();

            using (var context = new PacketAnalysisEntity())
            {
                var data = (from d in context.DisplayStatistics
                            where d.Marked == false && d.BatchType == 1
                            select d).ToList();

                foreach (var stat in data)
                {
                    DisplayStatistic statistic = stat;
                    statistics.Add(stat);
                }
            }
            return statistics;
        }
        public DisplayStatistic GetLastSingleUnmarkedDisplayStatistics()
        {
            DisplayStatistic statistics = new DisplayStatistic();

            if (GetSingleUnmarkedDisplayStatistics().Count > 0)
            {
                using (var context = new PacketAnalysisEntity())
                {
                    // Get the last Id value for single unmarked statistic data
                    var maxid = (from m in context.DisplayStatistics
                                 where m.Marked == false && m.BatchType == 1
                                 select m.DisplayStatisticId).Max();

                    var data = (from d in context.DisplayStatistics
                                where d.DisplayStatisticId == maxid
                                select d).FirstOrDefault();

                    statistics = data;
                }
            }
            return statistics;
        }
 public void TruncateAllIntervalStatisticAndTestTables()
 {
     using (var context = new PacketAnalysisEntity())
     {
         context.TruncateAllIntervalStatisticAndTestTables();
     }
 }
        public void UpdateDisplayStatsSavedFlag(int captureBatchId, BatchType batchType, bool saveData)
        {
            using (var context = new PacketAnalysisEntity())
            {
                var captureBatch = (from b in context.CaptureBatches
                                    where b.CaptureBatchId == captureBatchId
                                    select b).Single();

                if (batchType == BatchType.Single)
                {
                    captureBatch.SingleStatistics = saveData;
                }
                else if(batchType == BatchType.Cumulative)
                {
                    captureBatch.CumulativeStatistics = saveData;
                }
                else
                {
                    // no op - unknown batch type
                }

                context.SaveChanges();
            }
        }
 public void InsertHypothesisTestResults(HypothesisTest testResults)
 {
     using (var context = new PacketAnalysisEntity())
     {
         context.HypothesisTests.Add(testResults);
         context.SaveChanges();
     }
 }
        public BindingList<RawPacket> LoadPackets()
        {
            BindingList<RawPacket> packets = new BindingList<RawPacket>();

            try
            {
                //using (var context = new PacketCaptureContext())
                using (var context = new PacketAnalysisEntity())
                {
                    //var capturePackets = context.CapturePackets.ToList();
                    //var batch = context.BatchIntervals.ToList();

                    //foreach (var pkt in capturePackets)
                    foreach (var pkt in context.CapturePackets)
                    {
                        RawPacket packet = new RawPacket();
                        packet.CapturePacketId = pkt.CapturePacketId;
                        //packet.FileName = pkt.FileName;
                        packet.PacketNumber = pkt.PacketNumber;
                        packet.TimeStamp = pkt.TimeStamp;
                        packet.Marked = pkt.Marked == true? CaptureState.Marked : CaptureState.Unmarked;
                        packets.Add(packet);
                    }
                }
                return packets;
            }
            catch (Exception ex)
            {
                throw new Exception ("Error retrieving capture packet data: " + ex.Message);
            }
        }
        public BindingList<CapturePacket> GetCapturePackets(string fileName)
        {
            BindingList<CapturePacket> packets = new BindingList<CapturePacket>();

            try
            {
                //using (var context = new PacketCaptureContext())
                using (var context = new PacketAnalysisEntity())
                {
                    //var captureBatchId = (from c in context.CaptureBatches
                    //                      where c.FileName == fileName
                    //                      select c).FirstOrDefault();

                    //var capturePackets = context.CapturePackets.ToList();
                    //var batch = context.BatchIntervals.ToList();

                    //var capturePackets = (from c in context.CapturePackets
                    //                      where c.FileName == fileName
                    //                      select c).ToList();

                    //var capturePackets = (from c in context.CapturePackets
                    //                      where c.CaptureBatchId == captureBatchId
                    //                      select c).ToList();

                    var captureBatchId = (from b in context.CaptureBatches
                                          where b.FileName == fileName
                                          select b.CaptureBatchId).FirstOrDefault();

                    var capturePackets = (from c in context.CapturePackets
                                          where c.CaptureBatchId == captureBatchId
                                          select c).ToList();

                    foreach (var pkt in capturePackets)
                    {
                        CapturePacket packet = new CapturePacket();
                        packet.CapturePacketId = pkt.CapturePacketId;
                        packet.CaptureBatchId = pkt.CaptureBatchId;
                        packet.PacketNumber = pkt.PacketNumber;
                        packet.TimeStamp = pkt.TimeStamp;
                        packet.Marked = pkt.Marked;
                        packets.Add(packet);
                    }
                }
                return packets;
            }
            catch (Exception ex)
            {
                throw new Exception("Error retrieving capture packet data for file [" + fileName + "]: " + ex.Message);
            }
        }
        public BindingList<CumulativeInterval> GetCumulativeIntervals()
        {
            BindingList<CumulativeInterval> intervals = new BindingList<CumulativeInterval>();

            using (var context = new PacketAnalysisEntity())
            {
                var cumulativeIntervals = (from i in context.CumulativeIntervals
                                          orderby i.CumulativeIntervalNumber
                                          select i).ToList();

                if (cumulativeIntervals.Count > 0)
                {
                    foreach (CumulativeInterval ci in cumulativeIntervals)
                    {
                        intervals.Add(ci);
                    }
                }
            }

            return intervals;
        }
 public void RemoveAllBatchIntervals()
 {
     using(var context = new PacketAnalysisEntity())
     {
         context.TruncateAllIntervalTables();
     }
 }