feat(admin): marketplace console + analytics degraded banners
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
f150aea8b9
commit
025af147cd
9 files changed
+373
-57
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { ArrowLeftRight } from "lucide-react";
|
||||
import { getLocale, getTranslations } from "next-intl/server";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getLocale, getTranslations } from "next-intl/server";
|
||||
import { RevenueChart } from "@/components/admin/revenue-chart";
|
||||
import { StatsCard } from "@/components/admin/stats-card";
|
||||
import { CurrencyIcon } from "@/components/shared/currency-icon";
|
||||
@@ -42,17 +42,29 @@ async function safeCount(fn: () => Promise<number>): Promise<number> {
|
||||
}
|
||||
}
|
||||
|
||||
async function getEconomyData(locale: string): Promise<EconomyData> {
|
||||
async function getEconomyData(
|
||||
locale: string,
|
||||
): Promise<{ data: EconomyData; degraded: boolean }> {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const todayStart = now - (now % 86400);
|
||||
const weekAgo = now - 7 * 86400;
|
||||
const rangeStart = todayStart - 6 * 86400;
|
||||
let degraded = false;
|
||||
|
||||
try {
|
||||
const [creditsAgg, pixelsAgg, pointsAgg] = await Promise.all([
|
||||
prisma.user.aggregate({ _sum: { credits: true } }).catch(() => null),
|
||||
prisma.user.aggregate({ _sum: { pixels: true } }).catch(() => null),
|
||||
prisma.user.aggregate({ _sum: { points: true } }).catch(() => null),
|
||||
prisma.user.aggregate({ _sum: { credits: true } }).catch(() => {
|
||||
degraded = true;
|
||||
return null;
|
||||
}),
|
||||
prisma.user.aggregate({ _sum: { pixels: true } }).catch(() => {
|
||||
degraded = true;
|
||||
return null;
|
||||
}),
|
||||
prisma.user.aggregate({ _sum: { points: true } }).catch(() => {
|
||||
degraded = true;
|
||||
return null;
|
||||
}),
|
||||
]);
|
||||
const totalCredits = creditsAgg?._sum.credits ?? 0;
|
||||
const totalPixels = pixelsAgg?._sum.pixels ?? 0;
|
||||
@@ -72,24 +84,20 @@ async function getEconomyData(locale: string): Promise<EconomyData> {
|
||||
shopCreditsSpent = shopRevenue._sum?.costCredits ?? 0;
|
||||
shopPointsSpent = shopRevenue._sum?.costPoints ?? 0;
|
||||
} catch {
|
||||
/* shop logs table/columns may be missing */
|
||||
degraded = true;
|
||||
}
|
||||
|
||||
const [marketplaceActive, marketplaceSold, weekTrades] = await Promise.all(
|
||||
[
|
||||
safeCount(() =>
|
||||
prisma.marketplaceItems.count({ where: { state: 1 } }),
|
||||
),
|
||||
safeCount(() =>
|
||||
prisma.marketplaceItems.count({
|
||||
where: { state: 2, soldTimestamp: { gte: weekAgo } },
|
||||
}),
|
||||
),
|
||||
safeCount(() =>
|
||||
prisma.roomTradeLog.count({ where: { timestamp: { gte: weekAgo } } }),
|
||||
),
|
||||
],
|
||||
);
|
||||
const [marketplaceActive, marketplaceSold, weekTrades] = await Promise.all([
|
||||
safeCount(() => prisma.marketplaceItems.count({ where: { state: 1 } })),
|
||||
safeCount(() =>
|
||||
prisma.marketplaceItems.count({
|
||||
where: { state: 2, soldTimestamp: { gte: weekAgo } },
|
||||
}),
|
||||
),
|
||||
safeCount(() =>
|
||||
prisma.roomTradeLog.count({ where: { timestamp: { gte: weekAgo } } }),
|
||||
),
|
||||
]);
|
||||
|
||||
const revenueData: EconomyData["revenueData"] = [];
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
@@ -104,7 +112,11 @@ async function getEconomyData(locale: string): Promise<EconomyData> {
|
||||
|
||||
try {
|
||||
const revRows = await prisma.$queryRaw<
|
||||
{ day_bucket: bigint; credits: number | bigint; points: number | bigint }[]
|
||||
{
|
||||
day_bucket: bigint;
|
||||
credits: number | bigint;
|
||||
points: number | bigint;
|
||||
}[]
|
||||
>`
|
||||
SELECT FLOOR(timestamp / 86400) AS day_bucket,
|
||||
COALESCE(SUM(cost_credits), 0) AS credits,
|
||||
@@ -128,23 +140,26 @@ async function getEconomyData(locale: string): Promise<EconomyData> {
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* keep zero revenue series */
|
||||
degraded = true;
|
||||
}
|
||||
|
||||
return {
|
||||
totalCredits,
|
||||
totalPixels,
|
||||
totalPoints,
|
||||
shopPurchases,
|
||||
shopCreditsSpent,
|
||||
shopPointsSpent,
|
||||
marketplaceActive,
|
||||
marketplaceSold,
|
||||
weekTrades,
|
||||
revenueData,
|
||||
degraded,
|
||||
data: {
|
||||
totalCredits,
|
||||
totalPixels,
|
||||
totalPoints,
|
||||
shopPurchases,
|
||||
shopCreditsSpent,
|
||||
shopPointsSpent,
|
||||
marketplaceActive,
|
||||
marketplaceSold,
|
||||
weekTrades,
|
||||
revenueData,
|
||||
},
|
||||
};
|
||||
} catch {
|
||||
return EMPTY;
|
||||
return { data: EMPTY, degraded: true };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,10 +171,16 @@ export default async function EconomyPage() {
|
||||
|
||||
const locale = await getLocale();
|
||||
const t = await getTranslations("pages.admin.analytics.economy");
|
||||
const data = await getEconomyData(locale);
|
||||
const tRoot = await getTranslations("pages.admin.analytics");
|
||||
const { data, degraded } = await getEconomyData(locale);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{degraded ? (
|
||||
<div className="rounded-lg bg-[var(--admin-error-subtle)] p-3 text-xs text-[var(--admin-error)]">
|
||||
{tRoot("loadError")}
|
||||
</div>
|
||||
) : null}
|
||||
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
|
||||
<StatsCard
|
||||
title={t("creditsInCirculation")}
|
||||
@@ -203,13 +224,17 @@ export default async function EconomyPage() {
|
||||
<span className="font-medium">{data.shopPurchases}</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-muted-foreground">{t("creditsSpent")}</span>
|
||||
<span className="text-muted-foreground">
|
||||
{t("creditsSpent")}
|
||||
</span>
|
||||
<span className="font-medium">
|
||||
{data.shopCreditsSpent.toLocaleString(locale)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex justify-between">
|
||||
<span className="text-muted-foreground">{t("pointsSpent")}</span>
|
||||
<span className="text-muted-foreground">
|
||||
{t("pointsSpent")}
|
||||
</span>
|
||||
<span className="font-medium">
|
||||
{data.shopPointsSpent.toLocaleString(locale)}
|
||||
</span>
|
||||
|
||||
Reference in new issue
Block a user