import { parseLines, TaskBlock } from "../takker99%2Ftakker-scheduler/deps.ts"; import { startOfDay } from "../date-fns/startOfDay.ts"; import { addDays } from "../date-fns/addDays.ts"; import { hasRecord, split } from "./task.ts"; import { BaseLine } from "../scrapbox-jp%2Ftypes/userscript.ts"; export interface PersonalTimeStatus { todo: number; done: number; } export const getPersonalTimeStatus = (lines: BaseLine[], now: Date) => { const tasks: TaskBlock[] = []; for (let block of parseLines(lines)) { if (typeof block === "string") continue; // 見積もりできないタスクは除外 if (!block.plan.duration && !hasRecord(block)) continue; // 今日以降のタスクを切り出す const [, tail] = split(block, startOfDay(now)); if (!tail) continue; block = tail; // 今日までのタスクを切り出す const [head] = split(block, addDays(startOfDay(now), 1)); if (!head) continue; block = head; tasks.push(block); } // 完了したタスクの消費時間の合計 const done = tasks.reduce( (sum, task) => sum + (hasRecord(task) ? Math.floor( (task.record.end.getTime() - task.record.start.getTime()) / 1000 ) : 0 ), 0 ); // 未完了タスクの見積もり時間の合計 const todo = tasks.reduce( (sum, task) => sum + (hasRecord(task) ? 0 : (task.plan.duration ?? 0)), 0 ); return { done, todo }; };