From a5aeb2a3c1c0c78d4504d92b44f1143f482485e9 Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Sat, 30 Apr 2022 12:54:12 +0900 Subject: [PATCH] Fix performance issue on dashboard for big wallets (#3694) --- .../Components/StoreNumbers/Default.cshtml | 23 +++++++++++-------- .../Components/StoreNumbers/StoreNumbers.cs | 21 +++++++++-------- .../StoreNumbers/StoreNumbersViewModel.cs | 2 +- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/BTCPayServer/Components/StoreNumbers/Default.cshtml b/BTCPayServer/Components/StoreNumbers/Default.cshtml index 7fa94b197..ab03ab37b 100644 --- a/BTCPayServer/Components/StoreNumbers/Default.cshtml +++ b/BTCPayServer/Components/StoreNumbers/Default.cshtml @@ -8,16 +8,19 @@
@Model.PayoutsPending
-
-
-
TXs in the last @Model.TransactionDays days
- @if (Model.Transactions > 0) - { - View All - } -
-
@Model.Transactions
-
+ @if (Model.Transactions is not null) + { +
+
+
TXs in the last @Model.TransactionDays days
+ @if (Model.Transactions.Value > 0) + { + View All + } +
+
@Model.Transactions.Value
+
+ }
Refunds Issued
diff --git a/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs b/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs index 5229969b5..dde820a87 100644 --- a/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs +++ b/BTCPayServer/Components/StoreNumbers/StoreNumbers.cs @@ -1,10 +1,12 @@ using System; +using Dapper; using System.Globalization; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Client.Models; using BTCPayServer.Components.StoreRecentTransactions; using BTCPayServer.Data; +using BTCPayServer.Services; using BTCPayServer.Services.Stores; using BTCPayServer.Services.Wallets; using Microsoft.AspNetCore.Identity; @@ -23,16 +25,19 @@ public class StoreNumbers : ViewComponent private readonly StoreRepository _storeRepo; private readonly ApplicationDbContextFactory _dbContextFactory; private readonly BTCPayWalletProvider _walletProvider; + private readonly NBXplorerConnectionFactory _nbxConnectionFactory; private readonly BTCPayNetworkProvider _networkProvider; public StoreNumbers( StoreRepository storeRepo, ApplicationDbContextFactory dbContextFactory, BTCPayNetworkProvider networkProvider, - BTCPayWalletProvider walletProvider) + BTCPayWalletProvider walletProvider, + NBXplorerConnectionFactory nbxConnectionFactory) { _storeRepo = storeRepo; _walletProvider = walletProvider; + _nbxConnectionFactory = nbxConnectionFactory; _networkProvider = networkProvider; _dbContextFactory = dbContextFactory; CryptoCode = networkProvider.DefaultNetwork.CryptoCode; @@ -51,16 +56,14 @@ public class StoreNumbers : ViewComponent var walletId = new WalletId(store.Id, CryptoCode); var derivation = store.GetDerivationSchemeSettings(_networkProvider, walletId.CryptoCode); - var transactionsCount = 0; - if (derivation != null) + int? transactionsCount = null; + if (derivation != null && _nbxConnectionFactory.Available) { - var network = derivation.Network; - var wallet = _walletProvider.GetWallet(network); - var allTransactions = await wallet.FetchTransactions(derivation.AccountDerivation); + await using var conn = await _nbxConnectionFactory.OpenConnection(); + var wid = NBXplorer.Client.DBUtils.nbxv1_get_wallet_id(derivation.Network.CryptoCode, derivation.AccountDerivation.ToString()); var afterDate = DateTimeOffset.UtcNow - TimeSpan.FromDays(TransactionDays); - transactionsCount = allTransactions.UnconfirmedTransactions.Transactions - .Concat(allTransactions.ConfirmedTransactions.Transactions) - .Count(t => t.Timestamp > afterDate); + var count = await conn.ExecuteScalarAsync("SELECT COUNT(*) FROM wallets_history WHERE code=@code AND wallet_id=@wid AND seen_at > @afterDate", new { code = derivation.Network.CryptoCode, wid, afterDate }); + transactionsCount = (int)count; } var vm = new StoreNumbersViewModel diff --git a/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs b/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs index 7704ac828..5546d1d7a 100644 --- a/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs +++ b/BTCPayServer/Components/StoreNumbers/StoreNumbersViewModel.cs @@ -8,7 +8,7 @@ public class StoreNumbersViewModel public StoreData Store { get; set; } public WalletId WalletId { get; set; } public int PayoutsPending { get; set; } - public int Transactions { get; set; } + public int? Transactions { get; set; } public int RefundsIssued { get; set; } public int TransactionDays { get; set; } }