feat(admin): DAU/MAU tracking and expanded dashboard stats (Gitea #10 Phase 3, part 4/4)
Redis-backed visit tracking (src/lib/visit-tracking.ts): daily SADD sets keyed "visits:YYYY-MM-DD", same fail-open idiom as the existing rate-limit.ts. Recorded once per authenticated (app) layout render — SADD is idempotent so no client-side dedup is needed. DAU = today's SCARD, MAU = SUNION across the trailing 30 daily sets. Dashboard additions: - 6 new stat cards: DAU, MAU, shadowban count, blacklist entry count, verifications open/closed. - 3 new time-series charts (reusing the existing TimeSeriesChart/ GranularityToggle components): visits, comments, and reactions over time. Comments/reactions are plain Postgres bucket queries cloned from the existing getPostsOverTime pattern; visits reroutes through the new Redis helpers per bucket. This closes out Gitea #10 Phase 3 (hide-without-delete, shadowban, blacklist, and now analytics — all four parts shipped).
This commit is contained in:
1 parent
a35cb6640f
commit
4000a1eed9
4 files changed
+239
-4
No files matched your search
@@ -16,6 +16,7 @@ import { sendMail } from "@/lib/mail";
|
||||
import { deleteOwnerAccount } from "@/lib/delete-account";
|
||||
import { ensureTeamPet, sendTeamMessage } from "@/lib/team-pet";
|
||||
import { matchesWordBlacklist, matchesLinkBlacklist } from "@/lib/blacklist";
|
||||
import { getDailyVisitCount, getUniqueVisitorCount, lastNDays, daysInRange } from "@/lib/visit-tracking";
|
||||
|
||||
const DATA_EXPORT_TOKEN_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
|
||||
|
||||
@@ -139,15 +140,47 @@ export const adminRouter = router({
|
||||
// ─── Dashboard ───────────────────────────────────────────────────────────────
|
||||
getStats: protectedProcedure.query(async ({ ctx }) => {
|
||||
await assertAdmin(ctx.prisma, ctx.userId);
|
||||
const [owners, pets, posts, bans, ads, reports] = await Promise.all([
|
||||
const [
|
||||
owners,
|
||||
pets,
|
||||
posts,
|
||||
bans,
|
||||
ads,
|
||||
reports,
|
||||
shadowbans,
|
||||
blacklistEntries,
|
||||
verificationPending,
|
||||
verificationClosed,
|
||||
dau,
|
||||
mau,
|
||||
] = await Promise.all([
|
||||
ctx.prisma.owner.count(),
|
||||
ctx.prisma.pet.count(),
|
||||
ctx.prisma.post.count(),
|
||||
ctx.prisma.userBan.count(),
|
||||
ctx.prisma.advertisement.count({ where: { isActive: true } }),
|
||||
ctx.prisma.report.count(),
|
||||
ctx.prisma.shadowban.count(),
|
||||
ctx.prisma.blacklistEntry.count(),
|
||||
ctx.prisma.verificationRequest.count({ where: { status: "PENDING" } }),
|
||||
ctx.prisma.verificationRequest.count({ where: { status: { in: ["APPROVED", "REJECTED"] } } }),
|
||||
getDailyVisitCount(new Date()),
|
||||
getUniqueVisitorCount(lastNDays(30)),
|
||||
]);
|
||||
return { owners, pets, posts, bans, activeAds: ads, reports };
|
||||
return {
|
||||
owners,
|
||||
pets,
|
||||
posts,
|
||||
bans,
|
||||
activeAds: ads,
|
||||
reports,
|
||||
shadowbans,
|
||||
blacklistEntries,
|
||||
verificationPending,
|
||||
verificationClosed,
|
||||
dau,
|
||||
mau,
|
||||
};
|
||||
}),
|
||||
|
||||
/** New posts per bucket over the trailing window (30 days / 12 weeks / 12 months). */
|
||||
@@ -183,6 +216,56 @@ export const adminRouter = router({
|
||||
return buckets.map((date, i) => ({ date: date.toISOString(), value: cumulative[i]! }));
|
||||
}),
|
||||
|
||||
/** New comments per bucket over the trailing window. */
|
||||
getCommentsOverTime: protectedProcedure
|
||||
.input(z.object({ granularity: z.enum(["day", "week", "month"]) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
await assertAdmin(ctx.prisma, ctx.userId);
|
||||
const buckets = buildBuckets(input.granularity);
|
||||
const comments = await ctx.prisma.comment.findMany({
|
||||
where: { createdAt: { gte: buckets[0] } },
|
||||
select: { createdAt: true },
|
||||
});
|
||||
const counts = countByBucket(comments.map((c) => c.createdAt), buckets, input.granularity);
|
||||
return buckets.map((date, i) => ({ date: date.toISOString(), value: counts[i]! }));
|
||||
}),
|
||||
|
||||
/** New reactions ("likes") per bucket over the trailing window. */
|
||||
getReactionsOverTime: protectedProcedure
|
||||
.input(z.object({ granularity: z.enum(["day", "week", "month"]) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
await assertAdmin(ctx.prisma, ctx.userId);
|
||||
const buckets = buildBuckets(input.granularity);
|
||||
const reactions = await ctx.prisma.reaction.findMany({
|
||||
where: { createdAt: { gte: buckets[0] } },
|
||||
select: { createdAt: true },
|
||||
});
|
||||
const counts = countByBucket(reactions.map((r) => r.createdAt), buckets, input.granularity);
|
||||
return buckets.map((date, i) => ({ date: date.toISOString(), value: counts[i]! }));
|
||||
}),
|
||||
|
||||
/**
|
||||
* Unique daily visitors per bucket (Redis-backed, see visit-tracking.ts) —
|
||||
* the DAU trend line. Week/month buckets union each day's set in range
|
||||
* rather than summing daily counts, so a repeat visitor within the bucket
|
||||
* is still counted once.
|
||||
*/
|
||||
getVisitsOverTime: protectedProcedure
|
||||
.input(z.object({ granularity: z.enum(["day", "week", "month"]) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
await assertAdmin(ctx.prisma, ctx.userId);
|
||||
const buckets = buildBuckets(input.granularity);
|
||||
const tomorrow = truncateUTC(new Date(), "day");
|
||||
tomorrow.setUTCDate(tomorrow.getUTCDate() + 1);
|
||||
const values = await Promise.all(
|
||||
buckets.map((bucketStart, i) => {
|
||||
const bucketEnd = i + 1 < buckets.length ? buckets[i + 1]! : tomorrow;
|
||||
return getUniqueVisitorCount(daysInRange(bucketStart, bucketEnd));
|
||||
})
|
||||
);
|
||||
return buckets.map((date, i) => ({ date: date.toISOString(), value: values[i]! }));
|
||||
}),
|
||||
|
||||
// ─── Post moderation ─────────────────────────────────────────────────────────
|
||||
listPosts: protectedProcedure
|
||||
.input(
|
||||
|
||||
Reference in new issue
Block a user