ADR 0021-0026 for the new course of 2 December 2026, roadmap of phases 10-24. The desktop application is gone: Docker is the only form, and outside a container the server runs only for development and tests with --data-dir, on this computer only. Lighter image (829 MB to 410 MB), rotating logs, backtest and learning as one-shot commands. refs #113 #114 #115 #78 #80 #81 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
131 lines
6.0 KiB
C#
131 lines
6.0 KiB
C#
using System.Collections;
|
|
using System.Globalization;
|
|
using Anubis.Brokers;
|
|
using Anubis.Core;
|
|
using Anubis.Data;
|
|
using Anubis.Db;
|
|
|
|
namespace Anubis.Server;
|
|
|
|
/// <summary>
|
|
/// Command line tools for the data (Phase 2): the quality report, and the imports into the research history of the
|
|
/// data folder (SPEC 6.6). They read the same startup configuration as the server.
|
|
/// </summary>
|
|
internal static class DataCommand
|
|
{
|
|
private const string Usage =
|
|
"Uso: anubis data report [--days N] [--out file.md]\n" +
|
|
" anubis data binance <SIMBOLO> <1d|1m> <AAAA-MM> (per esempio BTCUSDT 1d 2017-08)\n" +
|
|
" anubis data csv <file.csv> (colonne descritte in file.mapping.json o mapping.json)\n" +
|
|
"Opzioni comuni: [--data-dir <cartella>, obbligatoria fuori dal container]";
|
|
|
|
public static async Task<int> RunAsync(string[] args, TextWriter? output = null, IReadOnlyDictionary<string, string?>? environment = null,
|
|
HttpMessageHandler? handler = null)
|
|
{
|
|
output ??= Console.Out;
|
|
if (args.Length < 1 || args[0] is not ("report" or "binance" or "csv"))
|
|
{
|
|
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
|
return 2;
|
|
}
|
|
|
|
var (positional, rest) = Split(args[1..]);
|
|
environment ??= Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal);
|
|
var options = ServerOptions.Parse([.. rest], environment);
|
|
var config = options.LoadConfig(environment);
|
|
var clock = new SystemClock();
|
|
try
|
|
{
|
|
switch (args[0])
|
|
{
|
|
case "report":
|
|
return await ReportAsync(config, clock, rest, output).ConfigureAwait(false);
|
|
case "binance" when positional.Count == 3:
|
|
config.Data.Ensure();
|
|
var month = DateOnly.ParseExact(positional[2] + "-01", "yyyy-MM-dd", CultureInfo.InvariantCulture);
|
|
using (var http = handler is null ? new HttpClient() : new HttpClient(handler, disposeHandler: false))
|
|
{
|
|
http.DefaultRequestHeaders.UserAgent.ParseAdd($"{AnubisInfo.Name}/{AnubisInfo.Version}");
|
|
var importer = new BinanceImporter(new ApiHttp(http, clock, "binance"), new ResearchStore(config.Data.Research), clock);
|
|
await WriteAsync(output, await importer.ImportAsync(positional[0].ToUpperInvariant(), positional[1], month.Year, month.Month, CancellationToken.None)
|
|
.ConfigureAwait(false)).ConfigureAwait(false);
|
|
}
|
|
|
|
return 0;
|
|
case "csv" when positional.Count == 1:
|
|
config.Data.Ensure();
|
|
var file = Path.GetFullPath(positional[0]);
|
|
var result = await new CsvImporter(new ResearchStore(config.Data.Research)).ImportAsync(file, CsvImporter.MappingFor(file), CancellationToken.None)
|
|
.ConfigureAwait(false);
|
|
await WriteAsync(output, result).ConfigureAwait(false);
|
|
return 0;
|
|
default:
|
|
await output.WriteLineAsync(Usage).ConfigureAwait(false);
|
|
return 2;
|
|
}
|
|
}
|
|
catch (Exception ex) when (ex is IOException or MarketDataException or HttpRequestException or FormatException or System.Text.Json.JsonException
|
|
or InvalidOperationException or TimeZoneNotFoundException or System.Data.Common.DbException or UnauthorizedAccessException)
|
|
{
|
|
await output.WriteLineAsync("Non riuscito: " + Redactor.Redact(ex.Message)).ConfigureAwait(false);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
private static async Task<int> ReportAsync(Platform.StartupConfig config, IClock clock, IReadOnlyList<string> rest, TextWriter output)
|
|
{
|
|
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);
|
|
var days = Option(rest, "--days") is { } d ? int.Parse(d, CultureInfo.InvariantCulture) : 7;
|
|
await using var db = Database.Open(config.Db!, allowCreate: false);
|
|
var markdown = (await QualityReport.BuildAsync(db, clock, TimeSpan.FromDays(days), CancellationToken.None).ConfigureAwait(false)).ToMarkdown();
|
|
if (Option(rest, "--out") is { } path)
|
|
{
|
|
await File.WriteAllTextAsync(path, markdown).ConfigureAwait(false);
|
|
await output.WriteLineAsync("Rapporto scritto in " + Path.GetFullPath(path)).ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
await output.WriteAsync(markdown).ConfigureAwait(false);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static Task WriteAsync(TextWriter output, ImportResult result)
|
|
{
|
|
var lines = new List<string>
|
|
{
|
|
string.Create(CultureInfo.InvariantCulture,
|
|
$"{result.Venue} {result.Symbol} {result.Timeframe}: {result.Bars} barre, dal {result.First:yyyy-MM-dd HH:mm} al {result.Last:yyyy-MM-dd HH:mm} (UTC)."),
|
|
};
|
|
lines.AddRange(result.Notes.Select(n => "- " + n));
|
|
return output.WriteLineAsync(string.Join(Environment.NewLine, lines));
|
|
}
|
|
|
|
private static string? Option(IReadOnlyList<string> args, string name)
|
|
{
|
|
for (var i = 0; i < args.Count - 1; i++)
|
|
{
|
|
if (args[i] == name)
|
|
{
|
|
return args[i + 1];
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// Positional arguments come first; from the first "--" option on, everything is options.
|
|
private static (List<string> Positional, List<string> Options) Split(string[] args)
|
|
{
|
|
var index = Array.FindIndex(args, a => a.StartsWith("--", StringComparison.Ordinal));
|
|
return index < 0 ? (args.ToList(), []) : (args[..index].ToList(), args[index..].ToList());
|
|
}
|
|
}
|