Do things in the demo periocically
Build and Deploy / build-deploy (push) Successful in 1m37s

This commit is contained in:
2026-07-14 14:47:37 +01:00
parent 817d5eb7f1
commit 3c9d7e4e9d
5 changed files with 77 additions and 27 deletions
+28
View File
@@ -0,0 +1,28 @@
name: Demo random tasks
# Keeps the guest demo sandbox lively by logging random task activity on a schedule.
on:
schedule:
- cron: '53 * * * *'
workflow_dispatch:
concurrency:
group: demo-random-tasks-fizalgo
cancel-in-progress: false
jobs:
seed-activity:
runs-on: ubuntu-latest
container:
image: docker:latest
steps:
- name: Add random activity to the demo database
run: |
cid=$(docker ps -q \
-f label=com.docker.compose.project=fizalgo \
-f label=com.docker.compose.service=app)
if [ -z "$cid" ]; then
echo "fizalgo app container is not running; skipping" >&2
exit 1
fi
docker exec "$cid" bun scripts/demo-random-tasks.ts
+1
View File
@@ -11,6 +11,7 @@
"db:migrate": "bun scripts/migrate.ts",
"db:seed": "bun scripts/seed.ts",
"db:reset-demo": "bun scripts/reset-demo.ts",
"db:demo-activity": "bun scripts/demo-random-tasks.ts",
"db:create-user": "bun scripts/create-user.ts"
},
"dependencies": {
+14
View File
@@ -0,0 +1,14 @@
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import { demoUrl } from '../src/db/connection';
import * as schema from '../src/db/schema';
import { insertRandomExecutions } from '../src/lib/random-executions';
const sql = postgres(demoUrl, { max: 1 });
const db = drizzle(sql, { schema });
const count = await insertRandomExecutions(db);
await sql.end();
console.log(`demo: inserted ${count} random executions`);
process.exit(0);
+2 -27
View File
@@ -2,40 +2,15 @@
import { notFound, redirect } from 'next/navigation';
import { getRequestDb } from '@/db/request';
import { execution, person, task } from '@/db/schema';
import { getCurrentUser } from '@/lib/auth';
import { insertRandomExecutions } from '@/lib/random-executions';
export async function doThings() {
if (await getCurrentUser()) {
notFound();
}
const db = await getRequestDb();
const personMaxTimes = 6;
const taskMaxTimes = 4;
const people = await db.select().from(person);
const tasks = await db.select().from(task);
const rows: (typeof execution.$inferInsert)[] = [];
for (let i = 0; i <= Math.random() * personMaxTimes; i++) {
const randomPerson = people[Math.floor(Math.random() * people.length)];
for (let j = 0; j <= Math.random() * taskMaxTimes; j++) {
const randomTask = tasks[Math.floor(Math.random() * tasks.length)];
rows.push({
personId: randomPerson.id,
taskId: randomTask.id,
date: new Date(Date.now() - Math.random() * 24 * 60 * 60 * 1000),
effort: Math.random() * 1.5 + 0.5,
});
}
}
if (rows.length) {
await db.insert(execution).values(rows);
}
await insertRandomExecutions(await getRequestDb());
return redirect('/stats');
}
+32
View File
@@ -0,0 +1,32 @@
import type { Db } from '@/db';
import { execution, person, task } from '@/db/schema';
const personMaxTimes = 6;
const taskMaxTimes = 4;
export const insertRandomExecutions = async (db: Db): Promise<number> => {
const people = await db.select().from(person);
const tasks = await db.select().from(task);
if (!people.length || !tasks.length) {
return 0;
}
const rows: (typeof execution.$inferInsert)[] = [];
for (let i = 0; i <= Math.random() * personMaxTimes; i++) {
const randomPerson = people[Math.floor(Math.random() * people.length)];
for (let j = 0; j <= Math.random() * taskMaxTimes; j++) {
const randomTask = tasks[Math.floor(Math.random() * tasks.length)];
rows.push({
personId: randomPerson.id,
taskId: randomTask.id,
date: new Date(Date.now() - Math.random() * 24 * 60 * 60 * 1000),
effort: Math.random() * 1.5 + 0.5,
});
}
}
await db.insert(execution).values(rows);
return rows.length;
};