-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement a scheduler that automatically creates needed assignm…
…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
1 parent
49c28c0
commit 0a29393
Showing
20 changed files
with
1,318 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "task_group_assignment_table" RENAME TO "task_group_assignment"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.