refactor: Hide AdminClient behind interface
This commit is contained in:
1 parent
02356cf97c
commit
7fae875ee5
8 files changed
+156
-66
No files matched your search
@@ -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<TableUpdate<BTSUserImmutable, uint>> rawUsers;
|
||||
readonly IObservable<TableUpdate<BTSAccountImmutable, uint>> rawAccounts;
|
||||
|
||||
IDisposable? _dispose;
|
||||
|
||||
public AdminClient(AdminService service, UserTuple user) {
|
||||
this.user = user;
|
||||
public AdminClient(AdminService service, IOptions<SparkApiCredentials> 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<UserTuple, uint>.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<uint, UserTuple> get_user_map(Update<AdminDataImmutable, int> up) =>
|
||||
up.all.FirstOrNull()
|
||||
Dictionary<uint, UserTuple> get_user_map(TableUpdate<AdminDataImmutable, int> up) =>
|
||||
up.state.Values.FirstOrNull()
|
||||
?.Users
|
||||
?.ToDictionary(u => u.Id, u => new UserTuple(u.Id, u.Name))
|
||||
?? new(0);
|
||||
|
||||
Dictionary<uint, AccountTuple> get_account_map(Update<AdminDataImmutable, int> update) {
|
||||
if (!update.all.TryGetFirst(out var admin_data)) {
|
||||
Dictionary<uint, AccountTuple> get_account_map(TableUpdate<AdminDataImmutable, int> 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<TableUpdate<UserTuple, uint>> users { get; }
|
||||
public IObservable<TableUpdate<AccountTuple, uint>> accounts { get; }
|
||||
|
||||
public IObservable<Update<T, TKey>> permission_filter<T, TKey>(
|
||||
IObservable<Update<T, TKey>> positions, Func<T, uint> get_account_id) where TKey : IEquatable<TKey> =>
|
||||
user.permissions == UserTuple.Permissions.Admin ?
|
||||
positions :
|
||||
positions.Filter(accounts.Select(
|
||||
t => new Func<T, bool>(p => t.state.ContainsKey(get_account_id(p)))));
|
||||
|
||||
public IObservable<Update<T, TKey>> permission_filter<T, TKey>(
|
||||
IObservable<Update<T, TKey>> positions, Func<T, string> get_account_name) where TKey : IEquatable<TKey> =>
|
||||
user.permissions == UserTuple.Permissions.Admin ?
|
||||
positions :
|
||||
positions.Filter(accounts.Select(t => {
|
||||
var hash = t.state.Values.Select(a => a.name).ToHashSet();
|
||||
return new Func<T, bool>(v => hash.Contains(get_account_name(v)));
|
||||
}));
|
||||
IObservable<TableUpdate<BTSUserImmutable, uint>> IAdminClient.Users => rawUsers;
|
||||
IObservable<TableUpdate<BTSAccountImmutable, uint>> IAdminClient.Accounts => rawAccounts;
|
||||
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
|
||||
static IObservable<TableUpdate<T, TKey>> getAdminBasedDataTables<T, TKey>(IObservable<TableUpdate<AdminDataImmutable, int>> adminDataUpdates, Func<AdminDataImmutable, IEnumerable<T>> getItems, Func<T, TKey> keySelector) where TKey : notnull, IEquatable<TKey> {
|
||||
return Observable.Create<TableUpdate<T, TKey>>(Subscribe);
|
||||
|
||||
IDisposable Subscribe(IObserver<TableUpdate<T, TKey>> observer) {
|
||||
var lastOut = TableUpdate<T, TKey>.mk_empty(keySelector);
|
||||
var sub = adminDataUpdates.Subscribe(OnAdminData);
|
||||
return sub;
|
||||
|
||||
void OnAdminData(TableUpdate<AdminDataImmutable, int> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<TableUpdate<BTSUserImmutable, uint>> Users =>
|
||||
Observable.Return(TableUpdate<BTSUserImmutable, uint>.mk_empty(u => u.Id));
|
||||
|
||||
public IObservable<TableUpdate<BTSAccountImmutable, uint>> Accounts =>
|
||||
Observable.Return(TableUpdate<BTSAccountImmutable, uint>.mk_empty(a => a.Id));
|
||||
}
|
||||
@@ -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<TableUpdate<BTSUserImmutable, uint>> Users { get; }
|
||||
IObservable<TableUpdate<BTSAccountImmutable, uint>> Accounts { get; }
|
||||
}
|
||||
|
||||
public static class AdminClientExtensions {
|
||||
public static IObservable<Update<T, TKey>> permission_filter<T, TKey>(
|
||||
this IAdminClient adminClient,
|
||||
IObservable<Update<T, TKey>> positions,
|
||||
Func<T, uint> get_account_id) where TKey : IEquatable<TKey> =>
|
||||
positions.Filter(adminClient.Accounts.Select(
|
||||
t => new Func<T, bool>(p => t.state.ContainsKey(get_account_id(p)))));
|
||||
|
||||
public static IObservable<Update<T, TKey>> permission_filter<T, TKey>(
|
||||
this IAdminClient adminClient,
|
||||
IObservable<Update<T, TKey>> positions, Func<T, string> get_account_name) where TKey : IEquatable<TKey> =>
|
||||
positions.Filter(adminClient.Accounts.Select(t => {
|
||||
var hash = t.state.Values.Select(a => a.Name).ToHashSet();
|
||||
return new Func<T, bool>(v => hash.Contains(get_account_name(v)));
|
||||
}));
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -43,7 +43,8 @@ public static class SparkApiServices {
|
||||
.AddSingleton<Api.IServiceProvider, CachedServiceProvider>(sp => new CachedServiceProvider(sp.GetRequiredService<ServiceContext>()));
|
||||
var credentials = configurationSection.GetSection("Credentials");
|
||||
if (credentials.GetChildren().Any()) {
|
||||
services.AddSparkLogin(credentials);
|
||||
services.AddSparkLogin(credentials)
|
||||
.AddSingleton<IUserDataPermissions, UserDataPermissions>();
|
||||
}
|
||||
if (configurationSection.GetValue("EnableOrders", true)) {
|
||||
services.AddSparkdService<MarketDataRelay>();
|
||||
@@ -64,7 +65,9 @@ public static class SparkApiServices {
|
||||
}
|
||||
if (configurationSection.GetValue("EnableAdmin", true)) {
|
||||
services.AddSparkdService<AdminService>();
|
||||
services.AddSparkdService<AdminClient>();
|
||||
services.AddSingleton<IAdminClient, AdminClient>();
|
||||
} else {
|
||||
services.AddSingleton<IAdminClient, EmptyAdminClient>();
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
public class WebsocketDataSync : IWebsocketDataSync {
|
||||
public sealed class WebsocketDataSync : IWebsocketDataSync, IDisposable {
|
||||
readonly IOrdersClient ordersClient;
|
||||
readonly IAdminClient adminClient;
|
||||
readonly IDataStreamFactory streamFactory;
|
||||
TableUpdate<BTSUserImmutable, uint> users = TableUpdate<BTSUserImmutable, uint>.mk_empty(u => u.Id);
|
||||
TableUpdate<BTSAccountImmutable, uint> accounts = TableUpdate<BTSAccountImmutable, uint>.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);
|
||||
|
||||
/// <summary>
|
||||
/// class to manage thread safe client store, will change this to blocking collection
|
||||
/// </summary>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<string, DynamicOrderDescriptor> 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<string, BTSUserImmutable?> getUser,
|
||||
Func<string, BTSAccountImmutable?> getAccount,
|
||||
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors,
|
||||
Func<Venue, string, uint?> getSecurityId
|
||||
) {
|
||||
@@ -386,25 +388,20 @@ namespace bts.websocket_core {
|
||||
return (false, null, status);
|
||||
}
|
||||
|
||||
Result<AdminData> 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<PrecomputedTheoPoint>? theos, out string message, Func<Venue, string?, uint?> 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<string, BTSUserImmutable?> getUser,
|
||||
Func<string, BTSAccountImmutable?> getAccount,
|
||||
IReadOnlyDictionary<string, DynamicOrderDescriptor> Dynamic_Descriptors,
|
||||
Func<Venue, string, uint?> 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 ||
|
||||
|
||||
@@ -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<SparkApiWebSocketServer> logger;
|
||||
|
||||
public SparkApiWebSocketServer(
|
||||
Api.IServiceProvider serviceProvider,
|
||||
IMarketDataClient mdClient,
|
||||
IOrdersClient ordersClient,
|
||||
IAdminClient adminClient,
|
||||
IEnumerable<IDataStreamFactory> streamFactories,
|
||||
ILogger<SparkApiWebSocketServer> 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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user