This commit is contained in:
@@ -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: '22 15 * * *'
|
||||
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
|
||||
@@ -30,6 +30,7 @@ COPY --from=builder /app/drizzle ./drizzle
|
||||
COPY --from=builder /app/scripts ./scripts
|
||||
COPY --from=builder /app/src/db ./src/db
|
||||
COPY --from=builder /app/src/lib ./src/lib
|
||||
COPY --from=builder /app/src/config ./src/config
|
||||
COPY --from=builder /app/drizzle.config.ts ./drizzle.config.ts
|
||||
COPY --from=builder /app/tsconfig.json ./tsconfig.json
|
||||
COPY --from=builder /app/seed/seed.example.json ./seed/seed.example.json
|
||||
|
||||
@@ -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": {
|
||||
|
||||
@@ -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,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');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
import { applyTimeOfDay, timesOfDay } from '@/config/time-of-day';
|
||||
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: applyTimeOfDay(new Date(), timesOfDay[Math.floor(Math.random() * timesOfDay.length)]),
|
||||
effort: Math.random() * 1.5 + 0.5,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await db.insert(execution).values(rows);
|
||||
return rows.length;
|
||||
};
|
||||
Reference in New Issue
Block a user