Dashboard: Paid invoices in the last 7 days (#5316)

Adjust the prior number of transactions metric as discussed with @pavlenex. We now show the number of paid invoices instead of transactions, as this metric is more meaningful.

Closes #5300.
This commit is contained in:
d11n 2023-09-13 02:02:02 +02:00 committed by GitHub
parent 2d38113c66
commit 4aedf76f1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 43 deletions

View file

@ -1,3 +1,4 @@
@using BTCPayServer.Client
@model BTCPayServer.Components.StoreNumbers.StoreNumbersViewModel
<div class="widget store-numbers" id="StoreNumbers-@Model.Store.Id">
@ -21,26 +22,23 @@
}
else
{
<div class="store-number">
<header>
<h6>Paid invoices in the last @Model.TimeframeDays days</h6>
@if (Model.PaidInvoices > 0)
{
<a asp-controller="UIInvoice" asp-action="ListInvoices" asp-route-storeId="@Model.Store.Id" permission="@Policies.CanViewInvoices">View All</a>
}
</header>
<div class="h3">@Model.PaidInvoices</div>
</div>
<div class="store-number">
<header>
<h6>Payouts Pending</h6>
<a asp-controller="UIStorePullPayments" asp-action="Payouts" asp-route-storeId="@Model.Store.Id">Manage</a>
<a asp-controller="UIStorePullPayments" asp-action="Payouts" asp-route-storeId="@Model.Store.Id" permission="@Policies.CanManagePullPayments">Manage</a>
</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.Value > 0)
{
<a asp-controller="UIWallets" asp-action="WalletTransactions" asp-route-walletId="@Model.WalletId">View All</a>
}
</header>
<div class="h3">@Model.Transactions.Value</div>
</div>
}
<div class="store-number">
<header>
<h6>Refunds Issued</h6>

View file

@ -6,6 +6,7 @@ using BTCPayServer.Client.Models;
using BTCPayServer.Components.StoreRecentTransactions;
using BTCPayServer.Data;
using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores;
using BTCPayServer.Services.Wallets;
using Dapper;
@ -21,22 +22,16 @@ public class StoreNumbers : ViewComponent
{
private readonly StoreRepository _storeRepo;
private readonly ApplicationDbContextFactory _dbContextFactory;
private readonly BTCPayWalletProvider _walletProvider;
private readonly NBXplorerConnectionFactory _nbxConnectionFactory;
private readonly BTCPayNetworkProvider _networkProvider;
private readonly InvoiceRepository _invoiceRepository;
public StoreNumbers(
StoreRepository storeRepo,
ApplicationDbContextFactory dbContextFactory,
BTCPayNetworkProvider networkProvider,
BTCPayWalletProvider walletProvider,
NBXplorerConnectionFactory nbxConnectionFactory)
InvoiceRepository invoiceRepository)
{
_storeRepo = storeRepo;
_walletProvider = walletProvider;
_nbxConnectionFactory = nbxConnectionFactory;
_networkProvider = networkProvider;
_dbContextFactory = dbContextFactory;
_invoiceRepository = invoiceRepository;
}
public async Task<IViewComponentResult> InvokeAsync(StoreNumbersViewModel vm)
@ -52,28 +47,17 @@ public class StoreNumbers : ViewComponent
return View(vm);
await using var ctx = _dbContextFactory.CreateContext();
var payoutsCount = await ctx.Payouts
var offset = DateTimeOffset.Now.AddDays(-vm.TimeframeDays).ToUniversalTime();
vm.PaidInvoices = await _invoiceRepository.GetInvoiceCount(
new InvoiceQuery { StoreId = new [] { vm.Store.Id }, StartDate = offset, Status = new [] { "paid", "confirmed" } });
vm.PayoutsPending = await ctx.Payouts
.Where(p => p.PullPaymentData.StoreId == vm.Store.Id && !p.PullPaymentData.Archived && p.State == PayoutState.AwaitingApproval)
.CountAsync();
var refundsCount = await ctx.Invoices
.Where(i => i.StoreData.Id == vm.Store.Id && !i.Archived && i.CurrentRefundId != null)
vm.RefundsIssued = await ctx.Invoices
.Where(i => i.StoreData.Id == vm.Store.Id && !i.Archived && i.CurrentRefundId != null && i.Created >= offset)
.CountAsync();
var derivation = vm.Store.GetDerivationSchemeSettings(_networkProvider, vm.CryptoCode);
int? transactionsCount = null;
if (derivation != null && _nbxConnectionFactory.Available)
{
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(vm.TransactionDays);
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;
}
vm.PayoutsPending = payoutsCount;
vm.Transactions = transactionsCount;
vm.RefundsIssued = refundsCount;
return View(vm);
}
}

View file

@ -7,9 +7,9 @@ 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 TimeframeDays { get; set; } = 7;
public int? PaidInvoices { get; set; }
public int RefundsIssued { get; set; }
public int TransactionDays { get; set; } = 7;
public bool InitialRendering { get; set; }
public string CryptoCode { get; set; }
}

View file

@ -738,6 +738,12 @@ namespace BTCPayServer.Services.Invoices
var data = await query.ToArrayAsync(cancellationToken).ConfigureAwait(false);
return data.Select(ToEntity).ToArray();
}
public async Task<int> GetInvoiceCount(InvoiceQuery queryObject)
{
await using var context = _applicationDbContextFactory.CreateContext();
return await GetInvoiceQuery(context, queryObject).CountAsync();
}
private string NormalizeExceptionStatus(string status)
{