This commit is contained in:
1 parent
e85e4d74ea
commit
2e4ed76121
378 files changed
+22404
-20686
No files matched your search
@@ -1,21 +1,21 @@
|
||||
import { withAdmin } from '@/lib/api-handler'
|
||||
import { apiError } from '@/lib/api-response'
|
||||
import { PERMS } from '@/lib/permission-slugs'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import { withAdmin } from "@/lib/api-handler";
|
||||
import { apiError } from "@/lib/api-response";
|
||||
import { PERMS } from "@/lib/permission-slugs";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
const VALID_REPORTS = new Set(['registrations', 'online-by-hour', 'economy'])
|
||||
const VALID_REPORTS = new Set(["registrations", "online-by-hour", "economy"]);
|
||||
|
||||
function csvEscape(value: unknown): string {
|
||||
if (value == null) return ''
|
||||
const str = String(value)
|
||||
if (value == null) return "";
|
||||
const str = String(value);
|
||||
if (/[",\n\r]/.test(str)) {
|
||||
return `"${str.replace(/"/g, '""')}"`
|
||||
return `"${str.replace(/"/g, '""')}"`;
|
||||
}
|
||||
return str
|
||||
return str;
|
||||
}
|
||||
|
||||
function csvRow(values: unknown[]): string {
|
||||
return `${values.map(csvEscape).join(',')}\n`
|
||||
return `${values.map(csvEscape).join(",")}\n`;
|
||||
}
|
||||
|
||||
async function streamRegistrations(): Promise<ReadableStream<Uint8Array>> {
|
||||
@@ -25,17 +25,17 @@ async function streamRegistrations(): Promise<ReadableStream<Uint8Array>> {
|
||||
WHERE account_created > UNIX_TIMESTAMP(NOW() - INTERVAL 90 DAY)
|
||||
GROUP BY day
|
||||
ORDER BY day ASC
|
||||
`
|
||||
const encoder = new TextEncoder()
|
||||
`;
|
||||
const encoder = new TextEncoder();
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(csvRow(['day', 'registrations'])))
|
||||
controller.enqueue(encoder.encode(csvRow(["day", "registrations"])));
|
||||
for (const r of rows) {
|
||||
controller.enqueue(encoder.encode(csvRow([r.day, Number(r.cnt)])))
|
||||
controller.enqueue(encoder.encode(csvRow([r.day, Number(r.cnt)])));
|
||||
}
|
||||
controller.close()
|
||||
controller.close();
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async function streamOnlineByHour(): Promise<ReadableStream<Uint8Array>> {
|
||||
@@ -45,57 +45,53 @@ async function streamOnlineByHour(): Promise<ReadableStream<Uint8Array>> {
|
||||
WHERE last_online > UNIX_TIMESTAMP(NOW() - INTERVAL 30 DAY)
|
||||
GROUP BY hour
|
||||
ORDER BY hour ASC
|
||||
`
|
||||
const encoder = new TextEncoder()
|
||||
`;
|
||||
const encoder = new TextEncoder();
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(csvRow(['hour', 'active_users'])))
|
||||
controller.enqueue(encoder.encode(csvRow(["hour", "active_users"])));
|
||||
for (const r of rows) {
|
||||
controller.enqueue(encoder.encode(csvRow([Number(r.hour), Number(r.cnt)])))
|
||||
controller.enqueue(encoder.encode(csvRow([Number(r.hour), Number(r.cnt)])));
|
||||
}
|
||||
controller.close()
|
||||
controller.close();
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
async function streamEconomy(): Promise<ReadableStream<Uint8Array>> {
|
||||
const [totals] = await prisma.$queryRaw<{ total_credits: bigint; total_pixels: bigint }[]>`
|
||||
SELECT SUM(credits) AS total_credits, SUM(pixels) AS total_pixels FROM users
|
||||
`
|
||||
const encoder = new TextEncoder()
|
||||
`;
|
||||
const encoder = new TextEncoder();
|
||||
return new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(csvRow(['metric', 'value'])))
|
||||
controller.enqueue(
|
||||
encoder.encode(csvRow(['total_credits', Number(totals?.total_credits ?? 0)])),
|
||||
)
|
||||
controller.enqueue(
|
||||
encoder.encode(csvRow(['total_pixels', Number(totals?.total_pixels ?? 0)])),
|
||||
)
|
||||
controller.close()
|
||||
controller.enqueue(encoder.encode(csvRow(["metric", "value"])));
|
||||
controller.enqueue(encoder.encode(csvRow(["total_credits", Number(totals?.total_credits ?? 0)])));
|
||||
controller.enqueue(encoder.encode(csvRow(["total_pixels", Number(totals?.total_pixels ?? 0)])));
|
||||
controller.close();
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
export const GET = withAdmin({ permission: PERMS.ANALYTICS_EXPORT }, async (request) => {
|
||||
const url = new URL(request.url)
|
||||
const report = url.searchParams.get('report') ?? ''
|
||||
const url = new URL(request.url);
|
||||
const report = url.searchParams.get("report") ?? "";
|
||||
if (!VALID_REPORTS.has(report)) {
|
||||
return apiError(`Unknown report. Use one of: ${[...VALID_REPORTS].join(', ')}`, 400)
|
||||
return apiError(`Unknown report. Use one of: ${[...VALID_REPORTS].join(", ")}`, 400);
|
||||
}
|
||||
|
||||
const stream =
|
||||
report === 'registrations'
|
||||
report === "registrations"
|
||||
? await streamRegistrations()
|
||||
: report === 'online-by-hour'
|
||||
: report === "online-by-hour"
|
||||
? await streamOnlineByHour()
|
||||
: await streamEconomy()
|
||||
: await streamEconomy();
|
||||
|
||||
return new Response(stream, {
|
||||
headers: {
|
||||
'Content-Type': 'text/csv; charset=utf-8',
|
||||
'Content-Disposition': `attachment; filename="${report}.csv"`,
|
||||
'Cache-Control': 'no-store',
|
||||
"Content-Type": "text/csv; charset=utf-8",
|
||||
"Content-Disposition": `attachment; filename="${report}.csv"`,
|
||||
"Cache-Control": "no-store",
|
||||
},
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user