From 8af9d13802fdd8b7bdff8e0a6d3b066027cc625f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 23 Aug 2025 12:54:49 -0400 Subject: [PATCH] Drop dump-books and dump-securities from spark_api_server cli These were subcommands were out of place. This exe has one purpose: run the Spark Api Server. --- src/client/spark_api_server/BookDumper.cs | 101 ------------------ .../spark_api_server/CommonOptionMetadata.cs | 12 --- .../spark_api_server/DumpBooksOptions.cs | 50 --------- .../spark_api_server/DumpSecuritiesOptions.cs | 54 ---------- src/client/spark_api_server/Program.cs | 55 ++++------ src/client/spark_api_server/RunOptions.cs | 71 ------------ src/client/spark_api_server/SecurityDumper.cs | 59 ---------- 7 files changed, 22 insertions(+), 380 deletions(-) delete mode 100644 src/client/spark_api_server/BookDumper.cs delete mode 100644 src/client/spark_api_server/CommonOptionMetadata.cs delete mode 100644 src/client/spark_api_server/DumpBooksOptions.cs delete mode 100644 src/client/spark_api_server/DumpSecuritiesOptions.cs delete mode 100644 src/client/spark_api_server/RunOptions.cs delete mode 100644 src/client/spark_api_server/SecurityDumper.cs diff --git a/src/client/spark_api_server/BookDumper.cs b/src/client/spark_api_server/BookDumper.cs deleted file mode 100644 index 482d7eaeb..000000000 --- a/src/client/spark_api_server/BookDumper.cs +++ /dev/null @@ -1,101 +0,0 @@ -namespace spark_api_server { - using bts; - using bts.api.core; - using bts.md; - - using mbts; - using mbts.spark; - - using Spark.Api.MarketData; - using Spark.Api.Server; - using Spark.Core; - using Spark.Core.Reactive; - - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reactive.Linq; - using System.Text; - using System.Threading.Tasks; - - static class BookDumper { - public static async Task run(DumpBooksOptions opts) { - Log.notnull(opts.user); - Log.notnull(opts.password); - var service_ctx = ServiceContext.create(opts.sparkd); - if (!await service_ctx.try_login(opts.user, opts.password)) { - return 1; - } - var md_source = new MDSource(TimeSpan.FromSeconds(0.5)); - var md_client = new MarketDataClient(md_source); - var name_set = opts.securities.empty_if_null().ToHashSet(); - var books_src = md_client.books(s => name_set.Contains(s.ToString()!)) - .Cache().ToObservable(); - var sub = books_src.Subscribe(on_books); - await books_src.LastOrDefaultAsync(); - return 0; - - void on_books(TableUpdate books) { - var updated_books = books.added.Concat(books.changed); - var desc = "\n".join(updated_books.Select(fmt_book)); - Console.WriteLine(desc); - - string fmt_book(BookTuple book_tuple) { - var sb = new StringBuilder(); - // Write the 2 line header (the security, followed by a separator line) - sb.AppendLine(book_tuple.security.ToString()); - var total_width = opts.quantity_width * 2 + opts.price_width + 2; - sb.AppendLine(new string('*', total_width)); - - // Format the book levels like the ladder. For example (w/ 2 price levels): - // ask_qty ask_price - // ask_qty ask_price - // ------------------------- - // bid_price bid_qty - // bid_price bid_qty - var bk = book_tuple.book; - var ask_lines = bk.asks - .TakeWhile((l, i) => i < opts.num_levels && l.size > 0) - .Reverse() - .Select(fmt_ask); - var bid_lines = bk.bids - .TakeWhile((l, i) => i < opts.num_levels && l.size > 0) - .Select(fmt_bid); - foreach (var l in ask_lines) { - sb.AppendLine(l); - } - - // write line to separate bids and asks (the length is the sum of col widths: - // qty + 1 + price + 1 + qty (the 1's represent space between cols) - sb.AppendLine(new string('-', total_width)); - - // Each bid level begins at the price column. Pad accordingly. - var bid_padding = new string(' ', opts.quantity_width + 1); - foreach (var l in bid_lines) { - sb.AppendLine(bid_padding + l); - } - sb.AppendLine(); - return sb.ToString(); - - string fmt_ask(PriceSize ps) { - var qty_text = fmt_qty(ps).PadRight(opts.quantity_width); - var price_text = fmt_price(ps).PadRight(opts.price_width); - return $"{qty_text} {price_text}"; - } - - string fmt_bid(PriceSize ps) { - var price_text = fmt_price(ps).PadRight(opts.price_width); - var qty_text = fmt_qty(ps).PadRight(opts.quantity_width); - return $"{price_text} {qty_text}"; - } - - string fmt_price(PriceSize ps) => - book_tuple.security.spec_or_default().format(ps.price); - - string fmt_qty(PriceSize ps) => - book_tuple.quantity_display.format((double)ps.size); - } - } - } - } -} diff --git a/src/client/spark_api_server/CommonOptionMetadata.cs b/src/client/spark_api_server/CommonOptionMetadata.cs deleted file mode 100644 index 8a29c9ee0..000000000 --- a/src/client/spark_api_server/CommonOptionMetadata.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Spark.Api.Server { - public class CommonOptionMetadata { -#if DEBUG - public const string DEFAULT_USER = "bts"; -#else - public const string DEFAULT_USER = ""; -#endif - public const string DEFAULT_PASSWORD = ""; - public const string SPARKD_META_VALUE = "URL"; - public const string SPARKD_HELP_TEXT = "The sparkd server"; - } -} diff --git a/src/client/spark_api_server/DumpBooksOptions.cs b/src/client/spark_api_server/DumpBooksOptions.cs deleted file mode 100644 index 5c6041911..000000000 --- a/src/client/spark_api_server/DumpBooksOptions.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace Spark.Api.Server { - using bts.utils; - - using CommandLine; - - using Spark.Core; - - using System.Collections.Generic; - - [Verb("dump-books")] - public class DumpBooksOptions { -#if DEBUG - const string _default_user = "bts"; -#else - const string _default_user = ""; -#endif - - [Option("user", Default = _default_user)] - public string? user { get; set; } - - [Option("password", Default = "")] - public string? password { get; set; } - - [Option("sparkd", MetaValue = "URL", HelpText = "The sparkd server")] - public string? sparkd { get; set; } - - [Option("securities", HelpText = "The securities for which to dump books", Separator = ' ')] - public IEnumerable? securities { get; set; } - - [Option("num-levels", HelpText = "The number of price levels to show (per side)", Default = 5)] - public int num_levels { get; set; } - - [Option("price-width", HelpText = "The price column width", Default = 10)] - public int price_width { get; set; } - - [Option("quantity-width", HelpText = "The quantity column width", Default = 10)] - public int quantity_width { get; set; } - - public override string ToString() => - TableFormatter.fmt_table(new (string, object?)[] { - ("user", user), - ("password", password), - ("sparkd", sparkd), - ("securities", ", ".join(securities.empty_if_null())), - ("num-levels", num_levels), - ("price-width", price_width), - ("quantity-width", quantity_width), - }); - } -} diff --git a/src/client/spark_api_server/DumpSecuritiesOptions.cs b/src/client/spark_api_server/DumpSecuritiesOptions.cs deleted file mode 100644 index 80815faac..000000000 --- a/src/client/spark_api_server/DumpSecuritiesOptions.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace Spark.Api.Server { - using bts.api; - using bts.utils; - - using CommandLine; - - using Spark.Core; - - using System; - using System.Collections.Generic; - using System.Text; - - [Verb("dump-securities")] - public class DumpSecuritiesOptions { - [Option("user", Default = CommonOptionMetadata.DEFAULT_USER)] - public string? user { get; set; } - - [Option("password", Default = CommonOptionMetadata.DEFAULT_PASSWORD)] - public string? password { get; set; } - - [Option( - "sparkd", - MetaValue = CommonOptionMetadata.SPARKD_META_VALUE, - HelpText = CommonOptionMetadata.SPARKD_HELP_TEXT)] - public string? sparkd { get; set; } - - [Option("types", HelpText = "The types of securities to dump", Separator = ' ')] - public IEnumerable? types { get; set; } - - [Option("venues", HelpText = "The venues to query", Separator = ' ')] - public IEnumerable? venues { get; set; } - - [Option("assets", HelpText = "The assets to query", Separator = ' ')] - public IEnumerable? assets { get; set; } - - [Option("verbose", HelpText = "Whether or not to show verbose output")] - public bool verbose { get; set; } - - [Option("timeout", HelpText = "The amount of time to wait for securities")] - public TimeSpan? timeout { get; set; } - - public override string ToString() => - TableFormatter.fmt_table(new (string, object?)[] { - ("user", user), - ("password", password), - ("sparkd", sparkd), - ("assets", ", ".join(assets.empty_if_null())), - ("venues", ", ".join(venues.empty_if_null())), - ("types", ", ".join(types.empty_if_null())), - ("verbose", verbose), - ("timeout", timeout), - }); - } -} diff --git a/src/client/spark_api_server/Program.cs b/src/client/spark_api_server/Program.cs index ea4ad26de..6b0793569 100644 --- a/src/client/spark_api_server/Program.cs +++ b/src/client/spark_api_server/Program.cs @@ -4,56 +4,45 @@ using bts.websocket_core; using Bts.DataStreamCore.Services; using Bts.PubSub.Services; -using CommandLine; - using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Spark.Api.Server; using Spark.Api.Services; using Spark.Core; using Spark.Core.Services; using Spark.WebSocket.Services; -using spark_api_server; - using System; using System.IO; using System.Reflection; using System.Threading.Tasks; -var res = Parser.Default.ParseArguments(args); -await res.MapResult>( - Run, SecurityDumper.run, BookDumper.run, errs => Task.FromResult(1)); - -static Task Run(RunOptions opts) { - var builder = WebApplication.CreateBuilder(new WebApplicationOptions { - Args = Environment.GetCommandLineArgs(), - ApplicationName = "spark_api_server", - }); - ConfigureApp(builder.Configuration, builder.Environment.EnvironmentName); - ConfigureServices(builder.Services, builder.Configuration); - var app = builder.Build(); - app.UseWebSockets(app.Configuration.GetValue("WebSocketOptions") ?? new() { KeepAliveInterval = TimeSpan.FromMinutes(2) }); - var gateway = new Lazy(app.Services.GetRequiredService); - app.Map("/", HandleRequest); - var errors = Errors.None; - Log.Api.info("Starting websocket server..."); - try { - app.Run(); - Log.Api.info("Websocket server stopped without error"); - } catch (Exception ex) { - Log.Api.error($"Websocket server threw ex: {ex}"); - errors |= Errors.Listen; - } - return Task.FromResult((int)errors); - - Task HandleRequest(HttpContext context) => - gateway.Value.HandleRequest(context); +var builder = WebApplication.CreateBuilder(new WebApplicationOptions { + Args = Environment.GetCommandLineArgs(), + ApplicationName = "spark_api_server", +}); +ConfigureApp(builder.Configuration, builder.Environment.EnvironmentName); +ConfigureServices(builder.Services, builder.Configuration); +var app = builder.Build(); +app.UseWebSockets(app.Configuration.GetValue("WebSocketOptions") ?? new() { KeepAliveInterval = TimeSpan.FromMinutes(2) }); +var gateway = new Lazy(app.Services.GetRequiredService); +app.Map("/", HandleRequest); +var errors = Errors.None; +Log.Api.info("Starting websocket server..."); +try { + app.Run(); + Log.Api.info("Websocket server stopped without error"); +} catch (Exception ex) { + Log.Api.error($"Websocket server threw ex: {ex}"); + errors |= Errors.Listen; } +Environment.Exit((int)errors); + +Task HandleRequest(HttpContext context) => + gateway.Value.HandleRequest(context); static void ConfigureApp(ConfigurationManager configBuilder, string environment) { // We prefix appsettings with the exe name because multiple exes will usually be in the build dir diff --git a/src/client/spark_api_server/RunOptions.cs b/src/client/spark_api_server/RunOptions.cs deleted file mode 100644 index 386b64866..000000000 --- a/src/client/spark_api_server/RunOptions.cs +++ /dev/null @@ -1,71 +0,0 @@ -namespace Spark.Api.Server { - using Bts.PubSub; - - using CommandLine; - - using System; - using System.IO; - using System.Text.Json; - using System.Text.Json.Serialization; - - [Verb("run")] - public class RunOptions { - [Option("user", Default = CommonOptionMetadata.DEFAULT_USER)] - public string? user { get; set; } - - [Option("password", Default = CommonOptionMetadata.DEFAULT_PASSWORD)] - public string? password { get; set; } - - [Option("port", Default = "8080")] - public string? port { get; set; } - - [Option("host", Default = "localhost")] - public string? host { get; set; } - - - [Option( - "sparkd", - MetaValue = CommonOptionMetadata.SPARKD_META_VALUE, - HelpText = CommonOptionMetadata.SPARKD_HELP_TEXT)] - public string? sparkd { get; set; } - - [JsonPropertyName("pubsub")] - public PubSubConfig? PubSub { get; set; } - - [JsonIgnore] - [Option( - "config", - MetaValue = "PATH", - HelpText = "Path to json file representing these options (RunOptions)")] - public string? ConfigFile { get; set; } - - public static RunOptions Default() => - new() { - PubSub = PubSubConfig.Default(), - }; - - public void MergeWith(RunOptions other) { - sparkd ??= other.sparkd; - PubSub ??= other.PubSub; - ConfigFile ??= other.ConfigFile; - } - - /// - /// Merges options with those defined in `ConfigFile` (does not overwrite - /// non-null existing values). - /// - /// - /// JsonSerializer.Deserialize returns null - /// - public void MergeWithConfigFile() { - if (ConfigFile is null) { - return; - } - var cfgString = File.ReadAllText(ConfigFile); - var parsedCfg = JsonSerializer.Deserialize(cfgString) ?? - throw new InvalidOperationException($"Failed to parse json from {ConfigFile}"); - MergeWith(parsedCfg); - ConfigFile = null; - } - } -} diff --git a/src/client/spark_api_server/SecurityDumper.cs b/src/client/spark_api_server/SecurityDumper.cs deleted file mode 100644 index 0ac4d2a10..000000000 --- a/src/client/spark_api_server/SecurityDumper.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace spark_api_server { - using bts; - using bts.api; - using bts.api.core; - using bts.md; - - using mbts.spark; - - using Spark.Api.Server; - using Spark.Core.Reactive; - - using System; - using System.Linq; - using System.Reactive.Linq; - using System.Threading.Tasks; - - static class SecurityDumper { - public static async Task run(DumpSecuritiesOptions opts) { - Log.notnull(opts.user); - Log.notnull(opts.password); - var service_ctx = ServiceContext.create(opts.sparkd); - if (!await service_ctx.try_login(opts.user, opts.password)) { - return 1; - } - var md_source = new MDSource(TimeSpan.FromSeconds(0.5)); - var md_client = new MarketDataClient(md_source); - var criteria = new SecurityCriteria( - new SecurityCriteriaClause[] { - new( - exchanges: opts.venues?.ToArray(), - security_types: opts.types?.ToArray(), - symbols: opts.assets?.ToArray()) - }); - var secs_src = md_client.securities_incremental(criteria); - var sub = secs_src.Subscribe(on_securities); - var wait_src = secs_src; - if (opts.timeout.HasValue) { - wait_src = wait_src.StopAfter(opts.timeout.Value); - } - await wait_src.LastOrDefaultAsync(); - return 0; - - void on_securities(TableUpdate securities) { - foreach (var sec in securities.added) { - Console.WriteLine(fmt(sec)); - } - - string fmt(SecurityImmutable sec) { - if (!opts.verbose) { - return sec.ToString()!; - } - var fmt_settings = Google.Protobuf.JsonFormatter.Settings.Default; - var formatter = new Google.Protobuf.JsonFormatter(fmt_settings); - return formatter.Format(sec.CloneToMutable()); - } - } - } - } -}