-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproject-summary.ts
More file actions
222 lines (206 loc) · 7.33 KB
/
Copy pathproject-summary.ts
File metadata and controls
222 lines (206 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { buildProjectStructure } from "./project-structure.js";
type ItemType = "to-do" | "project" | "heading";
type ItemStatus = "incomplete" | "canceled" | "completed";
type StartBucket = "Inbox" | "Anytime" | "Someday" | "";
type ChecklistItem = {
id: string;
title: string;
status: ItemStatus;
stopDate: string | null;
};
type TaskLike = {
id: string;
type: ItemType;
title: string;
status: ItemStatus;
trashed: boolean;
notes: string;
start: StartBucket;
startDate: string | null;
deadline: string | null;
deadlineSuppressed: boolean;
stopDate: string | null;
created: string | null;
modified: string | null;
areaId: string | null;
areaTitle: string | null;
projectId: string | null;
projectTitle: string | null;
headingId: string | null;
headingTitle: string | null;
tags: string[];
checklist: ChecklistItem[];
index: number;
todayIndex: number;
};
function todayDateOnly() {
return new Date().toISOString().slice(0, 10);
}
function headingStatsFor(structure: ReturnType<typeof buildProjectStructure>) {
return structure.headings.map((heading) => {
const headingTodos = structure.todos.filter((todo) => todo.headingId === heading.id);
const incomplete = headingTodos.filter((todo) => todo.status === "incomplete").length;
const completed = headingTodos.filter((todo) => todo.status === "completed").length;
const canceled = headingTodos.filter((todo) => todo.status === "canceled").length;
return {
headingId: heading.id,
headingTitle: heading.title,
todoCount: headingTodos.length,
incomplete,
completed,
canceled,
};
});
}
function upcomingTasks(todos: TaskLike[], today: string) {
return todos
.filter(
(todo) =>
todo.status === "incomplete" &&
((todo.deadline !== null && todo.deadline >= today) ||
(todo.startDate !== null && todo.startDate >= today))
)
.sort((a, b) => (a.deadline ?? a.startDate ?? "").localeCompare(b.deadline ?? b.startDate ?? ""))
.slice(0, 5)
.map((todo) => ({
id: todo.id,
title: todo.title,
headingTitle: todo.headingTitle,
startDate: todo.startDate,
deadline: todo.deadline,
}));
}
function overdueTasks(todos: TaskLike[], today: string) {
return todos
.filter(
(todo) =>
todo.status === "incomplete" &&
todo.deadline !== null &&
todo.deadline < today &&
!todo.deadlineSuppressed
)
.slice(0, 5)
.map((todo) => ({
id: todo.id,
title: todo.title,
headingTitle: todo.headingTitle,
deadline: todo.deadline,
}));
}
function inferPlanningPriority(input: {
overdueCount: number;
withoutHeadingCount: number;
emptyHeadingCount: number;
incompleteCount: number;
}) {
if (input.overdueCount > 0 || input.withoutHeadingCount >= 3) return "high";
if (input.withoutHeadingCount > 0 || input.emptyHeadingCount > 0 || input.incompleteCount >= 8) {
return "medium";
}
return "low";
}
export function summarizeProject(tasks: TaskLike[], projectUuid: string) {
const today = todayDateOnly();
const structure = buildProjectStructure(tasks, projectUuid);
const headingDistribution = headingStatsFor(structure);
const sortedByLoad = [...headingDistribution].sort(
(a, b) => b.todoCount - a.todoCount || a.headingTitle.localeCompare(b.headingTitle)
);
const busiestHeading = sortedByLoad.find((entry) => entry.todoCount > 0) ?? null;
const emptiestHeadings = headingDistribution.filter((entry) => entry.todoCount === 0);
const completedTodos = structure.todos.filter((todo) => todo.status === "completed").length;
const canceledTodos = structure.todos.filter((todo) => todo.status === "canceled").length;
const incompleteTodos = structure.todos.filter((todo) => todo.status === "incomplete").length;
const overdue = overdueTasks(structure.todos, today);
const upcoming = upcomingTasks(structure.todos, today);
const planningPriority = inferPlanningPriority({
overdueCount: overdue.length,
withoutHeadingCount: structure.todosWithoutHeading.length,
emptyHeadingCount: emptiestHeadings.length,
incompleteCount: incompleteTodos,
});
const observations: string[] = [];
if (busiestHeading) {
observations.push(
`El heading con más carga actual es ${busiestHeading.headingTitle} (${busiestHeading.todoCount} tareas, ${busiestHeading.incomplete} incompletas).`
);
}
if (emptiestHeadings.length > 0) {
observations.push(
`Hay ${emptiestHeadings.length} headings sin tareas: ${emptiestHeadings.map((entry) => entry.headingTitle).join(", ")}.`
);
}
if (structure.todosWithoutHeading.length > 0) {
observations.push(`Hay ${structure.todosWithoutHeading.length} tareas sin heading asignado.`);
}
if (overdue.length > 0) {
observations.push(`Hay ${overdue.length} tareas vencidas que requieren atención.`);
}
if (upcoming.length > 0) {
observations.push(`Hay ${upcoming.length} tareas con fecha próxima o activa para revisar.`);
}
if (observations.length === 0) {
observations.push("La estructura del proyecto se ve balanceada y sin huecos obvios.");
}
const nextActions: string[] = [];
if (overdue.length > 0) {
nextActions.push("Atender o reprogramar primero las tareas vencidas.");
}
if (structure.todosWithoutHeading.length > 0) {
nextActions.push("Ubicar las tareas sin heading para mantener la estructura consistente.");
}
if (emptiestHeadings.length > 0) {
nextActions.push("Revisar si los headings vacíos siguen siendo útiles o si conviene poblarlos con próximas tareas.");
}
if (upcoming.length > 0) {
nextActions.push("Confirmar las próximas tareas activas para que reflejen la prioridad real del proyecto.");
}
if (!nextActions.length) {
nextActions.push("Usar la estructura actual del proyecto como base para seguir agregando tareas nuevas.");
}
const planningSignals = {
planningPriority,
hasOverdueWork: overdue.length > 0,
hasUnassignedTasks: structure.todosWithoutHeading.length > 0,
hasEmptyHeadings: emptiestHeadings.length > 0,
isStructureBalanced:
overdue.length === 0 &&
structure.todosWithoutHeading.length === 0 &&
emptiestHeadings.length === 0,
};
return {
project: structure.compact.project,
counts: {
headings: structure.headings.length,
todos: structure.todos.length,
incompleteTodos,
completedTodos,
canceledTodos,
todosWithoutHeading: structure.todosWithoutHeading.length,
overdueTodos: overdue.length,
upcomingTodos: upcoming.length,
},
headingDistribution,
busiestHeading,
emptyHeadings: emptiestHeadings,
overdue,
upcoming,
planningSignals,
observations,
nextActions,
summaryText: [
`Proyecto: ${structure.project.title}.`,
`Tiene ${structure.headings.length} headings y ${structure.todos.length} tareas.`,
structure.todosWithoutHeading.length > 0
? `${structure.todosWithoutHeading.length} tareas siguen sin heading.`
: "No hay tareas sin heading.",
overdue.length > 0
? `Hay ${overdue.length} tareas vencidas.`
: "No hay tareas vencidas.",
busiestHeading
? `La mayor concentración de tareas está en ${busiestHeading.headingTitle}.`
: "Todavía no hay un heading dominante.",
`Prioridad de planificación: ${planningPriority}.`,
].join(" "),
};
}