using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using LTEMvcApp.Services; using LTEMvcApp.Models; using System.Text; using Microsoft.AspNetCore.Http; namespace LTEMvcApp.Controllers { /// /// 统计控制器 /// public class StatisticsController : Controller { private readonly WebSocketManagerService _webSocketManager; private readonly ILogger _logger; public StatisticsController(WebSocketManagerService webSocketManager, ILogger logger) { _webSocketManager = webSocketManager; _logger = logger; } /// /// 统计页面 /// public IActionResult Index() { var summary = _webSocketManager.GetStatisticsSummary(); ViewBag.Summary = summary; return View(); } /// /// 获取所有统计数据 /// [HttpGet] public IActionResult GetAllStats() { try { var stats = _webSocketManager.GetAllStatistics(); return Json(new { success = true, data = stats }); } catch (Exception ex) { _logger.LogError(ex, "获取所有统计数据时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 获取最新统计数据 /// [HttpGet] public IActionResult GetLatestStats() { try { var stats = _webSocketManager.GetLatestStatistics(); return Json(new { success = true, data = stats }); } catch (Exception ex) { _logger.LogError(ex, "获取最新统计数据时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 获取指定客户端的统计数据 /// [HttpGet] public IActionResult GetClientStats(string clientName) { try { var stats = _webSocketManager.GetClientStatistics(clientName); if (stats == null) { return Json(new { success = false, message = "客户端未找到" }); } return Json(new { success = true, data = stats }); } catch (Exception ex) { _logger.LogError(ex, "获取客户端统计数据时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 获取指定客户端的历史统计数据 /// [HttpGet] public IActionResult GetClientStatsHistory(string clientName) { try { var stats = _webSocketManager.GetClientStatisticsHistory(clientName); return Json(new { success = true, data = stats }); } catch (Exception ex) { _logger.LogError(ex, "获取客户端历史统计数据时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 获取所有客户端的最新统计数据 /// [HttpGet] public IActionResult GetAllClientStats() { try { var stats = _webSocketManager.GetAllClientStatistics(); return Json(new { success = true, data = stats }); } catch (Exception ex) { _logger.LogError(ex, "获取所有客户端统计数据时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 清空统计数据 /// [HttpPost] public IActionResult ClearStats() { try { _webSocketManager.ClearStatistics(); return Json(new { success = true, message = "统计数据已清空" }); } catch (Exception ex) { _logger.LogError(ex, "清空统计数据时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 获取统计摘要 /// [HttpGet] public IActionResult GetSummary() { try { var summary = _webSocketManager.GetStatisticsSummary(); return Json(new { success = true, data = summary }); } catch (Exception ex) { _logger.LogError(ex, "获取统计摘要时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 获取全局统计配置 /// [HttpGet] public IActionResult GetGlobalStatisticsConfig() { try { var config = _webSocketManager.GetGlobalStatisticsConfig(); return Json(new { success = true, data = config }); } catch (Exception ex) { _logger.LogError(ex, "获取全局统计配置时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 设置全局统计配置 /// [HttpPost] public IActionResult SetGlobalStatisticsConfig([FromBody] GlobalStatisticsConfig config) { try { _webSocketManager.SetGlobalStatisticsConfig(config); return Json(new { success = true, message = "全局统计配置已更新" }); } catch (Exception ex) { _logger.LogError(ex, "设置全局统计配置时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// 获取指定客户端的统计配置 /// [HttpGet] public IActionResult GetClientStatisticsConfig(string clientName) { try { var config = _webSocketManager.GetClientStatisticsConfig(clientName); return Json(new { success = true, data = config }); } catch (Exception ex) { _logger.LogError(ex, "获取客户端统计配置时出错: {ClientName}", clientName); return Json(new { success = false, message = ex.Message }); } } /// /// 设置指定客户端的统计配置 /// [HttpPost] public IActionResult SetClientStatisticsConfig([FromBody] StatisticsConfig config) { try { _webSocketManager.SetClientStatisticsConfig(config); return Json(new { success = true, message = "客户端统计配置已更新" }); } catch (Exception ex) { _logger.LogError(ex, "设置客户端统计配置时出错: {ClientName}", config.ClientName); return Json(new { success = false, message = ex.Message }); } } /// /// 根据IP地址获取统计配置 /// [HttpGet] public IActionResult GetStatisticsConfigByIp(string ipAddress) { try { var config = _webSocketManager.GetStatisticsConfigByIp(ipAddress); return Json(new { success = true, data = config }); } catch (Exception ex) { _logger.LogError(ex, "根据IP获取统计配置时出错: {IpAddress}", ipAddress); return Json(new { success = false, message = ex.Message }); } } /// /// 获取所有客户端的统计配置 /// [HttpGet] public IActionResult GetAllClientStatisticsConfigs() { try { var globalConfig = _webSocketManager.GetGlobalStatisticsConfig(); return Json(new { success = true, data = globalConfig.ClientConfigs }); } catch (Exception ex) { _logger.LogError(ex, "获取所有客户端统计配置时出错"); return Json(new { success = false, message = ex.Message }); } } /// /// SSE推送 - 所有统计数据 /// [HttpGet] public async Task SSEStats() { Response.Headers.Add("Content-Type", "text/event-stream"); Response.Headers.Add("Cache-Control", "no-cache"); Response.Headers.Add("Connection", "keep-alive"); Response.Headers.Add("Access-Control-Allow-Origin", "*"); try { while (!HttpContext.RequestAborted.IsCancellationRequested) { var sseData = _webSocketManager.GetStatisticsAsSSE(); var bytes = Encoding.UTF8.GetBytes(sseData); await Response.Body.WriteAsync(bytes, 0, bytes.Length); await Response.Body.FlushAsync(); await Task.Delay(1000); // 每秒推送一次 } } catch (Exception ex) { _logger.LogError(ex, "SSE推送统计数据时出错"); } } /// /// SSE推送 - 指定客户端统计数据 /// [HttpGet] public async Task SSEClientStats(string clientName) { Response.Headers.Add("Content-Type", "text/event-stream"); Response.Headers.Add("Cache-Control", "no-cache"); Response.Headers.Add("Connection", "keep-alive"); Response.Headers.Add("Access-Control-Allow-Origin", "*"); try { while (!HttpContext.RequestAborted.IsCancellationRequested) { var sseData = _webSocketManager.GetClientStatisticsAsSSE(clientName); var bytes = Encoding.UTF8.GetBytes(sseData); await Response.Body.WriteAsync(bytes, 0, bytes.Length); await Response.Body.FlushAsync(); await Task.Delay(1000); // 每秒推送一次 } } catch (Exception ex) { _logger.LogError(ex, "SSE推送客户端统计数据时出错: {ClientName}", clientName); } } /// /// 获取统计队列信息 /// [HttpGet] public IActionResult GetQueueInfo() { try { var queueCount = _webSocketManager.GetStatisticsQueueCount(); var clientCount = _webSocketManager.GetStatisticsClientCount(); return Json(new { success = true, data = new { queueCount = queueCount, clientCount = clientCount } }); } catch (Exception ex) { _logger.LogError(ex, "获取统计队列信息时出错"); return Json(new { success = false, message = ex.Message }); } } } }