Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

chore(seed): seed the organization in this machine's timezone

Carl-Gerhard Lindesvärd51f3c7f725c7
diff --git a/packages/seed/README.md b/packages/seed/README.md
index 53b6b5dde..a7adb1f66 100644
--- a/packages/seed/README.md
+++ b/packages/seed/README.md
@@ -8,6 +8,7 @@ bun run seed # small: 14 days, ~5k sessions
bun run seed --size large --reset # 90 days, ~1M sessions; --reset clears earlier seeded rows
bun run seed --days 3 --sessions-per-day 500 --projects saas,app --seed 7
bun run seed --dry-run # the plan only
+bun run seed --timezone UTC # the organization's timezone; defaults to this machine's
```
Same flags + seed ⇒ identical rows.
diff --git a/packages/seed/src/cli.ts b/packages/seed/src/cli.ts
index 6d20c9177..7ae4d5746 100644
--- a/packages/seed/src/cli.ts
+++ b/packages/seed/src/cli.ts
@@ -1,5 +1,6 @@
// `bun run seed [--size small|medium|large|xl] [--days N --sessions-per-day N]
-// [--seed N] [--projects website,saas,...] [--reset] [--dry-run] [--out path]`
+// [--seed N] [--projects website,saas,...] [--timezone zone] [--reset]
+// [--dry-run] [--out path]`
//
// Targets whatever DATABASE_URL and CLICKHOUSE_URL name (a worktree's own
// databases). Same flags and seed ⇒ identical data.
@@ -35,6 +36,7 @@ const USAGE = `Usage: bun run seed [options]
--sessions-per-day <n> override the preset's sessions per day (all projects)
--seed <n> deterministic seed (default: ${DEFAULT_SEED})
--projects <a,b> archetypes to seed (default: ${ARCHETYPE_IDS.join(',')})
+ --timezone <zone> the organization's timezone (default: this machine's)
--variance <x> 0 regular … 1 lively … 2 chaotic (default: ${DEFAULT_VARIANCE})
--reset delete earlier seeded rows for these projects first
--dry-run print the plan and exit
@@ -76,6 +78,10 @@ function maskedUrl(url: string | undefined): string {
}
}
+function machineTimezone(): string {
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
+}
+
function main(): Promise<void> {
const { values } = parseArgs({
args: process.argv.slice(2),
@@ -86,6 +92,7 @@ function main(): Promise<void> {
seed: { type: 'string' },
projects: { type: 'string' },
variance: { type: 'string' },
+ timezone: { type: 'string' },
reset: { type: 'boolean', default: false },
'dry-run': { type: 'boolean', default: false },
out: { type: 'string' },
@@ -125,6 +132,7 @@ function main(): Promise<void> {
seed,
variance,
archetypes,
+ timezone: values.timezone ?? machineTimezone(),
reset: values.reset,
dryRun: values['dry-run'],
outPath,
@@ -138,6 +146,7 @@ async function run(options: {
seed: number;
variance: number;
archetypes: typeof ARCHETYPES;
+ timezone: string;
reset: boolean;
dryRun: boolean;
outPath: string;
@@ -169,9 +178,13 @@ async function run(options: {
}
const rng = new Rng(`${options.seed}/postgres`);
- const postgres = await seedPostgres(rng, options.archetypes);
+ const postgres = await seedPostgres(
+ rng,
+ options.archetypes,
+ options.timezone
+ );
console.log(
- `Postgres ready: organization ${postgres.organizationId}, ${postgres.projects.length} projects`
+ `Postgres ready: organization ${postgres.organizationId} (${options.timezone}), ${postgres.projects.length} projects`
);
if (options.reset) {
diff --git a/packages/seed/src/postgres.ts b/packages/seed/src/postgres.ts
index 5dc418d96..8ba464413 100644
--- a/packages/seed/src/postgres.ts
+++ b/packages/seed/src/postgres.ts
@@ -83,7 +83,10 @@ async function upsertUser(): Promise<string> {
return user.id;
}
-async function upsertOrganization(userId: string): Promise<string> {
+async function upsertOrganization(
+ userId: string,
+ timezone: string
+): Promise<string> {
const now = new Date();
const subscription = {
subscriptionStatus: 'active',
@@ -107,11 +110,16 @@ async function upsertOrganization(userId: string): Promise<string> {
id: SEED_ORGANIZATION.id,
name: SEED_ORGANIZATION.name,
createdByUserId: userId,
- timezone: 'UTC',
+ timezone,
onboarding: '',
...subscription,
},
- update: { name: SEED_ORGANIZATION.name, deleteAt: null, ...subscription },
+ update: {
+ name: SEED_ORGANIZATION.name,
+ timezone,
+ deleteAt: null,
+ ...subscription,
+ },
});
await db.member.upsert({
where: {
@@ -220,10 +228,11 @@ async function upsertProject(
export async function seedPostgres(
rng: Rng,
- archetypes: readonly Archetype[]
+ archetypes: readonly Archetype[],
+ timezone: string
): Promise<PostgresSeed> {
const userId = await upsertUser();
- const organizationId = await upsertOrganization(userId);
+ const organizationId = await upsertOrganization(userId, timezone);
const rootClient = await upsertClient(rng, {
name: 'Seed root client',
organizationId,
diff --git a/packages/seed/src/seed.integration.test.ts b/packages/seed/src/seed.integration.test.ts
index 8b648a749..9415c23e9 100644
--- a/packages/seed/src/seed.integration.test.ts
+++ b/packages/seed/src/seed.integration.test.ts
@@ -46,7 +46,7 @@ function inList(): string {
describe('seed against real databases', () => {
beforeAll(async () => {
await bootstrapTestDatabases();
- await seedPostgres(new Rng(`${SEED}/postgres`), ARCHETYPES);
+ await seedPostgres(new Rng(`${SEED}/postgres`), ARCHETYPES, 'UTC');
await resetProjects(PROJECT_IDS);
const sink = new ClickhouseSink();
await generate(