namespace SQLInsight.Contracts;
///
/// Aggregated experience across SQLInsight Hub customers, sent back to agents. Contains no customer, instance or
/// server names: only counts, and only when at least different customers are behind them.
///
public sealed record HubInsights(DateTimeOffset GeneratedAt, int MinCustomers, IReadOnlyList Upgrades);
///
/// After instances of SQL Server major version were updated to or newer, findings
/// of they had went away within a few days on instances and stayed on
/// . Correlation, not proof of cause.
///
public sealed record UpgradeInsight(string RuleId, int Major, string ToBuild, string? ToLevel, int Customers, int Resolved, int Persisted)
{
public double ResolvedShare => Resolved + Persisted == 0 ? 0 : (double)Resolved / (Resolved + Persisted);
}
public static class HubInsightMatcher
{
///
/// Insights relevant to a finding: the same rule, the same major version, and a build newer than the instance's.
/// The one backed by most customers first.
///
public static IReadOnlyList For(HubInsights? insights, string ruleId, string? productVersion)
{
if (insights is null || productVersion is null || !Version.TryParse(productVersion, out var current))
{
return [];
}
return insights.Upgrades
.Where(i => i.RuleId == ruleId && i.Major == current.Major && Version.TryParse(i.ToBuild, out var build) && build > current)
.OrderByDescending(i => i.Customers).ThenBy(i => Version.Parse(i.ToBuild))
.ToList();
}
}