public async Task DeleteTrafficStatist(TrafficStatist trafficStatist) { if (trafficStatist == null) { throw new ArgumentNullException(nameof(trafficStatist)); } await _trafficStatistRepository.Delete(trafficStatist); }
public void ServerFlush() { lock ( SyncObj ) { var now = DateTimeHelper.GetTimeGroup(DateTime.Now); var keys = Clients.Keys.Where(w => w != now).ToList(); foreach (var key in keys) { Clients.TryGetValue(key, out List <Tuple <string, object> > logs); if (logs != null) { TrafficStatist trafficStatist = new TrafficStatist(); trafficStatist.DateTime = Convert.ToInt64(key); trafficStatist.TotalHttpReq = logs.Count(w => w.Item1 == MonitorContextKeys.http); trafficStatist.TotalSignalrReq = logs.Count(w => w.Item1 == MonitorContextKeys.signalr); trafficStatist.TotalReq = trafficStatist.TotalHttpReq + trafficStatist.TotalSignalrReq; trafficStatist.ExceptionReq = logs.Count(w => w.Item1 == MonitorContextKeys.fault); // trafficStatist.TotalSignalrConnectReq = logs.Count(w => w.Item1 == MonitorContextKeys.signalr_connect); trafficStatist.TotalSignalrDisconnectReq = logs.Count(w => w.Item1 == MonitorContextKeys.signalr_disconnect); var onlines = logs.Where(w => w.Item1 == MonitorContextKeys.online_device).ToList(); if (onlines.Any()) { trafficStatist.TotalOnlineDevice = (long)onlines.Average(a => Convert.ToInt64(a.Item2)); } // server var cpu_server = logs.Where(w => w.Item1 == MonitorContextKeys.cpu_server).ToList(); if (cpu_server.Any()) { trafficStatist.ServerCpuAvgRate = cpu_server.Average(a => Convert.ToDecimal(a.Item2)); trafficStatist.ServerCpuTopRate = cpu_server.Max(a => Convert.ToDecimal(a.Item2)); } var ram_server = logs.Where(w => w.Item1 == MonitorContextKeys.ram_server).ToList(); if (ram_server.Any()) { trafficStatist.ServerRamAvgRate = ram_server.Average(a => Convert.ToDecimal(a.Item2)); trafficStatist.ServerRamTopRate = ram_server.Max(a => Convert.ToDecimal(a.Item2)); } // app var cpu_app = logs.Where(w => w.Item1 == MonitorContextKeys.cpu_app).ToList(); if (cpu_app.Any()) { trafficStatist.AppliationCpuAvgRate = cpu_app.Average(a => Convert.ToDecimal(a.Item2)); trafficStatist.AppliationCpuTopRate = cpu_app.Max(a => Convert.ToDecimal(a.Item2)); } var ram_app_size = logs.Where(w => w.Item1 == MonitorContextKeys.ram_app_size).ToList(); if (ram_app_size.Any()) { trafficStatist.AppliationRamAvgSize = ram_app_size.Average(a => Convert.ToDecimal(a.Item2)); } //trafficStatist.AppliationRamAvgRate = logs // .Where( w => w.Item1 == MonitorContextKeys.ram_app ) // .Average( a => (decimal)a.Item2 ); //trafficStatist.AppliationRamTopRate = logs // .Where( w => w.Item1 == MonitorContextKeys.ram_app ) // .Max( a => (decimal)a.Item2 ); trafficStatist.CreateTime = DateTime.Now; var path = CommonHelper.MapPath(_settings.Value.TimeDataStorePath + $"{key}.log"); JsonHelper.ToPack(path, trafficStatist); } Clients.TryRemove(key, out var client); } } }
private static async Task TimeStore(ILogger <Program> logger, MonitorOptions options) { try { logger.LogInformation("开始处理 TimeStore 数据"); var now = DateTimeHelper.GetTimeGroup(DateTime.Now); var keys = timeStore.GetKeys().Where(w => w != now).ToList(); foreach (var key in keys) { var logs = timeStore.GetByKey(key); if (logs == null || !logs.Any()) { timeStore.Remove(key); continue; } var trafficStatistService = IoC.Resolve <ITrafficStatistService>(); if (dateStore.ContainsKey(key)) { var item = await trafficStatistService.GetTrafficStatistByDateTime(Convert.ToInt64(key)); if (item != null) { item.TotalHttpReq += logs.Count(w => w.Key == MonitorContextKeys.http); item.TotalSignalrReq += logs.Count(w => w.Key == MonitorContextKeys.signalr); item.TotalReq = item.TotalHttpReq + item.TotalSignalrReq; item.ExceptionReq += logs.Count(w => w.Key == MonitorContextKeys.fault); item.TotalSignalrConnectReq += logs.Count(w => w.Key == MonitorContextKeys.signalr_connect); item.TotalSignalrDisconnectReq += logs.Count(w => w.Key == MonitorContextKeys.signalr_disconnect); await trafficStatistService.UpdateTrafficStatist(item); continue; } else { logger.LogWarning("Syinpo.Monitor.Processor TimeStore key 与实际入库不一致:" + key); } } TrafficStatist trafficStatist = new TrafficStatist(); // request trafficStatist.DateTime = Convert.ToInt64(key); trafficStatist.TotalHttpReq = logs.Count(w => w.Key == MonitorContextKeys.http); trafficStatist.TotalSignalrReq = logs.Count(w => w.Key == MonitorContextKeys.signalr); trafficStatist.TotalReq = trafficStatist.TotalHttpReq + trafficStatist.TotalSignalrReq; trafficStatist.ExceptionReq = logs.Count(w => w.Key == MonitorContextKeys.fault); // signalr trafficStatist.TotalSignalrConnectReq = logs.Count(w => w.Key == MonitorContextKeys.signalr_connect); trafficStatist.TotalSignalrDisconnectReq = logs.Count(w => w.Key == MonitorContextKeys.signalr_disconnect); var onlines = logs.Where(w => w.Key == MonitorContextKeys.online_device).ToList(); if (onlines.Any()) { trafficStatist.TotalOnlineDevice = (long)onlines.Average(a => Convert.ToInt64(a.Val)); } // server var cpu_server = logs.Where(w => w.Key == MonitorContextKeys.cpu_server).ToList(); if (cpu_server.Any()) { trafficStatist.ServerCpuAvgRate = cpu_server.Average(a => Convert.ToDecimal(a.Val)); trafficStatist.ServerCpuTopRate = cpu_server.Max(a => Convert.ToDecimal(a.Val)); } var ram_server = logs.Where(w => w.Key == MonitorContextKeys.ram_server).ToList(); if (ram_server.Any()) { trafficStatist.ServerRamAvgRate = ram_server.Average(a => Convert.ToDecimal(a.Val)); trafficStatist.ServerRamTopRate = ram_server.Max(a => Convert.ToDecimal(a.Val)); } // app var cpu_app = logs.Where(w => w.Key == MonitorContextKeys.cpu_app).ToList(); if (cpu_app.Any()) { trafficStatist.AppliationCpuAvgRate = cpu_app.Average(a => Convert.ToDecimal(a.Val)); trafficStatist.AppliationCpuTopRate = cpu_app.Max(a => Convert.ToDecimal(a.Val)); } var ram_app_size = logs.Where(w => w.Key == MonitorContextKeys.ram_app_size).ToList(); if (ram_app_size.Any()) { trafficStatist.AppliationRamAvgSize = ram_app_size.Average(a => Convert.ToDecimal(a.Val)); } //trafficStatist.AppliationRamAvgRate = logs // .Where( w => w.Item1 == MonitorContextKeys.ram_app ) // .Average( a => (decimal)a.Item2 ); //trafficStatist.AppliationRamTopRate = logs // .Where( w => w.Item1 == MonitorContextKeys.ram_app ) // .Max( a => (decimal)a.Item2 ); trafficStatist.CreateTime = DateTime.Now; await trafficStatistService.InsertTrafficStatist(trafficStatist); trafficStatistService = null; timeStore.Remove(key); if (!dateStore.ContainsKey(key)) { dateStore.TryAdd(key, key); } if (dateStore.Keys.Count > 100) { dateStore.TryRemove(dateStore.Keys.First(), out string val); } } logger.LogInformation("结束处理 TimeStore."); } catch (Exception ex) { logger.LogError("Syinpo.Monitor.Processor TimeStore 错误:" + ex.Message); } }