From 2faa4b1e8ead5b7b6d2d530a58118e85d9f4fd75 Mon Sep 17 00:00:00 2001 From: Tushar Inani Date: Mon, 9 Oct 2023 09:29:54 +0530 Subject: [PATCH] removed: js scheduler --- src/hooks/useScheduler.tsx | 10 +- src/scheduler/ImpSlotManager.ts | 74 -------- src/scheduler/TaskGenerator.ts | 153 --------------- src/scheduler/freeSlotsManager.ts | 47 ----- src/scheduler/index.ts | 179 ------------------ src/scheduler/miniScheduler.ts | 303 ------------------------------ 6 files changed, 2 insertions(+), 764 deletions(-) delete mode 100644 src/scheduler/ImpSlotManager.ts delete mode 100644 src/scheduler/TaskGenerator.ts delete mode 100644 src/scheduler/freeSlotsManager.ts delete mode 100644 src/scheduler/index.ts delete mode 100644 src/scheduler/miniScheduler.ts diff --git a/src/hooks/useScheduler.tsx b/src/hooks/useScheduler.tsx index bcb4e3d8a..f99c3b73a 100644 --- a/src/hooks/useScheduler.tsx +++ b/src/hooks/useScheduler.tsx @@ -8,7 +8,6 @@ import { TaskItem } from "@src/models/TaskItem"; import { GoalItem } from "@src/models/GoalItem"; import { ITaskOfDay } from "@src/Interfaces/Task"; import { getAllGoals } from "@src/api/GoalsAPI"; -import { callJsScheduler } from "@src/scheduler/miniScheduler"; import { ISchedulerOutput } from "@src/Interfaces/IScheduler"; import { resetProgressOfToday } from "@src/api/TasksAPI"; import { lastAction, openDevMode } from "@src/store"; @@ -67,13 +66,8 @@ function useScheduler() { await resetProgressOfToday(); const { generatedInputId, schedulerInput: schedulerInputV2 } = await generateSchedule(); newGeneratedInputId = generatedInputId; - if (devMode) { - res = callJsScheduler(schedulerInputV2); - logIO(JSON.stringify(schedulerInputV2), res); - } else { - await init(); - res = schedule(schedulerInputV2); - } + await init(); + res = schedule(schedulerInputV2); } putSchedulerRes(cachedRes.code, newGeneratedInputId, JSON.stringify(res)) .then(() => console.log("schedule saved")) diff --git a/src/scheduler/ImpSlotManager.ts b/src/scheduler/ImpSlotManager.ts deleted file mode 100644 index d68437c84..000000000 --- a/src/scheduler/ImpSlotManager.ts +++ /dev/null @@ -1,74 +0,0 @@ -/* eslint-disable no-continue */ -import { ISchedulerOutputSlot } from "@src/Interfaces/IScheduler"; -import { formatDate, getHrFromDateString, replaceHrInDateString } from "@src/utils/SchedulerUtils"; -import { - getBlockingSlotsOfTheDayForGoalId, - getDueHrs, - getUsedBlockers, - initBlockers, - pushToImpossible, - updateBlockers, -} from "."; - -export const createImpossibleSlot = ( - task: ISchedulerOutputSlot, - tmpStartDate: Date, - selectedDay: number, - start: number, - end: number, -) => { - const predictedDeadline = start + task.duration; - const actualDeadline = - task.deadline < predictedDeadline && task.deadline < end - ? task.deadline - : predictedDeadline > end - ? end > task.deadline - ? task.deadline - : end - : predictedDeadline; - - return { - ...task, - start: formatDate(tmpStartDate.getDate() + selectedDay - 1, start), - deadline: formatDate(tmpStartDate.getDate() + selectedDay - 1, actualDeadline), - duration: actualDeadline - start, - }; -}; - -export const fillUpImpSlotsForGoalId = (goalId: string, startDay: number, lastDay: number) => { - let dueHrs = getDueHrs(goalId); - // console.log("🚀 ~ file: ImpSlotManager.ts:23 ~ fillUpImpSlotsForGoalId ~ dueHrs:", dueHrs); - if (dueHrs) { - for (let dayItr = startDay; dayItr < lastDay && dueHrs !== 0; dayItr += 1) { - const pickSlots = getBlockingSlotsOfTheDayForGoalId(goalId, dayItr); - if (pickSlots.length > 0) { - for (let psIndex = 0; psIndex < pickSlots.length && dueHrs !== 0; psIndex += 1) { - const thisSlot = pickSlots[psIndex]; - const currentBlockers = getUsedBlockers(thisSlot.goalid); - if (currentBlockers) { - if (currentBlockers[dayItr]?.includes(psIndex)) { - continue; - } - updateBlockers(thisSlot.goalid, dayItr, [...(currentBlockers[dayItr] || []), psIndex]); - } else { - initBlockers(thisSlot.goalid, dayItr, psIndex); - } - if (thisSlot.duration > dueHrs) { - pushToImpossible(dayItr, { - ...thisSlot, - duration: dueHrs, - deadline: replaceHrInDateString( - thisSlot.deadline, - getHrFromDateString(thisSlot.start) + (thisSlot.duration - dueHrs), - ), - }); - dueHrs = 0; - } else { - pushToImpossible(dayItr, { ...thisSlot }); - dueHrs -= thisSlot.duration; - } - } - } - } - } -}; diff --git a/src/scheduler/TaskGenerator.ts b/src/scheduler/TaskGenerator.ts deleted file mode 100644 index d36d3c12f..000000000 --- a/src/scheduler/TaskGenerator.ts +++ /dev/null @@ -1,153 +0,0 @@ -/* eslint-disable camelcase */ -import { ISchedulerInputGoal } from "@src/Interfaces/IScheduler"; -import { calDays, getDiffInDates } from "@src/utils"; -import { convertDateToDay, goalSplitter } from "@src/utils/SchedulerUtils"; - -import { - addGoalDueHrs, - getBufferValue, - initImplSlotsOfGoalId, - pushTaskToFlexibleArr, - pushTaskToMyDays, - setWeekEndOfGoal, - updateBufferOfGoal, -} from "."; - -interface ISlot { - goalid: string; - title: string; - start: number; - deadline: number; -} - -const processBudgetGoal = ( - goal: ISchedulerInputGoal, - currSlot: ISlot, - validDays: string[], - inputDuration: number, - minDuration: number, - iGoalStart: Date, - startDayItr: number, -) => { - let goalStart = iGoalStart < new Date() ? new Date() : new Date(iGoalStart); - const { after_time = 0 } = goal.filters || {}; - const slot = { ...currSlot }; - let totalDuration = inputDuration - (goal.hoursSpent || 0); - const min = minDuration; - const startedOn = convertDateToDay(iGoalStart); - const deadline = goal.deadline ? new Date(goal.deadline) : null; - for (let i = startDayItr; i < 7; i += 1) { - if (deadline && deadline < goalStart) { - break; - } - const dayItr = convertDateToDay(goalStart); - if (validDays.includes(dayItr)) { - if (dayItr === startedOn && i !== 0) { - if (totalDuration >= 0) { - addGoalDueHrs(goal.id, totalDuration); - } - totalDuration = inputDuration; - setWeekEndOfGoal(goal.id, dayItr); - } - const slotD = min > totalDuration ? totalDuration : min; - pushTaskToMyDays(i + 1, { - ...slot, - duration: slotD, - }); - if (min > totalDuration) { - const currentBufferValue = getBufferValue(goal.id) || []; - if (!currentBufferValue) { - updateBufferOfGoal(goal.id, []); - } - - updateBufferOfGoal(goal.id, [ - ...currentBufferValue, - { nextBuffer: i + 1, availableBuffer: min - totalDuration }, - ]); - } - if (totalDuration > 0) { - totalDuration -= min > totalDuration ? totalDuration : min; - } - } - if (goalStart.toDateString() === new Date().toDateString()) { - slot.start = after_time; - } - goalStart = new Date(goalStart.setDate(goalStart.getDate() + 1)); - } -}; - -const goalProcessor = (goal: ISchedulerInputGoal, weekStart: Date, pushToNext: boolean) => { - initImplSlotsOfGoalId(goal.id); - const totalDuration = goal.min_duration || 0; - const { after_time = 0, before_time = 24, on_days = calDays } = goal.filters || {}; - const slot = { - goalid: goal.id, - title: goal.title, - start: after_time, - deadline: before_time, - }; - const createdAt = new Date(goal.createdAt); - let goalStart = new Date(goal.start || goal.createdAt); - - if ( - !pushToNext && - createdAt.toDateString() === weekStart.toDateString() && - goalStart.toDateString() === createdAt.toDateString() - ) { - if (after_time <= createdAt.getHours()) { - slot.start = createdAt.getHours() + 1; - if (slot.start === 24) { - goalStart = new Date(goalStart.setDate(goalStart.getDate() + 1)); - } - } - } - let validDays = [...on_days]; - const startingDay = (goalStart > weekStart ? getDiffInDates(goalStart, weekStart) : 0) + (pushToNext ? 1 : 0); - if (goalStart > weekStart && !pushToNext) { - const startDayIndex = on_days.indexOf(convertDateToDay(goalStart)); - const today = on_days.indexOf(convertDateToDay(weekStart)); - validDays = [ - ...on_days.slice(...(today > startDayIndex ? [startDayIndex, today] : [startDayIndex])), - ...(today <= startDayIndex ? on_days.slice(0, today) : []), - ]; - } - - // validDays = validDays.filter((ele) => !not_on.includes(ele)); - if (goal.repeat || goal.filters?.on_days) { - if (goal.repeat === "daily") { - const skipToday = totalDuration - (goal.hoursSpent || 0) === 0; - for (let key = startingDay; key < 7; key += 1) { - pushTaskToMyDays(key + 1, { - ...slot, - duration: totalDuration - (skipToday ? 0 : goal.hoursSpent || 0), - }); - slot.start = after_time; - } - } else if (!goal.budgets) { - const newSlot = { - ...slot, - start: after_time, - duration: totalDuration - (goal.hoursSpent || 0), - }; - pushTaskToFlexibleArr(validDays, newSlot); - } else { - processBudgetGoal(goal, slot, validDays, totalDuration, goal.budgets[0].min, goalStart, startingDay); - } - } else { - processBudgetGoal(goal, slot, validDays, totalDuration, totalDuration, goalStart, startingDay); - } -}; - -export const taskGenerator = (goals: { [id: string]: ISchedulerInputGoal }, weekStart: Date, weekEnd: Date) => { - for (let index = 0; index < Object.keys(goals).length; index += 1) { - const id = Object.keys(goals)[index]; - const goal = goals[id]; - if (new Date(goal.start || goal.createdAt) < weekEnd) { - const splittedGoals: ISchedulerInputGoal[] = goalSplitter(goal); - goalProcessor(splittedGoals[0], weekStart, false); - if (splittedGoals.length === 2) { - goalProcessor(splittedGoals[1], weekStart, true); - } - } - } -}; diff --git a/src/scheduler/freeSlotsManager.ts b/src/scheduler/freeSlotsManager.ts deleted file mode 100644 index cf0b33f39..000000000 --- a/src/scheduler/freeSlotsManager.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { getHrFromDateString, replaceHrInDateString } from "@src/utils/SchedulerUtils"; -import { IFinalOutputSlot } from "@src/Interfaces/IScheduler"; - -export const fillUpFreeSlots = (scheduleOfTheDay: IFinalOutputSlot[]) => { - let finalSchedule: IFinalOutputSlot[] = []; - let prev = 0; - let next = 0; - for (let j = 0; j < scheduleOfTheDay.length; j += 1) { - next = getHrFromDateString(scheduleOfTheDay[j].start); - if (next !== prev) { - const freeStart = `${scheduleOfTheDay[j].start}`; - finalSchedule = [ - ...finalSchedule, - { - goalid: "free", - title: "free", - duration: next - prev, - deadline: replaceHrInDateString(freeStart, next), - start: replaceHrInDateString(freeStart, prev), - }, - ]; - } - finalSchedule.push({ - ...scheduleOfTheDay[j], - }); - prev = getHrFromDateString(scheduleOfTheDay[j].deadline); - } - if (finalSchedule.length > 0) { - const last = finalSchedule.slice(-1)[0]; - const deadline = getHrFromDateString(last.deadline); - if (deadline !== 24) { - finalSchedule.push({ - duration: 24 - deadline, - goalid: "free", - start: last.deadline, - deadline: replaceHrInDateString(last.deadline, 0), - title: "free", - }); - } else { - finalSchedule[finalSchedule.length - 1] = { - ...last, - deadline: replaceHrInDateString(last.deadline, 0), - }; - } - } - return finalSchedule; -}; diff --git a/src/scheduler/index.ts b/src/scheduler/index.ts deleted file mode 100644 index 21100247e..000000000 --- a/src/scheduler/index.ts +++ /dev/null @@ -1,179 +0,0 @@ -/* eslint-disable no-continue */ -/* eslint-disable no-var */ -import { IFinalOutputSlot, ISchedulerOutput, ISchedulerOutputSlot, TBufferValue } from "@src/Interfaces/IScheduler"; -import { formatDate, getHrFromDateString, replaceHrInDateString } from "@src/utils/SchedulerUtils"; - -var flexibleWeeklyGoals: { - slot: ISchedulerOutputSlot; - validDays: string[]; -}[] = []; -var dueTaskHrs: { [goalId: string]: number } = {}; -var buffer: { [goalId: string]: TBufferValue[] } = {}; -var myDays: ISchedulerOutputSlot[][] = [[], [], [], [], [], [], [], []]; -var { scheduled, impossible }: ISchedulerOutput = { scheduled: [], impossible: [] }; - -const blockingSlots: { [goalId: string]: IFinalOutputSlot[][] } = {}; -const usedBlockers: { [id: string]: { [day: number]: number[] } } = {}; -const goalWeekEnd: { [id: string]: string } = {}; - -export const initImplSlotsOfGoalId = (goalId: string) => { - blockingSlots[goalId] = [[], [], [], [], [], [], [], []]; -}; - -export const getWeekEndOfGoal = (goalId: string) => goalWeekEnd[goalId]; - -export const setWeekEndOfGoal = (goalId: string, value: string) => { - goalWeekEnd[goalId] = value; -}; - -export const pushTaskToMyDays = (selectedDay: number, slot: ISchedulerOutputSlot) => { - myDays[selectedDay] = [...myDays[selectedDay], { ...slot }]; -}; - -export const pushTaskToFlexibleArr = (validDays: string[], slot: ISchedulerOutputSlot) => { - flexibleWeeklyGoals.push({ - slot: { ...slot }, - validDays: [...validDays], - }); -}; - -export const addGoalDueHrs = (goalId: string, hrs: number) => { - dueTaskHrs[goalId] = hrs + (dueTaskHrs[goalId] || 0); -}; - -export const updateDueHrs = (goalId: string, hrs: number) => { - dueTaskHrs[goalId] = hrs; -}; - -export const updateBufferOfGoal = (goalId: string, value: TBufferValue[]) => { - buffer[goalId] = [...value]; -}; - -export const updateBufferOfTheDay = (goalId: string, day: number, value: TBufferValue) => { - buffer[goalId][day] = { ...value }; -}; - -export const getBufferValue = (goalId: string) => buffer[goalId]; - -export const getBlockingSlotsOfTheDayForGoalId = (goalId: string, day: number) => blockingSlots[goalId][day]; - -export const getAllDueTasks = () => ({ ...dueTaskHrs }); - -export const getDueHrs = (goalId: string) => dueTaskHrs[goalId]; - -export const getUsedBlockers = (goalId: string) => usedBlockers[goalId]; - -export const initBlockers = (goalId: string, dayItr: number, value: number) => { - usedBlockers[goalId] = { [dayItr]: [value] }; -}; -export const updateBlockers = (goalId: string, day: number, value: number[]) => { - usedBlockers[goalId][day] = [...value]; -}; - -export const pushToImpossible = (day: number, slot: IFinalOutputSlot) => { - impossible[day].tasks.push(slot); -}; - -export const createImpossibleSlot = ( - task: ISchedulerOutputSlot, - tmpStartDate: Date, - selectedDay: number, - start: number, - end: number, -) => { - const predictedDeadline = start + task.duration; - const actualDeadline = - task.deadline < predictedDeadline && task.deadline < end - ? task.deadline - : predictedDeadline > end - ? end > task.deadline - ? task.deadline - : end - : predictedDeadline; - - return { - ...task, - start: formatDate(tmpStartDate.getDate() + selectedDay - 1, start), - deadline: formatDate(tmpStartDate.getDate() + selectedDay - 1, actualDeadline), - duration: actualDeadline - start, - }; -}; - -export const generateAndPushImpSlot = ( - task: ISchedulerOutputSlot, - tmpStartDate: Date, - selectedDay: number, - start: number, - end: number, -) => { - const impSlot = createImpossibleSlot(task, tmpStartDate, selectedDay, start, end); - blockingSlots[task.goalid][selectedDay].push(impSlot); -}; - -export const fillUpImpSlotsForGoalId = (goalId: string, lastDay: number) => { - let dueHrs = getDueHrs(goalId); - if (dueHrs) { - for (let dayItr = 1; dayItr < lastDay && dueHrs !== 0; dayItr += 1) { - const pickSlots = getBlockingSlotsOfTheDayForGoalId(goalId, dayItr); - if (pickSlots.length > 0) { - for (let psIndex = 0; psIndex < pickSlots.length && dueHrs !== 0; psIndex += 1) { - const thisSlot = pickSlots[psIndex]; - const currentBlockers = getUsedBlockers(thisSlot.goalid); - if (currentBlockers) { - if (currentBlockers[dayItr]?.includes(psIndex)) { - // if (thisSlot.goalid === "6debddbd-6558-4a1f-9884-1b3467b887cb") { console.log("exists", thisSlot); } - continue; - } - updateBlockers(thisSlot.goalid, dayItr, [...(currentBlockers[dayItr] || []), psIndex]); - } else { - initBlockers(thisSlot.goalid, dayItr, psIndex); - } - // if (thisSlot.goalid === "53a8e2e5-b659-4f1f-8b44-b394158dc49d") { console.log("🚀 ~ file: MiniScheduler.ts:262 ~ thisSlot:", thisSlot, dueHrs, getHrFromDateString(thisSlot.start), (thisSlot.duration - dueHrs)); } - if (thisSlot.duration > dueHrs) { - pushToImpossible(dayItr, { - ...thisSlot, - duration: dueHrs, - deadline: replaceHrInDateString( - thisSlot.deadline, - getHrFromDateString(thisSlot.start) + (thisSlot.duration - dueHrs), - ), - }); - dueHrs = 0; - } else { - pushToImpossible(dayItr, { ...thisSlot }); - dueHrs -= thisSlot.duration; - } - } - } - } - } -}; - -export const initScheduled = (day: number) => { - scheduled[day] = { day: `${day}`, tasks: [] }; -}; - -export const initImpossible = (day: number) => { - impossible[day] = { day: `${day}`, tasks: [] }; -}; - -export const pushToScheduled = (selectedDay: number, task: IFinalOutputSlot) => { - scheduled[selectedDay].tasks.push({ ...task }); -}; - -export const getFlexibleWeeklyGoals = () => [...flexibleWeeklyGoals]; - -export const getTasksOfDay = (day: number) => [...myDays[day]]; - -export const getScheduledObj = () => scheduled; - -export const getImpossibleObj = () => impossible; - -export const resetAll = () => { - dueTaskHrs = {}; - myDays = [[], [], [], [], [], [], [], []]; - buffer = {}; - scheduled = []; - impossible = []; - flexibleWeeklyGoals = []; -}; diff --git a/src/scheduler/miniScheduler.ts b/src/scheduler/miniScheduler.ts deleted file mode 100644 index 3c8b0dbf5..000000000 --- a/src/scheduler/miniScheduler.ts +++ /dev/null @@ -1,303 +0,0 @@ -/* eslint-disable no-continue */ -import { v4 as uuidv4 } from "uuid"; -import { ISchedulerOutputSlot, ISchedulerInputGoal, IFinalOutputSlot } from "@src/Interfaces/IScheduler"; -import { formatDate, breakTheTree, convertDateToDay } from "@src/utils/SchedulerUtils"; -import { fillUpImpSlotsForGoalId } from "./ImpSlotManager"; -import { taskGenerator } from "./TaskGenerator"; -import { - addGoalDueHrs, - generateAndPushImpSlot, - getAllDueTasks, - getBufferValue, - getDueHrs, - getFlexibleWeeklyGoals, - getImpossibleObj, - getScheduledObj, - getTasksOfDay, - getWeekEndOfGoal, - initImpossible, - initScheduled, - pushToScheduled, - resetAll, - updateBufferOfGoal, - updateBufferOfTheDay, - updateDueHrs, -} from "."; -import { fillUpFreeSlots } from "./freeSlotsManager"; - -let impossibleHandled: { [key: string]: boolean } = {}; -let soloGoals: { [x: string]: ISchedulerInputGoal } = {}; - -const taskScheduler = (taskObj: ISchedulerOutputSlot, selectedDay: number, tmpStart: Date, currentHrs: number[]) => { - const defaultHrs = [...currentHrs]; - const { start, deadline, duration, ...task } = taskObj; - // if(task.title === "Report") - // console.log(task.title, duration, defaultHrs); - if (duration !== 0) { - let startHrFound = true; - let startHr = start; - const { skippedToday = [] } = soloGoals[task.goalid]; - let [badStart, badEnd] = (skippedToday.length ? skippedToday[0] : "25-25").split("-").map((ele) => Number(ele)); - if (badStart === 25) { - badStart = -1; - badEnd = -1; - } - let isThisBadPoint = selectedDay === 1 && badStart !== -1 && startHr >= badStart; - - while (defaultHrs[startHr] !== -1 || isThisBadPoint) { - if (startHr > 23 || startHr >= deadline) { - startHrFound = false; - break; - } - if (isThisBadPoint) { - generateAndPushImpSlot({ ...taskObj, duration }, tmpStart, selectedDay, startHr, badEnd); - startHr = badEnd; - skippedToday.shift(); - if (skippedToday.length > 0) { - [badStart, badEnd] = skippedToday[0].split("-").map((ele) => Number(ele)); - } else { - badStart = -1; - badEnd = -1; - } - } else { - generateAndPushImpSlot({ ...taskObj, duration }, tmpStart, selectedDay, startHr, defaultHrs[startHr]); - } - if (defaultHrs[startHr] === -1) { - break; - } - startHr = defaultHrs[startHr]; - isThisBadPoint = selectedDay === 1 && badStart !== -1 && startHr >= badStart; - } - let tmpD = duration; - if (startHrFound) { - let tmpS = startHr; - let ptr = startHr; - while (ptr <= deadline && tmpS !== deadline) { - tmpD -= 1; - ptr += 1; - isThisBadPoint = selectedDay === 1 && badStart !== -1 && ptr >= badStart; - if (defaultHrs[ptr] !== -1 || tmpD === 0 || ptr === deadline || isThisBadPoint) { - const nextAvailable = ptr; - defaultHrs.splice(tmpS, ptr - tmpS, ...[...Array(ptr - tmpS).keys()].map(() => nextAvailable)); - pushToScheduled(selectedDay, { - ...task, - start: formatDate(tmpStart.getDate() + selectedDay - 1, tmpS), - deadline: formatDate(tmpStart.getDate() + selectedDay - 1, ptr), - duration: ptr - tmpS, - }); - while (defaultHrs[ptr] !== -1) { - if (ptr > 23) { - break; - } - if (isThisBadPoint) { - generateAndPushImpSlot({ ...taskObj, duration: tmpD }, tmpStart, selectedDay, ptr, badEnd); - ptr = badEnd; - skippedToday.shift(); - if (skippedToday.length > 0) { - [badStart, badEnd] = skippedToday[0].split("-").map((ele) => Number(ele)); - } else { - badStart = -1; - badEnd = -1; - } - } else { - generateAndPushImpSlot({ ...taskObj, duration: tmpD }, tmpStart, selectedDay, ptr, defaultHrs[ptr]); - } - if (defaultHrs[ptr] === -1) { - break; - } - // if (taskObj.title.includes("project")) console.log(ptr, defaultHrs[ptr], defaultHrs); - ptr = defaultHrs[ptr]; - isThisBadPoint = selectedDay === 1 && badStart !== -1 && ptr >= badStart; - } - if (ptr > 23) { - break; - } - tmpS = ptr; - if (tmpD === 0) { - break; - } - } - } - } - if (tmpD !== 0) { - // if (["project a", "work", "Report"].includes(task.title)) { console.log("Incomplete Duration", task.title, tmpD); } - addGoalDueHrs(task.goalid, tmpD); - } - } - return defaultHrs; -}; - -const operator = (item: number, tmpStart: Date, defaultHrs: number[]) => { - let currentHrs = [...defaultHrs]; - const selectedDay = item + 1; - const arr = getTasksOfDay(selectedDay); - arr.sort((a, b) => - a.deadline - a.start === b.deadline - b.start - ? a.duration === b.duration - ? a.start === b.start - ? a.deadline - b.deadline - : a.start - b.start - : a.duration - b.duration - : a.deadline - a.start - (b.deadline - b.start), - ); - for (let arrItr = 0; arrItr < arr.length; arrItr += 1) { - // console.log(""); - const { goalid } = arr[arrItr]; - let { duration } = arr[arrItr]; - const goalHabit = soloGoals[goalid].repeat; - const today = new Date(new Date(tmpStart).setDate(tmpStart.getDate() + (selectedDay - 1))); - // if (arr[arrItr].title === "work") console.log(duration, getDueHrs(goalid)); - if (goalHabit === "weekly" && getWeekEndOfGoal(goalid) === convertDateToDay(today) && !impossibleHandled[goalid]) { - fillUpImpSlotsForGoalId(goalid, 1, selectedDay); - updateDueHrs(goalid, 0); - impossibleHandled[goalid] = true; - } - // if (["project a", "work"].includes(title)) console.log("🚀 ~ file: MiniScheduler.ts:182 ~ operator ~ arrItr:", arrItr); - // if (["project a", "work"].includes(title)) { console.log(title, duration, dueTaskHrs[goalid]); } - let pastDue = getDueHrs(goalid); - if (pastDue && pastDue > 0 && goalHabit !== "daily") { - // console.log("due exist"); - const currentBuffer = getBufferValue(goalid); - if (currentBuffer && currentBuffer.length > 0) { - // console.log("found buffer", buffer[goalid][0]); - if (currentBuffer[0].nextBuffer === selectedDay) { - const bufferForToday = currentBuffer[0].availableBuffer; - if (pastDue >= bufferForToday) { - duration += bufferForToday; - updateDueHrs(goalid, pastDue - bufferForToday); - } else { - duration += pastDue; - updateDueHrs(goalid, 0); - updateBufferOfTheDay(goalid, 0, { ...currentBuffer[0], availableBuffer: bufferForToday - pastDue }); - } - } - } - } - const currentBuffer = getBufferValue(goalid); - // if (["project a", "work"].includes(title)) { console.log("after adding due", title, duration, dueTaskHrs[goalid]); } - if (currentBuffer && currentBuffer.length > 0 && currentBuffer[0].nextBuffer === selectedDay) { - updateBufferOfGoal(goalid, [...currentBuffer.slice(1)]); - } - - currentHrs = [...taskScheduler({ ...arr[arrItr], duration }, selectedDay, tmpStart, currentHrs)]; - pastDue = getDueHrs(goalid); - if (soloGoals[goalid].repeat === "daily") { - // if (soloGoals[goalid].title === "Report") console.log(soloGoals[goalid].title, pastDue, selectedDay, currentHrs); - fillUpImpSlotsForGoalId(goalid, selectedDay, selectedDay + 1); - updateDueHrs(goalid, 0); - impossibleHandled[goalid] = true; - } - } - return currentHrs; -}; - -export const callJsScheduler = (inputObj: { - startDate: string; - endDate: string; - goals: { [id: string]: ISchedulerInputGoal }; -}) => { - resetAll(); - impossibleHandled = {}; - const { startDate, endDate, goals } = inputObj; - const tmpStart = new Date(startDate); - const tmpEnd = new Date(endDate); - soloGoals = breakTheTree(goals); - taskGenerator(soloGoals, tmpStart, tmpEnd); - // console.log(getBufferValue("2c33e46b-625d-45b1-9946-0141a3df9392")); - for (let ele = 0; ele < 7; ele += 1) { - initScheduled(ele + 1); - initImpossible(ele + 1); - } - - let tmpDate = new Date(tmpStart); - for (let item = 0; item < 7; item += 1) { - let hrsOfDay = [...Array(24).keys()].map(() => -1); - hrsOfDay = [...operator(item, tmpStart, hrsOfDay)]; - const flexibleWeeklyGoals = getFlexibleWeeklyGoals(); - // console.log("🚀 ~ file: miniScheduler.ts:157 ~ flexibleWeeklyGoals:", flexibleWeeklyGoals) - - for (let wgi = 0; wgi < flexibleWeeklyGoals.length; wgi += 1) { - const createdOn = convertDateToDay(new Date(soloGoals[flexibleWeeklyGoals[wgi].slot.goalid].createdAt)); - const goalHabit = soloGoals[flexibleWeeklyGoals[wgi].slot.goalid].repeat; - const { deadline: actualGoalDeadline } = goals[flexibleWeeklyGoals[wgi].slot.goalid]; - if (actualGoalDeadline && new Date(actualGoalDeadline) < tmpDate) { - continue; - } - if (flexibleWeeklyGoals[wgi].validDays.includes(convertDateToDay(tmpDate))) { - const task = { ...flexibleWeeklyGoals[wgi].slot }; - const pastDue = getDueHrs(task.goalid); - if (pastDue === 0 && (createdOn !== convertDateToDay(tmpDate) || goalHabit === "once")) { - continue; - } - const dueDuration = pastDue || task.duration; - updateDueHrs(task.goalid, 0); - if (task.duration !== 0) { - const createdAt = new Date(soloGoals[flexibleWeeklyGoals[wgi].slot.goalid].createdAt); - let { start } = task; - if (createdAt.toDateString() === tmpDate.toDateString()) { - if (task.start <= createdAt.getHours()) { - if (createdAt.getHours() + 1 === 24) { - continue; - } else start = createdAt.getHours() + 1; - } - } - hrsOfDay = [...taskScheduler({ ...task, duration: dueDuration, start }, item + 1, tmpStart, hrsOfDay)]; - flexibleWeeklyGoals[wgi] = { ...flexibleWeeklyGoals[wgi], slot: { ...task, duration: dueDuration } }; - } - } - } - tmpDate = new Date(tmpDate.setDate(tmpDate.getDate() + 1)); - } - - const dueTasks = Object.keys(getAllDueTasks()); - // console.log("🚀 ~ file: MiniScheduler.ts:320 ~ dueTaskHrs:", dueTaskHrs); - // console.log("🚀 ~ file: MiniScheduler.ts:251 ~ dueTasks:", blockingSlots); - for (let dti = 0; dti < dueTasks.length; dti += 1) { - const dueGoalId = dueTasks[dti]; - if (!impossibleHandled[dueGoalId]) { - fillUpImpSlotsForGoalId(dueGoalId, 1, 8); - } - } - const resSchedule: { day: string; tasks: IFinalOutputSlot[] }[] = []; - const resImpossible: { day: string; tasks: IFinalOutputSlot[] }[] = []; - const scheduled = getScheduledObj(); - const impossible = getImpossibleObj(); - for (let item = 0; item < 7; item += 1) { - const thisDate = new Date(tmpStart.setDate(tmpStart.getDate() + 1)); - scheduled[item + 1].tasks.sort((a, b) => { - const s1 = Number(a.start.slice(11, 13)); - const s2 = Number(b.start.slice(11, 13)); - const e1 = Number(a.start.slice(14, 16)); - const e2 = Number(b.start.slice(14, 16)); - if (s1 === s2) { - return e1 - e2; - } - return s1 - s2; - }); - impossible[item + 1].tasks.sort((a, b) => { - const s1 = Number(a.start.slice(11, 13)); - const s2 = Number(b.start.slice(11, 13)); - const e1 = Number(a.start.slice(14, 16)); - const e2 = Number(b.start.slice(14, 16)); - if (s1 === s2) { - return e1 - e2; - } - return s1 - s2; - }); - resSchedule.push({ - day: thisDate.toISOString().slice(0, 10), - tasks: fillUpFreeSlots(scheduled[item + 1].tasks).map((ele) => ({ - ...ele, - taskid: uuidv4(), - })), - }); - resImpossible.push({ - day: thisDate.toISOString().slice(0, 10), - tasks: [...impossible[item + 1].tasks.map((ele) => ({ ...ele, taskid: uuidv4() }))], - }); - } - return { - scheduled: resSchedule, - impossible: resImpossible, - }; -};