perf: optimize authorization scoreline and workers

This commit is contained in:
2026-07-30 09:12:27 +08:00
parent 4bea745b79
commit bedc77bffd
52 changed files with 21911 additions and 347 deletions

View File

@@ -26,35 +26,44 @@ internal sealed class PlatformAdminService(
await AssertPlatformPermissionAsync(actor, BackendPermissions.PlatformDashboardView, cancellationToken);
return await ExecuteSystemAsync("platform overview", async dbContext =>
{
var tenantCount = await dbContext.Tenants.CountAsync(tenant => tenant.Mode != TenantMode.PlatformOwned, cancellationToken);
var activeTenantCount = await dbContext.Tenants.CountAsync(tenant => tenant.Mode != TenantMode.PlatformOwned && tenant.Status == TenantStatus.Active, cancellationToken);
var suspendedTenantCount = await dbContext.Tenants.CountAsync(tenant => tenant.Mode != TenantMode.PlatformOwned && tenant.Status == TenantStatus.Suspended, cancellationToken);
var orderCount = await dbContext.Orders.CountAsync(cancellationToken);
var paidOrderCount = await dbContext.Orders.CountAsync(order => order.Status == OrderStatus.Paid, cancellationToken);
var revenueCents = await dbContext.Orders
.Where(order => order.Status == OrderStatus.Paid || order.Status == OrderStatus.PartiallyRefunded)
.SumAsync(order => order.AmountCents - order.RefundedAmountCents, cancellationToken);
var questionBankCount = await dbContext.QuestionBanks.CountAsync(cancellationToken);
var questionCount = await dbContext.Questions.CountAsync(question => question.Status == QuestionStatus.Published, cancellationToken);
var learningActiveUserCount = await dbContext.PracticeSessions
.Where(session => session.StartedAt >= DateTimeOffset.UtcNow.AddDays(-7))
.Select(session => session.UserId)
.Distinct()
.CountAsync(cancellationToken);
var row = await dbContext.Database.SqlQuery<PlatformOverviewRow>($"""
SELECT
(SELECT count(*)::integer FROM tenants WHERE mode <> 'platform_owned') AS "TenantCount",
(SELECT count(*)::integer FROM tenants WHERE mode <> 'platform_owned' AND status = 'active') AS "ActiveTenantCount",
(SELECT count(*)::integer FROM tenants WHERE mode <> 'platform_owned' AND status = 'suspended') AS "SuspendedTenantCount",
(SELECT count(*)::integer FROM orders) AS "OrderCount",
(SELECT count(*)::integer FROM orders WHERE status = 'paid') AS "PaidOrderCount",
(SELECT COALESCE(sum(amount_cents - refunded_amount_cents), 0)::integer FROM orders WHERE status IN ('paid', 'partially_refunded')) AS "RevenueCents",
(SELECT count(*)::integer FROM question_banks) AS "QuestionBankCount",
(SELECT count(*)::integer FROM questions WHERE status = 'published') AS "QuestionCount",
(SELECT count(DISTINCT user_id)::integer FROM practice_sessions WHERE started_at >= {DateTimeOffset.UtcNow.AddDays(-7)}) AS "LearningActiveUserCount"
""").SingleAsync(cancellationToken);
return new PlatformOverview(
tenantCount,
activeTenantCount,
suspendedTenantCount,
orderCount,
paidOrderCount,
revenueCents,
questionBankCount,
questionCount,
learningActiveUserCount);
row.TenantCount,
row.ActiveTenantCount,
row.SuspendedTenantCount,
row.OrderCount,
row.PaidOrderCount,
row.RevenueCents,
row.QuestionBankCount,
row.QuestionCount,
row.LearningActiveUserCount);
}, cancellationToken);
}
private sealed class PlatformOverviewRow
{
public int TenantCount { get; init; }
public int ActiveTenantCount { get; init; }
public int SuspendedTenantCount { get; init; }
public int OrderCount { get; init; }
public int PaidOrderCount { get; init; }
public int RevenueCents { get; init; }
public int QuestionBankCount { get; init; }
public int QuestionCount { get; init; }
public int LearningActiveUserCount { get; init; }
}
public async Task<PlatformTenantList> GetTenantsAsync(
PlatformAdminActor actor,
PlatformAdminQuery query,