@jsb188/schedule 1.1.12 → 1.1.13
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/LICENSE +21 -0
- package/package.json +33 -14
- package/src/constants/schedule.ts +710 -306
- package/src/types/schedule.d.ts +1827 -456
- package/src/ui/ScheduleSettingsUI.tsx +21 -23
- package/src/utils/autofill.ts +469 -0
- package/src/utils/availability.ts +167 -0
- package/src/utils/candidates.ts +447 -0
- package/src/utils/coverage.helpers.ts +509 -554
- package/src/utils/coverage.ts +1121 -526
- package/src/utils/cpm.helpers.ts +27 -29
- package/src/utils/cpm.ts +53 -41
- package/src/utils/cpmLeveling.helpers.ts +89 -16
- package/src/utils/cpmLeveling.ts +28 -26
- package/src/utils/cycle.ts +325 -0
- package/src/utils/demand.ts +239 -0
- package/src/utils/eligibility.ts +553 -400
- package/src/utils/ids.ts +47 -0
- package/src/utils/laborRules.ts +740 -0
- package/src/utils/localTime.ts +312 -0
- package/src/utils/offer.ts +300 -0
- package/src/utils/planCsv.ts +73 -19
- package/src/utils/predicate.ts +111 -0
- package/src/utils/request.ts +212 -114
- package/src/utils/resource.ts +412 -112
- package/src/utils/rule.ts +697 -163
- package/src/utils/rulePresets.ts +116 -0
- package/src/utils/run.ts +0 -8
- package/src/utils/scheduleText.ts +4074 -0
- package/src/utils/shiftChange.ts +279 -0
- package/src/utils/standing.ts +730 -0
- package/src/utils/template.ts +619 -0
- package/src/utils/violation.ts +184 -0
- package/src/utils/workTime.ts +341 -120
- package/src/utils/defaults.ts +0 -8
- package/src/utils/recurrence.ts +0 -616
- package/src/utils/series.ts +0 -161
- package/src/utils/slot.ts +0 -208
- package/tests/coverage.fixtures.ts +0 -111
- package/tests/coverage.test.ts +0 -503
- package/tests/cpm.bench.test.ts +0 -154
- package/tests/cpm.test.ts +0 -354
- package/tests/cpmCorrectness.test.ts +0 -327
- package/tests/cpmLeveling.test.ts +0 -471
- package/tests/eligibility.test.ts +0 -320
- package/tests/gantt-geometry.test.ts +0 -367
- package/tests/gantt-histogram.test.ts +0 -78
- package/tests/gantt-keyboard.test.ts +0 -69
- package/tests/helpers/cpmFixtures.ts +0 -101
- package/tests/projectInputs.test.ts +0 -165
- package/tests/recurrence.test.ts +0 -68
- package/tests/request.test.ts +0 -99
- package/tests/resource.test.ts +0 -95
- package/tests/rule.test.ts +0 -98
- package/tests/series.test.ts +0 -120
- package/tests/workTime.test.ts +0 -1007
- package/tests/workTimeContracts.test.ts +0 -111
- package/tsconfig.json +0 -13
- package/vitest.config.ts +0 -8
package/src/utils/coverage.ts
CHANGED
|
@@ -1,630 +1,1225 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SCHEDULE_COVERAGE_MAX_VIOLATIONS,
|
|
3
3
|
SCHEDULE_COVERAGE_MAX_WINDOW_DAYS,
|
|
4
|
+
SCHEDULE_COVERAGE_MAX_WINDOW_MS,
|
|
5
|
+
SCHEDULE_PERSON_RULE_KINDS,
|
|
4
6
|
} from '../constants/schedule.ts';
|
|
5
7
|
import type {
|
|
6
|
-
CoverageDiagnosticObj,
|
|
7
8
|
CoverageEvaluationInput,
|
|
8
9
|
CoverageEvaluationResult,
|
|
10
|
+
CoverageResourceInput,
|
|
11
|
+
CoverageRoleInput,
|
|
12
|
+
CoverageShiftInput,
|
|
9
13
|
CoverageViolationObj,
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
ScheduleCoverageViolationCode,
|
|
15
|
+
ScheduleDemandIntervalObj,
|
|
16
|
+
ScheduleEffectiveRule,
|
|
17
|
+
ScheduleEffectiveRuleIndex,
|
|
12
18
|
ScheduleOperatingHoursConfigObj,
|
|
19
|
+
SchedulePredicateObj,
|
|
20
|
+
ScheduleRatioConfigObj,
|
|
21
|
+
ScheduleShiftSimulationObj,
|
|
22
|
+
ScheduleStaffingRequirementConfigObj,
|
|
13
23
|
} from '../types/schedule.d.ts';
|
|
14
24
|
import {
|
|
25
|
+
addCoverageDiagnostic,
|
|
15
26
|
advanceSweepCursor,
|
|
16
|
-
buildSegmentState,
|
|
17
27
|
clipCoverageViolationsToWindow,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
28
|
+
collectQualificationExpiryDiagnostics,
|
|
29
|
+
collectSweepBoundaries,
|
|
30
|
+
combineCoverageViolationsByKey,
|
|
31
|
+
compareCodeUnits,
|
|
32
|
+
createCoverageDiagnostics,
|
|
33
|
+
createCoverageRawCollector,
|
|
34
|
+
expandStaffingWindows,
|
|
35
|
+
filterKnownDemandRows,
|
|
36
|
+
getCoverageLocalDates,
|
|
37
|
+
getSortedCoverageDiagnostics,
|
|
38
|
+
groupCoverageRowsByPerson,
|
|
39
|
+
groupCoverageSeatsByPerson,
|
|
40
|
+
indexCoverageCatalogs,
|
|
41
|
+
indexQualificationExpiries,
|
|
25
42
|
mergeAdjacentCoverageViolations,
|
|
26
|
-
|
|
43
|
+
normalizeCoverageSeats,
|
|
44
|
+
pushCoverageViolation,
|
|
45
|
+
roundCoverageQuantity,
|
|
27
46
|
sortCoverageViolations,
|
|
28
|
-
|
|
47
|
+
sortSweepIntervals,
|
|
48
|
+
subtractCoverageIntervals,
|
|
29
49
|
type CoverageContext,
|
|
30
|
-
type
|
|
50
|
+
type CoverageDiagnosticCollector,
|
|
51
|
+
type CoveragePresence,
|
|
52
|
+
type CoverageRawCollector,
|
|
31
53
|
type SweepInterval,
|
|
32
54
|
} from './coverage.helpers.ts';
|
|
55
|
+
import { expandScheduleDemand } from './demand.ts';
|
|
56
|
+
import { getShiftEligibilityFindings } from './eligibility.ts';
|
|
57
|
+
import { compareScheduleIds } from './ids.ts';
|
|
33
58
|
import {
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
evaluateNoticePeriodRules,
|
|
60
|
+
evaluatePersonRules,
|
|
61
|
+
} from './laborRules.ts';
|
|
62
|
+
import { getScheduleLocalDate, toScheduleTimeMs } from './localTime.ts';
|
|
63
|
+
import { doesResourceMatchPredicate } from './predicate.ts';
|
|
64
|
+
import {
|
|
65
|
+
getEffectiveScheduleRules,
|
|
36
66
|
resolveEffectiveScheduleRules,
|
|
37
|
-
type EffectiveRule,
|
|
38
|
-
type EffectiveRuleIndex,
|
|
39
67
|
} from './rule.ts';
|
|
40
|
-
import { getWorkingIntervalsInRange } from './workTime.ts';
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
68
|
+
import { getWorkingIntervalsInRange, isWorkingCalendar } from './workTime.ts';
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The staff-schedule ledger of one calendar over a half-open window. Pure:
|
|
72
|
+
* no I/O, no clock reads (`now` is input), no mutation of input; output is
|
|
73
|
+
* deterministic whatever the input order.
|
|
74
|
+
*
|
|
75
|
+
* | Question | Kind | Subject | How |
|
|
76
|
+
* | Is each seat's person allowed to hold it? | ELIGIBILITY | person x seat | utils/eligibility.ts findings |
|
|
77
|
+
* | Are enough people of the right roles on in each weekly window? | STAFFING_REQUIREMENT | location (or one room) | sweep |
|
|
78
|
+
* | Are enough adults in a room for its children? Is any age group too big? | RATIO | room | sweep over demand |
|
|
79
|
+
* | Are more children in a room than it is licensed for? | ROOM_CAPACITY (implicit) | room | sweep over demand |
|
|
80
|
+
* | Is the location (or a room) staffed while it is open? | OPERATING_HOURS | location / room | sweep over work hours |
|
|
81
|
+
* | Hours, rest, consecutive days, seat length | MAX_HOURS ... SHIFT_LENGTH | person | utils/laborRules.ts |
|
|
82
|
+
* | Published late, changed late | NOTICE_PERIOD | seat assignee | utils/laborRules.ts |
|
|
83
|
+
*
|
|
84
|
+
* Presence: an assigned live seat counts its assignee at an instant unless a
|
|
85
|
+
* BLOCK ELIGIBILITY finding of that seat covers the instant (UNAVAILABLE
|
|
86
|
+
* never stops counting). A person counts once per room and once in the
|
|
87
|
+
* location, however many seats they hold.
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
const FILE_CONSTANTS = {
|
|
91
|
+
ratioEpsilon: 1e-9,
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// The rules the sweep evaluates, resolved once per evaluation
|
|
95
|
+
interface CoverageSweepRules {
|
|
96
|
+
index: ScheduleEffectiveRuleIndex;
|
|
97
|
+
staffing: ScheduleEffectiveRule[];
|
|
98
|
+
staffingRoleIds: Array<Set<string>>;
|
|
99
|
+
operatingLocation: ScheduleEffectiveRule[];
|
|
100
|
+
operatingRooms: Array<{ roomId: string; rules: ScheduleEffectiveRule[] }>;
|
|
101
|
+
ratioByRoom: Map<string, ScheduleEffectiveRule[]>;
|
|
102
|
+
capacityByRoom: Map<string, ScheduleEffectiveRule[]>;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Who counts: predicate matches memoized per predicate object and person, and
|
|
106
|
+
// the roles a null-role seat may count its person in, memoized per seat
|
|
107
|
+
interface CoveragePeopleMatcher {
|
|
108
|
+
resourcesById: ReadonlyMap<string, CoverageResourceInput>;
|
|
109
|
+
rolesById: ReadonlyMap<string, CoverageRoleInput>;
|
|
110
|
+
expiries: Map<string, Map<string, number | null>>;
|
|
111
|
+
memo: WeakMap<SchedulePredicateObj, Map<string, boolean>>;
|
|
112
|
+
qualifiedRolesBySeat: Map<string, ReadonlySet<string>>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// One constant stretch of the sweep
|
|
116
|
+
interface CoverageSegment {
|
|
117
|
+
start: number;
|
|
118
|
+
end: number;
|
|
119
|
+
presences: CoveragePresence[];
|
|
120
|
+
presencesByRoom: Map<string, CoveragePresence[]>;
|
|
121
|
+
childrenByRoom: Map<string, Map<string, number>>;
|
|
122
|
+
staffingRuleIndexes: number[];
|
|
123
|
+
isOpen: boolean;
|
|
124
|
+
}
|
|
45
125
|
|
|
46
126
|
/*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
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) |
|
|
127
|
+
* Normalize the evaluation input once: catalogs, live seats, per-person
|
|
128
|
+
* seats / time off / availability, and the input diagnostics (unknown
|
|
129
|
+
* resources and roles, unreadable qualification expiries).
|
|
55
130
|
*/
|
|
131
|
+
function normalizeCoverageInput(
|
|
132
|
+
input: CoverageEvaluationInput,
|
|
133
|
+
from: number,
|
|
134
|
+
to: number,
|
|
135
|
+
): CoverageContext {
|
|
136
|
+
const diagnostics = createCoverageDiagnostics();
|
|
137
|
+
const { resourcesById, rolesById } = indexCoverageCatalogs(input);
|
|
138
|
+
const { ownSeats, externalSeats } = normalizeCoverageSeats(
|
|
139
|
+
input,
|
|
140
|
+
resourcesById,
|
|
141
|
+
rolesById,
|
|
142
|
+
diagnostics,
|
|
143
|
+
);
|
|
144
|
+
collectQualificationExpiryDiagnostics(resourcesById, diagnostics);
|
|
145
|
+
return {
|
|
146
|
+
input,
|
|
147
|
+
from,
|
|
148
|
+
to,
|
|
149
|
+
resourcesById,
|
|
150
|
+
rolesById,
|
|
151
|
+
ownSeats,
|
|
152
|
+
externalSeats,
|
|
153
|
+
seatsByPerson: groupCoverageSeatsByPerson(
|
|
154
|
+
[...ownSeats, ...externalSeats],
|
|
155
|
+
resourcesById,
|
|
156
|
+
),
|
|
157
|
+
timeOffByPerson: groupCoverageRowsByPerson(
|
|
158
|
+
input.timeOff,
|
|
159
|
+
resourcesById,
|
|
160
|
+
diagnostics,
|
|
161
|
+
),
|
|
162
|
+
availabilityByPerson: groupCoverageRowsByPerson(
|
|
163
|
+
input.availability,
|
|
164
|
+
resourcesById,
|
|
165
|
+
diagnostics,
|
|
166
|
+
),
|
|
167
|
+
diagnostics,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
56
170
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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,
|
|
171
|
+
/*
|
|
172
|
+
* Check whether a seat overlaps the evaluated window.
|
|
173
|
+
*/
|
|
174
|
+
function isSeatInWindow(
|
|
175
|
+
seat: { start: number; end: number },
|
|
73
176
|
ctx: CoverageContext,
|
|
74
|
-
):
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
177
|
+
): boolean {
|
|
178
|
+
return seat.start < ctx.to && seat.end > ctx.from;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/*
|
|
182
|
+
* Turn every ELIGIBILITY finding of every assigned live seat overlapping the
|
|
183
|
+
* window into a violation (OVERLAP once per pair, on the larger seat id), and
|
|
184
|
+
* return, per seat, the stretches in which a BLOCK finding other than
|
|
185
|
+
* UNAVAILABLE stops the seat counting its person.
|
|
186
|
+
*/
|
|
187
|
+
function collectEligibilityViolations(
|
|
188
|
+
ctx: CoverageContext,
|
|
189
|
+
index: ScheduleEffectiveRuleIndex,
|
|
190
|
+
raw: CoverageRawCollector,
|
|
191
|
+
): Map<string, Array<{ start: number; end: number }>> {
|
|
192
|
+
const blockedBySeat = new Map<
|
|
193
|
+
string,
|
|
194
|
+
Array<{ start: number; end: number }>
|
|
195
|
+
>();
|
|
196
|
+
for (const seat of ctx.ownSeats) {
|
|
197
|
+
const personId = seat.shift.assigneeResourceId;
|
|
198
|
+
const resource = personId === null ? null : ctx.resourcesById.get(personId);
|
|
199
|
+
if (!resource || !isSeatInWindow(seat, ctx)) continue;
|
|
200
|
+
|
|
201
|
+
const findings = getShiftEligibilityFindings({
|
|
202
|
+
shift: seat.shift,
|
|
203
|
+
resource,
|
|
204
|
+
rolesById: ctx.rolesById,
|
|
205
|
+
// Only an overlapping seat can be an OVERLAP finding
|
|
206
|
+
otherShifts: (ctx.seatsByPerson.get(resource.id) ?? [])
|
|
207
|
+
.filter(
|
|
208
|
+
(other) =>
|
|
209
|
+
other.shift.id !== seat.shift.id &&
|
|
210
|
+
other.start < seat.end &&
|
|
211
|
+
other.end > seat.start,
|
|
212
|
+
)
|
|
213
|
+
.map((other) => other.shift),
|
|
214
|
+
timeOff: ctx.timeOffByPerson.get(resource.id) ?? [],
|
|
215
|
+
availability: ctx.availabilityByPerson.get(resource.id) ?? [],
|
|
216
|
+
rule: getEffectiveScheduleRules(index, 'ELIGIBILITY', resource.id)[0],
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const blocked: Array<{ start: number; end: number }> = [];
|
|
220
|
+
for (const finding of findings) {
|
|
221
|
+
if (
|
|
222
|
+
finding.code === 'OVERLAP' &&
|
|
223
|
+
compareScheduleIds(seat.shift.id, String(finding.detail.otherShiftId)) <
|
|
224
|
+
0
|
|
89
225
|
)
|
|
226
|
+
continue;
|
|
227
|
+
const start = finding.startAt
|
|
228
|
+
? toScheduleTimeMs(finding.startAt)
|
|
229
|
+
: seat.start;
|
|
230
|
+
const end = finding.endAt ? toScheduleTimeMs(finding.endAt) : seat.end;
|
|
231
|
+
if (finding.severity === 'BLOCK' && finding.code !== 'UNAVAILABLE')
|
|
232
|
+
blocked.push({ start, end });
|
|
233
|
+
pushCoverageViolation(raw, {
|
|
234
|
+
kind: 'ELIGIBILITY',
|
|
235
|
+
code: finding.code as ScheduleCoverageViolationCode,
|
|
236
|
+
ruleId: finding.ruleId,
|
|
237
|
+
severity: finding.severity,
|
|
238
|
+
resourceId: resource.id,
|
|
239
|
+
shiftId: seat.shift.id,
|
|
240
|
+
startAt: new Date(start),
|
|
241
|
+
endAt: new Date(end),
|
|
242
|
+
degree: 1,
|
|
243
|
+
detail: finding.detail,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
if (blocked.length) blockedBySeat.set(seat.shift.id, blocked);
|
|
247
|
+
}
|
|
248
|
+
return blockedBySeat;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/*
|
|
252
|
+
* Build the presence stretches: each assigned live seat of a known person,
|
|
253
|
+
* inside the window, minus its blocked stretches.
|
|
254
|
+
*/
|
|
255
|
+
function buildCoveragePresences(
|
|
256
|
+
ctx: CoverageContext,
|
|
257
|
+
blockedBySeat: Map<string, Array<{ start: number; end: number }>>,
|
|
258
|
+
): SweepInterval<CoveragePresence>[] {
|
|
259
|
+
const stretches: SweepInterval<CoveragePresence>[] = [];
|
|
260
|
+
for (const seat of ctx.ownSeats) {
|
|
261
|
+
const personId = seat.shift.assigneeResourceId;
|
|
262
|
+
if (
|
|
263
|
+
personId === null ||
|
|
264
|
+
!ctx.resourcesById.has(personId) ||
|
|
265
|
+
!isSeatInWindow(seat, ctx)
|
|
90
266
|
)
|
|
91
|
-
|
|
267
|
+
continue;
|
|
268
|
+
const locationId = seat.shift.locationResourceId;
|
|
269
|
+
const presence: CoveragePresence = {
|
|
270
|
+
seat,
|
|
271
|
+
personId,
|
|
272
|
+
roomId:
|
|
273
|
+
locationId !== null &&
|
|
274
|
+
ctx.resourcesById.get(locationId)?.kind === 'SPACE'
|
|
275
|
+
? locationId
|
|
276
|
+
: null,
|
|
277
|
+
};
|
|
278
|
+
for (const part of subtractCoverageIntervals(
|
|
279
|
+
Math.max(seat.start, ctx.from),
|
|
280
|
+
Math.min(seat.end, ctx.to),
|
|
281
|
+
blockedBySeat.get(seat.shift.id) ?? [],
|
|
282
|
+
))
|
|
283
|
+
stretches.push({
|
|
284
|
+
startTime: part.start,
|
|
285
|
+
endTime: part.end,
|
|
286
|
+
value: presence,
|
|
287
|
+
});
|
|
92
288
|
}
|
|
93
|
-
return
|
|
289
|
+
return sortSweepIntervals(stretches);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/*
|
|
293
|
+
* Expand demand patterns and overrides of known rooms into child-count
|
|
294
|
+
* stretches inside the window (SPACE rooms, quantity above zero).
|
|
295
|
+
*/
|
|
296
|
+
function expandCoverageDemand(
|
|
297
|
+
ctx: CoverageContext,
|
|
298
|
+
dates: { fromDate: string; toDate: string },
|
|
299
|
+
): SweepInterval<ScheduleDemandIntervalObj>[] {
|
|
300
|
+
const patterns = filterKnownDemandRows(
|
|
301
|
+
ctx.input.demandPatterns,
|
|
302
|
+
ctx.resourcesById,
|
|
303
|
+
ctx.diagnostics,
|
|
304
|
+
);
|
|
305
|
+
const overrides = filterKnownDemandRows(
|
|
306
|
+
ctx.input.demandOverrides,
|
|
307
|
+
ctx.resourcesById,
|
|
308
|
+
ctx.diagnostics,
|
|
309
|
+
);
|
|
310
|
+
if (!patterns.length && !overrides.length) return [];
|
|
311
|
+
return sortSweepIntervals(
|
|
312
|
+
expandScheduleDemand({
|
|
313
|
+
patterns,
|
|
314
|
+
overrides,
|
|
315
|
+
fromDate: dates.fromDate,
|
|
316
|
+
toDate: dates.toDate,
|
|
317
|
+
timeZone: ctx.input.timeZone,
|
|
318
|
+
})
|
|
319
|
+
.filter(
|
|
320
|
+
(row) =>
|
|
321
|
+
row.quantity > 0 &&
|
|
322
|
+
ctx.resourcesById.get(row.resourceId)?.kind === 'SPACE',
|
|
323
|
+
)
|
|
324
|
+
.map((row) => ({
|
|
325
|
+
startTime: Math.max(ctx.from, row.startAt.getTime()),
|
|
326
|
+
endTime: Math.min(ctx.to, row.endAt.getTime()),
|
|
327
|
+
value: row,
|
|
328
|
+
})),
|
|
329
|
+
);
|
|
94
330
|
}
|
|
95
331
|
|
|
96
|
-
/*
|
|
97
|
-
|
|
98
|
-
|
|
332
|
+
/*
|
|
333
|
+
* Return the effective OPERATING_HOURS rules: the location's (subject null)
|
|
334
|
+
* and, separately, each room's own (a room never inherits the location's).
|
|
335
|
+
*/
|
|
336
|
+
function getOperatingHoursRules(index: ScheduleEffectiveRuleIndex): {
|
|
337
|
+
location: ScheduleEffectiveRule[];
|
|
338
|
+
rooms: Array<{ roomId: string; rules: ScheduleEffectiveRule[] }>;
|
|
339
|
+
} {
|
|
340
|
+
const roomIds = new Set<string>();
|
|
341
|
+
for (const group of index.byKind.get('OPERATING_HOURS')?.values() ?? [])
|
|
342
|
+
for (const roomId of group.bySubject.keys()) roomIds.add(roomId);
|
|
343
|
+
return {
|
|
344
|
+
location: getEffectiveScheduleRules(index, 'OPERATING_HOURS', null),
|
|
345
|
+
rooms: [...roomIds]
|
|
346
|
+
.sort(compareScheduleIds)
|
|
347
|
+
.map((roomId) => ({
|
|
348
|
+
roomId,
|
|
349
|
+
rules: getEffectiveScheduleRules(index, 'OPERATING_HOURS', roomId),
|
|
350
|
+
}))
|
|
351
|
+
.filter((room) => room.rules.length),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/*
|
|
356
|
+
* Return the calendar's open stretches inside the window, or null when an
|
|
357
|
+
* OPERATING_HOURS rule cannot be judged because the calendar has no work
|
|
358
|
+
* calendar, or one with no hours at all.
|
|
359
|
+
*/
|
|
360
|
+
function getCoverageOperatingIntervals(
|
|
99
361
|
ctx: CoverageContext,
|
|
100
|
-
|
|
101
|
-
|
|
362
|
+
): SweepInterval<null>[] | null {
|
|
363
|
+
const { operatingHours, timeZone } = ctx.input;
|
|
364
|
+
if (!operatingHours) return null;
|
|
365
|
+
const calendar = { ...operatingHours, timeZone };
|
|
366
|
+
if (!isWorkingCalendar(calendar)) return null;
|
|
367
|
+
return getWorkingIntervalsInRange(
|
|
368
|
+
calendar,
|
|
369
|
+
new Date(ctx.from),
|
|
370
|
+
new Date(ctx.to),
|
|
371
|
+
).map((interval) => ({
|
|
372
|
+
startTime: interval.startAt,
|
|
373
|
+
endTime: interval.endAt,
|
|
374
|
+
value: null,
|
|
375
|
+
}));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/*
|
|
379
|
+
* Report OPERATING_HOURS_NOT_CONFIGURED once per effective OPERATING_HOURS
|
|
380
|
+
* rule.
|
|
381
|
+
*/
|
|
382
|
+
function reportOperatingHoursNotConfigured(
|
|
383
|
+
operating: ReturnType<typeof getOperatingHoursRules>,
|
|
384
|
+
diagnostics: CoverageDiagnosticCollector,
|
|
102
385
|
): void {
|
|
103
|
-
for (const
|
|
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
|
-
|
|
386
|
+
for (const { roomId, rules } of [
|
|
387
|
+
{ roomId: null, rules: operating.location },
|
|
388
|
+
...operating.rooms,
|
|
389
|
+
])
|
|
390
|
+
for (const rule of rules)
|
|
391
|
+
addCoverageDiagnostic(diagnostics, {
|
|
392
|
+
code: 'OPERATING_HOURS_NOT_CONFIGURED',
|
|
393
|
+
ruleId: rule.ruleId,
|
|
394
|
+
resourceId: roomId,
|
|
395
|
+
shiftId: null,
|
|
396
|
+
detail: {},
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/*
|
|
401
|
+
* Check whether a person holds every given qualification, unexpired at an
|
|
402
|
+
* instant (a missing or unreadable one fails).
|
|
403
|
+
*/
|
|
404
|
+
function holdsQualificationsAt(
|
|
405
|
+
personId: string,
|
|
406
|
+
keys: readonly string[],
|
|
407
|
+
at: number,
|
|
408
|
+
matcher: CoveragePeopleMatcher,
|
|
409
|
+
): boolean {
|
|
410
|
+
const expiries = matcher.expiries.get(personId);
|
|
411
|
+
return keys.every((key) => {
|
|
412
|
+
const expiry = expiries?.get(key);
|
|
413
|
+
return expiry === null || (expiry !== undefined && expiry > at);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/*
|
|
418
|
+
* Check whether a person counts for a predicate at an instant: they match it
|
|
419
|
+
* (roles, qualifications, tags) and every qualification it names is
|
|
420
|
+
* unexpired at that instant. A null predicate matches anyone present.
|
|
421
|
+
*/
|
|
422
|
+
function isPersonCountedFor(
|
|
423
|
+
personId: string,
|
|
424
|
+
predicate: SchedulePredicateObj | null,
|
|
425
|
+
at: number,
|
|
426
|
+
matcher: CoveragePeopleMatcher,
|
|
427
|
+
): boolean {
|
|
428
|
+
if (!predicate) return true;
|
|
429
|
+
let memo = matcher.memo.get(predicate);
|
|
430
|
+
if (!memo) {
|
|
431
|
+
memo = new Map();
|
|
432
|
+
matcher.memo.set(predicate, memo);
|
|
433
|
+
}
|
|
434
|
+
let matches = memo.get(personId);
|
|
435
|
+
if (matches === undefined) {
|
|
436
|
+
matches = doesResourceMatchPredicate(
|
|
437
|
+
matcher.resourcesById.get(personId),
|
|
438
|
+
predicate,
|
|
439
|
+
);
|
|
440
|
+
memo.set(personId, matches);
|
|
441
|
+
}
|
|
442
|
+
return (
|
|
443
|
+
matches &&
|
|
444
|
+
holdsQualificationsAt(personId, predicate.qualifications ?? [], at, matcher)
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/*
|
|
449
|
+
* Return the roles a null-role seat may count its person in: the roles they
|
|
450
|
+
* hold whose required qualifications they all hold, unexpired at the seat's
|
|
451
|
+
* start. That is the test a seat naming the role passes in eligibility, so a
|
|
452
|
+
* blank role never counts someone a seat with the role would block. A role
|
|
453
|
+
* missing from the catalog keeps no qualification test (coverage reports
|
|
454
|
+
* UNKNOWN_ROLE). Memoized per seat.
|
|
455
|
+
*/
|
|
456
|
+
function getSeatQualifiedRoleIds(
|
|
457
|
+
presence: CoveragePresence,
|
|
458
|
+
matcher: CoveragePeopleMatcher,
|
|
459
|
+
): ReadonlySet<string> {
|
|
460
|
+
const { seat, personId } = presence;
|
|
461
|
+
let roleIds = matcher.qualifiedRolesBySeat.get(seat.shift.id);
|
|
462
|
+
if (!roleIds) {
|
|
463
|
+
roleIds = new Set(
|
|
464
|
+
(matcher.resourcesById.get(personId)?.roleIds ?? []).filter((roleId) =>
|
|
465
|
+
holdsQualificationsAt(
|
|
466
|
+
personId,
|
|
467
|
+
matcher.rolesById.get(roleId)?.requiredQualifications ?? [],
|
|
468
|
+
seat.start,
|
|
469
|
+
matcher,
|
|
470
|
+
),
|
|
471
|
+
),
|
|
472
|
+
);
|
|
473
|
+
matcher.qualifiedRolesBySeat.set(seat.shift.id, roleIds);
|
|
474
|
+
}
|
|
475
|
+
return roleIds;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/*
|
|
479
|
+
* Count the distinct present people who count for a predicate.
|
|
480
|
+
*/
|
|
481
|
+
function countPeopleMatching(
|
|
482
|
+
presences: CoveragePresence[],
|
|
483
|
+
predicate: SchedulePredicateObj | null,
|
|
484
|
+
at: number,
|
|
485
|
+
matcher: CoveragePeopleMatcher,
|
|
486
|
+
): number {
|
|
487
|
+
const people = new Set<string>();
|
|
488
|
+
for (const presence of presences)
|
|
489
|
+
if (
|
|
490
|
+
!people.has(presence.personId) &&
|
|
491
|
+
isPersonCountedFor(presence.personId, predicate, at, matcher)
|
|
492
|
+
)
|
|
493
|
+
people.add(presence.personId);
|
|
494
|
+
return people.size;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/*
|
|
498
|
+
* Add one coverage-kind violation (shiftId null) for a sweep segment.
|
|
499
|
+
*/
|
|
500
|
+
function pushSegmentViolation(
|
|
501
|
+
raw: CoverageRawCollector,
|
|
502
|
+
segment: CoverageSegment,
|
|
503
|
+
rule: ScheduleEffectiveRule,
|
|
504
|
+
fields: Pick<
|
|
505
|
+
CoverageViolationObj,
|
|
506
|
+
'kind' | 'code' | 'resourceId' | 'degree' | 'detail'
|
|
507
|
+
>,
|
|
508
|
+
): void {
|
|
509
|
+
pushCoverageViolation(raw, {
|
|
510
|
+
...fields,
|
|
511
|
+
ruleId: rule.ruleId,
|
|
512
|
+
severity: rule.severity,
|
|
513
|
+
shiftId: null,
|
|
514
|
+
startAt: new Date(segment.start),
|
|
515
|
+
endAt: new Date(segment.end),
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/*
|
|
520
|
+
* STAFFING_REQUIREMENT: inside an active window, count distinct present
|
|
521
|
+
* people on seats whose role is listed, or whose role is null and whose
|
|
522
|
+
* person holds a listed role together with its required qualifications
|
|
523
|
+
* (unexpired at the seat's start), in the configured room when one is set.
|
|
524
|
+
*/
|
|
525
|
+
function evaluateStaffingSegment(
|
|
526
|
+
segment: CoverageSegment,
|
|
527
|
+
rules: CoverageSweepRules,
|
|
528
|
+
matcher: CoveragePeopleMatcher,
|
|
529
|
+
raw: CoverageRawCollector,
|
|
530
|
+
): void {
|
|
531
|
+
for (const ruleIndex of segment.staffingRuleIndexes) {
|
|
532
|
+
const rule = rules.staffing[ruleIndex];
|
|
533
|
+
const roleIds = rules.staffingRoleIds[ruleIndex];
|
|
534
|
+
const config = rule.config as ScheduleStaffingRequirementConfigObj;
|
|
535
|
+
const people = new Set<string>();
|
|
536
|
+
for (const presence of segment.presences) {
|
|
537
|
+
const { shift } = presence.seat;
|
|
538
|
+
if (
|
|
539
|
+
config.locationResourceId !== null &&
|
|
540
|
+
shift.locationResourceId !== config.locationResourceId
|
|
541
|
+
)
|
|
542
|
+
continue;
|
|
543
|
+
const holdsRole =
|
|
544
|
+
shift.roleId !== null
|
|
545
|
+
? roleIds.has(shift.roleId)
|
|
546
|
+
: [...getSeatQualifiedRoleIds(presence, matcher)].some((roleId) =>
|
|
547
|
+
roleIds.has(roleId),
|
|
548
|
+
);
|
|
549
|
+
if (holdsRole) people.add(presence.personId);
|
|
200
550
|
}
|
|
551
|
+
if (people.size < config.minCount)
|
|
552
|
+
pushSegmentViolation(raw, segment, rule, {
|
|
553
|
+
kind: 'STAFFING_REQUIREMENT',
|
|
554
|
+
code: null,
|
|
555
|
+
resourceId: config.locationResourceId,
|
|
556
|
+
degree: config.minCount - people.size,
|
|
557
|
+
detail: { minCount: config.minCount, roleIdsAny: config.roleIdsAny },
|
|
558
|
+
});
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
/*
|
|
563
|
+
* Return the adults a RATIO rule requires for a room's children, reporting
|
|
564
|
+
* keys with no ratio (RATIO_NOT_CONFIGURED) and, in YOUNGEST mode, keys
|
|
565
|
+
* missing from the org's age groups (AGE_GROUP_UNKNOWN). BLENDED sums
|
|
566
|
+
* ceil(q / ratio) per key; YOUNGEST is ceil(all children / ratio of the
|
|
567
|
+
* youngest group present). Null when YOUNGEST cannot name a ratio.
|
|
568
|
+
*/
|
|
569
|
+
function getRatioRequiredAdults(
|
|
570
|
+
rule: ScheduleEffectiveRule,
|
|
571
|
+
roomId: string,
|
|
572
|
+
children: Map<string, number>,
|
|
573
|
+
total: number,
|
|
574
|
+
ctx: CoverageContext,
|
|
575
|
+
): number | null {
|
|
576
|
+
const config = rule.config as ScheduleRatioConfigObj;
|
|
577
|
+
/* Report a demand key this rule cannot judge, for this room. */
|
|
578
|
+
const report = (
|
|
579
|
+
code: 'RATIO_NOT_CONFIGURED' | 'AGE_GROUP_UNKNOWN',
|
|
580
|
+
key: string,
|
|
581
|
+
): void =>
|
|
582
|
+
addCoverageDiagnostic(ctx.diagnostics, {
|
|
583
|
+
code,
|
|
584
|
+
ruleId: rule.ruleId,
|
|
585
|
+
resourceId: roomId,
|
|
586
|
+
shiftId: null,
|
|
587
|
+
detail: { key },
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
for (const key of children.keys())
|
|
591
|
+
if (config.ratios[key] == null) report('RATIO_NOT_CONFIGURED', key);
|
|
592
|
+
|
|
593
|
+
if (config.mode === 'YOUNGEST') {
|
|
594
|
+
const ageGroupKeys = new Set(ctx.input.ageGroups.map((group) => group.key));
|
|
595
|
+
for (const key of children.keys())
|
|
596
|
+
if (!ageGroupKeys.has(key)) report('AGE_GROUP_UNKNOWN', key);
|
|
597
|
+
const youngest = ctx.input.ageGroups.find((group) =>
|
|
598
|
+
children.has(group.key),
|
|
599
|
+
)?.key;
|
|
600
|
+
const ratio = youngest === undefined ? undefined : config.ratios[youngest];
|
|
601
|
+
return ratio == null
|
|
602
|
+
? null
|
|
603
|
+
: Math.ceil(total / ratio - FILE_CONSTANTS.ratioEpsilon);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
let required = 0;
|
|
607
|
+
for (const [key, quantity] of children) {
|
|
608
|
+
const ratio = config.ratios[key];
|
|
609
|
+
if (ratio != null)
|
|
610
|
+
required += Math.ceil(quantity / ratio - FILE_CONSTANTS.ratioEpsilon);
|
|
201
611
|
}
|
|
612
|
+
return required;
|
|
202
613
|
}
|
|
203
614
|
|
|
204
|
-
/*
|
|
205
|
-
|
|
206
|
-
|
|
615
|
+
/*
|
|
616
|
+
* RATIO for one room: ADULTS when fewer counting adults than required are
|
|
617
|
+
* present, GROUP_SIZE when any age group exceeds its maximum group size
|
|
618
|
+
* (degree = total excess, detail.keys = the groups over).
|
|
619
|
+
*/
|
|
620
|
+
function evaluateRatioSegment(
|
|
621
|
+
segment: CoverageSegment,
|
|
622
|
+
roomId: string,
|
|
623
|
+
children: Map<string, number>,
|
|
624
|
+
total: number,
|
|
625
|
+
rules: ScheduleEffectiveRule[],
|
|
207
626
|
ctx: CoverageContext,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
diagnostics: CoverageDiagnosticObj[],
|
|
627
|
+
matcher: CoveragePeopleMatcher,
|
|
628
|
+
raw: CoverageRawCollector,
|
|
211
629
|
): void {
|
|
212
|
-
for (const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
630
|
+
for (const rule of rules) {
|
|
631
|
+
const config = rule.config as ScheduleRatioConfigObj;
|
|
632
|
+
const required = getRatioRequiredAdults(rule, roomId, children, total, ctx);
|
|
633
|
+
const adults = countPeopleMatching(
|
|
634
|
+
segment.presencesByRoom.get(roomId) ?? [],
|
|
635
|
+
config.predicate,
|
|
636
|
+
segment.start,
|
|
637
|
+
matcher,
|
|
219
638
|
);
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
639
|
+
if (required !== null && adults < required)
|
|
640
|
+
pushSegmentViolation(raw, segment, rule, {
|
|
641
|
+
kind: 'RATIO',
|
|
642
|
+
code: 'ADULTS',
|
|
643
|
+
resourceId: roomId,
|
|
644
|
+
degree: required - adults,
|
|
645
|
+
detail: { mode: config.mode, required },
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
const keysOver: string[] = [];
|
|
649
|
+
let excess = 0;
|
|
650
|
+
for (const [key, quantity] of children) {
|
|
651
|
+
const maxSize = config.maxGroupSize?.[key];
|
|
652
|
+
if (maxSize == null || quantity <= maxSize) continue;
|
|
653
|
+
keysOver.push(key);
|
|
654
|
+
excess += quantity - maxSize;
|
|
655
|
+
}
|
|
656
|
+
if (keysOver.length)
|
|
657
|
+
pushSegmentViolation(raw, segment, rule, {
|
|
658
|
+
kind: 'RATIO',
|
|
659
|
+
code: 'GROUP_SIZE',
|
|
660
|
+
resourceId: roomId,
|
|
661
|
+
degree: roundCoverageQuantity(excess),
|
|
662
|
+
detail: { keys: keysOver },
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
/*
|
|
668
|
+
* ROOM_CAPACITY for one room: more children than its licensed capacity
|
|
669
|
+
* (null capacity = unbounded).
|
|
670
|
+
*/
|
|
671
|
+
function evaluateRoomCapacitySegment(
|
|
672
|
+
segment: CoverageSegment,
|
|
673
|
+
roomId: string,
|
|
674
|
+
total: number,
|
|
675
|
+
rules: ScheduleEffectiveRule[],
|
|
676
|
+
ctx: CoverageContext,
|
|
677
|
+
raw: CoverageRawCollector,
|
|
678
|
+
): void {
|
|
679
|
+
const capacity = ctx.resourcesById.get(roomId)?.capacity ?? null;
|
|
680
|
+
if (capacity === null || total <= capacity) return;
|
|
681
|
+
for (const rule of rules)
|
|
682
|
+
pushSegmentViolation(raw, segment, rule, {
|
|
683
|
+
kind: 'ROOM_CAPACITY',
|
|
684
|
+
code: null,
|
|
685
|
+
resourceId: roomId,
|
|
686
|
+
degree: roundCoverageQuantity(total - capacity),
|
|
687
|
+
detail: { capacity, children: total },
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/*
|
|
692
|
+
* OPERATING_HOURS while the calendar is open: at least minAdults distinct
|
|
693
|
+
* counting adults in the location (location rules) and in each room that
|
|
694
|
+
* has its own rule.
|
|
695
|
+
*/
|
|
696
|
+
function evaluateOperatingHoursSegment(
|
|
697
|
+
segment: CoverageSegment,
|
|
698
|
+
rules: CoverageSweepRules,
|
|
699
|
+
matcher: CoveragePeopleMatcher,
|
|
700
|
+
raw: CoverageRawCollector,
|
|
701
|
+
): void {
|
|
702
|
+
if (!segment.isOpen) return;
|
|
703
|
+
for (const { roomId, rules: roomRules } of [
|
|
704
|
+
{ roomId: null, rules: rules.operatingLocation },
|
|
705
|
+
...rules.operatingRooms,
|
|
706
|
+
])
|
|
707
|
+
for (const rule of roomRules) {
|
|
708
|
+
const config = rule.config as ScheduleOperatingHoursConfigObj;
|
|
709
|
+
const adults = countPeopleMatching(
|
|
710
|
+
roomId === null
|
|
711
|
+
? segment.presences
|
|
712
|
+
: (segment.presencesByRoom.get(roomId) ?? []),
|
|
246
713
|
config.predicate,
|
|
247
|
-
|
|
248
|
-
|
|
714
|
+
segment.start,
|
|
715
|
+
matcher,
|
|
249
716
|
);
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
kind: 'DERIVED_COUNT',
|
|
717
|
+
if (adults < config.minAdults)
|
|
718
|
+
pushSegmentViolation(raw, segment, rule, {
|
|
719
|
+
kind: 'OPERATING_HOURS',
|
|
254
720
|
code: null,
|
|
255
721
|
resourceId: roomId,
|
|
256
|
-
|
|
257
|
-
slotKey: null,
|
|
258
|
-
degree: shortfall,
|
|
722
|
+
degree: config.minAdults - adults,
|
|
259
723
|
detail: {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
requiredCount,
|
|
263
|
-
presentCount,
|
|
264
|
-
shortfall,
|
|
724
|
+
minAdults: config.minAdults,
|
|
725
|
+
scope: roomId === null ? 'LOCATION' : 'ROOM',
|
|
265
726
|
},
|
|
266
727
|
});
|
|
267
728
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/*
|
|
732
|
+
* Gather one segment's state from the active members of each family:
|
|
733
|
+
* presences (all, and per room), children per room and age group (rounded),
|
|
734
|
+
* the active staffing rules and whether the calendar is open.
|
|
735
|
+
*/
|
|
736
|
+
function buildCoverageSegment(
|
|
737
|
+
start: number,
|
|
738
|
+
end: number,
|
|
739
|
+
presences: Set<SweepInterval<CoveragePresence>>,
|
|
740
|
+
demand: Set<SweepInterval<ScheduleDemandIntervalObj>>,
|
|
741
|
+
staffing: Set<SweepInterval<number>>,
|
|
742
|
+
isOpen: boolean,
|
|
743
|
+
): CoverageSegment {
|
|
744
|
+
const segment: CoverageSegment = {
|
|
745
|
+
start,
|
|
746
|
+
end,
|
|
747
|
+
presences: [],
|
|
748
|
+
presencesByRoom: new Map(),
|
|
749
|
+
childrenByRoom: new Map(),
|
|
750
|
+
staffingRuleIndexes: [
|
|
751
|
+
...new Set([...staffing].map((row) => row.value)),
|
|
752
|
+
].sort((a, b) => a - b),
|
|
753
|
+
isOpen,
|
|
754
|
+
};
|
|
755
|
+
for (const { value: presence } of presences) {
|
|
756
|
+
segment.presences.push(presence);
|
|
757
|
+
if (presence.roomId === null) continue;
|
|
758
|
+
const list = segment.presencesByRoom.get(presence.roomId) ?? [];
|
|
759
|
+
list.push(presence);
|
|
760
|
+
segment.presencesByRoom.set(presence.roomId, list);
|
|
761
|
+
}
|
|
762
|
+
for (const { value: row } of demand) {
|
|
763
|
+
const children = segment.childrenByRoom.get(row.resourceId) ?? new Map();
|
|
764
|
+
children.set(row.key, (children.get(row.key) ?? 0) + row.quantity);
|
|
765
|
+
segment.childrenByRoom.set(row.resourceId, children);
|
|
288
766
|
}
|
|
767
|
+
for (const children of segment.childrenByRoom.values())
|
|
768
|
+
for (const [key, quantity] of children)
|
|
769
|
+
children.set(key, roundCoverageQuantity(quantity));
|
|
770
|
+
return segment;
|
|
289
771
|
}
|
|
290
772
|
|
|
291
|
-
/*
|
|
292
|
-
|
|
293
|
-
|
|
773
|
+
/*
|
|
774
|
+
* Return a room's effective rules of one kind, resolved once per room.
|
|
775
|
+
*/
|
|
776
|
+
function getRoomRules(
|
|
777
|
+
cache: Map<string, ScheduleEffectiveRule[]>,
|
|
778
|
+
index: ScheduleEffectiveRuleIndex,
|
|
779
|
+
kind: 'RATIO' | 'ROOM_CAPACITY',
|
|
780
|
+
roomId: string,
|
|
781
|
+
): ScheduleEffectiveRule[] {
|
|
782
|
+
let rules = cache.get(roomId);
|
|
783
|
+
if (!rules) {
|
|
784
|
+
rules = getEffectiveScheduleRules(index, kind, roomId);
|
|
785
|
+
cache.set(roomId, rules);
|
|
786
|
+
}
|
|
787
|
+
return rules;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/*
|
|
791
|
+
* Evaluate the coverage kinds of one segment: staffing, then per room with
|
|
792
|
+
* children (in id order) RATIO and ROOM_CAPACITY, then operating hours.
|
|
793
|
+
*/
|
|
794
|
+
function evaluateCoverageSegment(
|
|
795
|
+
segment: CoverageSegment,
|
|
796
|
+
rules: CoverageSweepRules,
|
|
294
797
|
ctx: CoverageContext,
|
|
295
|
-
|
|
296
|
-
|
|
798
|
+
matcher: CoveragePeopleMatcher,
|
|
799
|
+
raw: CoverageRawCollector,
|
|
297
800
|
): void {
|
|
298
|
-
|
|
299
|
-
const
|
|
801
|
+
evaluateStaffingSegment(segment, rules, matcher, raw);
|
|
802
|
+
for (const roomId of [...segment.childrenByRoom.keys()].sort(
|
|
803
|
+
compareScheduleIds,
|
|
804
|
+
)) {
|
|
805
|
+
const children = new Map(
|
|
806
|
+
[...segment.childrenByRoom.get(roomId)!]
|
|
807
|
+
.filter(([, quantity]) => quantity > 0)
|
|
808
|
+
.sort(([a], [b]) => compareCodeUnits(a, b)),
|
|
809
|
+
);
|
|
810
|
+
if (!children.size) continue;
|
|
811
|
+
const total = roundCoverageQuantity(
|
|
812
|
+
[...children.values()].reduce((sum, quantity) => sum + quantity, 0),
|
|
813
|
+
);
|
|
814
|
+
evaluateRatioSegment(
|
|
815
|
+
segment,
|
|
816
|
+
roomId,
|
|
817
|
+
children,
|
|
818
|
+
total,
|
|
819
|
+
getRoomRules(rules.ratioByRoom, rules.index, 'RATIO', roomId),
|
|
820
|
+
ctx,
|
|
821
|
+
matcher,
|
|
822
|
+
raw,
|
|
823
|
+
);
|
|
824
|
+
evaluateRoomCapacitySegment(
|
|
825
|
+
segment,
|
|
826
|
+
roomId,
|
|
827
|
+
total,
|
|
828
|
+
getRoomRules(rules.capacityByRoom, rules.index, 'ROOM_CAPACITY', roomId),
|
|
829
|
+
ctx,
|
|
830
|
+
raw,
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
evaluateOperatingHoursSegment(segment, rules, matcher, raw);
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
/*
|
|
837
|
+
* Resolve the sweep's rules once: effective staffing rules (never
|
|
838
|
+
* resource-scoped) with their role sets, operating-hours rules, and empty
|
|
839
|
+
* per-room caches for RATIO and ROOM_CAPACITY.
|
|
840
|
+
*/
|
|
841
|
+
function getCoverageSweepRules(
|
|
842
|
+
index: ScheduleEffectiveRuleIndex,
|
|
843
|
+
): CoverageSweepRules {
|
|
844
|
+
const staffing = getEffectiveScheduleRules(
|
|
845
|
+
index,
|
|
846
|
+
'STAFFING_REQUIREMENT',
|
|
300
847
|
null,
|
|
301
|
-
|
|
302
|
-
|
|
848
|
+
);
|
|
849
|
+
const operating = getOperatingHoursRules(index);
|
|
850
|
+
return {
|
|
851
|
+
index,
|
|
852
|
+
staffing,
|
|
853
|
+
staffingRoleIds: staffing.map(
|
|
854
|
+
(rule) =>
|
|
855
|
+
new Set(
|
|
856
|
+
(rule.config as ScheduleStaffingRequirementConfigObj).roleIdsAny,
|
|
857
|
+
),
|
|
303
858
|
),
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
859
|
+
operatingLocation: operating.location,
|
|
860
|
+
operatingRooms: operating.rooms,
|
|
861
|
+
ratioByRoom: new Map(),
|
|
862
|
+
capacityByRoom: new Map(),
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
/*
|
|
867
|
+
* Run the per-instant sweep over the window: every boundary at which a
|
|
868
|
+
* presence, child count, open stretch, staffing window or qualification
|
|
869
|
+
* expiry changes starts a segment, and each segment is evaluated once.
|
|
870
|
+
* Returns the sweep statistics.
|
|
871
|
+
*/
|
|
872
|
+
function runCoverageSweep(
|
|
873
|
+
ctx: CoverageContext,
|
|
874
|
+
index: ScheduleEffectiveRuleIndex,
|
|
875
|
+
blockedBySeat: Map<string, Array<{ start: number; end: number }>>,
|
|
876
|
+
raw: CoverageRawCollector,
|
|
877
|
+
): {
|
|
878
|
+
boundaryCount: number;
|
|
879
|
+
segmentCount: number;
|
|
880
|
+
operatingIntervalCount: number;
|
|
881
|
+
demandIntervalCount: number;
|
|
882
|
+
} {
|
|
883
|
+
const rules = getCoverageSweepRules(index);
|
|
884
|
+
const dates = getCoverageLocalDates(ctx.from, ctx.to, ctx.input.timeZone);
|
|
885
|
+
const presences = buildCoveragePresences(ctx, blockedBySeat);
|
|
886
|
+
const demand = expandCoverageDemand(ctx, dates);
|
|
887
|
+
const staffing = sortSweepIntervals(
|
|
888
|
+
expandStaffingWindows(
|
|
889
|
+
rules.staffing,
|
|
890
|
+
dates.dates,
|
|
891
|
+
ctx.input.timeZone,
|
|
892
|
+
ctx.from,
|
|
893
|
+
ctx.to,
|
|
894
|
+
),
|
|
895
|
+
);
|
|
896
|
+
|
|
897
|
+
let operating: SweepInterval<null>[] = [];
|
|
898
|
+
if (rules.operatingLocation.length || rules.operatingRooms.length) {
|
|
899
|
+
const intervals = getCoverageOperatingIntervals(ctx);
|
|
900
|
+
if (intervals) operating = intervals;
|
|
901
|
+
else {
|
|
902
|
+
reportOperatingHoursNotConfigured(
|
|
903
|
+
{ location: rules.operatingLocation, rooms: rules.operatingRooms },
|
|
904
|
+
ctx.diagnostics,
|
|
905
|
+
);
|
|
906
|
+
rules.operatingLocation = [];
|
|
907
|
+
rules.operatingRooms = [];
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
const matcher: CoveragePeopleMatcher = {
|
|
912
|
+
resourcesById: ctx.resourcesById,
|
|
913
|
+
rolesById: ctx.rolesById,
|
|
914
|
+
expiries: indexQualificationExpiries(ctx.resourcesById),
|
|
915
|
+
memo: new WeakMap(),
|
|
916
|
+
qualifiedRolesBySeat: new Map(),
|
|
917
|
+
};
|
|
918
|
+
const expiryInstants: number[] = [];
|
|
919
|
+
for (const expiries of matcher.expiries.values())
|
|
920
|
+
for (const expiry of expiries.values())
|
|
921
|
+
if (expiry !== null) expiryInstants.push(expiry);
|
|
922
|
+
const boundaries = collectSweepBoundaries(
|
|
923
|
+
ctx.from,
|
|
924
|
+
ctx.to,
|
|
925
|
+
[presences, demand, staffing, operating],
|
|
926
|
+
expiryInstants,
|
|
927
|
+
);
|
|
928
|
+
|
|
929
|
+
const active = {
|
|
930
|
+
presences: new Set<SweepInterval<CoveragePresence>>(),
|
|
931
|
+
demand: new Set<SweepInterval<ScheduleDemandIntervalObj>>(),
|
|
932
|
+
staffing: new Set<SweepInterval<number>>(),
|
|
933
|
+
operating: new Set<SweepInterval<null>>(),
|
|
934
|
+
};
|
|
935
|
+
const cursors = { presences: 0, demand: 0, staffing: 0, operating: 0 };
|
|
936
|
+
let segmentCount = 0;
|
|
937
|
+
for (let i = 0; i < boundaries.length - 1 && !raw.capped; i++) {
|
|
938
|
+
const start = boundaries[i];
|
|
939
|
+
segmentCount++;
|
|
940
|
+
cursors.presences = advanceSweepCursor(
|
|
941
|
+
presences,
|
|
942
|
+
cursors.presences,
|
|
943
|
+
start,
|
|
944
|
+
active.presences,
|
|
945
|
+
);
|
|
946
|
+
cursors.demand = advanceSweepCursor(
|
|
947
|
+
demand,
|
|
948
|
+
cursors.demand,
|
|
949
|
+
start,
|
|
950
|
+
active.demand,
|
|
951
|
+
);
|
|
952
|
+
cursors.staffing = advanceSweepCursor(
|
|
953
|
+
staffing,
|
|
954
|
+
cursors.staffing,
|
|
955
|
+
start,
|
|
956
|
+
active.staffing,
|
|
957
|
+
);
|
|
958
|
+
cursors.operating = advanceSweepCursor(
|
|
959
|
+
operating,
|
|
960
|
+
cursors.operating,
|
|
961
|
+
start,
|
|
962
|
+
active.operating,
|
|
963
|
+
);
|
|
964
|
+
evaluateCoverageSegment(
|
|
965
|
+
buildCoverageSegment(
|
|
966
|
+
start,
|
|
967
|
+
boundaries[i + 1],
|
|
968
|
+
active.presences,
|
|
969
|
+
active.demand,
|
|
970
|
+
active.staffing,
|
|
971
|
+
active.operating.size > 0,
|
|
972
|
+
),
|
|
973
|
+
rules,
|
|
316
974
|
ctx,
|
|
975
|
+
matcher,
|
|
976
|
+
raw,
|
|
317
977
|
);
|
|
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
978
|
}
|
|
979
|
+
|
|
980
|
+
return {
|
|
981
|
+
boundaryCount: boundaries.length,
|
|
982
|
+
segmentCount,
|
|
983
|
+
operatingIntervalCount: operating.length,
|
|
984
|
+
demandIntervalCount: demand.length,
|
|
985
|
+
};
|
|
333
986
|
}
|
|
334
987
|
|
|
335
|
-
/*
|
|
336
|
-
|
|
988
|
+
/*
|
|
989
|
+
* Evaluate MAX_HOURS, MIN_REST, MAX_CONSECUTIVE_DAYS and SHIFT_LENGTH for
|
|
990
|
+
* every PERSON assigned in the calendar's seats, over their whole known
|
|
991
|
+
* timeline (every calendar). Findings attach to this calendar's seats in the
|
|
992
|
+
* window, or to any seat when options.anchorExternal is set.
|
|
993
|
+
*/
|
|
994
|
+
function collectPersonRuleViolations(
|
|
337
995
|
ctx: CoverageContext,
|
|
338
|
-
index:
|
|
339
|
-
|
|
996
|
+
index: ScheduleEffectiveRuleIndex,
|
|
997
|
+
raw: CoverageRawCollector,
|
|
340
998
|
): void {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
999
|
+
const kinds = SCHEDULE_PERSON_RULE_KINDS.filter(
|
|
1000
|
+
(kind) => kind !== 'ELIGIBILITY',
|
|
1001
|
+
);
|
|
1002
|
+
const personIds = new Set<string>();
|
|
1003
|
+
for (const seat of ctx.ownSeats) {
|
|
1004
|
+
const personId = seat.shift.assigneeResourceId;
|
|
1005
|
+
if (personId !== null && ctx.resourcesById.get(personId)?.kind === 'PERSON')
|
|
1006
|
+
personIds.add(personId);
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
for (const personId of [...personIds].sort(compareScheduleIds)) {
|
|
1010
|
+
if (raw.capped) return;
|
|
1011
|
+
const rules = kinds.flatMap((kind) =>
|
|
1012
|
+
getEffectiveScheduleRules(index, kind, personId),
|
|
347
1013
|
);
|
|
348
|
-
if (!
|
|
349
|
-
const
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
a.end - b.end || compareScheduleIds(a.occurrenceId, b.occurrenceId),
|
|
1014
|
+
if (!rules.length) continue;
|
|
1015
|
+
const timeline = ctx.seatsByPerson.get(personId) ?? [];
|
|
1016
|
+
const anchorShiftIds = ctx.input.options?.anchorExternal
|
|
1017
|
+
? null
|
|
1018
|
+
: new Set(
|
|
1019
|
+
timeline
|
|
1020
|
+
.filter(
|
|
1021
|
+
(seat) =>
|
|
1022
|
+
seat.shift.calendarId === ctx.input.calendarId &&
|
|
1023
|
+
isSeatInWindow(seat, ctx),
|
|
1024
|
+
)
|
|
1025
|
+
.map((seat) => seat.shift.id),
|
|
361
1026
|
);
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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
|
-
}
|
|
1027
|
+
if (anchorShiftIds?.size === 0) continue;
|
|
1028
|
+
for (const violation of evaluatePersonRules({
|
|
1029
|
+
resourceId: personId,
|
|
1030
|
+
timeline: timeline.map((seat) => seat.shift),
|
|
1031
|
+
anchorShiftIds,
|
|
1032
|
+
rules,
|
|
1033
|
+
timeZone: ctx.input.timeZone,
|
|
1034
|
+
payWeekStart: ctx.input.payWeekStart,
|
|
1035
|
+
}))
|
|
1036
|
+
pushEngineViolation(raw, violation);
|
|
423
1037
|
}
|
|
424
1038
|
}
|
|
425
1039
|
|
|
426
|
-
/*
|
|
1040
|
+
/*
|
|
1041
|
+
* Evaluate NOTICE_PERIOD (late publish, late change) over the calendar's
|
|
1042
|
+
* live seats and their journal rows.
|
|
1043
|
+
*/
|
|
1044
|
+
function collectNoticeViolations(
|
|
1045
|
+
ctx: CoverageContext,
|
|
1046
|
+
index: ScheduleEffectiveRuleIndex,
|
|
1047
|
+
raw: CoverageRawCollector,
|
|
1048
|
+
): void {
|
|
1049
|
+
if (raw.capped) return;
|
|
1050
|
+
for (const violation of evaluateNoticePeriodRules({
|
|
1051
|
+
now: ctx.input.now,
|
|
1052
|
+
window: ctx.input.window,
|
|
1053
|
+
shifts: ctx.ownSeats.map((seat) => seat.shift),
|
|
1054
|
+
shiftChanges: ctx.input.shiftChanges,
|
|
1055
|
+
index,
|
|
1056
|
+
}))
|
|
1057
|
+
pushEngineViolation(raw, violation);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
/*
|
|
1061
|
+
* Add a violation another engine module produced, rebuilding its identity
|
|
1062
|
+
* here so every finding goes through the same pipeline.
|
|
1063
|
+
*/
|
|
1064
|
+
function pushEngineViolation(
|
|
1065
|
+
raw: CoverageRawCollector,
|
|
1066
|
+
violation: CoverageViolationObj,
|
|
1067
|
+
): void {
|
|
1068
|
+
const { key, identity, ...finding } = violation;
|
|
1069
|
+
pushCoverageViolation(raw, {
|
|
1070
|
+
...finding,
|
|
1071
|
+
startAt: new Date(toScheduleTimeMs(finding.startAt)),
|
|
1072
|
+
endAt: new Date(toScheduleTimeMs(finding.endAt)),
|
|
1073
|
+
});
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
/*
|
|
1077
|
+
* Evaluate a calendar's coverage window (IMPLEMENTATION §1.5): input
|
|
1078
|
+
* diagnostics, effective rules, ELIGIBILITY per seat, the per-instant sweep
|
|
1079
|
+
* (STAFFING_REQUIREMENT, RATIO, ROOM_CAPACITY, OPERATING_HOURS), person
|
|
1080
|
+
* rules and NOTICE_PERIOD; then cap, merge adjacent stretches, clip to the
|
|
1081
|
+
* window, make keys unique and sort. Any diagnostic means the evaluation is
|
|
1082
|
+
* incomplete.
|
|
1083
|
+
*/
|
|
427
1084
|
export function evaluateScheduleCoverage(
|
|
428
1085
|
input: CoverageEvaluationInput,
|
|
429
1086
|
): CoverageEvaluationResult {
|
|
430
|
-
const
|
|
431
|
-
const
|
|
432
|
-
input.rules,
|
|
433
|
-
input.calendarId,
|
|
434
|
-
ctx.resourcesById,
|
|
435
|
-
);
|
|
436
|
-
const diagnostics = [...ctx.diagnostics, ...ruleDiagnostics];
|
|
437
|
-
const raw: CoverageViolationObj[] = [];
|
|
1087
|
+
const from = toScheduleTimeMs(input.window.from);
|
|
1088
|
+
const to = toScheduleTimeMs(input.window.to);
|
|
438
1089
|
const stats = {
|
|
439
1090
|
boundaryCount: 0,
|
|
440
1091
|
segmentCount: 0,
|
|
441
|
-
|
|
442
|
-
externalCount:
|
|
1092
|
+
shiftCount: 0,
|
|
1093
|
+
externalCount: 0,
|
|
443
1094
|
operatingIntervalCount: 0,
|
|
1095
|
+
demandIntervalCount: 0,
|
|
444
1096
|
rawViolationCount: 0,
|
|
445
1097
|
};
|
|
446
|
-
if (
|
|
447
|
-
!(ctx.to > ctx.from) ||
|
|
448
|
-
ctx.to - ctx.from > SCHEDULE_COVERAGE_MAX_WINDOW_DAYS * 86_400_000
|
|
449
|
-
) {
|
|
1098
|
+
if (!(to > from) || to - from > SCHEDULE_COVERAGE_MAX_WINDOW_MS)
|
|
450
1099
|
return {
|
|
451
1100
|
violations: [],
|
|
452
1101
|
diagnostics: [
|
|
453
|
-
...diagnostics,
|
|
454
1102
|
{
|
|
455
|
-
code: '
|
|
1103
|
+
code: 'WINDOW_INVALID',
|
|
456
1104
|
ruleId: null,
|
|
457
1105
|
resourceId: null,
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
detail: {
|
|
461
|
-
field: 'window',
|
|
462
|
-
maxDays: SCHEDULE_COVERAGE_MAX_WINDOW_DAYS,
|
|
463
|
-
},
|
|
1106
|
+
shiftId: null,
|
|
1107
|
+
detail: { maxDays: SCHEDULE_COVERAGE_MAX_WINDOW_DAYS },
|
|
464
1108
|
},
|
|
465
1109
|
],
|
|
466
1110
|
stats,
|
|
467
1111
|
};
|
|
468
|
-
|
|
469
|
-
const
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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,
|
|
1112
|
+
|
|
1113
|
+
const ctx = normalizeCoverageInput(input, from, to);
|
|
1114
|
+
stats.shiftCount = ctx.ownSeats.length;
|
|
1115
|
+
stats.externalCount = ctx.externalSeats.length;
|
|
1116
|
+
const { index, diagnostics: ruleDiagnostics } = resolveEffectiveScheduleRules(
|
|
1117
|
+
input.rules,
|
|
1118
|
+
input.calendarId,
|
|
1119
|
+
ctx.resourcesById,
|
|
1120
|
+
ctx.rolesById,
|
|
536
1121
|
);
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
const
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
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({
|
|
1122
|
+
for (const diagnostic of ruleDiagnostics)
|
|
1123
|
+
addCoverageDiagnostic(ctx.diagnostics, diagnostic);
|
|
1124
|
+
|
|
1125
|
+
const raw = createCoverageRawCollector();
|
|
1126
|
+
const blockedBySeat = collectEligibilityViolations(ctx, index, raw);
|
|
1127
|
+
Object.assign(stats, runCoverageSweep(ctx, index, blockedBySeat, raw));
|
|
1128
|
+
collectPersonRuleViolations(ctx, index, raw);
|
|
1129
|
+
collectNoticeViolations(ctx, index, raw);
|
|
1130
|
+
if (raw.capped)
|
|
1131
|
+
addCoverageDiagnostic(ctx.diagnostics, {
|
|
592
1132
|
code: 'VIOLATION_CAP',
|
|
593
1133
|
ruleId: null,
|
|
594
1134
|
resourceId: null,
|
|
595
|
-
|
|
596
|
-
demandId: null,
|
|
1135
|
+
shiftId: null,
|
|
597
1136
|
detail: { limit: SCHEDULE_COVERAGE_MAX_VIOLATIONS },
|
|
598
1137
|
});
|
|
599
|
-
stats.rawViolationCount = raw.length;
|
|
600
|
-
|
|
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
|
-
}
|
|
1138
|
+
stats.rawViolationCount = raw.violations.length;
|
|
1139
|
+
|
|
610
1140
|
return {
|
|
611
1141
|
violations: sortCoverageViolations(
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
1142
|
+
combineCoverageViolationsByKey(
|
|
1143
|
+
clipCoverageViolationsToWindow(
|
|
1144
|
+
mergeAdjacentCoverageViolations(raw.violations),
|
|
1145
|
+
from,
|
|
1146
|
+
to,
|
|
1147
|
+
),
|
|
616
1148
|
),
|
|
617
1149
|
),
|
|
618
|
-
diagnostics:
|
|
619
|
-
.sort(([a], [b]) => a.localeCompare(b))
|
|
620
|
-
.map(([, value]) => value),
|
|
1150
|
+
diagnostics: getSortedCoverageDiagnostics(ctx.diagnostics),
|
|
621
1151
|
stats,
|
|
622
1152
|
};
|
|
623
1153
|
}
|
|
624
1154
|
|
|
625
|
-
/*
|
|
1155
|
+
/*
|
|
1156
|
+
* Check whether any violation refuses publication (severity BLOCK).
|
|
1157
|
+
*/
|
|
626
1158
|
export function hasBlockingViolations(
|
|
627
|
-
violations:
|
|
1159
|
+
violations: CoverageViolationObj[],
|
|
628
1160
|
): boolean {
|
|
629
1161
|
return violations.some((violation) => violation.severity === 'BLOCK');
|
|
630
1162
|
}
|
|
1163
|
+
|
|
1164
|
+
/*
|
|
1165
|
+
* Apply a hypothetical change to one seat row: retime (shiftDate recomputed
|
|
1166
|
+
* in the snapshot's zone for seats of the snapshot's calendar), then
|
|
1167
|
+
* unassign, then assign. Untouched rows are returned as they are.
|
|
1168
|
+
*/
|
|
1169
|
+
function simulateShiftRow(
|
|
1170
|
+
row: CoverageShiftInput,
|
|
1171
|
+
snapshot: CoverageEvaluationInput,
|
|
1172
|
+
change: {
|
|
1173
|
+
retimes: Map<string, { startAt: Date | string; endAt: Date | string }>;
|
|
1174
|
+
unassign: Set<string>;
|
|
1175
|
+
assign: Map<string, string>;
|
|
1176
|
+
},
|
|
1177
|
+
): CoverageShiftInput {
|
|
1178
|
+
const retime = change.retimes.get(row.id);
|
|
1179
|
+
const assignee = change.assign.get(row.id);
|
|
1180
|
+
if (!retime && !change.unassign.has(row.id) && assignee === undefined)
|
|
1181
|
+
return row;
|
|
1182
|
+
const next = { ...row };
|
|
1183
|
+
if (retime) {
|
|
1184
|
+
next.startAt = retime.startAt;
|
|
1185
|
+
next.endAt = retime.endAt;
|
|
1186
|
+
if (row.calendarId === snapshot.calendarId)
|
|
1187
|
+
next.shiftDate = getScheduleLocalDate(retime.startAt, snapshot.timeZone);
|
|
1188
|
+
}
|
|
1189
|
+
if (change.unassign.has(row.id)) next.assigneeResourceId = null;
|
|
1190
|
+
if (assignee !== undefined) next.assigneeResourceId = assignee;
|
|
1191
|
+
return next;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/*
|
|
1195
|
+
* Return a copy of a coverage snapshot with a hypothetical seat change
|
|
1196
|
+
* applied to `shifts` and `external` (copy-on-write; unknown shift ids are
|
|
1197
|
+
* ignored). The caller must already have loaded the people gaining seats
|
|
1198
|
+
* (resources, external seats, time off, availability).
|
|
1199
|
+
*/
|
|
1200
|
+
export function simulateShiftChange(
|
|
1201
|
+
snapshot: CoverageEvaluationInput,
|
|
1202
|
+
change: ScheduleShiftSimulationObj,
|
|
1203
|
+
): CoverageEvaluationInput {
|
|
1204
|
+
const parsed = {
|
|
1205
|
+
retimes: new Map(
|
|
1206
|
+
(change.retime ?? []).map((item) => [
|
|
1207
|
+
item.shiftId,
|
|
1208
|
+
{ startAt: item.startAt, endAt: item.endAt },
|
|
1209
|
+
]),
|
|
1210
|
+
),
|
|
1211
|
+
unassign: new Set(change.unassign ?? []),
|
|
1212
|
+
assign: new Map(
|
|
1213
|
+
(change.assign ?? []).map((item) => [item.shiftId, item.resourceId]),
|
|
1214
|
+
),
|
|
1215
|
+
};
|
|
1216
|
+
return {
|
|
1217
|
+
...snapshot,
|
|
1218
|
+
shifts: snapshot.shifts.map((row) =>
|
|
1219
|
+
simulateShiftRow(row, snapshot, parsed),
|
|
1220
|
+
),
|
|
1221
|
+
external: snapshot.external.map((row) =>
|
|
1222
|
+
simulateShiftRow(row, snapshot, parsed),
|
|
1223
|
+
),
|
|
1224
|
+
};
|
|
1225
|
+
}
|