@jsb188/schedule 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +62 -0
- package/src/constants/schedule.ts +549 -0
- package/src/css.d.ts +7 -0
- package/src/libs/gantt-geometry.ts +771 -0
- package/src/libs/gantt-histogram.ts +124 -0
- package/src/libs/gantt-keyboard.ts +113 -0
- package/src/libs/gantt-state.ts +57 -0
- package/src/libs/gantt-url.ts +63 -0
- package/src/modules/Gantt.helpers.ts +119 -0
- package/src/modules/Gantt.tsx +584 -0
- package/src/types/schedule.d.ts +1149 -0
- package/src/ui/ActivityFormUI.tsx +133 -0
- package/src/ui/GanttEditorsUI.tsx +102 -0
- package/src/ui/GanttHistogramUI.tsx +236 -0
- package/src/ui/GanttUI.tsx +792 -0
- package/src/ui/ProjectFormUI.tsx +250 -0
- package/src/ui/ProjectMobileListUI.tsx +80 -0
- package/src/ui/ProjectToolbarUI.tsx +54 -0
- package/src/ui/RelationshipListUI.tsx +176 -0
- package/src/ui/ScheduleRunUI.tsx +173 -0
- package/src/ui/ScheduleSettingsUI.tsx +420 -0
- package/src/utils/coverage.helpers.ts +679 -0
- package/src/utils/coverage.ts +630 -0
- package/src/utils/cpm.helpers.ts +674 -0
- package/src/utils/cpm.ts +907 -0
- package/src/utils/cpmLeveling.helpers.ts +564 -0
- package/src/utils/cpmLeveling.ts +367 -0
- package/src/utils/defaults.ts +8 -0
- package/src/utils/eligibility.ts +457 -0
- package/src/utils/planCsv.ts +344 -0
- package/src/utils/predecessorText.ts +96 -0
- package/src/utils/recurrence.ts +616 -0
- package/src/utils/request.ts +153 -0
- package/src/utils/resource.ts +182 -0
- package/src/utils/rule.ts +216 -0
- package/src/utils/run.ts +43 -0
- package/src/utils/series.ts +161 -0
- package/src/utils/slot.ts +208 -0
- package/src/utils/workTime.ts +1382 -0
- package/tests/coverage.fixtures.ts +111 -0
- package/tests/coverage.test.ts +503 -0
- package/tests/cpm.bench.test.ts +154 -0
- package/tests/cpm.test.ts +354 -0
- package/tests/cpmCorrectness.test.ts +327 -0
- package/tests/cpmLeveling.test.ts +471 -0
- package/tests/eligibility.test.ts +320 -0
- package/tests/gantt-geometry.test.ts +367 -0
- package/tests/gantt-histogram.test.ts +78 -0
- package/tests/gantt-keyboard.test.ts +69 -0
- package/tests/helpers/cpmFixtures.ts +101 -0
- package/tests/projectInputs.test.ts +165 -0
- package/tests/recurrence.test.ts +68 -0
- package/tests/request.test.ts +99 -0
- package/tests/resource.test.ts +95 -0
- package/tests/rule.test.ts +98 -0
- package/tests/series.test.ts +120 -0
- package/tests/workTime.test.ts +1007 -0
- package/tests/workTimeContracts.test.ts +111 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
import { SCHEDULE_DIAGNOSTIC_MAX_ACTIVITY_IDS } from '../constants/schedule.ts';
|
|
2
|
+
import type {
|
|
3
|
+
CpmAssignmentInput,
|
|
4
|
+
CpmExternalUsageInput,
|
|
5
|
+
CpmOverallocation,
|
|
6
|
+
CpmResourceInput,
|
|
7
|
+
CpmResourceUsage,
|
|
8
|
+
} from '../types/schedule.d.ts';
|
|
9
|
+
import {
|
|
10
|
+
addMinutes,
|
|
11
|
+
instant,
|
|
12
|
+
minutesBetween,
|
|
13
|
+
snapStart,
|
|
14
|
+
type CpmActivityState,
|
|
15
|
+
type CpmCalendar,
|
|
16
|
+
type DiagnosticCollector,
|
|
17
|
+
} from './cpm.helpers.ts';
|
|
18
|
+
import {
|
|
19
|
+
buildWorkCalendarTable,
|
|
20
|
+
getDayIndexAt,
|
|
21
|
+
getDayIndexForDateKey,
|
|
22
|
+
getWorkCalendarTable,
|
|
23
|
+
getWorkingMinutesOnDayBetween,
|
|
24
|
+
type WorkCalendarTable,
|
|
25
|
+
} from './workTime.ts';
|
|
26
|
+
|
|
27
|
+
export interface ResourceLedger {
|
|
28
|
+
resourceId: string;
|
|
29
|
+
resource: CpmResourceInput;
|
|
30
|
+
calendar: CpmCalendar;
|
|
31
|
+
table: WorkCalendarTable;
|
|
32
|
+
size: number;
|
|
33
|
+
available: Float64Array;
|
|
34
|
+
used: Float64Array;
|
|
35
|
+
external: Float64Array;
|
|
36
|
+
externalSource: {
|
|
37
|
+
rows: CpmExternalUsageInput[];
|
|
38
|
+
floorMs: number;
|
|
39
|
+
tolerance: number;
|
|
40
|
+
} | null;
|
|
41
|
+
contributors: Array<string[] | undefined>;
|
|
42
|
+
firstOpenDay: number;
|
|
43
|
+
canLevel: boolean;
|
|
44
|
+
levelable: boolean;
|
|
45
|
+
maxAvailableDay: number;
|
|
46
|
+
}
|
|
47
|
+
export interface DemandProfile {
|
|
48
|
+
ledger: ResourceLedger;
|
|
49
|
+
days: Array<{ index: number; demand: number }>;
|
|
50
|
+
}
|
|
51
|
+
export interface LevelingAssignment extends CpmAssignmentInput {
|
|
52
|
+
ledger: ResourceLedger;
|
|
53
|
+
}
|
|
54
|
+
export interface LevelingSearchOptions {
|
|
55
|
+
tolerance: number;
|
|
56
|
+
horizonEndMs: number;
|
|
57
|
+
levelWithinFloat: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const FILE_CONSTANTS = { epsilon: 1e-6, dayMs: 86_400_000 };
|
|
61
|
+
|
|
62
|
+
/* Accept actual ISO local dates, refusing normalized impossible dates such as February 31. */
|
|
63
|
+
export function isDateKey(value: string): boolean {
|
|
64
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) return false;
|
|
65
|
+
const date = new Date(`${value}T00:00:00Z`);
|
|
66
|
+
return (
|
|
67
|
+
Number.isFinite(date.getTime()) && date.toISOString().slice(0, 10) === value
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Initialize per-day capacity, applying the latest availability window and touched time-off days. */
|
|
72
|
+
export function resolveDailyAvailability(
|
|
73
|
+
resource: CpmResourceInput,
|
|
74
|
+
calendar: CpmCalendar,
|
|
75
|
+
table: WorkCalendarTable,
|
|
76
|
+
collector?: DiagnosticCollector,
|
|
77
|
+
): Float64Array {
|
|
78
|
+
const available = new Float64Array(table.days.length);
|
|
79
|
+
const base =
|
|
80
|
+
resource.maxUnitsPerDay == null
|
|
81
|
+
? NaN
|
|
82
|
+
: Number.isFinite(resource.maxUnitsPerDay)
|
|
83
|
+
? Math.max(0, resource.maxUnitsPerDay)
|
|
84
|
+
: 0;
|
|
85
|
+
available.fill(base);
|
|
86
|
+
const windows = (resource.availability || [])
|
|
87
|
+
.filter((row) => {
|
|
88
|
+
const valid =
|
|
89
|
+
isDateKey(row.startsOn) &&
|
|
90
|
+
(!row.endsOn ||
|
|
91
|
+
(isDateKey(row.endsOn) && row.endsOn >= row.startsOn)) &&
|
|
92
|
+
Number.isFinite(row.maxUnitsPerDay) &&
|
|
93
|
+
row.maxUnitsPerDay >= 0;
|
|
94
|
+
if (!valid)
|
|
95
|
+
collector?.add({
|
|
96
|
+
code: 'INVALID_INPUT',
|
|
97
|
+
severity: 'WARN',
|
|
98
|
+
resourceId: resource.id,
|
|
99
|
+
message: 'Invalid resource availability window is ignored.',
|
|
100
|
+
});
|
|
101
|
+
return valid;
|
|
102
|
+
})
|
|
103
|
+
.sort((a, b) =>
|
|
104
|
+
a.startsOn < b.startsOn ? -1 : a.startsOn > b.startsOn ? 1 : 0,
|
|
105
|
+
);
|
|
106
|
+
for (const row of windows) {
|
|
107
|
+
const from = Math.max(0, getDayIndexForDateKey(table, row.startsOn));
|
|
108
|
+
const to = Math.min(
|
|
109
|
+
table.days.length - 1,
|
|
110
|
+
row.endsOn
|
|
111
|
+
? getDayIndexForDateKey(table, row.endsOn)
|
|
112
|
+
: table.days.length - 1,
|
|
113
|
+
);
|
|
114
|
+
if (to >= from) available.fill(row.maxUnitsPerDay, from, to + 1);
|
|
115
|
+
}
|
|
116
|
+
for (let i = 0; i < table.days.length; i++) {
|
|
117
|
+
const day = table.days[i];
|
|
118
|
+
available[i] =
|
|
119
|
+
day.workingMinutes === 0
|
|
120
|
+
? 0
|
|
121
|
+
: available[i] *
|
|
122
|
+
Math.min(1, day.workingMinutes / calendar.minutesPerDay);
|
|
123
|
+
if (resource.kind === 'SPACE' || resource.kind === 'MATERIAL')
|
|
124
|
+
available[i] = NaN;
|
|
125
|
+
}
|
|
126
|
+
for (const row of resource.timeOff || []) {
|
|
127
|
+
const fromMs = instant(row.startAt);
|
|
128
|
+
const toMs = instant(row.endAt);
|
|
129
|
+
if (
|
|
130
|
+
fromMs == null ||
|
|
131
|
+
toMs == null ||
|
|
132
|
+
!Number.isFinite(fromMs) ||
|
|
133
|
+
!Number.isFinite(toMs) ||
|
|
134
|
+
toMs <= fromMs
|
|
135
|
+
) {
|
|
136
|
+
collector?.add({
|
|
137
|
+
code: 'INVALID_INPUT',
|
|
138
|
+
severity: 'WARN',
|
|
139
|
+
resourceId: resource.id,
|
|
140
|
+
message: 'Invalid resource time-off range is ignored.',
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const from = Math.max(0, getDayIndexAt(table, new Date(fromMs)));
|
|
145
|
+
const to = Math.min(
|
|
146
|
+
table.days.length - 1,
|
|
147
|
+
getDayIndexAt(table, new Date(toMs - 1)),
|
|
148
|
+
);
|
|
149
|
+
if (to >= from && resource.kind !== 'SPACE' && resource.kind !== 'MATERIAL')
|
|
150
|
+
available.fill(0, from, to + 1);
|
|
151
|
+
}
|
|
152
|
+
return available;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/* Build a resource ledger over an immutable calendar table shared by resources in the same zone. */
|
|
156
|
+
export function buildResourceLedger(
|
|
157
|
+
resource: CpmResourceInput,
|
|
158
|
+
calendar: CpmCalendar,
|
|
159
|
+
window: { fromMs: number; toMs: number },
|
|
160
|
+
tolerance: number,
|
|
161
|
+
collector?: DiagnosticCollector,
|
|
162
|
+
): ResourceLedger {
|
|
163
|
+
if (
|
|
164
|
+
!calendar.table ||
|
|
165
|
+
window.fromMs < calendar.table.firstDayStartMs ||
|
|
166
|
+
window.toMs >= calendar.table.lastDayEndMs
|
|
167
|
+
)
|
|
168
|
+
calendar.table = buildWorkCalendarTable(
|
|
169
|
+
calendar.def,
|
|
170
|
+
new Date(window.fromMs),
|
|
171
|
+
new Date(window.toMs),
|
|
172
|
+
);
|
|
173
|
+
const table = calendar.table;
|
|
174
|
+
const size = table.days.length;
|
|
175
|
+
const available = resolveDailyAvailability(
|
|
176
|
+
resource,
|
|
177
|
+
calendar,
|
|
178
|
+
table,
|
|
179
|
+
collector,
|
|
180
|
+
);
|
|
181
|
+
let maxAvailableDay = 0;
|
|
182
|
+
for (const value of available)
|
|
183
|
+
if (Number.isFinite(value))
|
|
184
|
+
maxAvailableDay = Math.max(maxAvailableDay, value);
|
|
185
|
+
const ledger: ResourceLedger = {
|
|
186
|
+
resourceId: resource.id,
|
|
187
|
+
resource,
|
|
188
|
+
calendar,
|
|
189
|
+
table,
|
|
190
|
+
size,
|
|
191
|
+
available,
|
|
192
|
+
used: new Float64Array(size),
|
|
193
|
+
external: new Float64Array(size),
|
|
194
|
+
externalSource: null,
|
|
195
|
+
contributors: new Array(size),
|
|
196
|
+
firstOpenDay: 0,
|
|
197
|
+
canLevel: resource.canLevel !== false,
|
|
198
|
+
levelable:
|
|
199
|
+
resource.canLevel !== false &&
|
|
200
|
+
(resource.kind === 'LABOR' || resource.kind === 'EQUIPMENT') &&
|
|
201
|
+
resource.maxUnitsPerDay !== null,
|
|
202
|
+
maxAvailableDay,
|
|
203
|
+
};
|
|
204
|
+
advanceFirstOpenDay(ledger, tolerance);
|
|
205
|
+
return ledger;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/* Spread another project's fixed daily rate through resource-calendar working intervals. */
|
|
209
|
+
export function spreadExternalUsage(
|
|
210
|
+
ledger: ResourceLedger,
|
|
211
|
+
rows: CpmExternalUsageInput[],
|
|
212
|
+
floorMs: number,
|
|
213
|
+
tolerance: number,
|
|
214
|
+
): void {
|
|
215
|
+
ledger.externalSource = { rows, floorMs, tolerance };
|
|
216
|
+
for (const row of rows) {
|
|
217
|
+
const start = instant(row.startAt);
|
|
218
|
+
const finish = instant(row.finishAt);
|
|
219
|
+
if (
|
|
220
|
+
start == null ||
|
|
221
|
+
finish == null ||
|
|
222
|
+
!Number.isFinite(start) ||
|
|
223
|
+
!Number.isFinite(finish) ||
|
|
224
|
+
finish <= start ||
|
|
225
|
+
!Number.isFinite(row.unitsPerDay) ||
|
|
226
|
+
row.unitsPerDay <= 0
|
|
227
|
+
)
|
|
228
|
+
continue;
|
|
229
|
+
const from = Math.max(floorMs, start);
|
|
230
|
+
const to = finish;
|
|
231
|
+
if (to <= from) continue;
|
|
232
|
+
const first = Math.max(0, getDayIndexAt(ledger.table, new Date(from)));
|
|
233
|
+
const last = Math.min(
|
|
234
|
+
ledger.size - 1,
|
|
235
|
+
getDayIndexAt(ledger.table, new Date(to - 1)),
|
|
236
|
+
);
|
|
237
|
+
for (let i = first; i <= last; i++)
|
|
238
|
+
ledger.external[i] +=
|
|
239
|
+
row.unitsPerDay *
|
|
240
|
+
Math.min(
|
|
241
|
+
1,
|
|
242
|
+
getWorkingMinutesOnDayBetween(ledger.table.days[i], from, to) /
|
|
243
|
+
ledger.calendar.minutesPerDay,
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
advanceFirstOpenDay(ledger, tolerance);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* Extend capacity and bookings together so long or dependency-delayed work cannot fall outside the checked ledger. */
|
|
250
|
+
function extendResourceLedger(
|
|
251
|
+
ledger: ResourceLedger,
|
|
252
|
+
start: number,
|
|
253
|
+
finish: number,
|
|
254
|
+
): void {
|
|
255
|
+
if (
|
|
256
|
+
start >= ledger.table.firstDayStartMs &&
|
|
257
|
+
finish <= ledger.table.lastDayEndMs
|
|
258
|
+
)
|
|
259
|
+
return;
|
|
260
|
+
const previous = ledger.table;
|
|
261
|
+
const table = buildWorkCalendarTable(
|
|
262
|
+
ledger.calendar.def,
|
|
263
|
+
new Date(Math.min(start, previous.firstDayStartMs)),
|
|
264
|
+
new Date(Math.max(finish, previous.lastDayEndMs - 1)),
|
|
265
|
+
);
|
|
266
|
+
const offset = getDayIndexAt(table, new Date(previous.firstDayStartMs));
|
|
267
|
+
const used = new Float64Array(table.days.length);
|
|
268
|
+
used.set(ledger.used, offset);
|
|
269
|
+
const contributors: ResourceLedger['contributors'] = new Array(
|
|
270
|
+
table.days.length,
|
|
271
|
+
);
|
|
272
|
+
for (let i = 0; i < ledger.contributors.length; i++)
|
|
273
|
+
contributors[i + offset] = ledger.contributors[i];
|
|
274
|
+
ledger.table = table;
|
|
275
|
+
ledger.size = table.days.length;
|
|
276
|
+
ledger.available = resolveDailyAvailability(
|
|
277
|
+
ledger.resource,
|
|
278
|
+
ledger.calendar,
|
|
279
|
+
table,
|
|
280
|
+
);
|
|
281
|
+
ledger.used = used;
|
|
282
|
+
ledger.contributors = contributors;
|
|
283
|
+
ledger.external = new Float64Array(ledger.size);
|
|
284
|
+
ledger.firstOpenDay = 0;
|
|
285
|
+
ledger.maxAvailableDay = 0;
|
|
286
|
+
for (const value of ledger.available)
|
|
287
|
+
if (Number.isFinite(value))
|
|
288
|
+
ledger.maxAvailableDay = Math.max(ledger.maxAvailableDay, value);
|
|
289
|
+
if (ledger.externalSource) {
|
|
290
|
+
const { rows, floorMs, tolerance } = ledger.externalSource;
|
|
291
|
+
spreadExternalUsage(ledger, rows, floorMs, tolerance);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/* Rebuild a complete non-splittable daily demand profile at a candidate start. */
|
|
296
|
+
export function buildDemandProfile(
|
|
297
|
+
state: CpmActivityState,
|
|
298
|
+
start: number,
|
|
299
|
+
assignments: LevelingAssignment[],
|
|
300
|
+
): DemandProfile[] {
|
|
301
|
+
if (!state.remaining || !assignments.length) return [];
|
|
302
|
+
const finish = addMinutes(state.calendar, start, state.remaining);
|
|
303
|
+
for (const ledger of new Set(assignments.map((row) => row.ledger)))
|
|
304
|
+
extendResourceLedger(ledger, start, finish);
|
|
305
|
+
const table = getWorkCalendarTable(
|
|
306
|
+
state.calendar.def,
|
|
307
|
+
new Date(start),
|
|
308
|
+
new Date(finish),
|
|
309
|
+
);
|
|
310
|
+
const first = Math.max(0, getDayIndexAt(table, new Date(start)));
|
|
311
|
+
const last = Math.min(
|
|
312
|
+
table.days.length - 1,
|
|
313
|
+
getDayIndexAt(table, new Date(finish - 1)),
|
|
314
|
+
);
|
|
315
|
+
const demand = new Map<ResourceLedger, Map<number, number>>();
|
|
316
|
+
for (const assignment of assignments)
|
|
317
|
+
if (!demand.has(assignment.ledger))
|
|
318
|
+
demand.set(assignment.ledger, new Map());
|
|
319
|
+
for (let i = first; i <= last; i++) {
|
|
320
|
+
const day = table.days[i];
|
|
321
|
+
const minutes = getWorkingMinutesOnDayBetween(day, start, finish);
|
|
322
|
+
if (!minutes) continue;
|
|
323
|
+
const firstPeriod = day.periods.find(
|
|
324
|
+
(period) => period.endMs > start && period.startMs < finish,
|
|
325
|
+
)!;
|
|
326
|
+
const workingInstant = Math.max(start, firstPeriod.startMs);
|
|
327
|
+
for (const assignment of assignments) {
|
|
328
|
+
const { ledger } = assignment;
|
|
329
|
+
const index = getDayIndexAt(ledger.table, new Date(workingInstant));
|
|
330
|
+
if (index < 0 || index >= ledger.size) continue;
|
|
331
|
+
const scale = Math.min(
|
|
332
|
+
1,
|
|
333
|
+
ledger.table.days[index].workingMinutes / ledger.calendar.minutesPerDay,
|
|
334
|
+
);
|
|
335
|
+
const units =
|
|
336
|
+
assignment.unitsPerDay != null
|
|
337
|
+
? assignment.unitsPerDay *
|
|
338
|
+
Math.min(1, minutes / state.calendar.minutesPerDay)
|
|
339
|
+
: assignment.remainingUnits > 0
|
|
340
|
+
? (assignment.remainingUnits * minutes) / state.remaining
|
|
341
|
+
: Math.min(
|
|
342
|
+
(ledger.resource.maxUnitsPerDay || minutes) * (scale || 1),
|
|
343
|
+
minutes,
|
|
344
|
+
);
|
|
345
|
+
if (units > 0) {
|
|
346
|
+
const cells = demand.get(ledger)!;
|
|
347
|
+
cells.set(index, (cells.get(index) || 0) + units);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return [...demand].map(([ledger, cells]) => ({
|
|
352
|
+
ledger,
|
|
353
|
+
days: [...cells]
|
|
354
|
+
.map(([index, value]) => ({ index, demand: value }))
|
|
355
|
+
.sort((a, b) => a.index - b.index),
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/* Advance the monotone saturated prefix so later activities skip days that cannot fit any work. */
|
|
360
|
+
function advanceFirstOpenDay(ledger: ResourceLedger, tolerance: number): void {
|
|
361
|
+
while (ledger.firstOpenDay < ledger.size) {
|
|
362
|
+
const i = ledger.firstOpenDay;
|
|
363
|
+
if (
|
|
364
|
+
Number.isNaN(ledger.available[i]) ||
|
|
365
|
+
ledger.used[i] + ledger.external[i] <
|
|
366
|
+
ledger.available[i] * (1 + tolerance) - FILE_CONSTANTS.epsilon
|
|
367
|
+
)
|
|
368
|
+
break;
|
|
369
|
+
ledger.firstOpenDay++;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/* Commit precisely the demand checked by feasibility and retain bounded contributor ids. */
|
|
374
|
+
export function commitDemand(
|
|
375
|
+
activityId: string,
|
|
376
|
+
profiles: DemandProfile[],
|
|
377
|
+
tolerance: number,
|
|
378
|
+
): void {
|
|
379
|
+
for (const { ledger, days } of profiles) {
|
|
380
|
+
for (const cell of days) {
|
|
381
|
+
ledger.used[cell.index] += cell.demand;
|
|
382
|
+
const ids = ledger.contributors[cell.index] || [];
|
|
383
|
+
ledger.contributors[cell.index] = ids;
|
|
384
|
+
if (
|
|
385
|
+
ids.length < SCHEDULE_DIAGNOSTIC_MAX_ACTIVITY_IDS &&
|
|
386
|
+
!ids.includes(activityId)
|
|
387
|
+
)
|
|
388
|
+
ids.push(activityId);
|
|
389
|
+
}
|
|
390
|
+
advanceFirstOpenDay(ledger, tolerance);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/* Search successive calendar-day candidates, skipping saturated stretches and respecting float/horizon bounds. */
|
|
395
|
+
export function findFirstFeasibleStart(
|
|
396
|
+
state: CpmActivityState,
|
|
397
|
+
bound: number,
|
|
398
|
+
assignments: LevelingAssignment[],
|
|
399
|
+
options: LevelingSearchOptions,
|
|
400
|
+
): {
|
|
401
|
+
start: number;
|
|
402
|
+
constrainedBy: string | null;
|
|
403
|
+
failure: 'LEVELING_EXCEEDED_FLOAT' | 'LEVELING_EXCEEDED_HORIZON' | null;
|
|
404
|
+
profiles: DemandProfile[];
|
|
405
|
+
} {
|
|
406
|
+
let start = bound;
|
|
407
|
+
let constrainedBy: string | null = null;
|
|
408
|
+
while (true) {
|
|
409
|
+
if (start > options.horizonEndMs)
|
|
410
|
+
return {
|
|
411
|
+
start: bound,
|
|
412
|
+
constrainedBy,
|
|
413
|
+
failure: 'LEVELING_EXCEEDED_HORIZON',
|
|
414
|
+
profiles: buildDemandProfile(state, bound, assignments),
|
|
415
|
+
};
|
|
416
|
+
const finish = addMinutes(state.calendar, start, state.remaining);
|
|
417
|
+
if (
|
|
418
|
+
options.levelWithinFloat &&
|
|
419
|
+
minutesBetween(state.calendar, finish, state.lf) < 0
|
|
420
|
+
)
|
|
421
|
+
return {
|
|
422
|
+
start: bound,
|
|
423
|
+
constrainedBy,
|
|
424
|
+
failure: 'LEVELING_EXCEEDED_FLOAT',
|
|
425
|
+
profiles: buildDemandProfile(state, bound, assignments),
|
|
426
|
+
};
|
|
427
|
+
const profiles = buildDemandProfile(state, start, assignments);
|
|
428
|
+
let blocked: { ledger: ResourceLedger; index: number } | null = null;
|
|
429
|
+
for (const { ledger, days } of profiles) {
|
|
430
|
+
if (!ledger.levelable) continue;
|
|
431
|
+
for (const { index, demand } of days) {
|
|
432
|
+
if (
|
|
433
|
+
ledger.used[index] + ledger.external[index] + demand >
|
|
434
|
+
ledger.available[index] * (1 + options.tolerance) +
|
|
435
|
+
FILE_CONSTANTS.epsilon
|
|
436
|
+
) {
|
|
437
|
+
blocked = { ledger, index };
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
if (blocked) break;
|
|
442
|
+
}
|
|
443
|
+
if (!blocked) return { start, constrainedBy, failure: null, profiles };
|
|
444
|
+
const { ledger, index } = blocked;
|
|
445
|
+
constrainedBy = ledger.resourceId;
|
|
446
|
+
const saturated =
|
|
447
|
+
ledger.used[index] + ledger.external[index] >=
|
|
448
|
+
ledger.available[index] * (1 + options.tolerance) -
|
|
449
|
+
FILE_CONSTANTS.epsilon;
|
|
450
|
+
const table = getWorkCalendarTable(
|
|
451
|
+
state.calendar.def,
|
|
452
|
+
new Date(start),
|
|
453
|
+
new Date(start + 2 * FILE_CONSTANTS.dayMs),
|
|
454
|
+
);
|
|
455
|
+
const nextDay =
|
|
456
|
+
table.days[getDayIndexAt(table, new Date(start)) + 1]?.dayStartMs ??
|
|
457
|
+
table.lastDayEndMs;
|
|
458
|
+
let next = snapStart(
|
|
459
|
+
state.calendar,
|
|
460
|
+
saturated
|
|
461
|
+
? (ledger.table.days[index + 1]?.dayStartMs ??
|
|
462
|
+
ledger.table.lastDayEndMs)
|
|
463
|
+
: nextDay,
|
|
464
|
+
);
|
|
465
|
+
if (next <= start) next = snapStart(state.calendar, nextDay);
|
|
466
|
+
for (const assignment of assignments) {
|
|
467
|
+
const l = assignment.ledger;
|
|
468
|
+
if (l.levelable && l.firstOpenDay < l.size)
|
|
469
|
+
next = Math.max(
|
|
470
|
+
next,
|
|
471
|
+
snapStart(state.calendar, l.table.days[l.firstOpenDay].dayStartMs),
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
start = next;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/* Return populated histogram rows in resource and local-date order. */
|
|
479
|
+
export function collectResourceUsage(
|
|
480
|
+
ledgers: ResourceLedger[],
|
|
481
|
+
): CpmResourceUsage[] {
|
|
482
|
+
return ledgers.map((ledger) => ({
|
|
483
|
+
resourceId: ledger.resourceId,
|
|
484
|
+
calendarId: ledger.calendar.def.id,
|
|
485
|
+
days: ledger.table.days.flatMap((day, index) =>
|
|
486
|
+
ledger.used[index] + ledger.external[index] > 0
|
|
487
|
+
? [
|
|
488
|
+
{
|
|
489
|
+
dayIndex: index,
|
|
490
|
+
date: day.dateKey,
|
|
491
|
+
demandUnits: ledger.used[index],
|
|
492
|
+
externalUnits: ledger.external[index],
|
|
493
|
+
availableUnits: Number.isNaN(ledger.available[index])
|
|
494
|
+
? null
|
|
495
|
+
: ledger.available[index],
|
|
496
|
+
},
|
|
497
|
+
]
|
|
498
|
+
: [],
|
|
499
|
+
),
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/* Merge consecutive overallocated local days and report every residual capacity conflict. */
|
|
504
|
+
export function collectOverallocations(
|
|
505
|
+
ledgers: ResourceLedger[],
|
|
506
|
+
tolerance: number,
|
|
507
|
+
collector: DiagnosticCollector,
|
|
508
|
+
): {
|
|
509
|
+
overallocations: CpmOverallocation[];
|
|
510
|
+
overallocatedResourceDayCount: number;
|
|
511
|
+
} {
|
|
512
|
+
const overallocations: CpmOverallocation[] = [];
|
|
513
|
+
let overallocatedResourceDayCount = 0;
|
|
514
|
+
for (const ledger of ledgers) {
|
|
515
|
+
let stretch: CpmOverallocation | null = null;
|
|
516
|
+
/* Finish one contiguous stretch and persist its bounded explanation. */
|
|
517
|
+
const flush = () => {
|
|
518
|
+
if (!stretch) return;
|
|
519
|
+
overallocations.push(stretch);
|
|
520
|
+
collector.add({
|
|
521
|
+
code: 'RESOURCE_OVERALLOCATED',
|
|
522
|
+
severity: 'WARN',
|
|
523
|
+
resourceId: stretch.resourceId,
|
|
524
|
+
fromDate: stretch.fromDate,
|
|
525
|
+
toDate: stretch.toDate,
|
|
526
|
+
demandUnits: stretch.peakDemandUnits,
|
|
527
|
+
availableUnits: stretch.availableUnits,
|
|
528
|
+
activityIds: stretch.activityIds,
|
|
529
|
+
activityId: stretch.activityIds[0] || null,
|
|
530
|
+
message: `${stretch.resourceId} is over-allocated ${stretch.fromDate}–${stretch.toDate}: ${stretch.peakDemandUnits} of ${stretch.availableUnits} units/day${stretch.activityIds.length ? '' : ' by other projects'}.`,
|
|
531
|
+
});
|
|
532
|
+
stretch = null;
|
|
533
|
+
};
|
|
534
|
+
for (let i = 0; i < ledger.size; i++) {
|
|
535
|
+
const demand = ledger.used[i] + ledger.external[i];
|
|
536
|
+
const available = ledger.available[i];
|
|
537
|
+
if (!(demand > available * (1 + tolerance) + FILE_CONSTANTS.epsilon)) {
|
|
538
|
+
flush();
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
overallocatedResourceDayCount++;
|
|
542
|
+
if (!stretch)
|
|
543
|
+
stretch = {
|
|
544
|
+
resourceId: ledger.resourceId,
|
|
545
|
+
fromDate: ledger.table.days[i].dateKey,
|
|
546
|
+
toDate: ledger.table.days[i].dateKey,
|
|
547
|
+
peakDemandUnits: demand,
|
|
548
|
+
availableUnits: available,
|
|
549
|
+
activityIds: [],
|
|
550
|
+
};
|
|
551
|
+
stretch.toDate = ledger.table.days[i].dateKey;
|
|
552
|
+
stretch.peakDemandUnits = Math.max(stretch.peakDemandUnits, demand);
|
|
553
|
+
stretch.availableUnits = Math.min(stretch.availableUnits, available);
|
|
554
|
+
for (const id of ledger.contributors[i] || [])
|
|
555
|
+
if (
|
|
556
|
+
stretch.activityIds.length < SCHEDULE_DIAGNOSTIC_MAX_ACTIVITY_IDS &&
|
|
557
|
+
!stretch.activityIds.includes(id)
|
|
558
|
+
)
|
|
559
|
+
stretch.activityIds.push(id);
|
|
560
|
+
}
|
|
561
|
+
flush();
|
|
562
|
+
}
|
|
563
|
+
return { overallocations, overallocatedResourceDayCount };
|
|
564
|
+
}
|