@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,630 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SCHEDULE_COVERAGE_MAX_VIOLATIONS,
|
|
3
|
+
SCHEDULE_COVERAGE_MAX_WINDOW_DAYS,
|
|
4
|
+
} from '../constants/schedule.ts';
|
|
5
|
+
import type {
|
|
6
|
+
CoverageDiagnosticObj,
|
|
7
|
+
CoverageEvaluationInput,
|
|
8
|
+
CoverageEvaluationResult,
|
|
9
|
+
CoverageViolationObj,
|
|
10
|
+
ScheduleActorWindowConfigObj,
|
|
11
|
+
ScheduleDerivedCountConfigObj,
|
|
12
|
+
ScheduleOperatingHoursConfigObj,
|
|
13
|
+
} from '../types/schedule.d.ts';
|
|
14
|
+
import {
|
|
15
|
+
advanceSweepCursor,
|
|
16
|
+
buildSegmentState,
|
|
17
|
+
clipCoverageViolationsToWindow,
|
|
18
|
+
collectSegmentBoundaries,
|
|
19
|
+
coverageTime,
|
|
20
|
+
getAssigneeStanding,
|
|
21
|
+
getCoverageViolationIdentity,
|
|
22
|
+
getCoverageViolationKey,
|
|
23
|
+
getSortedSweepIntervals,
|
|
24
|
+
isCountableStanding,
|
|
25
|
+
mergeAdjacentCoverageViolations,
|
|
26
|
+
normalizeCoverageInput,
|
|
27
|
+
sortCoverageViolations,
|
|
28
|
+
stableStringify,
|
|
29
|
+
type CoverageContext,
|
|
30
|
+
type SegmentState,
|
|
31
|
+
type SweepInterval,
|
|
32
|
+
} from './coverage.helpers.ts';
|
|
33
|
+
import {
|
|
34
|
+
compareScheduleIds,
|
|
35
|
+
getEffectiveRule,
|
|
36
|
+
resolveEffectiveScheduleRules,
|
|
37
|
+
type EffectiveRule,
|
|
38
|
+
type EffectiveRuleIndex,
|
|
39
|
+
} from './rule.ts';
|
|
40
|
+
import { getWorkingIntervalsInRange } from './workTime.ts';
|
|
41
|
+
export {
|
|
42
|
+
getCoverageViolationIdentity,
|
|
43
|
+
getCoverageViolationKey,
|
|
44
|
+
} from './coverage.helpers.ts';
|
|
45
|
+
|
|
46
|
+
/*
|
|
47
|
+
* The inspector ledger, evaluated at every instant of a half-open window:
|
|
48
|
+
* | # | Question at instant t | Kind | Subject | Needs a rule row? |
|
|
49
|
+
* | 1 | Does every slot of every shift running at t have its people? | REQUIRED_COUNT | room (or no room) | no (implicit, BLOCK) |
|
|
50
|
+
* | 2 | Are enough adults in the room for the children present at t? | DERIVED_COUNT | room | yes (ratios) |
|
|
51
|
+
* | 3 | Are more children forecast in the room at t than it is licensed for? | ROOM_CAPACITY | room | no (implicit, BLOCK) |
|
|
52
|
+
* | 4 | Is the building (or a named room) staffed with ≥ N adults at every open minute? | OPERATING_HOURS | center / room | yes (minAdults) |
|
|
53
|
+
* | 5 | Is every person working at t allowed to be there (qualified, not off, not double-booked)? | ELIGIBILITY | person | no (implicit, BLOCK) |
|
|
54
|
+
* | 6 | Has anyone worked too many hours in the window, or too little rest? | ACTOR_WINDOW_AGGREGATE | person | yes (cap / rest) |
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
type ViolationFields = Omit<
|
|
58
|
+
CoverageViolationObj,
|
|
59
|
+
'key' | 'identity' | 'ruleId' | 'severity' | 'startAt' | 'endAt'
|
|
60
|
+
>;
|
|
61
|
+
type AddViolation = (
|
|
62
|
+
effective: EffectiveRule,
|
|
63
|
+
start: number,
|
|
64
|
+
end: number,
|
|
65
|
+
fields: ViolationFields,
|
|
66
|
+
) => void;
|
|
67
|
+
|
|
68
|
+
/* Count distinct people against this rule's predicate, independent of their shift seat. */
|
|
69
|
+
function countPresence(
|
|
70
|
+
presence: SegmentState['centerPresence'] | undefined,
|
|
71
|
+
predicate: ScheduleDerivedCountConfigObj['predicate'],
|
|
72
|
+
state: SegmentState,
|
|
73
|
+
ctx: CoverageContext,
|
|
74
|
+
): number {
|
|
75
|
+
let count = 0;
|
|
76
|
+
for (const standing of presence?.values() ?? []) {
|
|
77
|
+
const person = ctx.resourcesById.get(standing.personId)!;
|
|
78
|
+
if (
|
|
79
|
+
isCountableStanding(
|
|
80
|
+
getAssigneeStanding(
|
|
81
|
+
person,
|
|
82
|
+
standing.occurrence,
|
|
83
|
+
standing.assignment,
|
|
84
|
+
predicate,
|
|
85
|
+
state.start,
|
|
86
|
+
state.end,
|
|
87
|
+
ctx,
|
|
88
|
+
),
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
count++;
|
|
92
|
+
}
|
|
93
|
+
return count;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/* Report slot deficits and assignment eligibility for every active shift. */
|
|
97
|
+
function evaluateShiftSegments(
|
|
98
|
+
state: SegmentState,
|
|
99
|
+
ctx: CoverageContext,
|
|
100
|
+
index: EffectiveRuleIndex,
|
|
101
|
+
add: AddViolation,
|
|
102
|
+
): void {
|
|
103
|
+
for (const shift of state.shifts) {
|
|
104
|
+
const fill = state.fills.get(shift.id)!;
|
|
105
|
+
const requiredRule = getEffectiveRule(
|
|
106
|
+
index,
|
|
107
|
+
'REQUIRED_COUNT',
|
|
108
|
+
shift.roomKnown ? shift.resourceId : null,
|
|
109
|
+
)!;
|
|
110
|
+
for (const slot of shift.slots) {
|
|
111
|
+
const filledCount = fill.filled.get(slot.key) ?? 0;
|
|
112
|
+
const shortfall = slot.requiredCount - filledCount;
|
|
113
|
+
if (shortfall > 0)
|
|
114
|
+
add(requiredRule, state.start, state.end, {
|
|
115
|
+
kind: 'REQUIRED_COUNT',
|
|
116
|
+
code: null,
|
|
117
|
+
resourceId: shift.roomKnown ? shift.resourceId : null,
|
|
118
|
+
occurrenceId: shift.id,
|
|
119
|
+
slotKey: slot.key,
|
|
120
|
+
degree: shortfall,
|
|
121
|
+
detail: { requiredCount: slot.requiredCount, filledCount, shortfall },
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
for (const standing of fill.standings) {
|
|
125
|
+
const effective = getEffectiveRule(
|
|
126
|
+
index,
|
|
127
|
+
'ELIGIBILITY',
|
|
128
|
+
standing.personId,
|
|
129
|
+
)!;
|
|
130
|
+
const base = {
|
|
131
|
+
kind: 'ELIGIBILITY' as const,
|
|
132
|
+
resourceId: standing.personId,
|
|
133
|
+
occurrenceId: shift.id,
|
|
134
|
+
slotKey: standing.placedSlotKey,
|
|
135
|
+
};
|
|
136
|
+
const person = ctx.resourcesById.get(standing.personId)!;
|
|
137
|
+
if (standing.notLabor)
|
|
138
|
+
add(effective, state.start, state.end, {
|
|
139
|
+
...base,
|
|
140
|
+
code: 'NOT_LABOR',
|
|
141
|
+
degree: 1,
|
|
142
|
+
detail: { kind: person.kind },
|
|
143
|
+
});
|
|
144
|
+
if (standing.retired)
|
|
145
|
+
add(effective, state.start, state.end, {
|
|
146
|
+
...base,
|
|
147
|
+
code: 'RESOURCE_RETIRED',
|
|
148
|
+
degree: 1,
|
|
149
|
+
detail: {
|
|
150
|
+
retiredAt:
|
|
151
|
+
person.retiredAt instanceof Date
|
|
152
|
+
? person.retiredAt.toISOString()
|
|
153
|
+
: person.retiredAt,
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
if (standing.slotUnknown)
|
|
157
|
+
add(effective, state.start, state.end, {
|
|
158
|
+
...base,
|
|
159
|
+
code: 'SLOT_UNKNOWN',
|
|
160
|
+
degree: 1,
|
|
161
|
+
detail: { slotKey: standing.assignment.slotKey },
|
|
162
|
+
});
|
|
163
|
+
const missing = standing.predicateMissing;
|
|
164
|
+
const missingCount =
|
|
165
|
+
missing.roles.length +
|
|
166
|
+
missing.tags.length +
|
|
167
|
+
missing.qualifications.length;
|
|
168
|
+
if (missingCount)
|
|
169
|
+
add(effective, state.start, state.end, {
|
|
170
|
+
...base,
|
|
171
|
+
code: 'PREDICATE_MISMATCH',
|
|
172
|
+
degree: missingCount,
|
|
173
|
+
detail: {
|
|
174
|
+
missingRoles: missing.roles,
|
|
175
|
+
missingTags: missing.tags,
|
|
176
|
+
missingQualifications: missing.qualifications,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
if (standing.expired.length)
|
|
180
|
+
add(effective, state.start, state.end, {
|
|
181
|
+
...base,
|
|
182
|
+
code: 'QUALIFICATION_EXPIRED',
|
|
183
|
+
degree: standing.expired.length,
|
|
184
|
+
detail: { expired: standing.expired },
|
|
185
|
+
});
|
|
186
|
+
if (standing.onTimeOff.length)
|
|
187
|
+
add(effective, state.start, state.end, {
|
|
188
|
+
...base,
|
|
189
|
+
code: 'TIME_OFF',
|
|
190
|
+
degree: 1,
|
|
191
|
+
detail: { timeOffIds: standing.onTimeOff },
|
|
192
|
+
});
|
|
193
|
+
if (standing.overlapping.length)
|
|
194
|
+
add(effective, state.start, state.end, {
|
|
195
|
+
...base,
|
|
196
|
+
code: 'OVERLAP',
|
|
197
|
+
degree: standing.overlapping.length,
|
|
198
|
+
detail: { overlappingOccurrenceIds: standing.overlapping },
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* Compare summed child demand with blended staff ratios and licensed room capacity. */
|
|
205
|
+
function evaluateRoomSegments(
|
|
206
|
+
state: SegmentState,
|
|
207
|
+
ctx: CoverageContext,
|
|
208
|
+
index: EffectiveRuleIndex,
|
|
209
|
+
add: AddViolation,
|
|
210
|
+
diagnostics: CoverageDiagnosticObj[],
|
|
211
|
+
): void {
|
|
212
|
+
for (const roomId of [...state.demandByRoom.keys()].sort(
|
|
213
|
+
compareScheduleIds,
|
|
214
|
+
)) {
|
|
215
|
+
const quantities = Object.fromEntries(
|
|
216
|
+
[...state.demandByRoom.get(roomId)!].sort(([a], [b]) =>
|
|
217
|
+
a.localeCompare(b),
|
|
218
|
+
),
|
|
219
|
+
);
|
|
220
|
+
const derived = getEffectiveRule(index, 'DERIVED_COUNT', roomId);
|
|
221
|
+
if (derived) {
|
|
222
|
+
const config = derived.rule!.config as ScheduleDerivedCountConfigObj;
|
|
223
|
+
let load = 0;
|
|
224
|
+
const knownQuantities: Record<string, number> = {};
|
|
225
|
+
const ratios: Record<string, number> = {};
|
|
226
|
+
for (const [key, quantity] of Object.entries(quantities)) {
|
|
227
|
+
const ratio = config.ratios[key];
|
|
228
|
+
if (ratio == null) {
|
|
229
|
+
diagnostics.push({
|
|
230
|
+
code: 'RATIO_NOT_CONFIGURED',
|
|
231
|
+
ruleId: derived.ruleId,
|
|
232
|
+
resourceId: roomId,
|
|
233
|
+
occurrenceId: null,
|
|
234
|
+
demandId: null,
|
|
235
|
+
detail: { key },
|
|
236
|
+
});
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
load += quantity / ratio;
|
|
240
|
+
knownQuantities[key] = quantity;
|
|
241
|
+
ratios[key] = ratio;
|
|
242
|
+
}
|
|
243
|
+
const requiredCount = Math.ceil(load - 1e-9);
|
|
244
|
+
const presentCount = countPresence(
|
|
245
|
+
state.presenceByRoom.get(roomId),
|
|
246
|
+
config.predicate,
|
|
247
|
+
state,
|
|
248
|
+
ctx,
|
|
249
|
+
);
|
|
250
|
+
const shortfall = requiredCount - presentCount;
|
|
251
|
+
if (shortfall > 0)
|
|
252
|
+
add(derived, state.start, state.end, {
|
|
253
|
+
kind: 'DERIVED_COUNT',
|
|
254
|
+
code: null,
|
|
255
|
+
resourceId: roomId,
|
|
256
|
+
occurrenceId: null,
|
|
257
|
+
slotKey: null,
|
|
258
|
+
degree: shortfall,
|
|
259
|
+
detail: {
|
|
260
|
+
quantities: knownQuantities,
|
|
261
|
+
ratios,
|
|
262
|
+
requiredCount,
|
|
263
|
+
presentCount,
|
|
264
|
+
shortfall,
|
|
265
|
+
},
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
const capacity = ctx.resourcesById.get(roomId)?.capacity;
|
|
269
|
+
const demandTotal = Object.values(quantities).reduce(
|
|
270
|
+
(sum, value) => sum + value,
|
|
271
|
+
0,
|
|
272
|
+
);
|
|
273
|
+
if (capacity != null && demandTotal > capacity)
|
|
274
|
+
add(
|
|
275
|
+
getEffectiveRule(index, 'ROOM_CAPACITY', roomId)!,
|
|
276
|
+
state.start,
|
|
277
|
+
state.end,
|
|
278
|
+
{
|
|
279
|
+
kind: 'ROOM_CAPACITY',
|
|
280
|
+
code: null,
|
|
281
|
+
resourceId: roomId,
|
|
282
|
+
occurrenceId: null,
|
|
283
|
+
slotKey: null,
|
|
284
|
+
degree: demandTotal - capacity,
|
|
285
|
+
detail: { capacity, demandTotal, quantities },
|
|
286
|
+
},
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/* Require distinct adults throughout every open center or explicitly scoped room interval. */
|
|
292
|
+
function evaluateOperatingHours(
|
|
293
|
+
state: SegmentState,
|
|
294
|
+
ctx: CoverageContext,
|
|
295
|
+
index: EffectiveRuleIndex,
|
|
296
|
+
add: AddViolation,
|
|
297
|
+
): void {
|
|
298
|
+
if (!state.isOpen || state.centerBlocked) return;
|
|
299
|
+
const subjects: Array<string | null> = [
|
|
300
|
+
null,
|
|
301
|
+
...[...(index.byKind.get('OPERATING_HOURS')?.bySubject.keys() ?? [])].sort(
|
|
302
|
+
compareScheduleIds,
|
|
303
|
+
),
|
|
304
|
+
];
|
|
305
|
+
for (const resourceId of subjects) {
|
|
306
|
+
if (resourceId !== null && state.blockedRooms.has(resourceId)) continue;
|
|
307
|
+
const effective = getEffectiveRule(index, 'OPERATING_HOURS', resourceId);
|
|
308
|
+
if (!effective) continue;
|
|
309
|
+
const config = effective.rule!.config as ScheduleOperatingHoursConfigObj;
|
|
310
|
+
const presentCount = countPresence(
|
|
311
|
+
resourceId === null
|
|
312
|
+
? state.centerPresence
|
|
313
|
+
: state.presenceByRoom.get(resourceId),
|
|
314
|
+
config.predicate,
|
|
315
|
+
state,
|
|
316
|
+
ctx,
|
|
317
|
+
);
|
|
318
|
+
if (presentCount < config.minAdults)
|
|
319
|
+
add(effective, state.start, state.end, {
|
|
320
|
+
kind: 'OPERATING_HOURS',
|
|
321
|
+
code: null,
|
|
322
|
+
resourceId,
|
|
323
|
+
occurrenceId: null,
|
|
324
|
+
slotKey: null,
|
|
325
|
+
degree: config.minAdults - presentCount,
|
|
326
|
+
detail: {
|
|
327
|
+
scope: resourceId === null ? 'CENTER' : 'ROOM',
|
|
328
|
+
minAdults: config.minAdults,
|
|
329
|
+
presentCount,
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/* Judge rolling absolute-hour windows and rest using cross-calendar assignment evidence. */
|
|
336
|
+
function evaluateActorWindows(
|
|
337
|
+
ctx: CoverageContext,
|
|
338
|
+
index: EffectiveRuleIndex,
|
|
339
|
+
add: AddViolation,
|
|
340
|
+
): void {
|
|
341
|
+
for (const [personId, timeline] of ctx.timeline) {
|
|
342
|
+
if (ctx.resourcesById.get(personId)?.kind !== 'LABOR') continue;
|
|
343
|
+
const effective = getEffectiveRule(
|
|
344
|
+
index,
|
|
345
|
+
'ACTOR_WINDOW_AGGREGATE',
|
|
346
|
+
personId,
|
|
347
|
+
);
|
|
348
|
+
if (!effective) continue;
|
|
349
|
+
const config = effective.rule!.config as ScheduleActorWindowConfigObj;
|
|
350
|
+
/* Anchor violations only on evaluated rows unless candidate simulation needs the later tail. */
|
|
351
|
+
const isSubject = (row: (typeof timeline)[number]): boolean =>
|
|
352
|
+
row.start < ctx.to &&
|
|
353
|
+
row.end > ctx.from &&
|
|
354
|
+
(row.own || !!ctx.input.options?.anchorExternal);
|
|
355
|
+
if (config.windowDays && config.maxMinutes) {
|
|
356
|
+
const anchors = timeline
|
|
357
|
+
.filter(isSubject)
|
|
358
|
+
.sort(
|
|
359
|
+
(a, b) =>
|
|
360
|
+
a.end - b.end || compareScheduleIds(a.occurrenceId, b.occurrenceId),
|
|
361
|
+
);
|
|
362
|
+
for (const anchor of anchors) {
|
|
363
|
+
const windowStart = anchor.end - config.windowDays * 86_400_000;
|
|
364
|
+
let total = 0;
|
|
365
|
+
for (const row of timeline) {
|
|
366
|
+
if (row.start >= anchor.end) break;
|
|
367
|
+
total +=
|
|
368
|
+
Math.max(
|
|
369
|
+
0,
|
|
370
|
+
Math.min(row.end, anchor.end) - Math.max(row.start, windowStart),
|
|
371
|
+
) / 60_000;
|
|
372
|
+
}
|
|
373
|
+
if (total > config.maxMinutes)
|
|
374
|
+
add(effective, anchor.start, anchor.end, {
|
|
375
|
+
kind: 'ACTOR_WINDOW_AGGREGATE',
|
|
376
|
+
code: 'HOURS',
|
|
377
|
+
resourceId: personId,
|
|
378
|
+
occurrenceId: anchor.occurrenceId,
|
|
379
|
+
slotKey: null,
|
|
380
|
+
degree: total - config.maxMinutes,
|
|
381
|
+
detail: {
|
|
382
|
+
windowDays: config.windowDays,
|
|
383
|
+
maxMinutes: config.maxMinutes,
|
|
384
|
+
totalMinutes: Math.round(total),
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (config.minRestMinutes) {
|
|
390
|
+
let previous = timeline[0];
|
|
391
|
+
for (let i = 1; i < timeline.length; i++) {
|
|
392
|
+
const current = timeline[i];
|
|
393
|
+
const gap = (current.start - previous.end) / 60_000;
|
|
394
|
+
if (isSubject(current) && gap >= 0 && gap < config.minRestMinutes) {
|
|
395
|
+
// Adjacent shifts have zero rest; attach that violation to the following shift's positive interval.
|
|
396
|
+
add(
|
|
397
|
+
effective,
|
|
398
|
+
gap === 0 || current.start <= ctx.from
|
|
399
|
+
? current.start
|
|
400
|
+
: previous.end,
|
|
401
|
+
gap === 0 || current.start <= ctx.from
|
|
402
|
+
? current.end
|
|
403
|
+
: current.start,
|
|
404
|
+
{
|
|
405
|
+
kind: 'ACTOR_WINDOW_AGGREGATE',
|
|
406
|
+
code: 'REST',
|
|
407
|
+
resourceId: personId,
|
|
408
|
+
occurrenceId: current.occurrenceId,
|
|
409
|
+
slotKey: null,
|
|
410
|
+
degree: config.minRestMinutes - gap,
|
|
411
|
+
detail: {
|
|
412
|
+
minRestMinutes: config.minRestMinutes,
|
|
413
|
+
restMinutes: Math.round(gap),
|
|
414
|
+
previousOccurrenceId: previous.occurrenceId,
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
// Nested overlapping assignments cannot erase a later-finishing shift from the rest calculation.
|
|
420
|
+
if (current.end >= previous.end) previous = current;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/* Evaluate a coverage window without I/O, mutation of input or wall-clock access. */
|
|
427
|
+
export function evaluateScheduleCoverage(
|
|
428
|
+
input: CoverageEvaluationInput,
|
|
429
|
+
): CoverageEvaluationResult {
|
|
430
|
+
const ctx = normalizeCoverageInput(input);
|
|
431
|
+
const { index, diagnostics: ruleDiagnostics } = resolveEffectiveScheduleRules(
|
|
432
|
+
input.rules,
|
|
433
|
+
input.calendarId,
|
|
434
|
+
ctx.resourcesById,
|
|
435
|
+
);
|
|
436
|
+
const diagnostics = [...ctx.diagnostics, ...ruleDiagnostics];
|
|
437
|
+
const raw: CoverageViolationObj[] = [];
|
|
438
|
+
const stats = {
|
|
439
|
+
boundaryCount: 0,
|
|
440
|
+
segmentCount: 0,
|
|
441
|
+
occurrenceCount: ctx.occurrences.length,
|
|
442
|
+
externalCount: input.external.length,
|
|
443
|
+
operatingIntervalCount: 0,
|
|
444
|
+
rawViolationCount: 0,
|
|
445
|
+
};
|
|
446
|
+
if (
|
|
447
|
+
!(ctx.to > ctx.from) ||
|
|
448
|
+
ctx.to - ctx.from > SCHEDULE_COVERAGE_MAX_WINDOW_DAYS * 86_400_000
|
|
449
|
+
) {
|
|
450
|
+
return {
|
|
451
|
+
violations: [],
|
|
452
|
+
diagnostics: [
|
|
453
|
+
...diagnostics,
|
|
454
|
+
{
|
|
455
|
+
code: 'RULE_CONFIG_INVALID',
|
|
456
|
+
ruleId: null,
|
|
457
|
+
resourceId: null,
|
|
458
|
+
occurrenceId: null,
|
|
459
|
+
demandId: null,
|
|
460
|
+
detail: {
|
|
461
|
+
field: 'window',
|
|
462
|
+
maxDays: SCHEDULE_COVERAGE_MAX_WINDOW_DAYS,
|
|
463
|
+
},
|
|
464
|
+
},
|
|
465
|
+
],
|
|
466
|
+
stats,
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
const operating = index.byKind.get('OPERATING_HOURS')!;
|
|
470
|
+
if (operating.fallback || operating.bySubject.size) {
|
|
471
|
+
if (input.operatingHours) {
|
|
472
|
+
try {
|
|
473
|
+
ctx.operatingIntervals = getWorkingIntervalsInRange(
|
|
474
|
+
{ ...input.operatingHours, timeZone: input.timeZone },
|
|
475
|
+
new Date(ctx.from - 86_400_000),
|
|
476
|
+
new Date(ctx.to),
|
|
477
|
+
);
|
|
478
|
+
} catch {
|
|
479
|
+
ctx.operatingIntervals = [];
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (!ctx.operatingIntervals.length)
|
|
483
|
+
diagnostics.push({
|
|
484
|
+
code: 'OPERATING_HOURS_NOT_CONFIGURED',
|
|
485
|
+
ruleId:
|
|
486
|
+
operating.fallback?.id ??
|
|
487
|
+
[...operating.bySubject.values()].sort((a, b) =>
|
|
488
|
+
compareScheduleIds(a.id, b.id),
|
|
489
|
+
)[0]?.id ??
|
|
490
|
+
null,
|
|
491
|
+
resourceId: null,
|
|
492
|
+
occurrenceId: null,
|
|
493
|
+
demandId: null,
|
|
494
|
+
detail: {},
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
const bounds = collectSegmentBoundaries(ctx);
|
|
498
|
+
stats.boundaryCount = bounds.length;
|
|
499
|
+
stats.operatingIntervalCount = ctx.operatingIntervals.length;
|
|
500
|
+
let capped = false;
|
|
501
|
+
/* Apply the raw-output cap to every evaluator, including cross-segment actor rules. */
|
|
502
|
+
const add: AddViolation = (effective, start, end, fields) => {
|
|
503
|
+
if (!(end > start)) return;
|
|
504
|
+
if (raw.length >= SCHEDULE_COVERAGE_MAX_VIOLATIONS) {
|
|
505
|
+
capped = true;
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
const violation = {
|
|
509
|
+
...fields,
|
|
510
|
+
ruleId: effective.ruleId,
|
|
511
|
+
severity: effective.severity,
|
|
512
|
+
startAt: new Date(start),
|
|
513
|
+
endAt: new Date(end),
|
|
514
|
+
identity: '',
|
|
515
|
+
key: '',
|
|
516
|
+
};
|
|
517
|
+
violation.identity = getCoverageViolationIdentity(violation);
|
|
518
|
+
violation.key = getCoverageViolationKey(violation);
|
|
519
|
+
raw.push(violation);
|
|
520
|
+
};
|
|
521
|
+
const occurrences = getSortedSweepIntervals(ctx.occurrences, (row) => ({
|
|
522
|
+
startAt: row.start,
|
|
523
|
+
endAt: row.end,
|
|
524
|
+
}));
|
|
525
|
+
const demand = getSortedSweepIntervals(ctx.demand, (row) => ({
|
|
526
|
+
startAt: coverageTime(row.startAt),
|
|
527
|
+
endAt: coverageTime(row.endAt),
|
|
528
|
+
}));
|
|
529
|
+
const timeOff = getSortedSweepIntervals(ctx.timeOff, (row) => ({
|
|
530
|
+
startAt: coverageTime(row.startAt),
|
|
531
|
+
endAt: coverageTime(row.endAt),
|
|
532
|
+
}));
|
|
533
|
+
const operatingIntervals = getSortedSweepIntervals(
|
|
534
|
+
ctx.operatingIntervals,
|
|
535
|
+
(row) => row,
|
|
536
|
+
);
|
|
537
|
+
const activeOccurrences = new Set<(typeof occurrences)[number]>();
|
|
538
|
+
const activeDemand = new Set<(typeof demand)[number]>();
|
|
539
|
+
const activeTimeOff = new Set<(typeof timeOff)[number]>();
|
|
540
|
+
const activeOperating = new Set<
|
|
541
|
+
SweepInterval<{ startAt: number; endAt: number }>
|
|
542
|
+
>();
|
|
543
|
+
let occurrenceCursor = 0;
|
|
544
|
+
let demandCursor = 0;
|
|
545
|
+
let timeOffCursor = 0;
|
|
546
|
+
let operatingCursor = 0;
|
|
547
|
+
for (let i = 0; i < bounds.length - 1; i++) {
|
|
548
|
+
const start = bounds[i];
|
|
549
|
+
const end = bounds[i + 1];
|
|
550
|
+
stats.segmentCount++;
|
|
551
|
+
occurrenceCursor = advanceSweepCursor(
|
|
552
|
+
occurrences,
|
|
553
|
+
occurrenceCursor,
|
|
554
|
+
start,
|
|
555
|
+
activeOccurrences,
|
|
556
|
+
);
|
|
557
|
+
demandCursor = advanceSweepCursor(
|
|
558
|
+
demand,
|
|
559
|
+
demandCursor,
|
|
560
|
+
start,
|
|
561
|
+
activeDemand,
|
|
562
|
+
);
|
|
563
|
+
timeOffCursor = advanceSweepCursor(
|
|
564
|
+
timeOff,
|
|
565
|
+
timeOffCursor,
|
|
566
|
+
start,
|
|
567
|
+
activeTimeOff,
|
|
568
|
+
);
|
|
569
|
+
operatingCursor = advanceSweepCursor(
|
|
570
|
+
operatingIntervals,
|
|
571
|
+
operatingCursor,
|
|
572
|
+
start,
|
|
573
|
+
activeOperating,
|
|
574
|
+
);
|
|
575
|
+
const state = buildSegmentState(
|
|
576
|
+
start,
|
|
577
|
+
end,
|
|
578
|
+
activeOccurrences,
|
|
579
|
+
activeDemand,
|
|
580
|
+
activeTimeOff,
|
|
581
|
+
activeOperating.size > 0,
|
|
582
|
+
ctx,
|
|
583
|
+
);
|
|
584
|
+
evaluateShiftSegments(state, ctx, index, add);
|
|
585
|
+
evaluateRoomSegments(state, ctx, index, add, diagnostics);
|
|
586
|
+
evaluateOperatingHours(state, ctx, index, add);
|
|
587
|
+
if (capped) break;
|
|
588
|
+
}
|
|
589
|
+
if (!capped) evaluateActorWindows(ctx, index, add);
|
|
590
|
+
if (capped)
|
|
591
|
+
diagnostics.push({
|
|
592
|
+
code: 'VIOLATION_CAP',
|
|
593
|
+
ruleId: null,
|
|
594
|
+
resourceId: null,
|
|
595
|
+
occurrenceId: null,
|
|
596
|
+
demandId: null,
|
|
597
|
+
detail: { limit: SCHEDULE_COVERAGE_MAX_VIOLATIONS },
|
|
598
|
+
});
|
|
599
|
+
stats.rawViolationCount = raw.length;
|
|
600
|
+
const uniqueDiagnostics = new Map<string, CoverageDiagnosticObj>();
|
|
601
|
+
for (const diagnostic of diagnostics) {
|
|
602
|
+
const key =
|
|
603
|
+
diagnostic.code === 'UNKNOWN_RESOURCE'
|
|
604
|
+
? `${diagnostic.code}|${diagnostic.resourceId}`
|
|
605
|
+
: diagnostic.code === 'RATIO_NOT_CONFIGURED'
|
|
606
|
+
? `${diagnostic.code}|${diagnostic.ruleId}|${diagnostic.detail.key}`
|
|
607
|
+
: stableStringify(diagnostic);
|
|
608
|
+
if (!uniqueDiagnostics.has(key)) uniqueDiagnostics.set(key, diagnostic);
|
|
609
|
+
}
|
|
610
|
+
return {
|
|
611
|
+
violations: sortCoverageViolations(
|
|
612
|
+
clipCoverageViolationsToWindow(
|
|
613
|
+
mergeAdjacentCoverageViolations(raw),
|
|
614
|
+
ctx.from,
|
|
615
|
+
ctx.to,
|
|
616
|
+
),
|
|
617
|
+
),
|
|
618
|
+
diagnostics: [...uniqueDiagnostics]
|
|
619
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
620
|
+
.map(([, value]) => value),
|
|
621
|
+
stats,
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/* Determine whether any persisted or evaluated violation refuses publication. */
|
|
626
|
+
export function hasBlockingViolations(
|
|
627
|
+
violations: Array<Pick<CoverageViolationObj, 'severity'>>,
|
|
628
|
+
): boolean {
|
|
629
|
+
return violations.some((violation) => violation.severity === 'BLOCK');
|
|
630
|
+
}
|