public async Task <RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsFilterOption filterOption)
        {
            var where = BuildSqlFilter(filterOption);

            var dateFormat = GetDateFormat(filterOption);

            string sql = $@"Select {dateFormat} AS KeyField,COUNT(1) AS ValueField From ""{Prefix}RequestInfo"" {where} Group by KeyField;";

            TraceLogSql(sql);

            var result = new RequestTimesStatisticsResult()
            {
                Type = filterOption.Type,
            };

            await LoggingSqlOperation(async connection =>
            {
                result.Items = new Dictionary <string, int>();
                (await connection.QueryAsync <KVClass <string, int> >(sql)).ToList().ForEach(m =>
                {
                    result.Items.Add(m.KeyField, m.ValueField);
                });
            }, "获取请求次数统计异常");

            return(result);
        }
        /// <summary>
        /// 获取请求次数统计
        /// </summary>
        /// <param name="filterOption"></param>
        /// <returns></returns>
        public async Task<RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsFilterOption filterOption)
        {
            var where = BuildSqlFilter(filterOption);

            var dateFormat = GetDateFormat(filterOption);

            string sql = $"Select {dateFormat} KeyField,COUNT(1) ValueField From RequestInfo {where} Group by {dateFormat};";

            TraceLogSql(sql);

            var result = new RequestTimesStatisticsResult()
            {
                Type = filterOption.Type,
            };

            await LoggingSqlOperation(async connection =>
            {
                result.Items = new Dictionary<string, int>();
                (await connection.QueryAsync<KVClass<string, int>>(sql).ConfigureAwait(false)).ToList().ForEach(m =>
                {
                    result.Items.Add(m.KeyField.Split('-').Last().ToInt().ToString(), m.ValueField);
                });
            }, "获取请求次数统计异常").ConfigureAwait(false);

            return result;
        } 
        /// <summary>
        /// 获取请求次数统计
        /// </summary>
        /// <param name="filterOption"></param>
        /// <returns></returns>
        public async Task <RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsFilterOption filterOption)
        {
            var where = BuildSqlFilter(filterOption);

            var dateFormat = GetDateFormat(filterOption);

            string sql = $"Select {dateFormat} KeyField,COUNT(1) ValueField From RequestInfo {where} Group by {dateFormat} ";

            TraceLogSql(sql);

            var result = new RequestTimesStatisticsResult()
            {
                Type = filterOption.Type,
            };

            await LoggingSqlOperation(async connection =>
            {
                result.Items = new Dictionary <string, int>();

                var list = (await connection.QueryAsync <KVClass <string, int> >(sql)).ToList();

                foreach (var item in list)
                {
                    result.Items.Add(item.KeyField, item.ValueField);
                }
            }, "获取请求次数统计异常");

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// 获取请求次数统计
        /// </summary>
        /// <param name="filterOption"></param>
        /// <returns></returns>
        public async Task <RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsFilterOption filterOption)
        {
            var where = BuildSqlFilter(filterOption);

            var dateFormat = GetDateFormat(filterOption);

            string sql = $"Select {dateFormat} KeyField,COUNT(1) ValueField From RequestInfo {where} Group by {dateFormat};";

            TraceLogSql(sql);

            var result = new RequestTimesStatisticsResult()
            {
                Type = filterOption.Type,
            };

            await LoggingSqlOperation(async connection =>
            {
                result.Items = new Dictionary <string, int>();
                var list     = (await connection.QueryAsync <KVClass <string, int> >(sql).ConfigureAwait(false)).ToList();

                if (filterOption.Type == TimeUnit.Minute)
                {
                    foreach (var item in list)
                    {
                        result.Items.Add(item.KeyField.Substring(11).Replace(":", "-"), item.ValueField);
                    }
                }

                if (filterOption.Type == TimeUnit.Hour)
                {
                    foreach (var item in list)
                    {
                        result.Items.Add(item.KeyField.Substring(8).Replace(" ", "-"), item.ValueField);
                    }
                }


                if (filterOption.Type == TimeUnit.Day)
                {
                    foreach (var item in list)
                    {
                        result.Items.Add(item.KeyField, item.ValueField);
                    }
                }
            }, "获取请求次数统计异常").ConfigureAwait(false);

            return(result);
        }
        public async Task <RequestTimesStatisticsResult> GetRequestTimesStatisticsAsync(TimeSpanStatisticsFilterOption option)
        {
            var response = await Client.SearchAsync <RequestInfo>(x => x.Index(GetIndexName <RequestInfo>())

                                                                  .Query(c =>

                                                                         c.HasDateWhere(option.StartTime, option.EndTime)

                                                                         && c.Terms(f => f.Field(e => e.StatusCode).Terms(option.StatusCodes))

                                                                         && c.Terms(f => f.Field(e => e.Node).Terms(option.Nodes))

                                                                         )

                                                                  .Aggregations(a => a.DateHistogram("date", b => b.Field(c => c.CreateTime).AutoFormatTime(option.Type).MinimumDocumentCount(0)
                                                                                                     .ExtendedBounds(option.StartTime.Value, option.EndTime.Value)
                                                                                                     .TimeZone("+08:00").Order(HistogramOrder.KeyAscending)
                                                                                                     )).Size(0)

                                                                  );

            var result = new RequestTimesStatisticsResult()
            {
                Type  = option.Type,
                Items = new Dictionary <string, int>()
            };

            if (response != null && response.IsValid)
            {
                if (response.Aggregations.Count > 0)
                {
                    var buckets = (response.Aggregations.FirstOrDefault().Value as Nest.BucketAggregate).Items.ToList();

                    foreach (var item in buckets)
                    {
                        var model = item as Nest.DateHistogramBucket;

                        if (!result.Items.ContainsKey(model.KeyAsString.ToInt().ToString()))
                        {
                            result.Items.Add(model.KeyAsString.ToInt().ToString(), Convert.ToInt32(model.DocCount.Value));
                        }
                    }
                }
            }

            return(result);
        }