S8MonitoringService.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Admin.NET.Plugin.AiDOP.Dto.S8;
  2. using Admin.NET.Plugin.AiDOP.Entity.S8;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure.S8;
  4. namespace Admin.NET.Plugin.AiDOP.Service.S8;
  5. public class S8MonitoringService : ITransient
  6. {
  7. private readonly SqlSugarRepository<AdoS8Exception> _rep;
  8. public S8MonitoringService(SqlSugarRepository<AdoS8Exception> rep)
  9. {
  10. _rep = rep;
  11. }
  12. /// <summary>
  13. /// 异常监控汇总:按 module_code 分组统计红/黄/绿/超时数。
  14. /// 供综合全景页顶部徽标和模块汇总表使用。
  15. /// </summary>
  16. public async Task<AdoS8MonitoringSummaryDto> GetSummaryAsync(AdoS8MonitoringSummaryQueryDto q)
  17. {
  18. var query = _rep.AsQueryable()
  19. .Where(e => e.TenantId == q.TenantId && e.FactoryId == q.FactoryId && !e.IsDeleted)
  20. .WhereIF(!string.IsNullOrWhiteSpace(q.SceneCode), e => e.SceneCode == q.SceneCode)
  21. .WhereIF(!string.IsNullOrWhiteSpace(q.ModuleCode), e => e.ModuleCode == q.ModuleCode)
  22. .WhereIF(q.BizDateFrom.HasValue, e => e.CreatedAt >= q.BizDateFrom!.Value)
  23. .WhereIF(q.BizDateTo.HasValue, e => e.CreatedAt <= q.BizDateTo!.Value);
  24. // 聚合到内存(数据量在可控范围内,避免复杂 GROUP BY 兼容性问题)
  25. var raw = await query
  26. .Select(e => new
  27. {
  28. e.ModuleCode,
  29. e.SceneCode,
  30. e.Severity,
  31. e.TimeoutFlag,
  32. e.Status
  33. })
  34. .ToListAsync();
  35. var byModule = raw
  36. .GroupBy(e => new { mc = e.ModuleCode ?? string.Empty, sc = e.SceneCode ?? string.Empty })
  37. .Select(g => new AdoS8ModuleSummaryItem
  38. {
  39. ModuleCode = g.Key.mc,
  40. ModuleLabel = S8ModuleCode.Label(g.Key.mc),
  41. SceneCode = g.Key.sc,
  42. SceneLabel = S8SceneCode.Label(g.Key.sc),
  43. Total = g.Count(),
  44. Red = g.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  45. Yellow = g.Count(e => e.Severity == "MEDIUM"),
  46. Green = g.Count(e => e.Severity == "LOW"),
  47. Timeout = g.Count(e => e.TimeoutFlag && e.Status != "CLOSED")
  48. })
  49. // 按 S8ModuleCode.All 顺序排列
  50. .OrderBy(r => Array.IndexOf(S8ModuleCode.All, r.ModuleCode))
  51. .ToList();
  52. return new AdoS8MonitoringSummaryDto
  53. {
  54. Total = raw.Count,
  55. Red = raw.Count(e => e.Severity == "CRITICAL" || e.Severity == "HIGH"),
  56. Yellow = raw.Count(e => e.Severity == "MEDIUM"),
  57. Green = raw.Count(e => e.Severity == "LOW"),
  58. Timeout = raw.Count(e => e.TimeoutFlag && e.Status != "CLOSED"),
  59. ByModule = byModule
  60. };
  61. }
  62. }