Local Build and Deploy / deploy (push) Successful in 56s
Co-authored-by: Cursor <[email protected]>
263 lines
7.7 KiB
TypeScript
263 lines
7.7 KiB
TypeScript
import { ArrowLeftRight, TrendingDown, TrendingUp } from "lucide-react";
|
|
import { redirect } from "next/navigation";
|
|
import { RevenueChart } from "@/components/admin/revenue-chart";
|
|
import { StatsCard } from "@/components/admin/stats-card";
|
|
import { CurrencyIcon } from "@/components/shared/currency-icon";
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
|
import { prisma } from "@/lib/prisma";
|
|
|
|
type EconomyData = {
|
|
totalCredits: number;
|
|
totalPixels: number;
|
|
totalPoints: number;
|
|
shopPurchases: number;
|
|
shopCreditsSpent: number;
|
|
shopPointsSpent: number;
|
|
marketplaceActive: number;
|
|
marketplaceSold: number;
|
|
weekTrades: number;
|
|
revenueData: { label: string; credits: number; points: number }[];
|
|
};
|
|
|
|
const EMPTY: EconomyData = {
|
|
totalCredits: 0,
|
|
totalPixels: 0,
|
|
totalPoints: 0,
|
|
shopPurchases: 0,
|
|
shopCreditsSpent: 0,
|
|
shopPointsSpent: 0,
|
|
marketplaceActive: 0,
|
|
marketplaceSold: 0,
|
|
weekTrades: 0,
|
|
revenueData: [],
|
|
};
|
|
|
|
async function safeCount(fn: () => Promise<number>): Promise<number> {
|
|
try {
|
|
return await fn();
|
|
} catch {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function getEconomyData(): Promise<EconomyData> {
|
|
const now = Math.floor(Date.now() / 1000);
|
|
const todayStart = now - (now % 86400);
|
|
const weekAgo = now - 7 * 86400;
|
|
const rangeStart = todayStart - 6 * 86400;
|
|
|
|
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),
|
|
]);
|
|
const totalCredits = creditsAgg?._sum.credits ?? 0;
|
|
const totalPixels = pixelsAgg?._sum.pixels ?? 0;
|
|
const totalPoints = pointsAgg?._sum.points ?? 0;
|
|
|
|
let shopPurchases = 0;
|
|
let shopCreditsSpent = 0;
|
|
let shopPointsSpent = 0;
|
|
try {
|
|
const shopRevenue = await prisma.logsShopPurchases.aggregate({
|
|
where: { timestamp: { gte: weekAgo } },
|
|
_sum: { costCredits: true, costPoints: true },
|
|
_count: true,
|
|
});
|
|
shopPurchases =
|
|
typeof shopRevenue._count === "number" ? shopRevenue._count : 0;
|
|
shopCreditsSpent = shopRevenue._sum?.costCredits ?? 0;
|
|
shopPointsSpent = shopRevenue._sum?.costPoints ?? 0;
|
|
} catch {
|
|
/* shop logs table/columns may be missing */
|
|
}
|
|
|
|
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--) {
|
|
const dayStart = todayStart - i * 86400;
|
|
const date = new Date(dayStart * 1000);
|
|
revenueData.push({
|
|
label: date.toLocaleDateString("en", { weekday: "short" }),
|
|
credits: 0,
|
|
points: 0,
|
|
});
|
|
}
|
|
|
|
try {
|
|
const revRows = await prisma.$queryRaw<
|
|
{ day_bucket: bigint; credits: number | bigint; points: number | bigint }[]
|
|
>`
|
|
SELECT FLOOR(timestamp / 86400) AS day_bucket,
|
|
COALESCE(SUM(cost_credits), 0) AS credits,
|
|
COALESCE(SUM(cost_points), 0) AS points
|
|
FROM logs_shop_purchases
|
|
WHERE timestamp >= ${rangeStart}
|
|
GROUP BY day_bucket ORDER BY day_bucket
|
|
`;
|
|
const revMap = new Map(
|
|
revRows.map((r) => [
|
|
Number(r.day_bucket),
|
|
{ credits: Number(r.credits), points: Number(r.points) },
|
|
]),
|
|
);
|
|
for (let i = 0; i < revenueData.length; i++) {
|
|
const dayStart = todayStart - (6 - i) * 86400;
|
|
const bucket = Math.floor(dayStart / 86400);
|
|
const entry = revMap.get(bucket);
|
|
if (entry) {
|
|
revenueData[i] = { ...revenueData[i], ...entry };
|
|
}
|
|
}
|
|
} catch {
|
|
/* keep zero revenue series */
|
|
}
|
|
|
|
return {
|
|
totalCredits,
|
|
totalPixels,
|
|
totalPoints,
|
|
shopPurchases,
|
|
shopCreditsSpent,
|
|
shopPointsSpent,
|
|
marketplaceActive,
|
|
marketplaceSold,
|
|
weekTrades,
|
|
revenueData,
|
|
};
|
|
} catch {
|
|
return EMPTY;
|
|
}
|
|
}
|
|
|
|
export default async function EconomyPage() {
|
|
const { session, permissions } = await getAdminContext();
|
|
if (!canAccess(permissions, PERMS.ANALYTICS_VIEW, session.user.rank)) {
|
|
redirect("/admin");
|
|
}
|
|
|
|
const data = await getEconomyData();
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-4">
|
|
<StatsCard
|
|
title="Credits in Circulation"
|
|
value={data.totalCredits.toLocaleString()}
|
|
description="Total credits across all users"
|
|
icon={<CurrencyIcon kind="credits" size={16} />}
|
|
/>
|
|
<StatsCard
|
|
title="Pixels in Circulation"
|
|
value={data.totalPixels.toLocaleString()}
|
|
description="Total pixels across all users"
|
|
icon={<CurrencyIcon kind="pixels" size={16} />}
|
|
/>
|
|
<StatsCard
|
|
title="Points in Circulation"
|
|
value={data.totalPoints.toLocaleString()}
|
|
description="Total points across all users"
|
|
icon={<CurrencyIcon kind="diamonds" size={16} />}
|
|
/>
|
|
<StatsCard
|
|
title="Weekly Trades"
|
|
value={data.weekTrades.toLocaleString()}
|
|
description={`${data.marketplaceActive} marketplace listings`}
|
|
icon={<ArrowLeftRight className="h-4 w-4" />}
|
|
/>
|
|
</div>
|
|
|
|
<RevenueChart data={data.revenueData} />
|
|
|
|
<div className="grid gap-6 md:grid-cols-3">
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Shop Revenue (7d)</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="space-y-2">
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Purchases</span>
|
|
<span className="font-medium">{data.shopPurchases}</span>
|
|
</div>
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Credits spent</span>
|
|
<span className="font-medium">
|
|
{data.shopCreditsSpent.toLocaleString()}
|
|
</span>
|
|
</div>
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Points spent</span>
|
|
<span className="font-medium">
|
|
{data.shopPointsSpent.toLocaleString()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Marketplace</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="space-y-2">
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Active listings</span>
|
|
<span className="font-medium">{data.marketplaceActive}</span>
|
|
</div>
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Sold (7d)</span>
|
|
<span className="font-medium">{data.marketplaceSold}</span>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
<Card>
|
|
<CardHeader>
|
|
<CardTitle>Currency Distribution</CardTitle>
|
|
</CardHeader>
|
|
<CardContent>
|
|
<div className="space-y-2">
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Credits</span>
|
|
<span className="font-medium">
|
|
{data.totalCredits.toLocaleString()}
|
|
</span>
|
|
</div>
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Pixels</span>
|
|
<span className="font-medium">
|
|
{data.totalPixels.toLocaleString()}
|
|
</span>
|
|
</div>
|
|
<div className="flex justify-between">
|
|
<span className="text-muted-foreground">Points</span>
|
|
<span className="font-medium">
|
|
{data.totalPoints.toLocaleString()}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|