mirror of
https://github.com/documenso/documenso.git
synced 2025-11-19 19:21:39 +10:00
feat: mau
This commit is contained in:
@ -94,21 +94,24 @@ export const getUserWithSignedDocumentMonthlyGrowth = async () => {
|
||||
export type GetMonthlyActiveUsersResult = Array<{
|
||||
month: string;
|
||||
count: number;
|
||||
cume_count: number;
|
||||
}>;
|
||||
|
||||
type GetMonthlyActiveUsersQueryResult = Array<{
|
||||
month: Date;
|
||||
count: bigint;
|
||||
cume_count: bigint;
|
||||
}>;
|
||||
|
||||
export const getMonthlyActiveUsers = async () => {
|
||||
const result = await prisma.$queryRaw<GetMonthlyActiveUsersQueryResult>`
|
||||
SELECT
|
||||
DATE_TRUNC('month', "lastSignedIn") AS "month",
|
||||
COUNT(DISTINCT "id") as "count"
|
||||
COUNT(DISTINCT "id") as "count",
|
||||
SUM(COUNT(DISTINCT "id")) OVER (ORDER BY DATE_TRUNC('month', "lastSignedIn")) as "cume_count"
|
||||
FROM "User"
|
||||
WHERE "lastSignedIn" >= NOW() - INTERVAL '1 year'
|
||||
GROUP BY "month"
|
||||
GROUP BY DATE_TRUNC('month', "lastSignedIn")
|
||||
ORDER BY "month" DESC
|
||||
LIMIT 12
|
||||
`;
|
||||
@ -116,5 +119,6 @@ export const getMonthlyActiveUsers = async () => {
|
||||
return result.map((row) => ({
|
||||
month: DateTime.fromJSDate(row.month).toFormat('yyyy-MM'),
|
||||
count: Number(row.count),
|
||||
cume_count: Number(row.cume_count),
|
||||
}));
|
||||
};
|
||||
|
||||
62
packages/prisma/mau-seed.ts
Normal file
62
packages/prisma/mau-seed.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
import { hashSync } from '@documenso/lib/server-only/auth/hash';
|
||||
|
||||
import { prisma } from '.';
|
||||
import { Role } from './client';
|
||||
|
||||
const USERS_PER_MONTH = 20;
|
||||
const MONTHS_OF_HISTORY = 12;
|
||||
|
||||
export const seedMAUData = async () => {
|
||||
const now = DateTime.now();
|
||||
|
||||
for (let monthsAgo = MONTHS_OF_HISTORY - 1; monthsAgo >= 0; monthsAgo--) {
|
||||
const monthStart = now.minus({ months: monthsAgo }).startOf('month');
|
||||
const monthEnd = monthStart.endOf('month');
|
||||
|
||||
console.log(`Seeding users for ${monthStart.toFormat('yyyy-MM')}`);
|
||||
|
||||
const users = await Promise.all(
|
||||
Array.from({ length: USERS_PER_MONTH }).map(async (_, index) => {
|
||||
const createdAt = DateTime.fromMillis(
|
||||
monthStart.toMillis() + Math.random() * (monthEnd.toMillis() - monthStart.toMillis()),
|
||||
).toJSDate();
|
||||
|
||||
const lastSignedIn =
|
||||
Math.random() > 0.3
|
||||
? DateTime.fromMillis(
|
||||
createdAt.getTime() + Math.random() * (now.toMillis() - createdAt.getTime()),
|
||||
).toJSDate()
|
||||
: createdAt;
|
||||
|
||||
return prisma.user.create({
|
||||
data: {
|
||||
name: `MAU Test User ${monthsAgo}-${index}`,
|
||||
email: `mau-test-${monthsAgo}-${index}@documenso.com`,
|
||||
password: hashSync('password'),
|
||||
emailVerified: createdAt,
|
||||
createdAt,
|
||||
lastSignedIn,
|
||||
roles: [Role.USER],
|
||||
},
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
console.log(`Created ${users.length} users for ${monthStart.toFormat('yyyy-MM')}`);
|
||||
}
|
||||
};
|
||||
|
||||
// Run the seed if this file is executed directly
|
||||
if (require.main === module) {
|
||||
seedMAUData()
|
||||
.then(() => {
|
||||
console.log('MAU seed completed successfully');
|
||||
process.exit(0);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error seeding MAU data:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
@ -15,7 +15,8 @@
|
||||
"prisma:migrate-deploy": "prisma migrate deploy",
|
||||
"prisma:migrate-reset": "prisma migrate reset",
|
||||
"prisma:seed": "prisma db seed",
|
||||
"prisma:studio": "prisma studio"
|
||||
"prisma:studio": "prisma studio",
|
||||
"prisma:seed-mau": "tsx ./mau-seed.ts"
|
||||
},
|
||||
"prisma": {
|
||||
"seed": "tsx ./seed-database.ts"
|
||||
@ -36,4 +37,4 @@
|
||||
"typescript": "5.6.2",
|
||||
"zod-prisma-types": "3.1.9"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user