diff --git a/src/SQLInsight.Repository/InsightRepository.Read.cs b/src/SQLInsight.Repository/InsightRepository.Read.cs index d3ba057..7d7326a 100644 --- a/src/SQLInsight.Repository/InsightRepository.Read.cs +++ b/src/SQLInsight.Repository/InsightRepository.Read.cs @@ -93,18 +93,24 @@ public sealed partial class InsightRepository public async Task> GetInstancesAsync(CancellationToken cancellationToken = default) { await using var connection = await OpenAsync(cancellationToken).ConfigureAwait(false); + // 1.3: paused or removed in Servere og databaser. A name without any configuration row (history from before 1.3) + // counts as monitored, as it did. Guarded, so a 1.3 web UI still shows the overview against a repository the + // agent has not migrated yet (e.g. the web upgraded first on its own server). var rows = await connection.QueryAsync(""" + DECLARE @configured TABLE (Name nvarchar(128) NOT NULL PRIMARY KEY, Configured int NOT NULL, Active int NOT NULL, Paused int NOT NULL); + IF OBJECT_ID(N'insight.ConfiguredInstances') IS NOT NULL + INSERT @configured (Name, Configured, Active, Paused) + SELECT Name, COUNT(*), MAX(CASE WHEN RemovedAt IS NULL THEN 1 ELSE 0 END), MAX(CASE WHEN RemovedAt IS NULL AND Paused = 1 THEN 1 ELSE 0 END) + FROM insight.ConfiguredInstances + GROUP BY Name; + SELECT i.InstanceId, i.Name, i.ServerName, i.ProductVersion, i.ProductLevel, i.ProductUpdateLevel, i.Edition, i.EngineEdition, i.ConnectionProvider, i.UsedLegacyFallback, i.LastSeenAt, i.LastErrorAt, i.LastError, ISNULL(f.Critical, 0) AS Critical, ISNULL(f.High, 0) AS High, ISNULL(f.Medium, 0) AS Medium, ISNULL(f.Low, 0) AS Low, ISNULL(f.Info, 0) AS Info, ISNULL(r.NotMeasurable, 0) AS NotMeasurable, ISNULL(r.ManualChecks, 0) AS ManualChecks, CAST(ISNULL(c.Paused, 0) AS bit) AS Paused, CAST(CASE WHEN c.Configured > 0 AND c.Active = 0 THEN 1 ELSE 0 END AS bit) AS Removed FROM insight.Instances i - /* 1.3: paused or removed in Servere og databaser. A name without any configuration row (history from before - 1.3) counts as monitored, as it did. */ - OUTER APPLY (SELECT COUNT(*) AS Configured, MAX(CASE WHEN RemovedAt IS NULL THEN 1 ELSE 0 END) AS Active, - MAX(CASE WHEN RemovedAt IS NULL AND Paused = 1 THEN 1 ELSE 0 END) AS Paused - FROM insight.ConfiguredInstances WHERE Name = i.Name) c + LEFT JOIN @configured c ON c.Name = i.Name OUTER APPLY (SELECT SUM(CASE WHEN Severity = 4 THEN 1 ELSE 0 END) AS Critical, SUM(CASE WHEN Severity = 3 THEN 1 ELSE 0 END) AS High, SUM(CASE WHEN Severity = 2 THEN 1 ELSE 0 END) AS Medium, SUM(CASE WHEN Severity = 1 THEN 1 ELSE 0 END) AS Low, SUM(CASE WHEN Severity = 0 THEN 1 ELSE 0 END) AS Info