using System.Data.Common; using SQLInsight.Core.Instances; namespace SQLInsight.Collectors; /// /// Reads one kind of data from a monitored instance. Collectors are stateless; anything they need from the /// previous run (to compute deltas or continue reading) is passed in . /// public interface ICollector { string Name { get; } /// The type of the collected data, as rules look it up in CollectedData. Type DataType { get; } TimeSpan DefaultInterval { get; } /// What the instance must support. If anything is missing, the collector does not run. IReadOnlyCollection RequiredCapabilities { get; } Task CollectAsync(CollectorContext context); } /// This collector's result from the previous run on the same instance, if any. public sealed record CollectorContext( DbConnection Connection, InstanceInfo Instance, InstanceCapabilities Capabilities, DateTimeOffset Now, object? Previous, CancellationToken CancellationToken); /// /// Typed base class for collectors. /// public abstract class Collector : ICollector where T : class { public abstract string Name { get; } public Type DataType => typeof(T); public abstract TimeSpan DefaultInterval { get; } public virtual IReadOnlyCollection RequiredCapabilities => []; async Task ICollector.CollectAsync(CollectorContext context) => await CollectAsync(context, context.Previous as T).ConfigureAwait(false); protected abstract Task CollectAsync(CollectorContext context, T? previous); }