Esempio n. 1
0
        public async Task <ActionResult> Statics(string refresh)
        {
            return(await RunActionAsync(async() =>
            {
                var clear_cache = ValidateHelper.IsPlumpString(refresh);
                var now = DateTime.Now;
                var count = 10;
                var start = now.AddDays(-count);
                var expire = TimeSpan.FromMinutes(10);

                await this._ReqLogModelRepository.PrepareSessionAsync(async db =>
                {
                    var reqlog_query = db.Set <ReqLogEntity>().AsNoTrackingQueryable();
                    var cachehit_query = db.Set <CacheHitLogEntity>().AsNoTrackingQueryable();

                    #region 今天请求频次
                    var border = now.GetDateBorder();
                    var reqlog_groupbyhour = await this._cache.GetOrSetAsync(
                        CacheKeyManager.AuthStaticsReqLogGroupByHours(), async() =>
                    {
                        return await reqlog_query
                        .Where(x => x.CreateTime >= border.start && x.CreateTime < border.end && x.IsRemove <= 0)
                        .GroupBy(x => x.TimeHour)
                        .Select(x => new ReqLogGroupModel()
                        {
                            Hour = x.Key,
                            ReqCount = x.Count()
                        }).ToListAsync();
                    }, expire);

                    if (reqlog_groupbyhour != null)
                    {
                        for (var i = 0; i <= now.Hour; ++i)
                        {
                            var hour_data = reqlog_groupbyhour.Where(x => x.Hour == i).FirstOrDefault();
                            if (hour_data == null)
                            {
                                reqlog_groupbyhour.Add(new ReqLogGroupModel()
                                {
                                    Hour = i,
                                    ReqCount = 0
                                });
                            }
                        }
                        reqlog_groupbyhour = reqlog_groupbyhour.OrderBy(x => x.Hour).ToList();
                    }

                    ViewData[nameof(reqlog_groupbyhour)] = reqlog_groupbyhour;
                    #endregion

                    #region 请求日志
                    //请求日志按照时间分组
                    var reqlog_groupbytime = await this._cache.GetOrSetAsync(
                        CacheKeyManager.AuthStaticsReqLogGroupByDate(), async() =>
                    {
                        return await reqlog_query
                        .Where(x => x.CreateTime >= start && x.IsRemove <= 0)
                        .GroupBy(x => new { x.TimeYear, x.TimeMonth, x.TimeDay })
                        .Select(x => new ReqLogGroupModel()
                        {
                            Year = x.Key.TimeYear,
                            Month = x.Key.TimeMonth,
                            Day = x.Key.TimeDay,
                            ReqTime = x.Average(m => m.ReqTime),
                            ReqCount = x.Count()
                        }).Take(count).ToListAsync();
                    }, expire);

                    ViewData[nameof(reqlog_groupbytime)] = reqlog_groupbytime;
                    //请求日志按照控制器分组
                    var reqlog_groupbyaction = await this._cache.GetOrSetAsync(
                        CacheKeyManager.AuthStaticsReqLogGroupByAction(), async() =>
                    {
                        return await reqlog_query
                        .Where(x => x.CreateTime >= start && x.IsRemove <= 0)
                        .GroupBy(x => new { x.AreaName, x.ControllerName, x.ActionName })
                        .Select(x => new ReqLogGroupModel()
                        {
                            AreaName = x.Key.AreaName,
                            ControllerName = x.Key.ControllerName,
                            ActionName = x.Key.ActionName,
                            ReqTime = x.Average(m => m.ReqTime),
                            ReqCount = x.Count()
                        }).Take(count).ToListAsync();
                    }, expire);

                    ViewData[nameof(reqlog_groupbyaction)] = reqlog_groupbyaction;
                    #endregion

                    #region 缓存命中
                    //缓存命中按照时间分组
                    var cachehit_groupbytime = await this._cache.GetOrSetAsync(
                        CacheKeyManager.AuthStaticsCacheHitGroupByTime(), async() =>
                    {
                        return await cachehit_query
                        .Where(x => x.CreateTime >= start && x.IsRemove <= 0)
                        .GroupBy(x => new { x.TimeYear, x.TimeMonth, x.TimeDay })
                        .Select(x => new CacheHitGroupModel()
                        {
                            Year = x.Key.TimeYear,
                            Month = x.Key.TimeMonth,
                            Day = x.Key.TimeDay,
                            HitCount = x.Sum(m => m.Hit),
                            NotHitCount = x.Sum(m => m.NotHit)
                        }).Take(count).ToListAsync();
                    }, expire);

                    ViewData[nameof(cachehit_groupbytime)] = cachehit_groupbytime;
                    //缓存命中按照key分组
                    var cachehit_groupbykey = await this._cache.GetOrSetAsync(
                        CacheKeyManager.AuthStaticsCacheHitGroupByKeys(), async() =>
                    {
                        return await cachehit_query
                        .Where(x => x.CreateTime >= start && x.IsRemove <= 0)
                        .GroupBy(x => x.CacheKey).Select(x => new CacheHitGroupModel()
                        {
                            CacheKey = x.Key,
                            HitCount = x.Sum(m => m.Hit),
                            NotHitCount = x.Sum(m => m.NotHit)
                        }).Take(count).ToListAsync();
                    }, expire);

                    ViewData[nameof(cachehit_groupbykey)] = cachehit_groupbykey;
                    #endregion
                });
                return View();
            }));
        }