feat: complete phase one and two of task implementation

This commit is contained in:
Ayobami
2025-07-29 21:48:34 +01:00
parent b8fc924e7e
commit 42fec5708a
12 changed files with 1585 additions and 36 deletions
+52 -14
View File
@@ -1,25 +1,63 @@
const redis = require("redis");
require('dotenv').config();
// Number of tickets to seed; default is 100000, or can be passed as a command line argument
const numTickets = parseInt(process.argv[2]) || 100000;
// Configuration for seeding
const config = {
numEvents: parseInt(process.argv[2]) || 5, // Number of events to create
ticketsPerEvent: parseInt(process.argv[3]) || 10000, // Tickets per event
redisUrl: process.env.REDIS_URL || "redis://localhost:6379"
};
// Use environment variable for Redis URL if provided; otherwise default to localhost
const redisUrl = process.env.REDIS_URL || "redis://localhost:6379";
const client = redis.createClient({ url: redisUrl });
const client = redis.createClient({ url: config.redisUrl });
async function seedTickets() {
try {
await client.connect();
// Clear existing tickets if any
await client.del("tickets");
console.log(`Seeding ${numTickets} tickets into Redis...`);
let tickets = [];
for (let i = 1; i <= numTickets; i++) {
tickets.push(`ticket-${i}`);
console.log(`Seeding ${config.numEvents} events with ${config.ticketsPerEvent} tickets each...`);
// Clear existing event data
const existingKeys = await client.keys('event:*');
if (existingKeys.length > 0) {
await client.del(existingKeys);
console.log(`Cleared ${existingKeys.length} existing event keys.`);
}
// Push all tickets into the 'tickets' list
await client.rPush("tickets", tickets);
console.log(`Seeded ${numTickets} tickets.`);
// Seed multiple events
for (let eventId = 1; eventId <= config.numEvents; eventId++) {
const eventKey = `event:${eventId}:tickets`;
const metaKey = `event:${eventId}:meta`;
// Generate tickets for this event
const tickets = [];
for (let i = 1; i <= config.ticketsPerEvent; i++) {
tickets.push(`ticket-${eventId}-${i}`);
}
// Store tickets in Redis list
await client.rPush(eventKey, tickets);
// Store event metadata
await client.hSet(metaKey, {
eventId: eventId,
totalTickets: config.ticketsPerEvent,
soldTickets: 0,
createdAt: new Date().toISOString(),
name: `Event ${eventId}`,
description: `Sample event ${eventId} for load testing`
});
console.log(`✓ Event ${eventId}: ${config.ticketsPerEvent} tickets seeded`);
}
// Store global stats
await client.hSet('global:stats', {
totalEvents: config.numEvents,
totalTickets: config.numEvents * config.ticketsPerEvent,
totalSold: 0,
lastSeeded: new Date().toISOString()
});
console.log(`\n🎉 Successfully seeded ${config.numEvents} events with ${config.numEvents * config.ticketsPerEvent} total tickets!`);
process.exit(0);
} catch (err) {
console.error("Error during seed:", err);