diff --git a/src/client/spark_api/Admin/AdminClient.cs b/src/client/spark_api/Admin/AdminClient.cs index f335059cd..a5e6dd9be 100644 --- a/src/client/spark_api/Admin/AdminClient.cs +++ b/src/client/spark_api/Admin/AdminClient.cs @@ -3,8 +3,10 @@ namespace Spark.Api.Admin { using bts; using bts.api.admin; using bts.api.core; - using bts.utils; + using Microsoft.Extensions.Options; + + using Spark.Api.Services; using Spark.Core.Disposables; using Spark.Core.Reactive; @@ -13,33 +15,42 @@ namespace Spark.Api.Admin { using System.Linq; using System.Reactive.Linq; - public sealed class AdminClient : IDisposable { - public readonly UserTuple user; + public sealed class AdminClient : IAdminClient, IDisposable { + readonly IObservable> rawUsers; + readonly IObservable> rawAccounts; IDisposable? _dispose; - public AdminClient(AdminService service, UserTuple user) { - this.user = user; + public AdminClient(AdminService service, IOptions credentials) + : this(service, credentials.Value) { } + + public AdminClient(AdminService service, SparkApiCredentials credentials) + : this(service, credentials.User ?? string.Empty) { } + + public AdminClient(AdminService service, UserTuple user) + : this(service, user.name) { } + + public AdminClient(AdminService service, string userName) { + // this.user = user; var bootstraps = new DisposableBuilder(); + var adminDataTables = bootstraps.emplace(service.subscribei_admin_data().Table().Cache().ToObservable()); + rawAccounts = bootstraps.emplace(getAdminBasedDataTables(adminDataTables, a => a.Accounts, a => a.Id).Cache().ToObservable()); + rawUsers = bootstraps.emplace(getAdminBasedDataTables(adminDataTables, a => a.Users, u => u.Id).Cache().ToObservable()); + // accounts accounts = bootstraps.emplace( - service.subscribei_admin_data() + adminDataTables .Select(get_account_map) .Table(a => a.id) .Cache() .ToObservable()); // users - users = user.permissions switch { - UserTuple.Permissions.Admin => bootstraps.emplace( - service.subscribei_admin_data() - .Select(get_user_map) - .Table(UserTuple.key_fn) - .Cache() - .ToObservable()), - _ => Observable.Return( - TableUpdate.mk_empty(u => u.id).with_added(new[] { user })) - }; + var userTables = adminDataTables.Select(get_user_map).Table(UserTuple.key_fn); + if (userName != UserTuple.admin.name) { + userTables = userTables.Filter(u => u.name == userName); + } + users = bootstraps.emplace(userTables.Cache().ToObservable()); _dispose = bootstraps; static BTSUserImmutable? find_user(string name, AdminDataImmutable admin_data) => @@ -48,17 +59,17 @@ namespace Spark.Api.Admin { rv.UserData.HasAccountPermissions ? rv : null; - Dictionary get_user_map(Update up) => - up.all.FirstOrNull() + Dictionary get_user_map(TableUpdate up) => + up.state.Values.FirstOrNull() ?.Users ?.ToDictionary(u => u.Id, u => new UserTuple(u.Id, u.Name)) ?? new(0); - Dictionary get_account_map(Update update) { - if (!update.all.TryGetFirst(out var admin_data)) { + Dictionary get_account_map(TableUpdate update) { + if (!update.state.Values.TryGetFirst(out var admin_data)) { return new(0); } - var user_in = find_user(user.name, admin_data); + var user_in = find_user(userName, admin_data); if (!user_in.HasValue) { return new(0); } @@ -77,23 +88,36 @@ namespace Spark.Api.Admin { public IObservable> users { get; } public IObservable> accounts { get; } - public IObservable> permission_filter( - IObservable> positions, Func get_account_id) where TKey : IEquatable => - user.permissions == UserTuple.Permissions.Admin ? - positions : - positions.Filter(accounts.Select( - t => new Func(p => t.state.ContainsKey(get_account_id(p))))); - - public IObservable> permission_filter( - IObservable> positions, Func get_account_name) where TKey : IEquatable => - user.permissions == UserTuple.Permissions.Admin ? - positions : - positions.Filter(accounts.Select(t => { - var hash = t.state.Values.Select(a => a.name).ToHashSet(); - return new Func(v => hash.Contains(get_account_name(v))); - })); + IObservable> IAdminClient.Users => rawUsers; + IObservable> IAdminClient.Accounts => rawAccounts; public void Dispose() => Disposable.dispose(ref _dispose); + + static IObservable> getAdminBasedDataTables(IObservable> adminDataUpdates, Func> getItems, Func keySelector) where TKey : notnull, IEquatable { + return Observable.Create>(Subscribe); + + IDisposable Subscribe(IObserver> observer) { + var lastOut = TableUpdate.mk_empty(keySelector); + var sub = adminDataUpdates.Subscribe(OnAdminData); + return sub; + + void OnAdminData(TableUpdate update) { + var bd = lastOut.to_builder(); + bd.clear(); + if (update.state.Values.TryGetFirst(out var data)) { + var items = getItems(data); + foreach (var item in items) { + bd.add_or_update(item); + } + } + var nextOut = bd.to_table(); + if (nextOut.has_changes()) { + observer.OnNext(nextOut); + lastOut = nextOut; + } + } + } + } } } diff --git a/src/client/spark_api/Admin/EmptyAdminClient.cs b/src/client/spark_api/Admin/EmptyAdminClient.cs new file mode 100644 index 000000000..b329ea42e --- /dev/null +++ b/src/client/spark_api/Admin/EmptyAdminClient.cs @@ -0,0 +1,15 @@ +namespace Spark.Api.Admin; + +using bts.api.admin; +using bts.api.core; + +using System; +using System.Reactive.Linq; + +public class EmptyAdminClient : IAdminClient { + public IObservable> Users => + Observable.Return(TableUpdate.mk_empty(u => u.Id)); + + public IObservable> Accounts => + Observable.Return(TableUpdate.mk_empty(a => a.Id)); +} diff --git a/src/client/spark_api/Admin/IAdminClient.cs b/src/client/spark_api/Admin/IAdminClient.cs new file mode 100644 index 000000000..7dc874240 --- /dev/null +++ b/src/client/spark_api/Admin/IAdminClient.cs @@ -0,0 +1,30 @@ +namespace Spark.Api.Admin; + +using bts.api.admin; +using bts.api.core; + +using System; +using System.Linq; +using System.Reactive.Linq; + +public interface IAdminClient { + IObservable> Users { get; } + IObservable> Accounts { get; } +} + +public static class AdminClientExtensions { + public static IObservable> permission_filter( + this IAdminClient adminClient, + IObservable> positions, + Func get_account_id) where TKey : IEquatable => + positions.Filter(adminClient.Accounts.Select( + t => new Func(p => t.state.ContainsKey(get_account_id(p))))); + + public static IObservable> permission_filter( + this IAdminClient adminClient, + IObservable> positions, Func get_account_name) where TKey : IEquatable => + positions.Filter(adminClient.Accounts.Select(t => { + var hash = t.state.Values.Select(a => a.Name).ToHashSet(); + return new Func(v => hash.Contains(get_account_name(v))); + })); +} diff --git a/src/client/spark_api/Orders/OrdersClient.cs b/src/client/spark_api/Orders/OrdersClient.cs index af2ffb430..9625f962b 100644 --- a/src/client/spark_api/Orders/OrdersClient.cs +++ b/src/client/spark_api/Orders/OrdersClient.cs @@ -1,7 +1,6 @@ #nullable enable namespace Spark.Api.Orders { using bts; - using bts.api; using bts.api.core; using bts.api.orderman; @@ -34,7 +33,7 @@ namespace Spark.Api.Orders { public OrdersClient( OrderService order_service, PositionService position_service, - AdminClient admin_client, + IAdminClient admin_client, ISecurityResolver sec_resolver, TimeSpan? interval = null ) { @@ -94,23 +93,23 @@ namespace Spark.Api.Orders { .ToObservable()); var manual_fill_tuples_account_join = manual_fills_securities.LeftJoin( - admin_client.accounts, + admin_client.Accounts, mf => mf.val.RecordId, - a => a.id, + a => a.Id, mf => mf.val.AccountId, - (mf, mf_id, a, a_id) => new ManualFillTuple(mf.val, mf.sec, a, null), + (mf, mf_id, a, a_id) => new ManualFillTuple(mf.val, mf.sec, new(a.Id, a.Name), null), (mf, mf_id, a_id) => new ManualFillTuple(mf.val, mf.sec, null, null), mf => mf.id); _manual_fills = bootstraps.emplace( manual_fill_tuples_account_join .LeftJoin( - admin_client.users, + admin_client.Users, mf => mf.id, - u => u.id, + u => u.Id, mf => mf.manual_fill.UserId, (mf, mf_id, u, u_id) => new ManualFillTuple( - mf.manual_fill, mf.security, mf.account, u), + mf.manual_fill, mf.security, mf.account, new(u.Id, u.Name)), (mf, mf_id, u_id) => mf, mf => mf.id) .Cache() diff --git a/src/client/spark_api/Services/SparkApiServices.cs b/src/client/spark_api/Services/SparkApiServices.cs index 9d9290d7a..d090c8727 100644 --- a/src/client/spark_api/Services/SparkApiServices.cs +++ b/src/client/spark_api/Services/SparkApiServices.cs @@ -43,7 +43,8 @@ public static class SparkApiServices { .AddSingleton(sp => new CachedServiceProvider(sp.GetRequiredService())); var credentials = configurationSection.GetSection("Credentials"); if (credentials.GetChildren().Any()) { - services.AddSparkLogin(credentials); + services.AddSparkLogin(credentials) + .AddSingleton(); } if (configurationSection.GetValue("EnableOrders", true)) { services.AddSparkdService(); @@ -64,7 +65,9 @@ public static class SparkApiServices { } if (configurationSection.GetValue("EnableAdmin", true)) { services.AddSparkdService(); - services.AddSparkdService(); + services.AddSingleton(); + } else { + services.AddSingleton(); } return services; } diff --git a/src/client/spark_websocket/Core/WebsocketDataSync.cs b/src/client/spark_websocket/Core/WebsocketDataSync.cs index a8ce0929f..bb40b8bd4 100644 --- a/src/client/spark_websocket/Core/WebsocketDataSync.cs +++ b/src/client/spark_websocket/Core/WebsocketDataSync.cs @@ -14,22 +14,33 @@ namespace bts.websocket_core { using bts.api.synthetic; using Bts.DataStreamCore; using System.Diagnostics.CodeAnalysis; + using Spark.Core.Disposables; + using bts.api.core; + using bts; /// /// This does all the bookkeeping for connections and data, and passes /// it over to the business layer CURRENT VERSION IS JUST TO SHOW HOW TO USE GATEWAY /// - public class WebsocketDataSync : IWebsocketDataSync { + public sealed class WebsocketDataSync : IWebsocketDataSync, IDisposable { readonly IOrdersClient ordersClient; + readonly IAdminClient adminClient; readonly IDataStreamFactory streamFactory; + TableUpdate users = TableUpdate.mk_empty(u => u.Id); + TableUpdate accounts = TableUpdate.mk_empty(a => a.Id); + IDisposable? adminSubs; - public WebsocketDataSync(IMarketDataClient mdClient, IOrdersClient ordersClient, IDataStreamFactory streamFactory) { + public WebsocketDataSync(IMarketDataClient mdClient, IOrdersClient ordersClient, IAdminClient adminClient, IDataStreamFactory streamFactory) { this.ordersClient = ordersClient; this.mdClient = mdClient; + this.adminClient = adminClient; this.streamFactory = streamFactory; } + public void Dispose() => + Disposable.dispose(ref adminSubs); + /// /// class to manage thread safe client store, will change this to blocking collection /// @@ -181,6 +192,9 @@ namespace bts.websocket_core { Dynamic_Descriptors = r .DynamicOrderDesc .ToDictionary(d => d.DynamicOrderType); + adminSubs = Disposable.Create( + adminClient.Users.Subscribe(users => this.users = users), + adminClient.Accounts.Subscribe(accounts => this.accounts = accounts)); } public void handle_connection(WebsocketClientID client_id, WebsocketIncomingMessage? optional_message) { @@ -266,7 +280,7 @@ namespace bts.websocket_core { public async void handle_delete_order(WebsocketRequest req) { await HandleRestRequest(req,WebsocketRestHandler.DeleteOrderAsync);} public async void handle_set_theos(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.SetTheosAsync(req, Orders_Client, getSecurityId));} public async void handle_enter_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.EnterOrderAsync( - req, Orders_Client, adminService, Dynamic_Descriptors, getSecurityId + req, Orders_Client, getUser, getAccount, Dynamic_Descriptors, getSecurityId ));} public async void handle_update_order(WebsocketRequest req) { await HandleRestRequest(req,(req, Orders_Client) => WebsocketRestHandler.UpdateOrderAsync(req,Orders_Client,Dynamic_Descriptors));} @@ -324,5 +338,11 @@ namespace bts.websocket_core { uint? getSecurityId(Venue venue, string? name) => _securities.Values.TryGetFirst(s => s.Exchange == venue && s.ToString() == name, out var s) ? s.Id : null; + + BTSUserImmutable? getUser(string userName) => + users.state.Values.TryGetFirst(u => u!.Name == userName, out var u) ? u : null; + + BTSAccountImmutable? getAccount(string accountName) => + accounts.state.Values.TryGetFirst(a => a!.Name == accountName, out var a) ? a : null; } } diff --git a/src/client/spark_websocket/Core/WebsocketRestHandler.cs b/src/client/spark_websocket/Core/WebsocketRestHandler.cs index 35b826f4b..beda11c20 100644 --- a/src/client/spark_websocket/Core/WebsocketRestHandler.cs +++ b/src/client/spark_websocket/Core/WebsocketRestHandler.cs @@ -17,6 +17,7 @@ namespace bts.websocket_core { using System.Diagnostics.CodeAnalysis; using bts.spark; using bts.utils; + using Spark.Api.Admin; public class WebsocketRestHandler { public static (bool,DynamicOrderParam?,string) TryGetDynamicParam(JObject custom,string key, DynamicOrderParamDescriptor descriptor, IReadOnlyDictionary Dynamic_Descriptors) { @@ -367,9 +368,10 @@ namespace bts.websocket_core { return (true, customdef, status); } - static async Task<(bool, OrderDefinition?, string)> TryGetOrderDefAsync( + static (bool, OrderDefinition?, string) TryGetOrderDef( WebsocketRequest req, - AdminService adminService, + Func getUser, + Func getAccount, IReadOnlyDictionary Dynamic_Descriptors, Func getSecurityId ) { @@ -386,25 +388,20 @@ namespace bts.websocket_core { return (false, null, status); } - Result adminResult = await adminService.get_admin_data_async(new VoidMessage()); uint user_id; uint account_id; - if (!adminResult.success || adminResult.value == null) { - return (false, null, adminResult.error_message); - } - AdminData admin = adminResult.value; string username = usr.Value.Val; string accountname = act.Value.Val; - BTSUser? user = admin.Users.FirstOrDefault(x => username != null ? x.Name.ToLowerInvariant() == username.ToLowerInvariant() : true); + var user = getUser(username); if (user == null) { return (false, null, "user not correct"); } - user_id = (uint)user.Id; - BTSAccount? account = admin.Accounts.FirstOrDefault(x => x.Name.ToLowerInvariant() == accountname.ToLowerInvariant()); + user_id = (uint)user.Value.Id; + var account = getAccount(accountname); if (account == null) { return (false, null, "account not correct"); } - account_id = (uint)account.Id; + account_id = (uint)account.Value.Id; var secId = getSecurityId(exchange.Value, exchange_securty_name.Value.Val); if (!secId.HasValue) { @@ -424,8 +421,8 @@ namespace bts.websocket_core { UserId = user_id, AccountId = account_id, CustomDef = customdef, - Account = account.Name, - User = user.Name + Account = account.Value.Name, + User = user.Value.Name }, status); } static bool TryGetPoints(WebsocketRequest req, [NotNullWhen(true)] out List? theos, out string message, Func getSecurityId) { @@ -492,7 +489,8 @@ namespace bts.websocket_core { public static async Task<(bool success, uint? orderId, string status)> EnterOrderAsync( WebsocketRequest request, IOrdersClient ordersClient, - AdminService adminService, + Func getUser, + Func getAccount, IReadOnlyDictionary Dynamic_Descriptors, Func getSecurityId ) { @@ -500,7 +498,7 @@ namespace bts.websocket_core { uint? orderId = null; string status = string.Empty; - (bool success, orderDefinition, status) = await TryGetOrderDefAsync(request, adminService, Dynamic_Descriptors, getSecurityId); + (bool success, orderDefinition, status) = TryGetOrderDef(request, getUser, getAccount, Dynamic_Descriptors, getSecurityId); if ( !success || orderDefinition == null || diff --git a/src/client/spark_websocket/Services/SparkApiWebSocketServer.cs b/src/client/spark_websocket/Services/SparkApiWebSocketServer.cs index 538f0b22a..ce5a4e81a 100644 --- a/src/client/spark_websocket/Services/SparkApiWebSocketServer.cs +++ b/src/client/spark_websocket/Services/SparkApiWebSocketServer.cs @@ -8,6 +8,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using Spark.Api.Admin; using Spark.Api.MarketData; using Spark.Api.Orders; @@ -20,14 +21,14 @@ public class SparkApiWebSocketServer : BackgroundService, ISparkApiWebSocketServ readonly ILogger logger; public SparkApiWebSocketServer( - Api.IServiceProvider serviceProvider, IMarketDataClient mdClient, IOrdersClient ordersClient, + IAdminClient adminClient, IEnumerable streamFactories, ILogger logger ) { this.logger = logger; - dataSync = new(serviceProvider, mdClient, ordersClient, DataStreamFactory.Create(streamFactories)); + dataSync = new(mdClient, ordersClient, adminClient, DataStreamFactory.Create(streamFactories)); gateway = new(); gateway.init(dataSync); }