AdoSmartOpsKpiRunLogQueryService.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Admin.NET.Core;
  2. using Admin.NET.Plugin.AiDOP.Dto.SmartOps;
  3. using Admin.NET.Plugin.AiDOP.Entity;
  4. using SqlSugar;
  5. namespace Admin.NET.Plugin.AiDOP.SmartOps;
  6. /// <summary>
  7. /// KPI 计算运行日志查询(只读、分页、脱敏)。租户由 MetricCode 解析,不信任前端;
  8. /// 输出仅可展示字段,不返回 SQL 明文 / 参数快照 / 连接串;错误摘要截断。
  9. /// </summary>
  10. public sealed class AdoSmartOpsKpiRunLogQueryService : ITransient
  11. {
  12. private const int DefaultPageSize = 20;
  13. private const int MaxPageSize = 100;
  14. private const int ErrorMessageMaxLen = 300;
  15. private readonly ISqlSugarClient _db;
  16. public AdoSmartOpsKpiRunLogQueryService(ISqlSugarClient db)
  17. {
  18. _db = db;
  19. }
  20. public async Task<KpiRunLogPageDto> QueryAsync(KpiRunLogQueryDto dto)
  21. {
  22. if (string.IsNullOrWhiteSpace(dto.MetricCode))
  23. throw Oops.Bah("metricCode 必填");
  24. var moduleCode = AdoSmartOpsKpiBusinessInputService.ResolveModuleCode(dto.MetricCode);
  25. var tenantId = AdoSmartOpsKpiCalcConfigService.ResolveKpiTenantId(moduleCode);
  26. var page = dto.Page <= 0 ? 1 : dto.Page;
  27. var pageSize = dto.PageSize <= 0 ? DefaultPageSize : Math.Min(dto.PageSize, MaxPageSize);
  28. var status = string.IsNullOrWhiteSpace(dto.Status) ? null : dto.Status.Trim().ToUpperInvariant();
  29. RefAsync<int> total = 0;
  30. var rows = await _db.Queryable<AdoSmartOpsKpiCalcRunLog>().ClearFilter<ITenantIdFilter>()
  31. .Where(x => x.MetricCode == dto.MetricCode && x.TenantId == tenantId)
  32. .WhereIF(status != null, x => x.Status == status)
  33. .WhereIF(dto.StartTime != null, x => x.StartedAt >= dto.StartTime!.Value)
  34. .WhereIF(dto.EndTime != null, x => x.StartedAt <= dto.EndTime!.Value)
  35. .OrderBy(x => x.StartedAt, OrderByType.Desc)
  36. .ToPageListAsync(page, pageSize, total);
  37. return new KpiRunLogPageDto
  38. {
  39. Total = total.Value,
  40. Page = page,
  41. PageSize = pageSize,
  42. List = rows.Select(ToItem).ToList(),
  43. };
  44. }
  45. private static KpiRunLogItemDto ToItem(AdoSmartOpsKpiCalcRunLog e) => new()
  46. {
  47. Id = e.Id,
  48. BatchId = e.BatchId,
  49. MetricCode = e.MetricCode,
  50. EngineType = e.EngineType,
  51. VersionNo = e.VersionNo,
  52. DataSourceCode = e.DataSourceCode,
  53. BizDate = e.BizDate.ToString("yyyy-MM-dd"),
  54. StartedAt = e.StartedAt.ToString("yyyy-MM-dd HH:mm:ss"),
  55. DurationMs = e.DurationMs,
  56. Status = e.Status,
  57. RowCount = e.RowCount,
  58. MetricValue = e.MetricValue,
  59. ErrorCode = e.ErrorCode,
  60. ErrorMessage = Truncate(e.ErrorMessage, ErrorMessageMaxLen),
  61. TriggerType = e.TriggerType,
  62. };
  63. private static string? Truncate(string? s, int max) =>
  64. string.IsNullOrEmpty(s) ? s : (s.Length <= max ? s : s.Substring(0, max) + "…");
  65. }