Fix performance issue on dashboard for big wallets (#3694)

This commit is contained in:
Nicolas Dorier 2022-04-30 12:54:12 +09:00 committed by GitHub
parent 410bd5ab9c
commit a5aeb2a3c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 20 deletions

View File

@ -8,16 +8,19 @@
</header>
<div class="h3">@Model.PayoutsPending</div>
</div>
@if (Model.Transactions is not null)
{
<div class="store-number">
<header>
<h6>TXs in the last @Model.TransactionDays days</h6>
@if (Model.Transactions > 0)
@if (Model.Transactions.Value > 0)
{
<a asp-controller="UIWallets" asp-action="WalletTransactions" asp-route-walletId="@Model.WalletId">View All</a>
}
</header>
<div class="h3">@Model.Transactions</div>
<div class="h3">@Model.Transactions.Value</div>
</div>
}
<div class="store-number">
<header>
<h6>Refunds Issued</h6>

View File

@ -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<long>("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

View File

@ -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; }
}