using System.Diagnostics; using System.Globalization; using System.Text.Json; using Anubis.Core; using Anubis.Data; using Anubis.Db; using Anubis.Platform; namespace Anubis.Server; /// How a component is doing: working, off by choice or not there yet, or in need of a look. internal enum PartState { Ok, Off, Warn, } /// A component of the system with a line on how it is doing. internal sealed record SystemPart(string Name, string Detail, PartState State); /// A resource of the computer: what is used, out of how much, as a share for the bar. internal sealed record HostResource(string Label, string Value, string? Of, decimal Share); /// A job in the scheduler, with its time as people say it ("15:35", "Dom 03:00"). internal sealed record PlannedJob(string Time, string Text); /// /// The "Diario e sistema" right column and the Sistema tile of the Panoramica (SPEC 13.6, mockups 01 and 08): the /// components with their state, the jobs in the scheduler, the resources of the computer, version and platform. /// In demo mode the values of appendix F.10 (SPEC 13.1); otherwise the real services, process and disk. /// internal sealed record SystemView( IReadOnlyList Parts, IReadOnlyList Tiles, IReadOnlyList Jobs, IReadOnlyList Resources, string Running) { private static readonly CultureInfo _it = CultureInfo.InvariantCulture; private static readonly string[] _days = ["Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"]; private static readonly string[] _months = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"]; /// Nothing needs a look; components that are off by choice or not active yet do not count. public bool AllWell => Parts.All(p => p.State != PartState.Warn); public static async Task LoadAsync(AnubisHost host, ShellData shell, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(host); ArgumentNullException.ThrowIfNull(shell); using var aggregates = shell.Aggregates(); return aggregates is not null ? Demo(aggregates.RootElement, shell) : await RealAsync(host, shell, cancellationToken).ConfigureAwait(false); } private static SystemView Demo(JsonElement root, ShellData shell) { var system = root.GetProperty("sistema"); var accounts = root.GetProperty("conti").EnumerateArray().ToList(); var okxMs = accounts[0].GetProperty("risposta_ms").GetInt32(); var alpacaMs = accounts[1].GetProperty("risposta_ms").GetInt32(); var database = root.GetProperty("database").GetString()!; var size = Format.Number(Dec(system, "database_gb"), 1) + " GB"; var telegram = system.GetProperty("telegram_ultimo").GetString(); var parts = new List { new("Motore di trading", $"attivo, un controllo ogni {system.GetProperty("controllo_secondi").GetInt32()} secondi", PartState.Ok), new("Prezzi da OKX Europe", $"in diretta, risposta in {okxMs} ms", PartState.Ok), new("Prezzi da Alpaca", "collegato, " + Market(shell), PartState.Ok), new("Lettura delle notizie", $"{system.GetProperty("notizie_fonti").GetInt32()} fonti, ultima lettura alle {system.GetProperty("notizie_ultima").GetString()}", PartState.Ok), new("Database " + database, $"{size}, ultimo salvataggio alle {system.GetProperty("ultimo_salvataggio").GetString()}", PartState.Ok), new("Telegram", $"collegato, ultimo messaggio alle {telegram}", PartState.Ok), }; var tiles = new List { new("OKX Europe", $"connesso, {okxMs} ms", PartState.Ok), new("Alpaca", $"connesso, {alpacaMs} ms", PartState.Ok), new("Prezzi di mercato", $"aggiornati {root.GetProperty("dati_aggiornati_secondi").GetInt32()} s fa", PartState.Ok), new("Notizie", $"{root.GetProperty("notizie").GetProperty("lette_24h").GetInt32()} lette in 24 ore", PartState.Ok), new("Database", $"{database}, {size}", PartState.Ok), new("Telegram", $"ultimo messaggio alle {telegram}", PartState.Ok), }; var jobs = system.GetProperty("lavori").EnumerateArray() .Select(j => new PlannedJob(j.GetProperty("ora").GetString()!, j.GetProperty("testo").GetString()!)).ToList(); var memory = Dec(system, "memoria_gb"); var memoryTotal = system.GetProperty("memoria_totale_gb").GetDecimal(); var disk = Dec(system, "disco_gb"); var diskTotal = system.GetProperty("disco_totale_gb").GetDecimal(); var resources = new List { new("Processore", system.GetProperty("cpu_pct").GetInt32().ToString(_it) + "%", null, system.GetProperty("cpu_pct").GetDecimal() / 100m), new("Memoria", Format.Number(memory, 1) + " GB", Format.Number(memoryTotal), memory / memoryTotal), new("Spazio per i dati", Format.Number(disk, 1) + " GB", Format.Number(diskTotal), disk / diskTotal), }; var started = DateOnly.ParseExact(root.GetProperty("avvio").GetString()!, "yyyy-MM-dd", _it); var running = $"Anubis {root.GetProperty("versione").GetString()} in {root.GetProperty("piattaforma").GetString()}, acceso dal {started.Day} {_months[started.Month - 1]}: " + $"{root.GetProperty("giorno").GetInt32()} giorni senza interruzioni."; return new SystemView(parts, tiles, jobs, resources, running); } private static async Task RealAsync(AnubisHost host, ShellData shell, CancellationToken cancellationToken) { var services = host.Services!; var now = shell.Now; var zone = shell.Zone; var parts = new List(); var tiles = new List(); // The trading engine, and the state the user chose for the bot. var bot = (await services.Settings.GetAsync(SettingsStore.BotState.Key, cancellationToken).ConfigureAwait(false)).Value; var trading = services.Trading; parts.Add(trading switch { null => new SystemPart("Motore di trading", "spento: le strategie non lavorano", PartState.Off), { IsStarted: false } => new SystemPart("Motore di trading", "non รจ ancora partito: se non parte entro qualche minuto, guarda i registri tecnici", PartState.Warn), _ when bot == "stopped" => new SystemPart("Motore di trading", "fermo con Ferma tutto: riparte solo con la tua conferma, dalla pagina Rischio", PartState.Warn), _ => new SystemPart("Motore di trading", $"{(bot == "paused" ? "in pausa" : "attivo")}, un controllo ogni {(int)trading.TickInterval.TotalSeconds} secondi", PartState.Ok), }); // The two price feeds: the live OKX connection, the state of the Alpaca source. var market = services.MarketData; var sources = market is null ? [] : await new SourceStateStore(services.Db, host.Clock).ListAsync(cancellationToken).ConfigureAwait(false); var okx = sources.FirstOrDefault(s => s.Source == MarketDataService.OkxSource); var (okxPart, okxTile) = market switch { null => (new SystemPart("Prezzi da OKX Europe", "spenti", PartState.Off), new SystemPart("OKX Europe", "spento", PartState.Off)), { LiveConnected: true } => (new SystemPart("Prezzi da OKX Europe", "in diretta", PartState.Ok), new SystemPart("OKX Europe", "connesso", PartState.Ok)), _ when Failing(okx) => (new SystemPart("Prezzi da OKX Europe", "non rispondono: riprovo da solo", PartState.Warn), new SystemPart("OKX Europe", "non risponde", PartState.Warn)), _ => (new SystemPart("Prezzi da OKX Europe", "in collegamento", PartState.Off), new SystemPart("OKX Europe", "in collegamento", PartState.Off)), }; parts.Add(okxPart); var alpaca = sources.FirstOrDefault(s => s.Source == MarketDataService.AlpacaSource); var (alpacaPart, alpacaTile) = (market, alpaca) switch { (null, _) => (new SystemPart("Prezzi da Alpaca", "spenti", PartState.Off), new SystemPart("Alpaca", "spento", PartState.Off)), (_, null) => (new SystemPart("Prezzi da Alpaca", "in attesa del primo collegamento", PartState.Off), new SystemPart("Alpaca", "in attesa", PartState.Off)), (_, { Enabled: false }) => (new SystemPart("Prezzi da Alpaca", "spenti: mancano le chiavi del conto di prova, in Impostazioni", PartState.Off), new SystemPart("Alpaca", "senza chiavi", PartState.Off)), _ when Failing(alpaca) => (new SystemPart("Prezzi da Alpaca", "non rispondono: riprovo da solo", PartState.Warn), new SystemPart("Alpaca", "non risponde", PartState.Warn)), _ => (new SystemPart("Prezzi da Alpaca", "collegato, " + Market(shell, market.UsCalendar), PartState.Ok), new SystemPart("Alpaca", "connesso", PartState.Ok)), }; parts.Add(alpacaPart); tiles.Add(okxTile); tiles.Add(alpacaTile); var newest = market?.Freshness.Snapshot().Select(s => s.LastDataAt).Max(); tiles.Add(newest is { } at ? new SystemPart("Prezzi di mercato", "aggiornati " + Ago(now - at), now - at > TimeSpan.FromMinutes(5) ? PartState.Warn : PartState.Ok) : new SystemPart("Prezzi di mercato", "in attesa dei primi prezzi", PartState.Off)); // News arrive with phase 7 (SPEC 10). parts.Add(new SystemPart("Lettura delle notizie", "non ancora attiva", PartState.Off)); tiles.Add(new SystemPart("Notizie", "non ancora attive", PartState.Off)); // The database: engine, size, last backup. var engine = services.Db.Engine == DbEngine.Postgres ? "PostgreSQL" : "SQLite"; var bytes = await SizeAsync(services.Db, host.Config.Db, cancellationToken).ConfigureAwait(false); var backups = services.Backups.List(); var lastBackup = backups.Count == 0 ? null : (DateTimeOffset?)backups.Max(b => b.TakenAt); var size = bytes is { } b ? Bytes(b) : null; var saved = lastBackup is { } l ? "ultimo salvataggio " + When(l, now, zone) : "nessun salvataggio ancora"; parts.Add(new SystemPart("Database " + engine, (size is null ? "" : size + ", ") + saved, PartState.Ok)); tiles.Add(new SystemPart("Database", engine + (size is null ? "" : ", " + size), PartState.Ok)); // Telegram: the last message that really left (SPEC 14), never one only queued. var telegram = services.Telegram; var settings = telegram is null ? null : await telegram.ReadAsync(cancellationToken).ConfigureAwait(false); if (telegram is null || settings is null) { parts.Add(new SystemPart("Telegram", "non configurato: si imposta in Impostazioni", PartState.Off)); tiles.Add(new SystemPart("Telegram", "non configurato", PartState.Off)); } else { var summary = await telegram.Sender.SummaryAsync(cancellationToken).ConfigureAwait(false); var last = summary.LastSentAt is { } sent ? "ultimo messaggio " + When(sent, now, zone) : "nessun messaggio inviato ancora"; var stuck = summary.Retrying + summary.GaveUp > 0; parts.Add(new SystemPart("Telegram", stuck ? "alcuni messaggi non sono partiti: riprovo da solo; " + last : "collegato, " + last, stuck ? PartState.Warn : PartState.Ok)); tiles.Add(new SystemPart("Telegram", last, stuck ? PartState.Warn : PartState.Ok)); } // Appointments only: the jobs that run every minute or every hour are background work. var jobs = services.Scheduler.Upcoming(200).Where(u => !u.Periodic).Take(7) .Select(u => new PlannedJob(JobTime(u.DueUtc, now, zone), u.Description)).ToList(); var since = host.Status.Since; var platform = host.Config.Platform == PlatformKind.Docker ? "in Docker" : "come applicazione desktop"; var localSince = TimeZoneInfo.ConvertTime(since, zone); var days = (int)Math.Floor((now - since).TotalDays); var running = days >= 1 ? $"Anubis {AnubisInfo.Version} {platform}, acceso dal {localSince.Day} {_months[localSince.Month - 1]}: {days} {(days == 1 ? "giorno" : "giorni")} senza interruzioni." : $"Anubis {AnubisInfo.Version} {platform}, acceso oggi alle {localSince.ToString("HH:mm", _it)}."; return new SystemView(parts, tiles, jobs, MeasureHost(host.Config.Data), running); } private static bool Failing(SourceState? state) => state is { LastError: not null } && (state.LastSuccessAt is null || state.LastRequestAt > state.LastSuccessAt); private static string Market(ShellData shell, IMarketCalendar? calendar = null) { var (open, next) = AccountsView.MarketState(calendar, shell.Now); var time = TimeZoneInfo.ConvertTime(next, shell.Zone).ToString("HH:mm", _it); return open ? "mercato aperto fino alle " + time : "mercato chiuso fino alle " + time; } private static async Task SizeAsync(Database db, DbSettings? settings, CancellationToken cancellationToken) { try { if (db.Engine == DbEngine.Postgres) { return await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT pg_database_size(current_database()) AS n", r => (long?)r.Number("n")), cancellationToken) .ConfigureAwait(false); } if (settings?.SqlitePath is { } path && File.Exists(path)) { var wal = path + "-wal"; return new FileInfo(path).Length + (File.Exists(wal) ? new FileInfo(wal).Length : 0); } } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Data.Common.DbException) { // The size is a detail: without it the line still says engine and backups. } return null; } /// /// Processor of this process (share of all cores since the last look, or since it started), its memory against /// what the container or computer gives it, and the disk that holds the data folder. /// private static List MeasureHost(DataDirectory data) { var list = new List(); var cpu = CpuSampler.Share(); list.Add(new HostResource("Processore", Format.Number(cpu * 100m) + "%", null, cpu)); using var process = Process.GetCurrentProcess(); var used = process.WorkingSet64; var total = GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; if (total > 0) { list.Add(new HostResource("Memoria", Bytes(used), Bytes(total, unit: false), Math.Clamp((decimal)used / total, 0m, 1m))); } try { var drive = new DriveInfo(Path.GetPathRoot(Path.GetFullPath(data.Root))!); if (drive.IsReady && drive.TotalSize > 0) { var taken = drive.TotalSize - drive.AvailableFreeSpace; list.Add(new HostResource("Spazio per i dati", Bytes(taken), Bytes(drive.TotalSize, unit: false), Math.Clamp((decimal)taken / drive.TotalSize, 0m, 1m))); } } catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or ArgumentException) { // A disk that cannot be measured is left out rather than shown wrong. } return list; } /// A size as people read it: "780 MB", "1,8 GB"; without the unit for the "su 50" after the value. public static string Bytes(long bytes, bool unit = true) { const decimal giga = 1024m * 1024m * 1024m; var gb = bytes / giga; if (!unit) { return Format.Number(gb, gb >= 10m ? 0 : 1); } return gb >= 1m ? Format.Number(gb, 1) + " GB" : Format.Number(Math.Max(1m, bytes / (1024m * 1024m))) + " MB"; } private static string When(DateTimeOffset moment, DateTimeOffset now, TimeZoneInfo zone) { var text = Format.ShortDate(moment, now, zone); return text.StartsWith("Oggi ", StringComparison.Ordinal) ? "alle " + text["Oggi ".Length..] : text.StartsWith("Ieri ", StringComparison.Ordinal) ? "ieri alle " + text["Ieri ".Length..] : "del " + text; } private static string Ago(TimeSpan elapsed) => elapsed.TotalSeconds < 60 ? $"{Math.Max(0, (int)elapsed.TotalSeconds)} s fa" : Format.Ago(elapsed); /// The time alone within a day, otherwise the day too: "15:35", "Dom 03:00". public static string JobTime(DateTimeOffset due, DateTimeOffset now, TimeZoneInfo zone) { var local = TimeZoneInfo.ConvertTime(due, zone); var time = local.ToString("HH:mm", _it); return due - now < TimeSpan.FromHours(24) ? time : _days[(int)local.DayOfWeek] + " " + time; } private static decimal Dec(JsonElement element, string name) => decimal.Parse(element.GetProperty(name).GetString()!, _it); /// The processor used by this process between two looks, as a share of all cores. private static class CpuSampler { private static readonly Lock _gate = new(); private static (long Ticks, TimeSpan Cpu)? _last; public static decimal Share() { using var process = Process.GetCurrentProcess(); var cpu = process.TotalProcessorTime; var ticks = Stopwatch.GetTimestamp(); lock (_gate) { TimeSpan wall; TimeSpan used; if (_last is { } last && Stopwatch.GetElapsedTime(last.Ticks, ticks) >= TimeSpan.FromSeconds(1)) { wall = Stopwatch.GetElapsedTime(last.Ticks, ticks); used = cpu - last.Cpu; } else { // First look, or two looks too close together: the average since the process started. wall = Uptime(process); used = cpu; } _last = (ticks, cpu); var share = wall <= TimeSpan.Zero ? 0d : used.TotalMilliseconds / (wall.TotalMilliseconds * Environment.ProcessorCount); return Math.Clamp((decimal)share, 0m, 1m); } } // A measure of the process, not a business time: the system clock against the process's own start, since the // injected clock may stand still (demo) or run in another world (tests). private static TimeSpan Uptime(Process process) => DateTime.UtcNow - process.StartTime.ToUniversalTime(); } }