From 287e784d5ebfee070065c1b962293cdf5a350951 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Mon, 21 Oct 2024 04:15:18 +0000 Subject: [PATCH] feat: Add stream subscriptions to websocket api --- Spark.Api.Server.sln | 50 +++++++++++ src/client/spark_api_server/appsettings.json | 57 ++++++++++++ .../Core/WebsocketSubscriptionHandler.cs | 86 +++++++++++++++++++ .../spark_websocket/spark_websocket.csproj | 2 + 4 files changed, 195 insertions(+) diff --git a/Spark.Api.Server.sln b/Spark.Api.Server.sln index 54a66e32c..4c5948466 100644 --- a/Spark.Api.Server.sln +++ b/Spark.Api.Server.sln @@ -14,6 +14,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "spark_test", "src\client\sp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spark.Console", "src\client\Spark.Console\Spark.Console.csproj", "{89D666E6-B500-498F-B026-4D167F68ADFC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bts.Net", "src\DataStreamSuite\Bts.Net\Bts.Net.csproj", "{CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStreamCore", "src\DataStreamSuite\DataStreamCore\DataStreamCore.csproj", "{CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStream", "src\DataStreamSuite\DataStream\DataStream.csproj", "{05995ED5-650A-4955-9DFC-3242B737E77A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedStreams", "src\DataStreamSuite\SharedStreams\SharedStreams.csproj", "{A8B861DE-E098-4291-98EE-76AA0F5F5A7B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PubSub", "src\DataStreamSuite\PubSub\PubSub.csproj", "{EFA9F78D-083E-457A-BABB-9386C88C32C0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -70,6 +80,46 @@ Global {89D666E6-B500-498F-B026-4D167F68ADFC}.Release|x64.Build.0 = Release|x64 {89D666E6-B500-498F-B026-4D167F68ADFC}.RelWithDebInfo|x64.ActiveCfg = Debug|x64 {89D666E6-B500-498F-B026-4D167F68ADFC}.RelWithDebInfo|x64.Build.0 = Debug|x64 + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.Debug|x64.ActiveCfg = Debug|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.Debug|x64.Build.0 = Debug|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.Release|x64.ActiveCfg = Release|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.Release|x64.Build.0 = Release|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {CCDCB00A-CABF-4D82-8A5F-33EEC8ACE51D}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.Debug|x64.ActiveCfg = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.Debug|x64.Build.0 = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.Release|x64.ActiveCfg = Release|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.Release|x64.Build.0 = Release|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {CB62D5D2-3495-4488-BFBC-7C7A3725A1EE}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.Debug|x64.ActiveCfg = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.Debug|x64.Build.0 = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.Release|x64.ActiveCfg = Release|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.Release|x64.Build.0 = Release|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {05995ED5-650A-4955-9DFC-3242B737E77A}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.Debug|x64.ActiveCfg = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.Debug|x64.Build.0 = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.Release|x64.ActiveCfg = Release|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.Release|x64.Build.0 = Release|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {A8B861DE-E098-4291-98EE-76AA0F5F5A7B}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.Debug|x64.ActiveCfg = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.Debug|x64.Build.0 = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.MinSizeRel|x64.Build.0 = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.Release|x64.ActiveCfg = Release|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.Release|x64.Build.0 = Release|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.RelWithDebInfo|x64.ActiveCfg = Debug|Any CPU + {EFA9F78D-083E-457A-BABB-9386C88C32C0}.RelWithDebInfo|x64.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/client/spark_api_server/appsettings.json b/src/client/spark_api_server/appsettings.json index d4b713c25..985d4ee34 100644 --- a/src/client/spark_api_server/appsettings.json +++ b/src/client/spark_api_server/appsettings.json @@ -25,5 +25,62 @@ "User": "bts", "Password": "" } + }, + "MasterWorker": { + "Interval": "00:00:00.1" + }, + "PubSub": { + "HeartbeatInterval": 15000, + "MissingHeartbeatsBeforeGc": 50, + "DisposedConnectionCheckPeriod": 1000, + "ServerPort": 22019, + "Routing": { + "PublisherRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://pubsub:22019", + "TagRegExp": ".*" + } + ], + "SubscriberRoutes": [ + { + "Name": "default", + "ServerAddress": "bts-cloud://pubsub:22019", + "TagRegExp": ".*" + } + ] + }, + "TagTypeToAssemblyMap": { + "Entries": [ + { + "HandlerClass": "BCG.PubSubServerTagHandler", + "Assembly": "DataStreamCore.dll" + } + ] + }, + "TagHandlerFactory": { + "Handlers": [ + { + "TagType": "DataStream", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerStreamTagHandler" + }, + { + "TagType": "System_Sub", + "Assembly": "PubSub", + "Class": "BCG.SystemSubscriptionMapPublisher" + }, + { + "TagType": "System_Stats", + "Assembly": "PubSub", + "Class": "BCG.SystemStatsPublisher" + }, + { + "TagType": ".*", + "Assembly": "DataStreamCore", + "Class": "bts.PubsubServerGenericDataHandler" + } + ] + } } } diff --git a/src/client/spark_websocket/Core/WebsocketSubscriptionHandler.cs b/src/client/spark_websocket/Core/WebsocketSubscriptionHandler.cs index df16fee38..c80cbcaf6 100644 --- a/src/client/spark_websocket/Core/WebsocketSubscriptionHandler.cs +++ b/src/client/spark_websocket/Core/WebsocketSubscriptionHandler.cs @@ -1,14 +1,21 @@ namespace bts.websocket_core { + using BCG; + using bts.api; using bts.api.orderman; using bts.md; + using Bts.DataStream; + using Bts.DataStreamCore; + using Bts.DataStreamExpressions; + using mbts.spark; using Newtonsoft.Json.Linq; using Spark.Api.MarketData; using Spark.Api.Orders; + using Spark.Core.Collections; using System; using System.Diagnostics.CodeAnalysis; @@ -22,6 +29,7 @@ namespace bts.websocket_core { PRICE_STATS, PRICE_TICKERS, FILLS, + DATA_STREAM, UNKNOWN = 0 } public bool Valid { get; set; } = true; @@ -91,6 +99,47 @@ namespace bts.websocket_core { hdlr.SendMessage(WebsocketJsonParsers.to_json(upd))); }; } + + private static Action GetDataStreamSubscriber(ViewStream vs, string subId) { + return Subscribe; + + void Subscribe(WebsocketSubscriptionHandler hdlr) { + // Declare dictionaries here to reduce allocations in the callback. Use a lock + // to be safe (callbacks are likely single-threaded, this is just insurance). + var syncRoot = new object(); + var removedRows = new Dictionary(); + var changedRows = new Dictionary(); + var addedRows = new Dictionary(); + + hdlr.dis = vs.AsObservable().Subscribe(OnData); + + void OnData(DataPacket dp) { + lock (syncRoot) { + removedRows.Clear(); + changedRows.Clear(); + addedRows.Clear(); + foreach (IBlueRow r in dp.deletedRows) { + var id = r.Id(); + removedRows[id] = r; + } + foreach (IBlueRow r in dp.addedRows) { + var id = r.Id(); + if (removedRows.Remove(id)) { + changedRows.Add(id, r); + } else { + addedRows.Add(id, r); + } + } + hdlr.SendMessage(new { + added = addedRows.Values, + changed = changedRows.Values, + removed = removedRows.Values, + }); + } + } + } + } + private static Action GetFillsSubscriber(FillsSubscriptionFilter cri) { return (WebsocketSubscriptionHandler hdlr) => { hdlr.dis = hdlr._OrdersClient.fills(cri).Subscribe( @@ -149,6 +198,35 @@ namespace bts.websocket_core { return true; } + static bool TryGetDataStream(WebsocketRequest req, [NotNullWhen(true)] out ViewStream? stream, [NotNullWhen(false)] out string error) { + stream = null; + error = ""; + var query = (req.parameters?["query"] as JValue)?.Value(); + if (query is null) { + error = "'params.query' field not found (expected a string)"; + return false; + } + var exp = DataStreamExpressionsInterpreter.Evaluate(query); + if (exp is null) { + error = "'params.query' expression has invalid syntax"; + return false; + } + Exception? exception = null; + try { + stream = exp.getView(new Dictionary(0)); + } catch (Exception ex) { + exception = ex; + } + if (stream is null) { + error = "Failed to create ViewStream from 'params.query' expression (check for typos in arguments/typenames)"; + if (exception is not null) { + error += ". " + exception.Message; + } + return false; + } + return true; + } + static bool TryGetFilter(WebsocketRequest req, [NotNullWhen(true)] out SecurityCriteria? cri, out string message) { cri = null; if (req.parameters?["filter"] is not JObject filter) { @@ -247,6 +325,14 @@ namespace bts.websocket_core { sub = GetOrdersSubscriber(filter); break; } + case SUBSCRIPTION_TYPE.DATA_STREAM: { + // TODO: make this nullable + if (!TryGetDataStream(req, out var filter, out status)) { + return false; + } + sub = GetDataStreamSubscriber(filter, id.Value.Val); + break; + } default: { status = "Incorrect subscription type"; return false; diff --git a/src/client/spark_websocket/spark_websocket.csproj b/src/client/spark_websocket/spark_websocket.csproj index 1d0e23046..c1a7dadaa 100644 --- a/src/client/spark_websocket/spark_websocket.csproj +++ b/src/client/spark_websocket/spark_websocket.csproj @@ -26,6 +26,8 @@ + +