Skip to content

Commit

Permalink
feat: implement a scheduler that automatically creates needed assignm…
Browse files Browse the repository at this point in the history
…ents (#26)

* feat: add taskGroupAssignment table, rename userTaskGroup table

* feat: add taskGroupAssignment table, rename userTaskGroup table

* feat: db functions for getting task groups that need assignments and getting taskgroupusers

* wip

* fix: unused table referenced

* feat: add task scheduling logic

* feat: create assignments as needed

* fix: refetch task groups after task group creation

* feat: remove task_group_assignment from schema

* feat: scheduling logic

* chore: formatting

* refactor: move getting current assignemnts for taskgroup into function

* refactor: use map, remove unnecessary variable

* fix: ignore time in timestamp comparison

* feat: add env to eas.json

* docs: add build command to readme

* feat: decrease cron job interval

---------

Co-authored-by: julian-wasmeier-titanom <[email protected]>
  • Loading branch information
invertedEcho and julian-wasmeier-titanom authored May 21, 2024
1 parent 49c28c0 commit 0a29393
Show file tree
Hide file tree
Showing 20 changed files with 1,318 additions and 15 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# wg-app WIP

## User flow

https://excalidraw.com/#room=2a90cf068f4d87bce613,24I3dxykUBivS35QOdgTqw

## setup
Expand Down Expand Up @@ -59,3 +60,7 @@ The user flow should be the following
- all tasks in the group will be assigned to the same user in one period

This means we will remove the frequency column from the task table and add it to the task_group table.

## How to build apk

- `eas build --profile production --platform android --local`
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/jwt": "^10.2.0",
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.2",
"bcrypt": "^5.1.1",
"drizzle-orm": "^0.30.9",
"passport": "^0.7.0",
Expand Down
36 changes: 36 additions & 0 deletions backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { AuthController } from './auth/auth.controller';
import { APP_GUARD } from '@nestjs/core';
import { JwtAuthGuard } from './auth/jwt-auth.guard';
import { TaskGroupController } from './task-group.controller';
import { ScheduleModule } from '@nestjs/schedule';
import { AssignmentsModule } from './assignment.module';

@Module({
imports: [AuthModule],
imports: [AuthModule, ScheduleModule.forRoot(), AssignmentsModule],
controllers: [
TasksController,
AssignmentController,
Expand Down
84 changes: 84 additions & 0 deletions backend/src/assignment-scheduler.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { db } from './db';
import {
dbGetAssignmentsForTaskGroup,
dbGetCurrentAssignmentsForTaskGroup,
dbGetTasksToAssignForCurrentInterval,
} from './db/functions/assignment';
import { dbGetTaskGroupUsers } from './db/functions/task-group';
import { assignmentTable } from './db/schema';
import { randomFromArray } from './utils/array';

@Injectable()
export class AssignmentSchedulerService {
@Cron(CronExpression.EVERY_30_SECONDS)
async handleCron() {
const tasksToCreateAssignmentsFor =
await dbGetTasksToAssignForCurrentInterval();

console.debug(
'Running task scheduling cron job for',
tasksToCreateAssignmentsFor,
);

const tasksByGroup = tasksToCreateAssignmentsFor.reduce<
Map<number, number[]>
>((acc, curr) => {
if (!acc.get(curr.taskGroupId)) {
acc.set(curr.taskGroupId, []);
}
acc.get(curr.taskGroupId)?.push(curr.taskId);
return acc;
}, new Map());

for (const [taskGroupId, taskIds] of tasksByGroup) {
const userIds = await dbGetTaskGroupUsers(taskGroupId);
if (userIds.length === 0) {
continue;
}

const nextResponsibleUserId = await getNextResponsibleUserId(
taskGroupId,
userIds.map(({ userId }) => userId),
);

await db
.insert(assignmentTable)
.values(
taskIds.map((taskId) => ({ taskId, userId: nextResponsibleUserId })),
);
}
}
}

async function getNextResponsibleUserId(
taskGroupId: number,
userIds: number[],
) {
const currentAssignments =
await dbGetCurrentAssignmentsForTaskGroup(taskGroupId);

/* If there already are current assignments, return the userId of one of the current assignments
(It doesn't matter which one, they should all be assigned to the same user) */
if (currentAssignments.length != 0) {
return currentAssignments[0].assignment.userId;
}

const lastAssignments = await dbGetAssignmentsForTaskGroup(
taskGroupId,
userIds.length,
);
const userIdsWithoutAnyAssignments = userIds.filter(
(userId) =>
!lastAssignments.some(({ assignment }) => assignment.userId === userId),
);

/* If all users were already assigned the task, the next responsible user is the one who was assigned the task the longest ago.
Otherwise it is randomly chosen from the users that were not assigned the task yet */
if (userIdsWithoutAnyAssignments.length === 0) {
return lastAssignments[lastAssignments.length - 1].assignment.userId;
} else {
return randomFromArray(userIdsWithoutAnyAssignments);
}
}
7 changes: 7 additions & 0 deletions backend/src/assignment.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { AssignmentSchedulerService } from './assignment-scheduler.service';

@Module({
providers: [AssignmentSchedulerService],
})
export class AssignmentsModule {}
59 changes: 58 additions & 1 deletion backend/src/db/functions/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { db } from '..';
import {
AssignmentState,
assignmentTable,
taskGroupTable,
taskTable,
userTable,
} from '../schema';
import { eq } from 'drizzle-orm';
import { count, desc, eq, or, sql } from 'drizzle-orm';

export async function dbGetAllAssignments(): Promise<AssignmentResponse[]> {
try {
Expand Down Expand Up @@ -47,3 +48,59 @@ export async function dbChangeAssignmentState(
throw error;
}
}

export async function dbGetAssignmentsForTaskGroup(
taskGroupId: number,
limit?: number,
) {
const result = db
.select({ assignment: { ...assignmentTable } })
.from(taskGroupTable)
.innerJoin(taskTable, eq(taskGroupTable.id, taskTable.taskGroupId))
.innerJoin(assignmentTable, eq(taskTable.id, assignmentTable.taskId))
.where(eq(taskGroupTable.id, taskGroupId))
.orderBy(desc(assignmentTable.createdAt));
if (limit === undefined) {
return await result;
}
return await result.limit(limit);
}

export async function dbGetCurrentAssignmentsForTaskGroup(taskGroupId: number) {
const currentAssignments = await db
.select()
.from(assignmentTable)
.innerJoin(taskTable, eq(taskTable.id, assignmentTable.taskId))
.innerJoin(taskGroupTable, eq(taskGroupTable.id, taskTable.taskGroupId))
.where(
sql`${assignmentTable.createdAt}::date >= NOW()::date - ${taskGroupTable.interval} AND ${taskGroupTable.id} = ${taskGroupId}`,
);

return currentAssignments;
}

export async function dbGetTasksToAssignForCurrentInterval() {
try {
// Get all tasks that either have no assignments yet or don't have an assignment in the current period
const taskIdsToCreateAssignmentsFor = await db
.select({
taskId: taskTable.id,
taskGroupId: taskGroupTable.id,
})
.from(taskGroupTable)
.innerJoin(taskTable, eq(taskGroupTable.id, taskTable.taskGroupId))
.leftJoin(assignmentTable, eq(taskTable.id, assignmentTable.taskId))
.groupBy(taskGroupTable.id, taskTable.id)
.having(
or(
eq(count(assignmentTable.id), 0),
sql`MAX(${assignmentTable.createdAt})::date <= (NOW()::date - ${taskGroupTable.interval})`,
),
);

return taskIdsToCreateAssignmentsFor;
} catch (error) {
console.error({ error });
throw error;
}
}
32 changes: 30 additions & 2 deletions backend/src/db/functions/task-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { eq } from 'drizzle-orm';
import { CreateTaskGroup } from 'src/task-group.controller';
import { db } from '..';
import { taskGroupTable, userTaskGroupTable } from '../schema';
import {
taskGroupTable,
taskGroupUserTable,
taskTable,
userTable,
} from '../schema';

export async function dbGetTaskGroups() {
return await db.select().from(taskGroupTable);
Expand All @@ -26,7 +32,7 @@ export async function dbCreateTaskGroup({

const { taskGroupId } = res[0];

await db.insert(userTaskGroupTable).values(
await db.insert(taskGroupUserTable).values(
userIds.map((userId) => ({
taskGroupId,
userId,
Expand All @@ -37,3 +43,25 @@ export async function dbCreateTaskGroup({
throw error;
}
}

export async function dbGetTaskGroupUsers(taskGroupId: number) {
try {
const taskGroupUsers = await db
.select({ userId: userTable.id })
.from(taskGroupUserTable)
.innerJoin(userTable, eq(taskGroupUserTable.userId, userTable.id))
.where(eq(taskGroupUserTable.taskGroupId, taskGroupId));

return taskGroupUsers;
} catch (error) {
console.error({ error });
throw error;
}
}

export async function dbGetTasksOfTaskGroup(taskGroupId: number) {
return await db
.select()
.from(taskTable)
.where(eq(taskTable.taskGroupId, taskGroupId));
}
19 changes: 19 additions & 0 deletions backend/src/db/migrations/0004_flippant_black_bird.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CREATE TABLE IF NOT EXISTS "task_group_assignment_table" (
"id" serial PRIMARY KEY NOT NULL,
"task_group_id" integer NOT NULL,
"user_id" integer NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "user_task_group" RENAME TO "task_group_user";--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_task_group_id_task_group_id_fk" FOREIGN KEY ("task_group_id") REFERENCES "public"."task_group"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "task_group_assignment_table" ADD CONSTRAINT "task_group_assignment_table_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
1 change: 1 addition & 0 deletions backend/src/db/migrations/0005_new_ultimatum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "task_group_assignment_table" RENAME TO "task_group_assignment";
2 changes: 2 additions & 0 deletions backend/src/db/migrations/0006_tough_pandemic.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE "task_group_assignment";--> statement-breakpoint
ALTER TABLE "assignment" ALTER COLUMN "state" SET NOT NULL;
Loading

0 comments on commit 0a29393

Please sign in to comment.