コード例 #1
0
        public virtual void Transfer(DateTime uptil, Granularity interval)
        {
            using (var tx = buckets.BeginTransaction())
            {
                var slotEndTime      = uptil.GetSlot(interval, endOfSlot: true);
                var collectedBuckets = buckets.Find().Where(b => b.TimeSlot < slotEndTime).OrderBy(b => b.TimeSlot).ToArray();
                if (collectedBuckets.Length == 0)
                {
                    return;
                }

                ClearBuckets(collectedBuckets);

                var start     = collectedBuckets[0].TimeSlot;
                var pageViews = collectedBuckets
                                .GroupBy(b => new KeyValuePair <DateTime, int>(b.TimeSlot.GetSlot(interval), b.PageID), b => b.Views)
                                .ToDictionary(b => b.Key, b => b.Sum());

                var existingStatistics = IncrementExistingStatistics(uptil, interval, start, pageViews);

                var addedStatistics = InsertNewStatistics(pageViews);

                tx.Commit();

                logger.InfoFormat("Transferred {0} buckets into {1} new and {2} updated statistics", collectedBuckets.Length, addedStatistics.Count, existingStatistics.Count);
            }
        }
コード例 #2
0
        private List <Statistic> IncrementExistingStatistics(DateTime uptil, Granularity interval, DateTime start, Dictionary <KeyValuePair <DateTime, int>, int> pageViews)
        {
            var existingStatistics = statistics.Find(Parameter.GreaterOrEqual("TimeSlot", start) & Parameter.LessThan("TimeSlot", uptil)).ToList();

            foreach (var s in existingStatistics)
            {
                var key = new KeyValuePair <DateTime, int>(s.TimeSlot.GetSlot(interval), s.PageID);
                if (!pageViews.ContainsKey(key))
                {
                    continue;
                }
                s.Views += pageViews[key];
                pageViews.Remove(key);
            }

            statistics.SaveOrUpdate(existingStatistics);
            statistics.Flush();
            return(existingStatistics);
        }