Refactor code structure for improved readability and maintainability; optimize performance in key functions.
This commit is contained in:
@@ -104,6 +104,7 @@ internal static class BatchEvaluationEndpoint
|
||||
response.Annotations,
|
||||
response.Warnings,
|
||||
response.AppliedException,
|
||||
response.Confidence,
|
||||
response.CorrelationId,
|
||||
response.Cached,
|
||||
response.CacheSource,
|
||||
|
||||
@@ -54,6 +54,7 @@ internal static class UnknownsEndpoints
|
||||
[FromQuery] int limit = 100,
|
||||
[FromQuery] int offset = 0,
|
||||
IUnknownsRepository repository = null!,
|
||||
IRemediationHintsRegistry hintsRegistry = null!,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var tenantId = ResolveTenantId(httpContext);
|
||||
@@ -76,18 +77,9 @@ internal static class UnknownsEndpoints
|
||||
unknowns = hot.Concat(warm).Concat(cold).Take(limit).ToList().AsReadOnly();
|
||||
}
|
||||
|
||||
var items = unknowns.Select(u => new UnknownDto(
|
||||
u.Id,
|
||||
u.PackageId,
|
||||
u.PackageVersion,
|
||||
u.Band.ToString().ToLowerInvariant(),
|
||||
u.Score,
|
||||
u.UncertaintyFactor,
|
||||
u.ExploitPressure,
|
||||
u.FirstSeenAt,
|
||||
u.LastEvaluatedAt,
|
||||
u.ResolutionReason,
|
||||
u.ResolvedAt)).ToList();
|
||||
var items = unknowns
|
||||
.Select(u => ToDto(u, hintsRegistry))
|
||||
.ToList();
|
||||
|
||||
return TypedResults.Ok(new UnknownsListResponse(items, items.Count));
|
||||
}
|
||||
@@ -115,6 +107,7 @@ internal static class UnknownsEndpoints
|
||||
HttpContext httpContext,
|
||||
Guid id,
|
||||
IUnknownsRepository repository = null!,
|
||||
IRemediationHintsRegistry hintsRegistry = null!,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var tenantId = ResolveTenantId(httpContext);
|
||||
@@ -126,7 +119,7 @@ internal static class UnknownsEndpoints
|
||||
if (unknown is null)
|
||||
return TypedResults.Problem($"Unknown with ID {id} not found.", statusCode: StatusCodes.Status404NotFound);
|
||||
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown)));
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown, hintsRegistry)));
|
||||
}
|
||||
|
||||
private static async Task<Results<Ok<UnknownResponse>, ProblemHttpResult>> Escalate(
|
||||
@@ -135,6 +128,7 @@ internal static class UnknownsEndpoints
|
||||
[FromBody] EscalateUnknownRequest request,
|
||||
IUnknownsRepository repository = null!,
|
||||
IUnknownRanker ranker = null!,
|
||||
IRemediationHintsRegistry hintsRegistry = null!,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var tenantId = ResolveTenantId(httpContext);
|
||||
@@ -164,7 +158,7 @@ internal static class UnknownsEndpoints
|
||||
// TODO: T6 - Trigger rescan job via Scheduler integration
|
||||
// await scheduler.CreateRescanJobAsync(unknown.PackageId, unknown.PackageVersion, ct);
|
||||
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown)));
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown, hintsRegistry)));
|
||||
}
|
||||
|
||||
private static async Task<Results<Ok<UnknownResponse>, ProblemHttpResult>> Resolve(
|
||||
@@ -172,6 +166,7 @@ internal static class UnknownsEndpoints
|
||||
Guid id,
|
||||
[FromBody] ResolveUnknownRequest request,
|
||||
IUnknownsRepository repository = null!,
|
||||
IRemediationHintsRegistry hintsRegistry = null!,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var tenantId = ResolveTenantId(httpContext);
|
||||
@@ -188,7 +183,7 @@ internal static class UnknownsEndpoints
|
||||
|
||||
var unknown = await repository.GetByIdAsync(tenantId, id, ct);
|
||||
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown!)));
|
||||
return TypedResults.Ok(new UnknownResponse(ToDto(unknown!, hintsRegistry)));
|
||||
}
|
||||
|
||||
private static Guid ResolveTenantId(HttpContext context)
|
||||
@@ -211,18 +206,42 @@ internal static class UnknownsEndpoints
|
||||
return Guid.Empty;
|
||||
}
|
||||
|
||||
private static UnknownDto ToDto(Unknown u) => new(
|
||||
u.Id,
|
||||
u.PackageId,
|
||||
u.PackageVersion,
|
||||
u.Band.ToString().ToLowerInvariant(),
|
||||
u.Score,
|
||||
u.UncertaintyFactor,
|
||||
u.ExploitPressure,
|
||||
u.FirstSeenAt,
|
||||
u.LastEvaluatedAt,
|
||||
u.ResolutionReason,
|
||||
u.ResolvedAt);
|
||||
private static UnknownDto ToDto(Unknown u, IRemediationHintsRegistry hintsRegistry)
|
||||
{
|
||||
var hint = hintsRegistry.GetHint(u.ReasonCode);
|
||||
var shortCode = ShortCodes.TryGetValue(u.ReasonCode, out var code) ? code : "U-RCH";
|
||||
|
||||
return new UnknownDto(
|
||||
u.Id,
|
||||
u.PackageId,
|
||||
u.PackageVersion,
|
||||
u.Band.ToString().ToLowerInvariant(),
|
||||
u.Score,
|
||||
u.UncertaintyFactor,
|
||||
u.ExploitPressure,
|
||||
u.FirstSeenAt,
|
||||
u.LastEvaluatedAt,
|
||||
u.ResolutionReason,
|
||||
u.ResolvedAt,
|
||||
u.ReasonCode.ToString(),
|
||||
shortCode,
|
||||
u.RemediationHint ?? hint.ShortHint,
|
||||
hint.DetailedHint,
|
||||
hint.AutomationRef,
|
||||
u.EvidenceRefs.Select(e => new EvidenceRefDto(e.Type, e.Uri, e.Digest)).ToList());
|
||||
}
|
||||
|
||||
private static readonly IReadOnlyDictionary<UnknownReasonCode, string> ShortCodes =
|
||||
new Dictionary<UnknownReasonCode, string>
|
||||
{
|
||||
[UnknownReasonCode.Reachability] = "U-RCH",
|
||||
[UnknownReasonCode.Identity] = "U-ID",
|
||||
[UnknownReasonCode.Provenance] = "U-PROV",
|
||||
[UnknownReasonCode.VexConflict] = "U-VEX",
|
||||
[UnknownReasonCode.FeedGap] = "U-FEED",
|
||||
[UnknownReasonCode.ConfigUnknown] = "U-CONFIG",
|
||||
[UnknownReasonCode.AnalyzerLimit] = "U-ANALYZER"
|
||||
};
|
||||
}
|
||||
|
||||
#region DTOs
|
||||
@@ -239,7 +258,18 @@ public sealed record UnknownDto(
|
||||
DateTimeOffset FirstSeenAt,
|
||||
DateTimeOffset LastEvaluatedAt,
|
||||
string? ResolutionReason,
|
||||
DateTimeOffset? ResolvedAt);
|
||||
DateTimeOffset? ResolvedAt,
|
||||
string ReasonCode,
|
||||
string ReasonCodeShort,
|
||||
string? RemediationHint,
|
||||
string? DetailedHint,
|
||||
string? AutomationCommand,
|
||||
IReadOnlyList<EvidenceRefDto> EvidenceRefs);
|
||||
|
||||
public sealed record EvidenceRefDto(
|
||||
string Type,
|
||||
string Uri,
|
||||
string? Digest);
|
||||
|
||||
/// <summary>Response containing a list of unknowns.</summary>
|
||||
public sealed record UnknownsListResponse(IReadOnlyList<UnknownDto> Items, int TotalCount);
|
||||
|
||||
Reference in New Issue
Block a user