using Khaemhat.Application.Instance; using Khaemhat.Core.Instance; using Khaemhat.Data; using Khaemhat.Data.Companies; using Khaemhat.Data.Querying; namespace Khaemhat.Application.Dashboard; /// What the Stato del sistema widget shows besides version, cache and licence. /// The database engine. /// The companies that are not disabled. /// Those that failed to open at start-up. /// Scheduled task runs of today that succeeded. /// Those that failed. public sealed record SystemStatus(DbEngine Engine, int Companies, int FailedCompanies, int JobsSucceeded, int JobsFailed); /// Reads the state of the instance for the administrators' widget. /// Opens the system database. /// The system database. /// The companies. /// What happened at start-up. /// The clock. public sealed class SystemStatusService(IUnitOfWorkFactory factory, DatabaseTarget system, ICompanyRegistry companies, BootstrapState bootstrap, TimeProvider clock) { /// The state now. /// Cancellation. /// The state. public async Task GetAsync(CancellationToken cancellationToken = default) { var all = await companies.ListAsync(cancellationToken).ConfigureAwait(false); var now = clock.GetLocalNow(); var since = new DateTimeOffset(now.Date, now.Offset).UtcDateTime; await using var unitOfWork = await factory.CreateAsync(DatabaseScope.System, cancellationToken).ConfigureAwait(false); var runs = (await unitOfWork.Repository().ListAsync(Query.For().Where(r => r.StartedAt >= since), cancellationToken).ConfigureAwait(false)).Items; return new SystemStatus( system.Engine, all.Count(c => c.Status != CompanyStatus.Disabled), bootstrap.FailedCompanies.Count, runs.Count(r => r.Outcome == JobOutcome.Succeeded), runs.Count(r => r.Outcome == JobOutcome.Failed)); } }