@jsb188/schedule 1.1.11
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 +61 -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 +138 -0
- package/src/modules/Gantt.tsx +719 -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 +273 -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,367 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEDULE_LEVELING_DEFAULT_PRIORITY_RULE,
|
|
3
|
+
SCHEDULE_LEVELING_PRIORITY_RULE_ENUMS,
|
|
4
|
+
} from '../constants/schedule.ts';
|
|
5
|
+
import type {
|
|
6
|
+
ScheduleLevelingOptionsObj,
|
|
7
|
+
ScheduleLevelingPriorityRuleEnum,
|
|
8
|
+
} from '../types/schedule.d.ts';
|
|
9
|
+
import {
|
|
10
|
+
addLag,
|
|
11
|
+
addMinutes,
|
|
12
|
+
compareActivityIds,
|
|
13
|
+
getActivityCalendar,
|
|
14
|
+
minutesBetween,
|
|
15
|
+
snapFinish,
|
|
16
|
+
snapStart,
|
|
17
|
+
type CpmActivityState,
|
|
18
|
+
type CpmContext,
|
|
19
|
+
} from './cpm.helpers.ts';
|
|
20
|
+
import {
|
|
21
|
+
buildDemandProfile,
|
|
22
|
+
buildResourceLedger,
|
|
23
|
+
collectOverallocations,
|
|
24
|
+
collectResourceUsage,
|
|
25
|
+
commitDemand,
|
|
26
|
+
findFirstFeasibleStart,
|
|
27
|
+
spreadExternalUsage,
|
|
28
|
+
type LevelingAssignment,
|
|
29
|
+
type ResourceLedger,
|
|
30
|
+
} from './cpmLeveling.helpers.ts';
|
|
31
|
+
|
|
32
|
+
/* Normalize resource-leveling knobs so all engine callers receive bounded searches. */
|
|
33
|
+
export function resolveLevelingOptions(
|
|
34
|
+
input?: ScheduleLevelingOptionsObj | null,
|
|
35
|
+
) {
|
|
36
|
+
return {
|
|
37
|
+
enabled: input?.enabled === true,
|
|
38
|
+
priorityRule:
|
|
39
|
+
input?.priorityRule?.length &&
|
|
40
|
+
input.priorityRule.every((key) =>
|
|
41
|
+
(SCHEDULE_LEVELING_PRIORITY_RULE_ENUMS as readonly string[]).includes(
|
|
42
|
+
key,
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
? input.priorityRule
|
|
46
|
+
: [...SCHEDULE_LEVELING_DEFAULT_PRIORITY_RULE],
|
|
47
|
+
levelWithinFloat: input?.levelWithinFloat === true,
|
|
48
|
+
tolerance: Number.isFinite(input?.maxOverallocationPercent)
|
|
49
|
+
? Math.min(100, Math.max(0, input!.maxOverallocationPercent!)) / 100
|
|
50
|
+
: 0,
|
|
51
|
+
horizonDays: Number.isFinite(input?.horizonDays)
|
|
52
|
+
? Math.min(3660, Math.max(30, Math.round(input!.horizonDays!)))
|
|
53
|
+
: 730,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/* Compare immutable CPM priorities, ending in ordinal and a numeric-aware activity id. */
|
|
58
|
+
export function compareLevelingPriority(
|
|
59
|
+
rule: readonly ScheduleLevelingPriorityRuleEnum[],
|
|
60
|
+
a: CpmActivityState,
|
|
61
|
+
b: CpmActivityState,
|
|
62
|
+
): number {
|
|
63
|
+
/* Resolve one priority key with finite defaults for user-supplied ordinal/priority values. */
|
|
64
|
+
const value = (
|
|
65
|
+
state: CpmActivityState,
|
|
66
|
+
key: ScheduleLevelingPriorityRuleEnum,
|
|
67
|
+
) => {
|
|
68
|
+
switch (key) {
|
|
69
|
+
case 'TOTAL_FLOAT':
|
|
70
|
+
return state.totalFloat || 0;
|
|
71
|
+
case 'LATE_START':
|
|
72
|
+
return state.ls;
|
|
73
|
+
case 'EARLY_START':
|
|
74
|
+
return state.es;
|
|
75
|
+
case 'PRIORITY':
|
|
76
|
+
return Number.isInteger(state.input.levelingPriority) &&
|
|
77
|
+
state.input.levelingPriority! >= 1 &&
|
|
78
|
+
state.input.levelingPriority! <= 10
|
|
79
|
+
? state.input.levelingPriority!
|
|
80
|
+
: 5;
|
|
81
|
+
case 'ORDINAL':
|
|
82
|
+
return Number.isFinite(state.input.ordinal) ? state.input.ordinal! : 0;
|
|
83
|
+
case 'DURATION':
|
|
84
|
+
return -state.remaining;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
for (const key of rule) {
|
|
88
|
+
const difference = value(a, key) - value(b, key);
|
|
89
|
+
if (difference) return difference;
|
|
90
|
+
}
|
|
91
|
+
return (
|
|
92
|
+
value(a, 'ORDINAL') - value(b, 'ORDINAL') ||
|
|
93
|
+
compareActivityIds(a.input.id, b.input.id)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/* Maintain the ready set as a binary heap without repeatedly sorting the project graph. */
|
|
98
|
+
class ActivityHeap {
|
|
99
|
+
private readonly rows: CpmActivityState[] = [];
|
|
100
|
+
/* Capture the chosen total priority comparator. */
|
|
101
|
+
constructor(
|
|
102
|
+
private readonly compare: (
|
|
103
|
+
a: CpmActivityState,
|
|
104
|
+
b: CpmActivityState,
|
|
105
|
+
) => number,
|
|
106
|
+
) {}
|
|
107
|
+
/* Insert one newly ready activity in logarithmic time. */
|
|
108
|
+
push(state: CpmActivityState): void {
|
|
109
|
+
let i = this.rows.length;
|
|
110
|
+
this.rows.push(state);
|
|
111
|
+
while (i > 0) {
|
|
112
|
+
const parent = (i - 1) >>> 1;
|
|
113
|
+
if (this.compare(this.rows[parent], state) <= 0) break;
|
|
114
|
+
this.rows[i] = this.rows[parent];
|
|
115
|
+
i = parent;
|
|
116
|
+
}
|
|
117
|
+
this.rows[i] = state;
|
|
118
|
+
}
|
|
119
|
+
/* Remove the highest-priority ready activity in logarithmic time. */
|
|
120
|
+
pop(): CpmActivityState | undefined {
|
|
121
|
+
const first = this.rows[0];
|
|
122
|
+
const last = this.rows.pop();
|
|
123
|
+
if (!this.rows.length || !last) return first;
|
|
124
|
+
let i = 0;
|
|
125
|
+
while (i * 2 + 1 < this.rows.length) {
|
|
126
|
+
let child = i * 2 + 1;
|
|
127
|
+
if (
|
|
128
|
+
child + 1 < this.rows.length &&
|
|
129
|
+
this.compare(this.rows[child + 1], this.rows[child]) < 0
|
|
130
|
+
)
|
|
131
|
+
child++;
|
|
132
|
+
if (this.compare(last, this.rows[child]) <= 0) break;
|
|
133
|
+
this.rows[i] = this.rows[child];
|
|
134
|
+
i = child;
|
|
135
|
+
}
|
|
136
|
+
this.rows[i] = last;
|
|
137
|
+
return first;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/* Level one project around fixed actuals, mandatory dates, and other projects' resource usage. */
|
|
142
|
+
export function runResourceLeveling(
|
|
143
|
+
context: CpmContext,
|
|
144
|
+
order: CpmActivityState[],
|
|
145
|
+
) {
|
|
146
|
+
const options = resolveLevelingOptions(context.input.options?.leveling);
|
|
147
|
+
const horizonEndMs = context.floorMs + options.horizonDays * 86_400_000;
|
|
148
|
+
const referenced = new Set([
|
|
149
|
+
...(context.input.assignments || []).map((a) => a.resourceId),
|
|
150
|
+
...(context.input.externalUsage || []).map((a) => a.resourceId),
|
|
151
|
+
]);
|
|
152
|
+
const ledgers = new Map<string, ResourceLedger>();
|
|
153
|
+
for (const resource of context.input.resources || [])
|
|
154
|
+
if (referenced.has(resource.id)) {
|
|
155
|
+
ledgers.set(
|
|
156
|
+
resource.id,
|
|
157
|
+
buildResourceLedger(
|
|
158
|
+
resource,
|
|
159
|
+
getActivityCalendar(context.calendarSet, resource.calendarId),
|
|
160
|
+
{
|
|
161
|
+
fromMs: Math.min(context.floorMs, context.dataDateMs),
|
|
162
|
+
toMs: horizonEndMs + 366 * 86_400_000,
|
|
163
|
+
},
|
|
164
|
+
options.tolerance,
|
|
165
|
+
context.collector,
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const externalByResource = new Map<
|
|
170
|
+
string,
|
|
171
|
+
NonNullable<CpmContext['input']['externalUsage']>
|
|
172
|
+
>();
|
|
173
|
+
for (const row of context.input.externalUsage || []) {
|
|
174
|
+
const rows = externalByResource.get(row.resourceId) || [];
|
|
175
|
+
rows.push(row);
|
|
176
|
+
externalByResource.set(row.resourceId, rows);
|
|
177
|
+
}
|
|
178
|
+
for (const ledger of ledgers.values())
|
|
179
|
+
spreadExternalUsage(
|
|
180
|
+
ledger,
|
|
181
|
+
externalByResource.get(ledger.resourceId) || [],
|
|
182
|
+
context.floorMs,
|
|
183
|
+
options.tolerance,
|
|
184
|
+
);
|
|
185
|
+
const assignments = new Map<string, LevelingAssignment[]>();
|
|
186
|
+
for (const row of context.input.assignments || []) {
|
|
187
|
+
const state = context.states.get(row.activityId);
|
|
188
|
+
const ledger = ledgers.get(row.resourceId);
|
|
189
|
+
if (!state?.isDriving || !ledger) {
|
|
190
|
+
context.collector.add({
|
|
191
|
+
code: 'INVALID_INPUT',
|
|
192
|
+
severity: 'WARN',
|
|
193
|
+
activityId: row.activityId,
|
|
194
|
+
resourceId: row.resourceId,
|
|
195
|
+
message:
|
|
196
|
+
'Assignment has an unknown resource, unknown activity, or non-driving activity and is ignored.',
|
|
197
|
+
});
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
const validUnits =
|
|
201
|
+
Number.isFinite(row.remainingUnits) && row.remainingUnits >= 0;
|
|
202
|
+
const validRate =
|
|
203
|
+
row.unitsPerDay == null ||
|
|
204
|
+
(Number.isFinite(row.unitsPerDay) && row.unitsPerDay >= 0);
|
|
205
|
+
if (!validUnits || !validRate)
|
|
206
|
+
context.collector.add({
|
|
207
|
+
code: 'INVALID_INPUT',
|
|
208
|
+
severity: 'WARN',
|
|
209
|
+
activityId: row.activityId,
|
|
210
|
+
resourceId: row.resourceId,
|
|
211
|
+
message:
|
|
212
|
+
'Invalid assignment units are normalized to zero or an unspecified daily rate.',
|
|
213
|
+
});
|
|
214
|
+
const rows = assignments.get(row.activityId) || [];
|
|
215
|
+
rows.push({
|
|
216
|
+
...row,
|
|
217
|
+
remainingUnits: validUnits ? row.remainingUnits : 0,
|
|
218
|
+
unitsPerDay: validRate ? row.unitsPerDay : null,
|
|
219
|
+
ledger,
|
|
220
|
+
});
|
|
221
|
+
assignments.set(row.activityId, rows);
|
|
222
|
+
}
|
|
223
|
+
const pinned = new Set<string>();
|
|
224
|
+
for (const state of order)
|
|
225
|
+
if (
|
|
226
|
+
state.status === 'IN_PROGRESS' ||
|
|
227
|
+
(state.status === 'NOT_STARTED' && state.constraints.hasMandatory)
|
|
228
|
+
) {
|
|
229
|
+
context.cursorActivityId = state.input.id;
|
|
230
|
+
pinned.add(state.input.id);
|
|
231
|
+
commitDemand(
|
|
232
|
+
state.input.id,
|
|
233
|
+
buildDemandProfile(
|
|
234
|
+
state,
|
|
235
|
+
state.status === 'IN_PROGRESS' ? state.resumeStart : state.start,
|
|
236
|
+
assignments.get(state.input.id) || [],
|
|
237
|
+
),
|
|
238
|
+
options.tolerance,
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
const indegree = new Map<string, number>();
|
|
242
|
+
const heap = new ActivityHeap((a, b) =>
|
|
243
|
+
compareLevelingPriority(options.priorityRule, a, b),
|
|
244
|
+
);
|
|
245
|
+
for (const state of order) {
|
|
246
|
+
const count = state.predecessors.filter(
|
|
247
|
+
(rel) => context.states.get(rel.predecessorId)!.isDriving,
|
|
248
|
+
).length;
|
|
249
|
+
indegree.set(state.input.id, count);
|
|
250
|
+
if (!count) heap.push(state);
|
|
251
|
+
}
|
|
252
|
+
while (true) {
|
|
253
|
+
const state = heap.pop();
|
|
254
|
+
if (!state) break;
|
|
255
|
+
context.cursorActivityId = state.input.id;
|
|
256
|
+
if (state.status !== 'COMPLETE' && !pinned.has(state.input.id)) {
|
|
257
|
+
let bound = state.es;
|
|
258
|
+
for (const rel of state.predecessors) {
|
|
259
|
+
const pred = context.states.get(rel.predecessorId)!;
|
|
260
|
+
if (!pred.isDriving) continue;
|
|
261
|
+
let candidate = addLag(
|
|
262
|
+
context,
|
|
263
|
+
rel.type === 'SS' || rel.type === 'SF' ? pred.start : pred.finish,
|
|
264
|
+
rel.lagMinutes || 0,
|
|
265
|
+
pred,
|
|
266
|
+
state,
|
|
267
|
+
);
|
|
268
|
+
if (rel.type === 'FF' || rel.type === 'SF')
|
|
269
|
+
candidate = state.remaining
|
|
270
|
+
? addMinutes(state.calendar, candidate, -state.remaining)
|
|
271
|
+
: candidate;
|
|
272
|
+
candidate =
|
|
273
|
+
state.input.type === 'FINISH_MILESTONE'
|
|
274
|
+
? snapFinish(state.calendar, candidate)
|
|
275
|
+
: snapStart(state.calendar, candidate);
|
|
276
|
+
bound = Math.max(bound, candidate);
|
|
277
|
+
}
|
|
278
|
+
const rows = assignments.get(state.input.id) || [];
|
|
279
|
+
let impossible = false;
|
|
280
|
+
for (const row of rows) {
|
|
281
|
+
const need =
|
|
282
|
+
row.unitsPerDay != null
|
|
283
|
+
? row.unitsPerDay *
|
|
284
|
+
Math.min(1, state.remaining / state.calendar.minutesPerDay)
|
|
285
|
+
: row.remainingUnits > 0
|
|
286
|
+
? (row.remainingUnits *
|
|
287
|
+
Math.min(state.calendar.minutesPerDay, state.remaining)) /
|
|
288
|
+
state.remaining
|
|
289
|
+
: 0;
|
|
290
|
+
if (
|
|
291
|
+
state.remaining &&
|
|
292
|
+
row.ledger.levelable &&
|
|
293
|
+
need > row.ledger.maxAvailableDay * (1 + options.tolerance) + 1e-6
|
|
294
|
+
) {
|
|
295
|
+
impossible = true;
|
|
296
|
+
context.collector.add({
|
|
297
|
+
code: 'RESOURCE_OVERALLOCATED',
|
|
298
|
+
severity: 'WARN',
|
|
299
|
+
activityId: state.input.id,
|
|
300
|
+
resourceId: row.resourceId,
|
|
301
|
+
demandUnits: need,
|
|
302
|
+
availableUnits: row.ledger.maxAvailableDay,
|
|
303
|
+
message: "Assignment demand exceeds the resource's daily capacity.",
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const needsSearch =
|
|
308
|
+
state.remaining > 0 &&
|
|
309
|
+
!impossible &&
|
|
310
|
+
rows.some((row) => row.ledger.levelable && row.unitsPerDay !== 0);
|
|
311
|
+
const placement = needsSearch
|
|
312
|
+
? findFirstFeasibleStart(state, bound, rows, {
|
|
313
|
+
...options,
|
|
314
|
+
horizonEndMs,
|
|
315
|
+
})
|
|
316
|
+
: {
|
|
317
|
+
start: bound,
|
|
318
|
+
constrainedBy: null,
|
|
319
|
+
failure: null,
|
|
320
|
+
profiles: buildDemandProfile(state, bound, rows),
|
|
321
|
+
};
|
|
322
|
+
if (placement.failure)
|
|
323
|
+
context.collector.add({
|
|
324
|
+
code: placement.failure,
|
|
325
|
+
severity: 'WARN',
|
|
326
|
+
activityId: state.input.id,
|
|
327
|
+
resourceId: placement.constrainedBy,
|
|
328
|
+
minutes: minutesBetween(state.calendar, state.es, bound),
|
|
329
|
+
message:
|
|
330
|
+
placement.failure === 'LEVELING_EXCEEDED_FLOAT'
|
|
331
|
+
? 'Resource leveling could not place this activity within its total float.'
|
|
332
|
+
: 'Resource leveling could not place this activity inside the search horizon.',
|
|
333
|
+
});
|
|
334
|
+
state.start = placement.start;
|
|
335
|
+
state.finish = state.remaining
|
|
336
|
+
? addMinutes(state.calendar, state.start, state.remaining)
|
|
337
|
+
: state.start;
|
|
338
|
+
commitDemand(state.input.id, placement.profiles, options.tolerance);
|
|
339
|
+
state.levelingDelay = Math.max(
|
|
340
|
+
0,
|
|
341
|
+
minutesBetween(state.calendar, state.es, state.start),
|
|
342
|
+
);
|
|
343
|
+
state.constrainedBy =
|
|
344
|
+
state.levelingDelay > 0 ? placement.constrainedBy : null;
|
|
345
|
+
if (state.levelingDelay > 0 && state.constrainedBy)
|
|
346
|
+
context.collector.add({
|
|
347
|
+
code: 'LEVELING_DELAYED',
|
|
348
|
+
severity: 'INFO',
|
|
349
|
+
activityId: state.input.id,
|
|
350
|
+
resourceId: state.constrainedBy,
|
|
351
|
+
minutes: state.levelingDelay,
|
|
352
|
+
message: 'Activity delayed to fit resource availability.',
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
for (const rel of state.successors) {
|
|
356
|
+
const count = indegree.get(rel.successorId);
|
|
357
|
+
if (count == null) continue;
|
|
358
|
+
indegree.set(rel.successorId, count - 1);
|
|
359
|
+
if (count === 1) heap.push(context.states.get(rel.successorId)!);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const rows = [...ledgers.values()];
|
|
363
|
+
return {
|
|
364
|
+
resourceUsage: collectResourceUsage(rows),
|
|
365
|
+
...collectOverallocations(rows, options.tolerance, context.collector),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SCHEDULE_DEFAULTS_BY_OPERATION, SCHEDULE_DEFAULT_SURFACE } from '../constants/schedule.ts';
|
|
2
|
+
|
|
3
|
+
/* Resolve an organization's preferred surface while leaving both scheduling tools available. */
|
|
4
|
+
export function getScheduleDefaultSurface(operation?: string | null, settings?: {schedule?: {defaultSurface?: string | null} | null} | null): 'PROJECT' | 'COVERAGE' {
|
|
5
|
+
const override = settings?.schedule?.defaultSurface;
|
|
6
|
+
if (override === 'PROJECT' || override === 'COVERAGE') return override;
|
|
7
|
+
return SCHEDULE_DEFAULTS_BY_OPERATION[operation as keyof typeof SCHEDULE_DEFAULTS_BY_OPERATION]?.defaultSurface || SCHEDULE_DEFAULT_SURFACE;
|
|
8
|
+
}
|