feat(admin): plan statistics + node restart/close-ae actions
Build backend images / build content-svc (push) Failing after 1m22s
Build backend images / build file-svc (push) Failing after 3m8s
Build backend images / build gateway (push) Failing after 53s
Build backend images / build identity-svc (push) Failing after 57s
Build backend images / build notification-svc (push) Failing after 1m25s
Build backend images / build render-svc (push) Failing after 2m5s
Build backend images / build studio-svc (push) Failing after 3m59s

Final legacy-admin items:
- identity GET /v1/admin/plan-statistics (active/total users + revenue per plan
  from user_plans); surfaced as a breakdown table in /admin/stats
- NodesTable: wire Restart + Close-AE actions (backend already supported them) via
  new proxy routes; was only drain/release before

Full DivineGateWeb legacy-admin parity achieved.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-06-02 23:02:03 +03:30
parent 3091911260
commit 151970accd
9 changed files with 89 additions and 3 deletions
+3 -1
View File
@@ -398,7 +398,9 @@
"colTags": "Tags", "colTags": "Tags",
"colActions": "Actions", "colActions": "Actions",
"actionDrain": "Drain", "actionDrain": "Drain",
"actionRelease": "Release" "actionRelease": "Release",
"actionRestart": "Restart",
"actionCloseAe": "Close AE"
}, },
"componentsAdminRenderQueueTable": { "componentsAdminRenderQueueTable": {
"emptyState": "No render jobs found for the selected filter.", "emptyState": "No render jobs found for the selected filter.",
+3 -1
View File
@@ -398,7 +398,9 @@
"colTags": "برچسب‌ها", "colTags": "برچسب‌ها",
"colActions": "عملیات", "colActions": "عملیات",
"actionDrain": "تخلیه", "actionDrain": "تخلیه",
"actionRelease": "آزادسازی" "actionRelease": "آزادسازی",
"actionRestart": "ری‌استارت",
"actionCloseAe": "بستن AE"
}, },
"componentsAdminRenderQueueTable": { "componentsAdminRenderQueueTable": {
"emptyState": "هیچ کار رندری برای فیلتر انتخاب‌شده یافت نشد.", "emptyState": "هیچ کار رندری برای فیلتر انتخاب‌شده یافت نشد.",
@@ -57,6 +57,23 @@ public class AdminService(IdentityDbContext db)
pays.Sum(p => p.AmountMinor), payingAllTime, daily); pays.Sum(p => p.AmountMinor), payingAllTime, daily);
} }
// ── Plan statistics ──────────────────────────────────────────────────────
public async Task<List<PlanStatRow>> GetPlanStatisticsAsync(Guid tenantId)
{
var now = DateTime.UtcNow;
return await db.UserPlans
.Where(p => p.TenantId == tenantId)
.GroupBy(p => p.PlanName)
.Select(g => new PlanStatRow(
g.Key,
g.Count(),
g.Count(x => x.ExpiresAt > now && x.CancelledAt == null),
g.Sum(x => x.PriceMinorPaid)))
.OrderByDescending(r => r.RevenueMinor)
.ToListAsync();
}
// ── CRM notes / tags ──────────────────────────────────────────────────── // ── CRM notes / tags ────────────────────────────────────────────────────
public async Task<UserCrmResponse> GetUserCrmAsync(Guid userId) public async Task<UserCrmResponse> GetUserCrmAsync(Guid userId)
@@ -22,6 +22,10 @@ public class AdminController(AdminService svc) : ControllerBase
return Ok(await svc.GetCrmAnalyticsAsync(TenantId, s, e)); return Ok(await svc.GetCrmAnalyticsAsync(TenantId, s, e));
} }
// ── Plan statistics ──────────────────────────────────────────────────────
[HttpGet("v1/admin/plan-statistics")]
public async Task<IActionResult> PlanStats() => Ok(await svc.GetPlanStatisticsAsync(TenantId));
// ── CRM notes / tags ─────────────────────────────────────────────────────── // ── CRM notes / tags ───────────────────────────────────────────────────────
[HttpGet("v1/users/{userId:guid}/crm")] [HttpGet("v1/users/{userId:guid}/crm")]
public async Task<IActionResult> GetCrm(Guid userId) => Ok(await svc.GetUserCrmAsync(userId)); public async Task<IActionResult> GetCrm(Guid userId) => Ok(await svc.GetUserCrmAsync(userId));
@@ -14,6 +14,10 @@ public record CrmAnalyticsResponse(
List<CrmDailyPoint> Daily List<CrmDailyPoint> Daily
); );
// ── Plan statistics breakdown ────────────────────────────────────────────────
public record PlanStatRow(string PlanName, int Total, int Active, long RevenueMinor);
// ── CRM notes / tags per customer ──────────────────────────────────────────── // ── CRM notes / tags per customer ────────────────────────────────────────────
public record UserCrmResponse(string[] Tags, string? Note, string Status); public record UserCrmResponse(string[] Tags, string? Note, string Status);
@@ -0,0 +1,9 @@
import { type NextRequest } from "next/server";
import { adminProxy } from "@/app/api/admin/_adminProxy";
export const runtime = "nodejs";
interface Ctx { params: { nodeId: string } }
export async function POST(req: NextRequest, { params }: Ctx) {
return adminProxy(req, `/v1/nodes/${params.nodeId}/close-ae`);
}
@@ -0,0 +1,9 @@
import { type NextRequest } from "next/server";
import { adminProxy } from "@/app/api/admin/_adminProxy";
export const runtime = "nodejs";
interface Ctx { params: { nodeId: string } }
export async function POST(req: NextRequest, { params }: Ctx) {
return adminProxy(req, `/v1/nodes/${params.nodeId}/restart`);
}
+14
View File
@@ -107,6 +107,20 @@ export function NodesTable({ nodes }: { nodes: V2Node[] }) {
> >
{t("actionDrain")} {t("actionDrain")}
</button> </button>
<button
onClick={() => action(node.id, "restart")}
disabled={loading[node.id]}
className="rounded px-2.5 py-1 text-xs text-blue-300 border border-blue-500/30 hover:bg-blue-500/10 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
>
{t("actionRestart")}
</button>
<button
onClick={() => action(node.id, "close-ae")}
disabled={loading[node.id]}
className="rounded px-2.5 py-1 text-xs text-orange-300 border border-orange-500/30 hover:bg-orange-500/10 disabled:opacity-40 disabled:cursor-not-allowed transition-colors"
>
{t("actionCloseAe")}
</button>
<button <button
onClick={() => action(node.id, "release")} onClick={() => action(node.id, "release")}
disabled={loading[node.id]} disabled={loading[node.id]}
+26 -1
View File
@@ -3,6 +3,7 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
interface Crm { total_signups: number; buyers: number; conversion_rate: number; revenue_minor: number; paying_users_all_time: number } interface Crm { total_signups: number; buyers: number; conversion_rate: number; revenue_minor: number; paying_users_all_time: number }
interface PlanStat { plan_name: string; total: number; active: number; revenue_minor: number }
const card = "rounded-xl border border-[#1e2235] bg-[#0f1120] p-5"; const card = "rounded-xl border border-[#1e2235] bg-[#0f1120] p-5";
function toman(minor: number) { return (minor / 10).toLocaleString("fa-IR"); } function toman(minor: number) { return (minor / 10).toLocaleString("fa-IR"); }
@@ -19,14 +20,16 @@ async function total(path: string): Promise<number> {
export function StatsAdmin() { export function StatsAdmin() {
const [crm, setCrm] = useState<Crm | null>(null); const [crm, setCrm] = useState<Crm | null>(null);
const [counts, setCounts] = useState<Record<string, number>>({}); const [counts, setCounts] = useState<Record<string, number>>({});
const [plans, setPlans] = useState<PlanStat[]>([]);
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const since = new Date(Date.now() - 365 * 864e5).toISOString().slice(0, 10); const since = new Date(Date.now() - 365 * 864e5).toISOString().slice(0, 10);
const to = new Date().toISOString().slice(0, 10); const to = new Date().toISOString().slice(0, 10);
const [c, users, templates, categories, campaigns, blogs] = await Promise.all([ const [c, ps, users, templates, categories, campaigns, blogs] = await Promise.all([
fetch(`/api/admin/resource/admin/crm/analytics?start=${since}&end=${to}`, { cache: "no-store" }).then((x) => x.ok ? x.json() : null).catch(() => null), fetch(`/api/admin/resource/admin/crm/analytics?start=${since}&end=${to}`, { cache: "no-store" }).then((x) => x.ok ? x.json() : null).catch(() => null),
fetch(`/api/admin/resource/admin/plan-statistics`, { cache: "no-store" }).then((x) => x.ok ? x.json() : []).catch(() => []),
total("users?pageSize=1"), total("users?pageSize=1"),
total("templates?pageSize=1"), total("templates?pageSize=1"),
total("categories"), total("categories"),
@@ -34,6 +37,7 @@ export function StatsAdmin() {
total("blogs?pageSize=1"), total("blogs?pageSize=1"),
]); ]);
setCrm(c); setCrm(c);
setPlans(Array.isArray(ps) ? ps : (ps?.data ?? []));
setCounts({ users, templates, categories, campaigns, blogs }); setCounts({ users, templates, categories, campaigns, blogs });
setLoading(false); setLoading(false);
})(); })();
@@ -73,6 +77,27 @@ export function StatsAdmin() {
<div><div className="text-xl font-bold text-indigo-300">{(crm?.conversion_rate ?? 0).toLocaleString("fa-IR")}٪</div><div className="text-xs text-gray-500">تبدیل</div></div> <div><div className="text-xl font-bold text-indigo-300">{(crm?.conversion_rate ?? 0).toLocaleString("fa-IR")}٪</div><div className="text-xs text-gray-500">تبدیل</div></div>
</div> </div>
</div> </div>
<div className={`${card} !p-0 overflow-hidden`}>
<div className="p-5 pb-2 text-sm font-semibold text-white">آمار پلنها</div>
<table className="w-full text-sm">
<thead><tr className="border-b border-[#1e2235] text-right text-xs text-gray-500">
<th className="px-5 py-2">پلن</th><th className="px-5 py-2">فعال</th><th className="px-5 py-2">کل</th><th className="px-5 py-2">درآمد</th>
</tr></thead>
<tbody>
{plans.length === 0 ? (
<tr><td colSpan={4} className="px-5 py-6 text-center text-gray-500">دادهای نیست.</td></tr>
) : plans.map((p) => (
<tr key={p.plan_name} className="border-b border-[#161a2e]">
<td className="px-5 py-2 text-gray-200">{p.plan_name}</td>
<td className="px-5 py-2 text-emerald-300">{(p.active ?? 0).toLocaleString("fa-IR")}</td>
<td className="px-5 py-2 text-gray-400">{(p.total ?? 0).toLocaleString("fa-IR")}</td>
<td className="px-5 py-2 text-gray-300">{toman(p.revenue_minor ?? 0)} ت</td>
</tr>
))}
</tbody>
</table>
</div>
</> </>
)} )}
</div> </div>