72 lines
1.9 KiB
TypeScript
72 lines
1.9 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import pg from 'pg';
|
|
import dotenv from 'dotenv';
|
|
|
|
dotenv.config({ path: '.env.local' });
|
|
|
|
const connectionString = process.env.DATABASE_URL!;
|
|
const pool = new pg.Pool({ connectionString });
|
|
const adapter = new PrismaPg(pool);
|
|
const prisma = new PrismaClient({ adapter });
|
|
|
|
async function main() {
|
|
const business = await prisma.business.findFirst();
|
|
if (!business) {
|
|
console.log("No business found to seed stats for.");
|
|
return;
|
|
}
|
|
|
|
console.log(`Seeding stats for business: ${business.name} (${business.id})`);
|
|
|
|
const events = [];
|
|
const typeViews = 'BUSINESS_VIEW';
|
|
const typeClicks = 'BUSINESS_CONTACT_CLICK';
|
|
|
|
// Seed last 30 days
|
|
for (let i = 0; i < 30; i++) {
|
|
const date = new Date();
|
|
date.setDate(date.getDate() - i);
|
|
|
|
// Random number of views (5 to 50)
|
|
const viewCount = Math.floor(Math.random() * 45) + 5;
|
|
for (let j = 0; j < viewCount; j++) {
|
|
events.push({
|
|
type: typeViews,
|
|
path: `/annuaire/${business.id}`,
|
|
label: business.name,
|
|
metadata: { businessId: business.id },
|
|
createdAt: new Date(date.getTime() + Math.random() * 3600000 * 24) // Random time in that day
|
|
});
|
|
}
|
|
|
|
// Random number of clicks (0 to 10)
|
|
const clickCount = Math.floor(Math.random() * 11);
|
|
for (let k = 0; k < clickCount; k++) {
|
|
events.push({
|
|
type: typeClicks,
|
|
path: `/annuaire/${business.id}`,
|
|
label: business.name,
|
|
metadata: { businessId: business.id },
|
|
createdAt: new Date(date.getTime() + Math.random() * 3600000 * 24)
|
|
});
|
|
}
|
|
}
|
|
|
|
// Batch insert
|
|
await prisma.analyticsEvent.createMany({
|
|
data: events
|
|
});
|
|
|
|
console.log(`Successfully seeded ${events.length} events.`);
|
|
}
|
|
|
|
main()
|
|
.catch(e => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
})
|
|
.finally(async () => {
|
|
await prisma.$disconnect();
|
|
});
|