using System.Collections; using System.Globalization; using Anubis.Core; using Anubis.Db; using Anubis.Supervision; namespace Anubis.Server; /// /// "anubis soak": the report of an endurance run in real time (SPEC 17, phase 6) from the bot's own records. It only /// reads, so it can run while the bot works: in the container with docker exec. /// internal static class SoakCommand { private const string Usage = "Uso: anubis soak [--from AAAA-MM-GGTHH:MMZ] [--to AAAA-MM-GGTHH:MMZ] [--hours 48] [--out file.md]\n" + " senza --from la prova è delle ultime --hours ore fino a --to (o adesso).\n" + "Opzioni comuni: [--data-dir , obbligatoria fuori dal container]"; /// 0 when the run is in order, 4 when something must be looked at, 1 when the report could not be made. public static async Task RunAsync(string[] args, TextWriter? output = null, IReadOnlyDictionary? environment = null, IClock? clock = null) { output ??= Console.Out; clock ??= new SystemClock(); var own = new Dictionary(StringComparer.Ordinal); var rest = new List(); for (var i = 0; i < args.Length; i++) { if (args[i] is "--from" or "--to" or "--hours" or "--out" && i + 1 < args.Length) { own[args[i]] = args[++i]; } else if (args[i] is "--help" or "-h") { await output.WriteLineAsync(Usage).ConfigureAwait(false); return 2; } else { rest.Add(args[i]); } } environment ??= Environment.GetEnvironmentVariables().Cast().ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal); try { var hours = own.TryGetValue("--hours", out var h) ? double.Parse(h, CultureInfo.InvariantCulture) : 48; var to = own.TryGetValue("--to", out var t) ? DateTimeOffset.Parse(t, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) : clock.UtcNow; var from = own.TryGetValue("--from", out var f) ? DateTimeOffset.Parse(f, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) : to - TimeSpan.FromHours(hours); var options = ServerOptions.Parse([.. rest], environment); var config = options.LoadConfig(environment); if (!config.IsComplete) { await output.WriteLineAsync("La configurazione d'avvio non è completa: avvia prima il server una volta.").ConfigureAwait(false); return 1; } Redactor.Register(config.Db!.Password); await using var db = Database.Open(config.Db!, allowCreate: false); var report = await SoakReporter.BuildAsync(db, from, to, TimeSpan.FromHours(hours), config.TimeZone, CancellationToken.None).ConfigureAwait(false); var text = report.Markdown(config.TimeZone); if (own.TryGetValue("--out", out var file)) { await File.WriteAllTextAsync(Path.GetFullPath(file), text).ConfigureAwait(false); } await output.WriteAsync(text).ConfigureAwait(false); return report.Passed ? 0 : 4; } catch (Exception ex) when (ex is IOException or FormatException or ArgumentException or InvalidOperationException or SchemaException or System.Data.Common.DbException or UnauthorizedAccessException or TimeZoneNotFoundException) { await output.WriteLineAsync("Non riuscito: " + Redactor.Redact(ex.Message)).ConfigureAwait(false); return 1; } } }