Esempio n. 1
0
        /// <summary>
        /// 获取并清空该 Domain 下的所有数据
        /// </summary>
        /// <returns></returns>
        /// <param name="removeReadItems">是否移除已读取的项目,默认为 true</param>
        public List <MinuteDataPack> ReadAndCleanDataItems(bool removeReadItems = true)
        {
            try
            {
                var dt1 = SystemTime.Now;

                var cacheStragety = Cache.CacheStrategyFactory.GetObjectCacheStrategyInstance();
                Dictionary <string, List <DataItem> > tempDataItems = new Dictionary <string, List <DataItem> >();

                var systemNow     = SystemTime.Now;
                var nowMinuteTime = new DateTime(systemNow.Year, systemNow.Month, systemNow.Day, systemNow.Hour, systemNow.Minute, 0);

                //快速获取并清理数据
                foreach (var item in KindNameStore[_domain])
                {
                    var kindName = item.Key;
                    var finalKey = BuildFinalKey(kindName);
                    using (cacheStragety.BeginCacheLock("SenparcAPM", finalKey))
                    {
                        var list       = GetDataItemList(item.Key);//获取列表
                        var toveRemove = list.Where(z => z.DateTime < nowMinuteTime);

                        tempDataItems[kindName] = toveRemove.ToList();//添加到列表

                        if (removeReadItems)
                        {
                            //移除已读取的项目
                            if (toveRemove.Count() == list.Count())
                            {
                                //已经全部删除
                                cacheStragety.RemoveFromCache(finalKey, true);//删除
                            }
                            else
                            {
                                //部分删除
                                var newList = list.Except(toveRemove).ToList();
                                cacheStragety.Set(finalKey, newList, Config.DataExpire, true);
                            }
                        }
                    }
                }


                //开始处理数据(分两步是为了减少同步锁的时间)
                var result = new List <MinuteDataPack>();
                foreach (var kv in tempDataItems)
                {
                    var kindName   = kv.Key;
                    var domainData = kv.Value;

                    DateTime lastDataItemTime = DateTime.MinValue;

                    MinuteDataPack minuteDataPack = new MinuteDataPack();
                    minuteDataPack.KindName = kindName;
                    result.Add(minuteDataPack);   //添加一个指标

                    MinuteData minuteData = null; //某一分钟的指标
                    foreach (var dataItem in domainData)
                    {
                        if (DataHelper.IsLaterMinute(lastDataItemTime, dataItem.DateTime))
                        {
                            //新的一分钟
                            minuteData = new MinuteData();
                            minuteDataPack.MinuteDataList.Add(minuteData);

                            minuteData.KindName     = dataItem.KindName;
                            minuteData.Time         = new DateTime(dataItem.DateTime.Year, dataItem.DateTime.Month, dataItem.DateTime.Day, dataItem.DateTime.Hour, dataItem.DateTime.Minute, 0);
                            minuteData.StartValue   = dataItem.Value;
                            minuteData.HighestValue = dataItem.Value;
                            minuteData.LowestValue  = dataItem.Value;
                        }

                        minuteData.EndValue  = dataItem.Value;
                        minuteData.SumValue += dataItem.Value;

                        if (dataItem.Value > minuteData.HighestValue)
                        {
                            minuteData.HighestValue = dataItem.Value;
                        }

                        if (dataItem.Value < minuteData.LowestValue)
                        {
                            minuteData.LowestValue = dataItem.Value;
                        }


                        minuteData.SampleSize++;

                        lastDataItemTime = dataItem.DateTime;
                    }
                }

                SenparcTrace.SendCustomLog("APM 性能记录 - DataOperation.ReadAndCleanDataItems", (SystemTime.Now - dt1).TotalMilliseconds + " ms");

                return(result);
            }
            catch (Exception e)
            {
                new APMException(e.Message, _domain, "", "DataOperation.ReadAndCleanDataItems");
                return(null);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 获取并清空该 Domain 下的所有数据
        /// </summary>
        /// <returns></returns>
        /// <param name="removeReadItems">是否移除已读取的项目,默认为 true</param>
        /// <param name="keepTodayData">当 removeReadItems = true 时有效,在清理的时候是否保留当天的数据</param>
        public async Task <List <MinuteDataPack> > ReadAndCleanDataItemsAsync(bool removeReadItems = true, bool keepTodayData = true)
        {
            try
            {
                var dt1 = SystemTime.Now;

                var cacheStragety = Cache.CacheStrategyFactory.GetObjectCacheStrategyInstance();
                Dictionary <string, List <DataItem> > tempDataItems = new Dictionary <string, List <DataItem> >();

                var systemNow     = SystemTime.Now.UtcDateTime;                                                                     //统一UTC时间
                var nowMinuteTime = SystemTime.Now.AddSeconds(-SystemTime.Now.Second).AddMilliseconds(-SystemTime.Now.Millisecond); // new DateTimeOffset(systemNow.Year, systemNow.Month, systemNow.Day, systemNow.Hour, systemNow.Minute, 0, TimeSpan.Zero);

                //快速获取并清理数据
                foreach (var item in KindNameStore[_domain])
                {
                    var kindName = item.Key;
                    var finalKey = BuildFinalKey(kindName);
                    using (await cacheStragety.BeginCacheLockAsync("SenparcAPM", finalKey).ConfigureAwait(false))
                    {
                        var list = await GetDataItemListAsync(item.Key).ConfigureAwait(false);        //获取列表

                        var completedStatData = list.Where(z => z.DateTime < nowMinuteTime).ToList(); //统计范围内的所有数据

                        tempDataItems[kindName] = completedStatData;                                  //添加到列表

                        if (removeReadItems)
                        {
                            //筛选需要删除的数据
                            var tobeRemove = completedStatData.Where(z => keepTodayData ? z.DateTime < SystemTime.Today : true);

                            //移除已读取的项目
                            if (tobeRemove.Count() == list.Count())
                            {
                                //已经全部删除
                                await cacheStragety.RemoveFromCacheAsync(finalKey, true).ConfigureAwait(false);//删除
                            }
                            else
                            {
                                //部分删除
                                var newList = list.Except(tobeRemove).ToList();
                                await cacheStragety.SetAsync(finalKey, newList, Config.DataExpire, true).ConfigureAwait(false);
                            }
                        }
                    }
                }


                //开始处理数据(分两步是为了减少同步锁的时间)
                var result = new List <MinuteDataPack>();
                foreach (var kv in tempDataItems)
                {
                    var kindName   = kv.Key;
                    var domainData = kv.Value;

                    var lastDataItemTime = DateTimeOffset.MinValue;

                    MinuteDataPack minuteDataPack = new MinuteDataPack();
                    minuteDataPack.KindName = kindName;
                    result.Add(minuteDataPack);   //添加一个指标

                    MinuteData minuteData = null; //某一分钟的指标
                    foreach (var dataItem in domainData)
                    {
                        if (DataHelper.IsLaterMinute(lastDataItemTime, dataItem.DateTime))
                        {
                            //新的一分钟
                            minuteData = new MinuteData();
                            minuteDataPack.MinuteDataList.Add(minuteData);

                            minuteData.KindName     = dataItem.KindName;
                            minuteData.Time         = new DateTimeOffset(dataItem.DateTime.Year, dataItem.DateTime.Month, dataItem.DateTime.Day, dataItem.DateTime.Hour, dataItem.DateTime.Minute, 0, TimeSpan.Zero);
                            minuteData.StartValue   = dataItem.Value;
                            minuteData.HighestValue = dataItem.Value;
                            minuteData.LowestValue  = dataItem.Value;
                        }

                        minuteData.EndValue  = dataItem.Value;
                        minuteData.SumValue += dataItem.Value;

                        if (dataItem.Value > minuteData.HighestValue)
                        {
                            minuteData.HighestValue = dataItem.Value;
                        }

                        if (dataItem.Value < minuteData.LowestValue)
                        {
                            minuteData.LowestValue = dataItem.Value;
                        }


                        minuteData.SampleSize++;

                        lastDataItemTime = dataItem.DateTime;
                    }
                }

                //if (SenparcTrace.RecordAPMLog)
                {
                    SenparcTrace.SendCustomLog("APM 记录 - DataOperation.ReadAndCleanDataItems", SystemTime.DiffTotalMS(dt1) + " ms");
                }

                return(result);
            }
            catch (Exception e)
            {
                new APMException(e.Message, _domain, "", "DataOperation.ReadAndCleanDataItems");
                return(null);
            }
        }