监控数据分组

This commit is contained in:
niyyzf 2024-07-23 17:38:53 +08:00
parent 055c011167
commit 3b9d43acb7
1 changed files with 40 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc; using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
namespace LoongPanel_Asp.Controllers; namespace LoongPanel_Asp.Controllers;
@ -9,16 +10,47 @@ public class JobController(ApplicationDbContext dbContext) : ControllerBase
[HttpGet("GetJobList")] [HttpGet("GetJobList")]
public IActionResult GetJobList([FromQuery]string? serverId=null) public IActionResult GetJobList([FromQuery]string? serverId=null)
{ {
var serverMonitoringData = dbContext.ServerMonitoringData; var wildcardRules = new Dictionary<string, List<string>>
var dataNameTypesWithDataType = serverMonitoringData {
.Where(x=>!string.IsNullOrEmpty(serverId) || x.ServerId==serverId) { "CPU总体参数", ["CpuTotalUsage","CpuTotalSpeed","CpuIOWaitUsage","CpuSystemUsage","CpuUserUsage"] },
.GroupBy(x => x.DataName) { "CPU单核参数", ["CpuSingleSpeed-*"] },
{ "CPU用户参数", ["CpuUsage-*"] },
{ "内存总体参数", ["MemoryCache","MemoryFree","MemoryTotal","MemoryTotalUsage","MemoryUsed"] },
{ "内存用户参数", ["MemoryUsage-*"] },
{ "Swap参数", ["SwapFree","SwapTotal","SwapTotalUsage","SwapUsed"] },
{ "各个磁盘参数", ["DiskTps-*","DiskTps-*","DiskUtil-*","DiskWriteKB-*","DiskWriteKB-*","DiskAwait-*","DiskReadKB-*"] },
{ "磁盘总体参数", ["DiskTotalUsage"] },
{ "网络总体参数", ["InterfaceTotalUtilizationPercentage"] },
{ "各个网络接口参数", ["InterfaceUtilizationPercentage-*","InterfaceUtilizationPercentage-*","ReceivedPacketsPerSecond-*","TransmittedPacketsPerSecond-*"] },
{ "进程", ["PhrasePatternCount","ProcessTotalCount","ThreadsTotalCount"] },
{ "用户进程", ["UserProcesses-*"] },
};
var filteredData = dbContext.ServerMonitoringData
.Where(x => string.IsNullOrEmpty(serverId) || x.ServerId == serverId)
.GroupBy(item => item.DataType).Select(item => item.First()).ToList();
// 先对每个元素应用匹配规则,然后根据匹配结果进行分组
var groupedData = filteredData
.Select(x => new
{
x.DataName,
x.DataType,
GroupName = wildcardRules.FirstOrDefault(rule => x.DataType != null && IsMatch(x.DataType, rule.Value)).Key ?? "其他"
})
.GroupBy(x => x.GroupName)
.Select(group => new .Select(group => new
{ {
DataName = group.Key, group.First().DataType ,group.First().ServerId GroupName=group.Key,
Items = group.ToList()
}) })
.ToList(); .ToList();
return Ok(dataNameTypesWithDataType); return Ok(groupedData);
} }
} private bool IsMatch(string input, List<string> patterns)
{
return patterns.Select(pattern => "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$").Any(regexPattern => Regex.IsMatch(input, regexPattern));
}
}