using SQLInsight.Contracts;
using SQLInsight.Core.Instances;
using SQLInsight.Repository;
using SQLInsight.Rules.Engine;
namespace SQLInsight.Agent.Hub;
///
/// Turns an instance's current state into a hub submission, leaving out what the customer has not allowed.
///
public static class SubmissionBuilder
{
public static readonly string AgentVersion =
typeof(SubmissionBuilder).Assembly.GetName().Version?.ToString() ?? "0.0.0";
public static FindingsSubmission Build(
string instanceName, InstanceInfo info, IReadOnlyList results, IReadOnlyList activeFindings,
HubOptions options, DateTimeOffset now)
{
ArgumentNullException.ThrowIfNull(info);
ArgumentNullException.ThrowIfNull(results);
ArgumentNullException.ThrowIfNull(activeFindings);
ArgumentNullException.ThrowIfNull(options);
return new FindingsSubmission
{
SubmissionId = Guid.NewGuid(),
AgentVersion = AgentVersion,
CollectedAt = now,
Instance = new InstanceSnapshot
{
InstanceKey = instanceName,
Release = info.Release.DisplayName(),
ProductVersion = info.Version.ToString(),
ProductLevel = info.ProductLevel,
ProductUpdateLevel = info.ProductUpdateLevel,
Edition = info.Edition,
ServerName = options.SendServerName ? info.ServerName : null,
},
Rules = results.Select(r => new RuleOutcomeDto { RuleId = r.RuleId, Outcome = r.Outcome.ToString(), Reason = r.Reason }).ToList(),
Findings = activeFindings.Select(f => new FindingDto
{
RuleId = f.RuleId,
Subject = f.Subject,
Severity = f.Severity.ToString(),
Title = f.Title,
Explanation = f.Explanation,
Recommendation = f.Recommendation,
FirstSeenAt = new DateTimeOffset(DateTime.SpecifyKind(f.FirstSeenAt, DateTimeKind.Utc)),
Evidence = f.Evidence
.Select(e => new EvidenceDto(e.Name, e.Sensitive && !options.SendSensitiveEvidence ? FindingsSubmission.Redacted : e.Value))
.ToList(),
}).ToList(),
};
}
}