assignment-user-matcher 1.13.0 → 1.15.0
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/README.md +56 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/matcher.class.d.ts +1 -1
- package/dist/matcher.class.js +1 -1
- package/dist/scheduling/constraints/constraint.d.ts.map +1 -1
- package/dist/scheduling/constraints/constraint.js +7 -1
- package/dist/scheduling/constraints/contract.d.ts +1 -0
- package/dist/scheduling/constraints/contract.d.ts.map +1 -1
- package/dist/scheduling/constraints/contract.js +61 -16
- package/dist/scheduling/constraints/fairness.js +1 -1
- package/dist/scheduling/constraints/overtime.d.ts.map +1 -1
- package/dist/scheduling/constraints/overtime.js +4 -2
- package/dist/scheduling/constraints/site-eligibility.d.ts +11 -0
- package/dist/scheduling/constraints/site-eligibility.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-eligibility.js +52 -0
- package/dist/scheduling/constraints/site-home.d.ts +14 -0
- package/dist/scheduling/constraints/site-home.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-home.js +43 -0
- package/dist/scheduling/constraints/site-travel-gap.d.ts +19 -0
- package/dist/scheduling/constraints/site-travel-gap.d.ts.map +1 -0
- package/dist/scheduling/constraints/site-travel-gap.js +88 -0
- package/dist/scheduling/contract-hours.d.ts +42 -0
- package/dist/scheduling/contract-hours.d.ts.map +1 -0
- package/dist/scheduling/contract-hours.js +104 -0
- package/dist/scheduling/engine/construction.d.ts +22 -1
- package/dist/scheduling/engine/construction.d.ts.map +1 -1
- package/dist/scheduling/engine/construction.js +139 -4
- package/dist/scheduling/engine/objective.d.ts.map +1 -1
- package/dist/scheduling/engine/objective.js +5 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -1
- package/dist/scheduling/engine/state.js +1 -0
- package/dist/scheduling/engine/timeline.d.ts +2 -0
- package/dist/scheduling/engine/timeline.d.ts.map +1 -1
- package/dist/scheduling/index.d.ts +4 -1
- package/dist/scheduling/index.d.ts.map +1 -1
- package/dist/scheduling/index.js +14 -1
- package/dist/scheduling/model.d.ts +11 -0
- package/dist/scheduling/model.d.ts.map +1 -1
- package/dist/scheduling/model.js +86 -1
- package/dist/scheduling/operations.d.ts +17 -1
- package/dist/scheduling/operations.d.ts.map +1 -1
- package/dist/scheduling/operations.js +71 -3
- package/dist/scheduling/provenance.d.ts +2 -2
- package/dist/scheduling/provenance.d.ts.map +1 -1
- package/dist/scheduling/provenance.js +15 -1
- package/dist/scheduling/scheduler.class.d.ts.map +1 -1
- package/dist/scheduling/scheduler.class.js +4 -1
- package/dist/scheduling/sites.d.ts +35 -0
- package/dist/scheduling/sites.d.ts.map +1 -0
- package/dist/scheduling/sites.js +112 -0
- package/dist/scheduling/types.d.ts +133 -1
- package/dist/scheduling/types.d.ts.map +1 -1
- package/package.json +13 -1
package/dist/scheduling/model.js
CHANGED
|
@@ -26,16 +26,52 @@
|
|
|
26
26
|
* modeled properly here.
|
|
27
27
|
*/
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.MINUTES_PER_DAY = exports.availabilityApplies = exports.buildModel = exports.resolveConstraints = exports.mergeRules = exports.expandTemplate = exports.addDays = exports.daysBetween = void 0;
|
|
29
|
+
exports.MINUTES_PER_DAY = exports.availabilityApplies = exports.buildModel = exports.resolveConstraints = exports.mergeRules = exports.expandTemplate = exports.expandShiftInstances = exports.addDays = exports.daysBetween = void 0;
|
|
30
30
|
const types_1 = require("./types");
|
|
31
31
|
const constraint_1 = require("./constraints/constraint");
|
|
32
32
|
const min_rest_1 = require("./constraints/min-rest");
|
|
33
33
|
const overtime_1 = require("./constraints/overtime");
|
|
34
|
+
const contract_hours_1 = require("./contract-hours");
|
|
35
|
+
const sites_1 = require("./sites");
|
|
34
36
|
const time_1 = require("./time");
|
|
35
37
|
Object.defineProperty(exports, "MINUTES_PER_DAY", { enumerable: true, get: function () { return time_1.MINUTES_PER_DAY; } });
|
|
36
38
|
var time_2 = require("./time");
|
|
37
39
|
Object.defineProperty(exports, "daysBetween", { enumerable: true, get: function () { return time_2.daysBetween; } });
|
|
38
40
|
Object.defineProperty(exports, "addDays", { enumerable: true, get: function () { return time_2.addDays; } });
|
|
41
|
+
/**
|
|
42
|
+
* Expand a whole input's templates into the dated occurrences the engine will
|
|
43
|
+
* judge — the grid a host draws before anything is assigned.
|
|
44
|
+
*
|
|
45
|
+
* It takes the `ScheduleInput` rather than a clock so the caller cannot hand in
|
|
46
|
+
* a period clock built on a different timezone or length than the solve will
|
|
47
|
+
* use: the ids and wall-clock minutes here are the ones every later call
|
|
48
|
+
* (`explainCandidate`, `checkCompliance`, a returned `ScheduledAssignment`)
|
|
49
|
+
* refers to.
|
|
50
|
+
*/
|
|
51
|
+
function expandShiftInstances(input) {
|
|
52
|
+
(0, time_1.assertIsoDate)(input.period.startDate, 'period.startDate');
|
|
53
|
+
(0, time_1.assertIsoDate)(input.period.endDate, 'period.endDate');
|
|
54
|
+
const periodDays = (0, time_1.daysBetween)(input.period.startDate, input.period.endDate) + 1;
|
|
55
|
+
if (periodDays <= 0)
|
|
56
|
+
throw new types_1.ScheduleValidationError('period.endDate must not precede period.startDate');
|
|
57
|
+
const clock = new time_1.PeriodClock(input.period.startDate, periodDays, input.period.timeZone ?? 'UTC');
|
|
58
|
+
const publicHolidays = new Set(input.calendar?.publicHolidays ?? []);
|
|
59
|
+
for (const date of publicHolidays)
|
|
60
|
+
(0, time_1.assertIsoDate)(date, 'calendar.publicHolidays');
|
|
61
|
+
const context = { nightRule: input.rules?.nightWork, publicHolidays };
|
|
62
|
+
const instances = [];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
for (const template of input.shifts) {
|
|
65
|
+
for (const inst of expandTemplate(template, clock, periodDays, context)) {
|
|
66
|
+
if (seen.has(inst.id))
|
|
67
|
+
throw new types_1.ScheduleValidationError(`Duplicate shift instance id "${inst.id}"`);
|
|
68
|
+
seen.add(inst.id);
|
|
69
|
+
instances.push(inst);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return instances;
|
|
73
|
+
}
|
|
74
|
+
exports.expandShiftInstances = expandShiftInstances;
|
|
39
75
|
/** Expand one template into its dated instances inside the period. */
|
|
40
76
|
function expandTemplate(template, clock, periodDays, context) {
|
|
41
77
|
if (!template.id)
|
|
@@ -208,6 +244,22 @@ function validateEmployee(employee) {
|
|
|
208
244
|
(0, time_1.assertIsoDate)(q.validUntil, `employee "${employee.id}" qualifications.validUntil`);
|
|
209
245
|
}
|
|
210
246
|
}
|
|
247
|
+
function assertValidContractRule(rule, owner) {
|
|
248
|
+
if (!rule)
|
|
249
|
+
return;
|
|
250
|
+
for (const [field, value] of [
|
|
251
|
+
['fullTimeWeeklyMinutes', rule.fullTimeWeeklyMinutes],
|
|
252
|
+
['maxOverMinutes', rule.maxOverMinutes],
|
|
253
|
+
['maxUnderMinutes', rule.maxUnderMinutes],
|
|
254
|
+
]) {
|
|
255
|
+
if (value !== undefined && (!Number.isFinite(value) || value < 0)) {
|
|
256
|
+
throw new types_1.ScheduleValidationError(`${owner}: contract.${field} must be a non-negative number`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (rule.fullTimeWeeklyMinutes !== undefined && rule.fullTimeWeeklyMinutes === 0) {
|
|
260
|
+
throw new types_1.ScheduleValidationError(`${owner}: contract.fullTimeWeeklyMinutes must be positive`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
211
263
|
/**
|
|
212
264
|
* Merge the global rule set with one person's overrides.
|
|
213
265
|
*
|
|
@@ -262,6 +314,7 @@ function buildHistory(history, clock, personIdOf) {
|
|
|
262
314
|
end,
|
|
263
315
|
workingMinutes: entry.workingMinutes ?? end - start,
|
|
264
316
|
tag: entry.shiftTypeTag,
|
|
317
|
+
siteId: entry.siteId,
|
|
265
318
|
historical: true,
|
|
266
319
|
});
|
|
267
320
|
out.set(personId, entries);
|
|
@@ -280,6 +333,7 @@ function buildModel(input) {
|
|
|
280
333
|
throw new types_1.ScheduleValidationError('period.endDate must not precede period.startDate');
|
|
281
334
|
const clock = new time_1.PeriodClock(input.period.startDate, periodDays, input.period.timeZone ?? 'UTC');
|
|
282
335
|
const rules = input.rules ?? {};
|
|
336
|
+
const siteIndex = (0, sites_1.buildSiteIndex)(input.sites, input.travelSpeedKmh);
|
|
283
337
|
const publicHolidays = new Set(input.calendar?.publicHolidays ?? []);
|
|
284
338
|
for (const date of publicHolidays)
|
|
285
339
|
(0, time_1.assertIsoDate)(date, 'calendar.publicHolidays');
|
|
@@ -315,6 +369,32 @@ function buildModel(input) {
|
|
|
315
369
|
personIdOf.set(employee.id, personId);
|
|
316
370
|
employeesOfPerson.set(personId, [...(employeesOfPerson.get(personId) ?? []), employee.id]);
|
|
317
371
|
}
|
|
372
|
+
// The contracted week resolves exactly once, against the rules that apply
|
|
373
|
+
// to the person (a per-person override may carry its own full-time week),
|
|
374
|
+
// and every rule reads the resolved figure from the context.
|
|
375
|
+
const contractedWeeklyMinutes = new Map();
|
|
376
|
+
const contractedPeriod = new Map();
|
|
377
|
+
for (const employee of input.employees) {
|
|
378
|
+
const weekly = (0, contract_hours_1.resolveContractedWeeklyMinutes)(employee, rulesByEmployee.get(employee.id) ?? rules);
|
|
379
|
+
if (weekly === undefined)
|
|
380
|
+
continue;
|
|
381
|
+
contractedWeeklyMinutes.set(employee.id, weekly);
|
|
382
|
+
contractedPeriod.set(employee.id, (0, contract_hours_1.contractedPeriodMinutes)(weekly, periodDays));
|
|
383
|
+
}
|
|
384
|
+
const contractHoursWeight = input.objectives?.contractHoursWeight ?? contract_hours_1.DEFAULT_CONTRACT_HOURS_WEIGHT;
|
|
385
|
+
if (!Number.isFinite(contractHoursWeight) || contractHoursWeight < 0) {
|
|
386
|
+
throw new types_1.ScheduleValidationError(`objectives.contractHoursWeight must be a non-negative number`);
|
|
387
|
+
}
|
|
388
|
+
const fillToContract = input.objectives?.fillToContract ?? true;
|
|
389
|
+
if (typeof fillToContract !== 'boolean') {
|
|
390
|
+
throw new types_1.ScheduleValidationError(`objectives.fillToContract must be a boolean`);
|
|
391
|
+
}
|
|
392
|
+
if (rules.contract)
|
|
393
|
+
assertValidContractRule(rules.contract, 'rules');
|
|
394
|
+
for (const employee of input.employees) {
|
|
395
|
+
if (employee.rules?.contract)
|
|
396
|
+
assertValidContractRule(employee.rules.contract, `employee "${employee.id}"`);
|
|
397
|
+
}
|
|
318
398
|
const absences = new Map();
|
|
319
399
|
for (const absence of input.absences ?? []) {
|
|
320
400
|
const span = spanToMinutes(absence.from, absence.to, clock, 'absences');
|
|
@@ -349,12 +429,17 @@ function buildModel(input) {
|
|
|
349
429
|
employeeTags,
|
|
350
430
|
instances,
|
|
351
431
|
instanceById,
|
|
432
|
+
siteIndex,
|
|
352
433
|
employeeBlockedIntervals,
|
|
353
434
|
minRestMinutes: input.constraints?.minRestMinutes ?? rules.dailyRest?.minMinutes ?? min_rest_1.DEFAULT_MIN_REST_MINUTES,
|
|
354
435
|
constraints: resolveConstraints(input.constraints, rules, input.objectives),
|
|
355
436
|
clock,
|
|
356
437
|
rules,
|
|
357
438
|
rulesByEmployee,
|
|
439
|
+
contractedWeeklyMinutes,
|
|
440
|
+
contractedPeriodMinutes: contractedPeriod,
|
|
441
|
+
contractHoursWeight,
|
|
442
|
+
fillToContract,
|
|
358
443
|
personIdOf,
|
|
359
444
|
employeesOfPerson,
|
|
360
445
|
history: buildHistory(input.history, clock, personIdOf),
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
* - `diagnoseInfeasibility` — why the problem cannot be solved, before it is
|
|
18
18
|
* attempted.
|
|
19
19
|
*/
|
|
20
|
-
import type { AssignmentPair, ConstraintViolation, LedgerEntry, RuleVerdict, ScheduleInput, ScheduledAssignment } from './types';
|
|
20
|
+
import type { AssignmentPair, ConstraintViolation, LedgerEntry, RuleVerdict, ScheduleInput, ScheduledAssignment, ContractHoursSummary } from './types';
|
|
21
21
|
export interface ComplianceReport {
|
|
22
22
|
compliant: boolean;
|
|
23
23
|
violations: ConstraintViolation[];
|
|
@@ -33,6 +33,8 @@ export interface ComplianceReport {
|
|
|
33
33
|
* never validate as "compliant but owing nothing".
|
|
34
34
|
*/
|
|
35
35
|
ledger: LedgerEntry[];
|
|
36
|
+
/** Planned against contracted hours — the same summary the solver reports for the same assignments. */
|
|
37
|
+
contractHours: ContractHoursSummary[];
|
|
36
38
|
}
|
|
37
39
|
/**
|
|
38
40
|
* Validate an externally-produced or hand-edited roster.
|
|
@@ -74,9 +76,23 @@ export interface RepairCandidate {
|
|
|
74
76
|
marginalCostCents: number;
|
|
75
77
|
/** How far below their fair share of extra work this person is. Higher means more owed. */
|
|
76
78
|
fairnessDebt: number;
|
|
79
|
+
/**
|
|
80
|
+
* Where this shift would leave the person against their contracted period
|
|
81
|
+
* total: `(planned + this shift) − contracted`, negative while still under
|
|
82
|
+
* contract. Only for employees whose contracted week resolves.
|
|
83
|
+
*/
|
|
84
|
+
contractDeltaMinutes?: number;
|
|
77
85
|
/** Lower is a better call. */
|
|
78
86
|
rank: number;
|
|
79
87
|
rationale: string;
|
|
88
|
+
/** Site the candidate is travelling from (same-day assignment, then home site). */
|
|
89
|
+
originSiteId?: string;
|
|
90
|
+
/** Estimated travel minutes from origin to the shift's site. */
|
|
91
|
+
travelMinutes?: number;
|
|
92
|
+
/** Straight-line distance from origin to the shift's site, when minutes are unavailable. */
|
|
93
|
+
distanceKm?: number;
|
|
94
|
+
/** Whether the shift is at this employee's home site. */
|
|
95
|
+
isHomeSite: boolean;
|
|
80
96
|
}
|
|
81
97
|
export interface RepairResult {
|
|
82
98
|
diff: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../src/scheduling/operations.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,KAAK,EACR,cAAc,EACd,mBAAmB,EACnB,WAAW,EACX,WAAW,EACX,aAAa,EACb,mBAAmB,
|
|
1
|
+
{"version":3,"file":"operations.d.ts","sourceRoot":"","sources":["../../src/scheduling/operations.ts"],"names":[],"mappings":"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"}
|
|
@@ -28,11 +28,17 @@ const verdicts_1 = require("./engine/verdicts");
|
|
|
28
28
|
const scheduler_class_1 = require("./scheduler.class");
|
|
29
29
|
const availability_1 = require("./constraints/availability");
|
|
30
30
|
const cost_1 = require("./cost");
|
|
31
|
+
const contract_hours_1 = require("./contract-hours");
|
|
32
|
+
const sites_1 = require("./sites");
|
|
31
33
|
const rest_days_1 = require("./constraints/rest-days");
|
|
32
34
|
const notice_1 = require("./constraints/notice");
|
|
33
35
|
const protections_1 = require("./constraints/protections");
|
|
34
36
|
const overtime_1 = require("./constraints/overtime");
|
|
35
37
|
const time_1 = require("./time");
|
|
38
|
+
/** Soft-ranking weight for travel minutes in `rankCandidates`. */
|
|
39
|
+
const TRAVEL_MINUTE_WEIGHT = 1;
|
|
40
|
+
/** Soft-ranking weight per hour a candidate would be pushed over their contracted period total. */
|
|
41
|
+
const CONTRACT_HOUR_WEIGHT = 10;
|
|
36
42
|
/**
|
|
37
43
|
* Validate an externally-produced or hand-edited roster.
|
|
38
44
|
*
|
|
@@ -88,6 +94,7 @@ function checkCompliance(input, roster) {
|
|
|
88
94
|
violations,
|
|
89
95
|
verdicts,
|
|
90
96
|
ledger,
|
|
97
|
+
contractHours: (0, contract_hours_1.contractHoursSummary)(state),
|
|
91
98
|
};
|
|
92
99
|
}
|
|
93
100
|
exports.checkCompliance = checkCompliance;
|
|
@@ -193,6 +200,22 @@ function rankCandidates(input, shiftInstanceId, roster, disruption) {
|
|
|
193
200
|
const costCents = (0, cost_1.marginalCostCents)(inst, employee, state.minutesByEmployee.get(employee.id) ?? 0, ctx.rules.engagement);
|
|
194
201
|
const fairnessDebt = meanExtra - (extraLoad.get(employee.id) ?? 0);
|
|
195
202
|
const preference = (0, availability_1.preferenceScore)(ctx.clock, employee.availability, inst);
|
|
203
|
+
const originSiteId = originSiteFor(state, employee.id, inst);
|
|
204
|
+
const travelMinutes = originSiteId !== undefined && inst.siteId !== undefined
|
|
205
|
+
? (0, sites_1.travelMinutesBetween)(ctx.siteIndex, originSiteId, inst.siteId)
|
|
206
|
+
: undefined;
|
|
207
|
+
const distanceKm = travelMinutes === undefined && originSiteId !== undefined && inst.siteId !== undefined
|
|
208
|
+
? (0, sites_1.distanceKmBetween)(ctx.siteIndex, originSiteId, inst.siteId)
|
|
209
|
+
: undefined;
|
|
210
|
+
const isHomeSite = employee.homeSiteId !== undefined && employee.homeSiteId === inst.siteId;
|
|
211
|
+
// Contract headroom: a person still short of their contracted hours is
|
|
212
|
+
// a better call than one this shift would push past them — declared
|
|
213
|
+
// terms and realised counts only, nothing behavioural.
|
|
214
|
+
const contracted = ctx.contractedPeriodMinutes.get(employee.id);
|
|
215
|
+
const contractDeltaMinutes = contracted === undefined
|
|
216
|
+
? undefined
|
|
217
|
+
: (state.minutesByEmployee.get(employee.id) ?? 0) + inst.workingMinutes - contracted;
|
|
218
|
+
const contractPenalty = contractDeltaMinutes === undefined ? 0 : Math.max(0, contractDeltaMinutes) / 60;
|
|
196
219
|
candidates.push({
|
|
197
220
|
employeeId: employee.id,
|
|
198
221
|
shiftInstanceId,
|
|
@@ -201,26 +224,71 @@ function rankCandidates(input, shiftInstanceId, roster, disruption) {
|
|
|
201
224
|
blockers,
|
|
202
225
|
marginalCostCents: costCents,
|
|
203
226
|
fairnessDebt,
|
|
227
|
+
...(contractDeltaMinutes !== undefined ? { contractDeltaMinutes } : {}),
|
|
228
|
+
originSiteId,
|
|
229
|
+
travelMinutes,
|
|
230
|
+
distanceKm,
|
|
231
|
+
isHomeSite,
|
|
204
232
|
// Ineligible candidates sort to the bottom but stay visible.
|
|
205
|
-
rank: eligible
|
|
206
|
-
|
|
233
|
+
rank: eligible
|
|
234
|
+
? costCents / 100 -
|
|
235
|
+
fairnessDebt * 10 +
|
|
236
|
+
preference * 5 +
|
|
237
|
+
(travelMinutes ?? 0) * TRAVEL_MINUTE_WEIGHT +
|
|
238
|
+
contractPenalty * CONTRACT_HOUR_WEIGHT
|
|
239
|
+
: Number.MAX_SAFE_INTEGER,
|
|
240
|
+
rationale: rationaleFor(eligible, blockers, costCents, fairnessDebt, preference, travelMinutes, distanceKm, originSiteId, isHomeSite, contractDeltaMinutes),
|
|
207
241
|
});
|
|
208
242
|
}
|
|
209
243
|
return candidates.sort((a, b) => a.rank - b.rank || (a.employeeId < b.employeeId ? -1 : 1));
|
|
210
244
|
}
|
|
211
245
|
exports.rankCandidates = rankCandidates;
|
|
212
|
-
function rationaleFor(eligible, blockers, costCents, fairnessDebt, preference) {
|
|
246
|
+
function rationaleFor(eligible, blockers, costCents, fairnessDebt, preference, travelMinutes, distanceKm, originSiteId, isHomeSite, contractDeltaMinutes) {
|
|
213
247
|
if (!eligible)
|
|
214
248
|
return blockers.map((b) => b.message).join('; ') || 'not eligible';
|
|
215
249
|
const parts = [`€${(costCents / 100).toFixed(2)} marginal cost`];
|
|
216
250
|
if (fairnessDebt > 0)
|
|
217
251
|
parts.push(`${fairnessDebt.toFixed(1)} fewer extra shifts than average`);
|
|
252
|
+
if (contractDeltaMinutes !== undefined) {
|
|
253
|
+
parts.push(contractDeltaMinutes > 0
|
|
254
|
+
? `${(contractDeltaMinutes / 60).toFixed(1)}h over contract with this shift`
|
|
255
|
+
: `${(-contractDeltaMinutes / 60).toFixed(1)}h under contract with this shift`);
|
|
256
|
+
}
|
|
218
257
|
if (preference < 0)
|
|
219
258
|
parts.push('prefers this window');
|
|
220
259
|
if (preference > 0)
|
|
221
260
|
parts.push('would rather avoid this window');
|
|
261
|
+
if (isHomeSite) {
|
|
262
|
+
parts.push('home site');
|
|
263
|
+
}
|
|
264
|
+
else if (travelMinutes !== undefined && originSiteId !== undefined) {
|
|
265
|
+
parts.push(`${travelMinutes} min from ${originSiteId}`);
|
|
266
|
+
}
|
|
267
|
+
else if (distanceKm !== undefined && originSiteId !== undefined) {
|
|
268
|
+
parts.push(`${distanceKm.toFixed(1)} km from ${originSiteId}`);
|
|
269
|
+
}
|
|
222
270
|
return parts.join(' · ');
|
|
223
271
|
}
|
|
272
|
+
/** Site the candidate would travel from: nearest preceding same-day assignment, then home site. */
|
|
273
|
+
function originSiteFor(state, employeeId, inst) {
|
|
274
|
+
const assigned = state.byEmployee.get(employeeId);
|
|
275
|
+
const homeSite = state.ctx.employeeById.get(employeeId)?.homeSiteId;
|
|
276
|
+
if (!assigned)
|
|
277
|
+
return homeSite;
|
|
278
|
+
let best;
|
|
279
|
+
for (const instanceId of assigned) {
|
|
280
|
+
const other = state.ctx.instanceById.get(instanceId);
|
|
281
|
+
if (!other || other.date !== inst.date || !other.siteId)
|
|
282
|
+
continue;
|
|
283
|
+
if (other.endMinute <= inst.startMinute) {
|
|
284
|
+
const gap = inst.startMinute - other.endMinute;
|
|
285
|
+
if (!best || gap < best.gap) {
|
|
286
|
+
best = { siteId: other.siteId, gap };
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return best?.siteId ?? homeSite;
|
|
291
|
+
}
|
|
224
292
|
/** Shifts each person already holds, as the basis for fairness debt. */
|
|
225
293
|
function extraShiftCounts(state) {
|
|
226
294
|
const counts = new Map();
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* commitment: it exists to detect that the rules changed between two runs, and
|
|
12
12
|
* to key an audit record. It is deliberately dependency-free.
|
|
13
13
|
*/
|
|
14
|
-
import type { ConstraintOptions, ObjectiveWeights, WorkingTimeRules } from './types';
|
|
14
|
+
import type { ConstraintOptions, ObjectiveWeights, Site, WorkingTimeRules } from './types';
|
|
15
15
|
/**
|
|
16
16
|
* A stable digest of the rule set, constraint options and objective weights.
|
|
17
17
|
*
|
|
@@ -19,7 +19,7 @@ import type { ConstraintOptions, ObjectiveWeights, WorkingTimeRules } from './ty
|
|
|
19
19
|
* in property order produce the same digest — otherwise a round-trip through
|
|
20
20
|
* JSON would look like a rule change.
|
|
21
21
|
*/
|
|
22
|
-
export declare function hashRules(rules: WorkingTimeRules | undefined, options: ConstraintOptions | undefined, objectives?: ObjectiveWeights): string;
|
|
22
|
+
export declare function hashRules(rules: WorkingTimeRules | undefined, options: ConstraintOptions | undefined, objectives?: ObjectiveWeights, sites?: Site[], travelSpeedKmh?: number): string;
|
|
23
23
|
/** JSON with object keys sorted recursively. */
|
|
24
24
|
export declare function stableStringify(value: unknown): string;
|
|
25
25
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provenance.d.ts","sourceRoot":"","sources":["../../src/scheduling/provenance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"provenance.d.ts","sourceRoot":"","sources":["../../src/scheduling/provenance.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,IAAI,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAE3F;;;;;;GAMG;AACH,wBAAgB,SAAS,CACrB,KAAK,EAAE,gBAAgB,GAAG,SAAS,EACnC,OAAO,EAAE,iBAAiB,GAAG,SAAS,EACtC,UAAU,CAAC,EAAE,gBAAgB,EAC7B,KAAK,CAAC,EAAE,IAAI,EAAE,EACd,cAAc,CAAC,EAAE,MAAM,GACxB,MAAM,CAgBR;AAaD,gDAAgD;AAChD,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAOtD;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAY7C"}
|
|
@@ -21,7 +21,7 @@ exports.fnv1a64 = exports.stableStringify = exports.hashRules = void 0;
|
|
|
21
21
|
* in property order produce the same digest — otherwise a round-trip through
|
|
22
22
|
* JSON would look like a rule change.
|
|
23
23
|
*/
|
|
24
|
-
function hashRules(rules, options, objectives) {
|
|
24
|
+
function hashRules(rules, options, objectives, sites, travelSpeedKmh) {
|
|
25
25
|
const payload = stableStringify({
|
|
26
26
|
rules: rules ?? null,
|
|
27
27
|
// Only the parts of `constraints` that change outcomes: custom rules are
|
|
@@ -31,10 +31,24 @@ function hashRules(rules, options, objectives) {
|
|
|
31
31
|
overrides: options?.overrides ?? null,
|
|
32
32
|
custom: (options?.custom ?? []).map((c) => ({ id: c.id, hardness: c.hardness, weight: c.weight ?? null })),
|
|
33
33
|
objectives: objectives ?? null,
|
|
34
|
+
// Omit these keys entirely when absent so existing inputs without site data
|
|
35
|
+
// keep the same rulesHash they had before this feature existed.
|
|
36
|
+
...(sites ? { sites: sites.map(siteDigest) } : {}),
|
|
37
|
+
...(travelSpeedKmh !== undefined ? { travelSpeedKmh } : {}),
|
|
34
38
|
});
|
|
35
39
|
return fnv1a64(payload);
|
|
36
40
|
}
|
|
37
41
|
exports.hashRules = hashRules;
|
|
42
|
+
function siteDigest(site) {
|
|
43
|
+
return {
|
|
44
|
+
id: site.id,
|
|
45
|
+
lat: site.lat ?? null,
|
|
46
|
+
lng: site.lng ?? null,
|
|
47
|
+
travelMinutesTo: site.travelMinutesTo
|
|
48
|
+
? Object.fromEntries(Object.entries(site.travelMinutesTo).sort(([a], [b]) => (a < b ? -1 : 1)))
|
|
49
|
+
: null,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
38
52
|
/** JSON with object keys sorted recursively. */
|
|
39
53
|
function stableStringify(value) {
|
|
40
54
|
if (value === null || typeof value !== 'object')
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scheduler.class.d.ts","sourceRoot":"","sources":["../../src/scheduling/scheduler.class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAoC,aAAa,EAAE,cAAc,EAAuB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"scheduler.class.d.ts","sourceRoot":"","sources":["../../src/scheduling/scheduler.class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAoC,aAAa,EAAE,cAAc,EAAuB,MAAM,SAAS,CAAC;AAsBpH,eAAO,MAAM,cAAc,MAAM,CAAC;AAElC,qBAAa,cAAc;IACvB,qGAAqG;IACrG,KAAK,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc;IA+C3C,qFAAqF;IACrF,OAAO,CAAC,QAAQ;CA8EnB;AAUD,+EAA+E;AAC/E,wBAAgB,aAAa,CAAC,KAAK,EAAE,aAAa,GAAG,cAAc,CAElE"}
|
|
@@ -28,6 +28,7 @@ const notice_1 = require("./constraints/notice");
|
|
|
28
28
|
const protections_1 = require("./constraints/protections");
|
|
29
29
|
const overtime_1 = require("./constraints/overtime");
|
|
30
30
|
const cost_1 = require("./cost");
|
|
31
|
+
const contract_hours_1 = require("./contract-hours");
|
|
31
32
|
const prng_1 = require("./engine/prng");
|
|
32
33
|
const verdicts_1 = require("./engine/verdicts");
|
|
33
34
|
const provenance_1 = require("./provenance");
|
|
@@ -110,6 +111,7 @@ class ShiftScheduler {
|
|
|
110
111
|
...(0, overtime_1.overtimeLedger)(state),
|
|
111
112
|
];
|
|
112
113
|
const cost = (0, cost_1.rosterCost)(state);
|
|
114
|
+
const contractHours = (0, contract_hours_1.contractHoursSummary)(state);
|
|
113
115
|
return {
|
|
114
116
|
status,
|
|
115
117
|
assignments,
|
|
@@ -122,7 +124,7 @@ class ShiftScheduler {
|
|
|
122
124
|
provenance: {
|
|
123
125
|
engineVersion: exports.ENGINE_VERSION,
|
|
124
126
|
seed,
|
|
125
|
-
rulesHash: (0, provenance_1.hashRules)(input.rules, input.constraints, input.objectives),
|
|
127
|
+
rulesHash: (0, provenance_1.hashRules)(input.rules, input.constraints, input.objectives, input.sites, input.travelSpeedKmh),
|
|
126
128
|
dutyClassificationNotes: dutyNotes(ctx),
|
|
127
129
|
// Nothing in this module consumes a behavioural or predictive
|
|
128
130
|
// per-worker signal, so the claim is structural rather than a
|
|
@@ -131,6 +133,7 @@ class ShiftScheduler {
|
|
|
131
133
|
},
|
|
132
134
|
...(ledger.length ? { ledger } : {}),
|
|
133
135
|
...(cost ? { cost } : {}),
|
|
136
|
+
...(contractHours.length ? { contractHours } : {}),
|
|
134
137
|
};
|
|
135
138
|
}
|
|
136
139
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Site registry and travel-time resolution.
|
|
3
|
+
*
|
|
4
|
+
* Multi-site rosters need two numbers that the caller owns: where a site is
|
|
5
|
+
* (coordinates) and how long it takes to move between sites (a travel-time
|
|
6
|
+
* matrix or a fallback speed). The engine never calls an external routing
|
|
7
|
+
* service and never invents default values — it only does pure arithmetic on
|
|
8
|
+
* caller-supplied data.
|
|
9
|
+
*/
|
|
10
|
+
import type { Site, SiteIndex } from './types';
|
|
11
|
+
/**
|
|
12
|
+
* Build a validated site index from caller input.
|
|
13
|
+
*
|
|
14
|
+
* Throws `ScheduleValidationError` for duplicate ids, malformed coordinates,
|
|
15
|
+
* negative matrix entries, or a non-positive fallback speed. Site ids that are
|
|
16
|
+
* referenced by templates or employees but not declared here are tolerated;
|
|
17
|
+
* they simply have no coordinates or matrix entries.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildSiteIndex(sites: Site[] | undefined, travelSpeedKmh?: number): SiteIndex;
|
|
20
|
+
/**
|
|
21
|
+
* Travel time between two sites, in minutes.
|
|
22
|
+
*
|
|
23
|
+
* Returns 0 for the same site, undefined when either site is unknown or no
|
|
24
|
+
* travel data exists for the ordered pair.
|
|
25
|
+
*/
|
|
26
|
+
export declare function travelMinutesBetween(index: SiteIndex, from?: string, to?: string): number | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Straight-line distance between two sites, in kilometres.
|
|
29
|
+
*
|
|
30
|
+
* Returns 0 for same site, undefined when either site is unknown or lacks
|
|
31
|
+
* coordinates. Used for display and host-side re-sorting when minutes are not
|
|
32
|
+
* available.
|
|
33
|
+
*/
|
|
34
|
+
export declare function distanceKmBetween(index: SiteIndex, from?: string, to?: string): number | undefined;
|
|
35
|
+
//# sourceMappingURL=sites.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sites.d.ts","sourceRoot":"","sources":["../../src/scheduling/sites.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAI/C;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,SAAS,EAAE,cAAc,CAAC,EAAE,MAAM,GAAG,SAAS,CA2C5F;AAsBD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAIrG;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAalG"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Site registry and travel-time resolution.
|
|
4
|
+
*
|
|
5
|
+
* Multi-site rosters need two numbers that the caller owns: where a site is
|
|
6
|
+
* (coordinates) and how long it takes to move between sites (a travel-time
|
|
7
|
+
* matrix or a fallback speed). The engine never calls an external routing
|
|
8
|
+
* service and never invents default values — it only does pure arithmetic on
|
|
9
|
+
* caller-supplied data.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.distanceKmBetween = exports.travelMinutesBetween = exports.buildSiteIndex = void 0;
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const geo_1 = require("../utils/geo");
|
|
15
|
+
/**
|
|
16
|
+
* Build a validated site index from caller input.
|
|
17
|
+
*
|
|
18
|
+
* Throws `ScheduleValidationError` for duplicate ids, malformed coordinates,
|
|
19
|
+
* negative matrix entries, or a non-positive fallback speed. Site ids that are
|
|
20
|
+
* referenced by templates or employees but not declared here are tolerated;
|
|
21
|
+
* they simply have no coordinates or matrix entries.
|
|
22
|
+
*/
|
|
23
|
+
function buildSiteIndex(sites, travelSpeedKmh) {
|
|
24
|
+
if (travelSpeedKmh !== undefined && (!Number.isFinite(travelSpeedKmh) || travelSpeedKmh <= 0)) {
|
|
25
|
+
throw new types_1.ScheduleValidationError(`travelSpeedKmh must be a positive finite number: ${travelSpeedKmh}`);
|
|
26
|
+
}
|
|
27
|
+
const byId = new Map();
|
|
28
|
+
for (const site of sites ?? []) {
|
|
29
|
+
if (!site.id)
|
|
30
|
+
throw new types_1.ScheduleValidationError('Site is missing `id`');
|
|
31
|
+
if (byId.has(site.id))
|
|
32
|
+
throw new types_1.ScheduleValidationError(`Duplicate site id "${site.id}"`);
|
|
33
|
+
if (site.lat !== undefined || site.lng !== undefined) {
|
|
34
|
+
if (!(0, geo_1.hasValidCoordinates)(site.lat, site.lng)) {
|
|
35
|
+
throw new types_1.ScheduleValidationError(`Site "${site.id}" has invalid coordinates: lat=${site.lat}, lng=${site.lng}`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (site.travelMinutesTo) {
|
|
39
|
+
for (const [target, minutes] of Object.entries(site.travelMinutesTo)) {
|
|
40
|
+
if (!Number.isFinite(minutes) || minutes < 0) {
|
|
41
|
+
throw new types_1.ScheduleValidationError(`Site "${site.id}".travelMinutesTo["${target}"] has invalid minutes: ${minutes}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
byId.set(site.id, site);
|
|
46
|
+
}
|
|
47
|
+
const resolvedTravelMinutes = new Map();
|
|
48
|
+
const siteList = [...byId.values()];
|
|
49
|
+
for (const from of siteList) {
|
|
50
|
+
const row = new Map();
|
|
51
|
+
resolvedTravelMinutes.set(from.id, row);
|
|
52
|
+
for (const to of siteList) {
|
|
53
|
+
const minutes = resolvePairMinutes(from, to, travelSpeedKmh);
|
|
54
|
+
if (minutes !== undefined)
|
|
55
|
+
row.set(to.id, minutes);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { byId, travelSpeedKmh, resolvedTravelMinutes };
|
|
59
|
+
}
|
|
60
|
+
exports.buildSiteIndex = buildSiteIndex;
|
|
61
|
+
function resolvePairMinutes(from, to, travelSpeedKmh) {
|
|
62
|
+
if (from.id === to.id)
|
|
63
|
+
return 0;
|
|
64
|
+
const matrix = from.travelMinutesTo?.[to.id];
|
|
65
|
+
if (matrix !== undefined)
|
|
66
|
+
return matrix;
|
|
67
|
+
if (from.lat !== undefined &&
|
|
68
|
+
from.lng !== undefined &&
|
|
69
|
+
to.lat !== undefined &&
|
|
70
|
+
to.lng !== undefined &&
|
|
71
|
+
travelSpeedKmh !== undefined) {
|
|
72
|
+
const km = (0, geo_1.haversineDistanceKm)(from.lat, from.lng, to.lat, to.lng);
|
|
73
|
+
return Math.round((km / travelSpeedKmh) * 60);
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Travel time between two sites, in minutes.
|
|
79
|
+
*
|
|
80
|
+
* Returns 0 for the same site, undefined when either site is unknown or no
|
|
81
|
+
* travel data exists for the ordered pair.
|
|
82
|
+
*/
|
|
83
|
+
function travelMinutesBetween(index, from, to) {
|
|
84
|
+
if (from === to && from !== undefined)
|
|
85
|
+
return 0;
|
|
86
|
+
if (from === undefined || to === undefined)
|
|
87
|
+
return undefined;
|
|
88
|
+
return index.resolvedTravelMinutes.get(from)?.get(to);
|
|
89
|
+
}
|
|
90
|
+
exports.travelMinutesBetween = travelMinutesBetween;
|
|
91
|
+
/**
|
|
92
|
+
* Straight-line distance between two sites, in kilometres.
|
|
93
|
+
*
|
|
94
|
+
* Returns 0 for same site, undefined when either site is unknown or lacks
|
|
95
|
+
* coordinates. Used for display and host-side re-sorting when minutes are not
|
|
96
|
+
* available.
|
|
97
|
+
*/
|
|
98
|
+
function distanceKmBetween(index, from, to) {
|
|
99
|
+
if (from === to && from !== undefined)
|
|
100
|
+
return 0;
|
|
101
|
+
if (from === undefined || to === undefined)
|
|
102
|
+
return undefined;
|
|
103
|
+
const fromSite = index.byId.get(from);
|
|
104
|
+
const toSite = index.byId.get(to);
|
|
105
|
+
if (!fromSite || !toSite)
|
|
106
|
+
return undefined;
|
|
107
|
+
if (fromSite.lat !== undefined && fromSite.lng !== undefined && toSite.lat !== undefined && toSite.lng !== undefined) {
|
|
108
|
+
return (0, geo_1.haversineDistanceKm)(fromSite.lat, fromSite.lng, toSite.lat, toSite.lng);
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
exports.distanceKmBetween = distanceKmBetween;
|