You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

274 lines
8.8 KiB

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
{
/// <summary>
/// 统计控制器
/// </summary>
public class StatisticsController : Controller
{
private readonly WebSocketManagerService _webSocketManager;
private readonly ILogger<StatisticsController> _logger;
public StatisticsController(WebSocketManagerService webSocketManager, ILogger<StatisticsController> logger)
{
_webSocketManager = webSocketManager;
_logger = logger;
}
/// <summary>
/// 统计页面
/// </summary>
public IActionResult Index()
{
var summary = _webSocketManager.GetStatisticsSummary();
ViewBag.Summary = summary;
return View();
}
/// <summary>
/// 获取所有统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 获取最新统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 获取指定客户端的统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 获取指定客户端的历史统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 获取所有客户端的最新统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 清空统计数据
/// </summary>
[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 });
}
}
/// <summary>
/// 获取统计摘要
/// </summary>
[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 });
}
}
/// <summary>
/// 获取所有客户端的统计配置
/// </summary>
[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 });
}
}
/// <summary>
/// 统计配置管理页面
/// </summary>
public IActionResult Config()
{
return View();
}
/// <summary>
/// SSE推送 - 所有统计数据
/// </summary>
[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推送统计数据时出错");
}
}
/// <summary>
/// SSE推送 - 指定客户端统计数据
/// </summary>
[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);
}
}
/// <summary>
/// 获取统计队列信息
/// </summary>
[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 });
}
}
}
}