assignment-user-matcher 1.16.0 → 1.22.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 +312 -20
- package/dist/escalation/policy.d.ts +11 -0
- package/dist/escalation/policy.d.ts.map +1 -1
- package/dist/escalation/policy.js +22 -1
- package/dist/forecasting/backtest.d.ts +52 -0
- package/dist/forecasting/backtest.d.ts.map +1 -0
- package/dist/forecasting/backtest.js +195 -0
- package/dist/forecasting/calendar.d.ts +44 -0
- package/dist/forecasting/calendar.d.ts.map +1 -0
- package/dist/forecasting/calendar.js +85 -0
- package/dist/forecasting/cover.d.ts +69 -0
- package/dist/forecasting/cover.d.ts.map +1 -0
- package/dist/forecasting/cover.js +103 -0
- package/dist/forecasting/days.d.ts +17 -0
- package/dist/forecasting/days.d.ts.map +1 -0
- package/dist/forecasting/days.js +35 -0
- package/dist/forecasting/distributions.d.ts +31 -0
- package/dist/forecasting/distributions.d.ts.map +1 -0
- package/dist/forecasting/distributions.js +154 -0
- package/dist/forecasting/effects.d.ts +88 -0
- package/dist/forecasting/effects.d.ts.map +1 -0
- package/dist/forecasting/effects.js +303 -0
- package/dist/forecasting/erlang.d.ts +31 -0
- package/dist/forecasting/erlang.d.ts.map +1 -0
- package/dist/forecasting/erlang.js +86 -0
- package/dist/forecasting/forecast.d.ts +7 -0
- package/dist/forecasting/forecast.d.ts.map +1 -0
- package/dist/forecasting/forecast.js +221 -0
- package/dist/forecasting/index.d.ts +18 -0
- package/dist/forecasting/index.d.ts.map +1 -0
- package/dist/forecasting/index.js +54 -0
- package/dist/forecasting/model.d.ts +145 -0
- package/dist/forecasting/model.d.ts.map +1 -0
- package/dist/forecasting/model.js +583 -0
- package/dist/forecasting/nowcast.d.ts +26 -0
- package/dist/forecasting/nowcast.d.ts.map +1 -0
- package/dist/forecasting/nowcast.js +92 -0
- package/dist/forecasting/simulation.d.ts +91 -0
- package/dist/forecasting/simulation.d.ts.map +1 -0
- package/dist/forecasting/simulation.js +478 -0
- package/dist/forecasting/spikes.d.ts +6 -0
- package/dist/forecasting/spikes.d.ts.map +1 -0
- package/dist/forecasting/spikes.js +73 -0
- package/dist/forecasting/staffing.d.ts +64 -0
- package/dist/forecasting/staffing.d.ts.map +1 -0
- package/dist/forecasting/staffing.js +765 -0
- package/dist/forecasting/stats.d.ts +11 -0
- package/dist/forecasting/stats.d.ts.map +1 -0
- package/dist/forecasting/stats.js +45 -0
- package/dist/forecasting/types.d.ts +299 -0
- package/dist/forecasting/types.d.ts.map +1 -0
- package/dist/forecasting/types.js +8 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/learning/auto-weights.d.ts +1 -5
- package/dist/learning/auto-weights.d.ts.map +1 -1
- package/dist/learning/auto-weights.js +0 -3
- package/dist/lua/book-slot.lua +84 -0
- package/dist/lua/claim-queued.lua +47 -0
- package/dist/managers/DecisionTraceManager.d.ts +15 -0
- package/dist/managers/DecisionTraceManager.d.ts.map +1 -1
- package/dist/managers/DecisionTraceManager.js +117 -11
- package/dist/managers/LearningManager.d.ts +12 -0
- package/dist/managers/LearningManager.d.ts.map +1 -1
- package/dist/managers/LearningManager.js +21 -7
- package/dist/managers/ReliabilityManager.d.ts +1 -2
- package/dist/managers/ReliabilityManager.d.ts.map +1 -1
- package/dist/managers/ReliabilityManager.js +1 -1
- package/dist/managers/TelemetryManager.d.ts.map +1 -1
- package/dist/managers/TelemetryManager.js +9 -3
- package/dist/managers/WorkflowManager.d.ts.map +1 -1
- package/dist/managers/WorkflowManager.js +3 -4
- package/dist/matcher.class.d.ts +319 -4
- package/dist/matcher.class.d.ts.map +1 -1
- package/dist/matcher.class.js +1428 -102
- package/dist/queries/lane-stats.d.ts +51 -0
- package/dist/queries/lane-stats.d.ts.map +1 -0
- package/dist/queries/lane-stats.js +78 -0
- package/dist/schedule/recurrence.d.ts +11 -0
- package/dist/schedule/recurrence.d.ts.map +1 -1
- package/dist/schedule/recurrence.js +19 -2
- package/dist/schedule/slot.d.ts +60 -0
- package/dist/schedule/slot.d.ts.map +1 -0
- package/dist/schedule/slot.js +86 -0
- package/dist/scheduling/constraints/availability.d.ts +2 -14
- package/dist/scheduling/constraints/availability.d.ts.map +1 -1
- package/dist/scheduling/constraints/availability.js +14 -15
- package/dist/scheduling/constraints/consecutive.d.ts +10 -0
- package/dist/scheduling/constraints/consecutive.d.ts.map +1 -1
- package/dist/scheduling/constraints/consecutive.js +64 -1
- package/dist/scheduling/constraints/contract.d.ts.map +1 -1
- package/dist/scheduling/constraints/contract.js +12 -3
- package/dist/scheduling/constraints/employee-scope.d.ts.map +1 -1
- package/dist/scheduling/constraints/employee-scope.js +10 -2
- package/dist/scheduling/constraints/group-composition.js +2 -2
- package/dist/scheduling/constraints/min-staffing.d.ts.map +1 -1
- package/dist/scheduling/constraints/min-staffing.js +51 -8
- package/dist/scheduling/constraints/rolling-hours.d.ts.map +1 -1
- package/dist/scheduling/constraints/rolling-hours.js +29 -4
- package/dist/scheduling/constraints/support.d.ts +4 -8
- package/dist/scheduling/constraints/support.d.ts.map +1 -1
- package/dist/scheduling/constraints/support.js +23 -2
- package/dist/scheduling/constraints/weekly-rest.d.ts +28 -0
- package/dist/scheduling/constraints/weekly-rest.d.ts.map +1 -1
- package/dist/scheduling/constraints/weekly-rest.js +108 -21
- package/dist/scheduling/engine/construction.d.ts.map +1 -1
- package/dist/scheduling/engine/construction.js +56 -7
- package/dist/scheduling/engine/objective.d.ts +0 -1
- package/dist/scheduling/engine/objective.d.ts.map +1 -1
- package/dist/scheduling/engine/objective.js +65 -6
- package/dist/scheduling/engine/search.d.ts +4 -0
- package/dist/scheduling/engine/search.d.ts.map +1 -1
- package/dist/scheduling/engine/search.js +2 -1
- package/dist/scheduling/engine/state.d.ts +14 -0
- package/dist/scheduling/engine/state.d.ts.map +1 -1
- package/dist/scheduling/engine/state.js +7 -0
- package/dist/scheduling/engine/timeline.d.ts +23 -1
- package/dist/scheduling/engine/timeline.d.ts.map +1 -1
- package/dist/scheduling/engine/timeline.js +85 -27
- package/dist/scheduling/engine/verdicts.d.ts +40 -1
- package/dist/scheduling/engine/verdicts.d.ts.map +1 -1
- package/dist/scheduling/engine/verdicts.js +177 -17
- package/dist/scheduling/index.d.ts +3 -2
- package/dist/scheduling/index.d.ts.map +1 -1
- package/dist/scheduling/index.js +7 -1
- package/dist/scheduling/model.d.ts +8 -4
- package/dist/scheduling/model.d.ts.map +1 -1
- package/dist/scheduling/model.js +239 -23
- package/dist/scheduling/operations.d.ts +174 -2
- package/dist/scheduling/operations.d.ts.map +1 -1
- package/dist/scheduling/operations.js +558 -21
- package/dist/scheduling/preference-report.d.ts +15 -0
- package/dist/scheduling/preference-report.d.ts.map +1 -0
- package/dist/scheduling/preference-report.js +111 -0
- package/dist/scheduling/preferences.d.ts +30 -0
- package/dist/scheduling/preferences.d.ts.map +1 -0
- package/dist/scheduling/preferences.js +85 -0
- package/dist/scheduling/resilience.d.ts +46 -0
- package/dist/scheduling/resilience.d.ts.map +1 -0
- package/dist/scheduling/resilience.js +45 -0
- package/dist/scheduling/scheduler.class.d.ts +12 -1
- package/dist/scheduling/scheduler.class.d.ts.map +1 -1
- package/dist/scheduling/scheduler.class.js +28 -4
- package/dist/scheduling/sites.d.ts +1 -1
- package/dist/scheduling/sites.d.ts.map +1 -1
- package/dist/scheduling/sites.js +34 -1
- package/dist/scheduling/time.d.ts +28 -6
- package/dist/scheduling/time.d.ts.map +1 -1
- package/dist/scheduling/time.js +59 -17
- package/dist/scheduling/types.d.ts +247 -7
- package/dist/scheduling/types.d.ts.map +1 -1
- package/dist/scoring/fair-arbitration.d.ts.map +1 -1
- package/dist/scoring/fair-arbitration.js +2 -1
- package/dist/tracing/decision-trace.d.ts +5 -0
- package/dist/tracing/decision-trace.d.ts.map +1 -1
- package/dist/tracing/decision-trace.js +3 -0
- package/dist/types/matcher.d.ts +369 -2
- package/dist/types/matcher.d.ts.map +1 -1
- package/dist/updates/assignment-update.d.ts +93 -0
- package/dist/updates/assignment-update.d.ts.map +1 -0
- package/dist/updates/assignment-update.js +91 -0
- package/dist/utils/keys.d.ts +10 -0
- package/dist/utils/keys.d.ts.map +1 -1
- package/dist/utils/keys.js +43 -0
- package/package.json +25 -3
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.applyNowcast = exports.nowcastRatio = exports.NOWCAST_MAX_LAG_HOURS = exports.NOWCAST_PRIOR = exports.NOWCAST_HOURS = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Nowcast (spec §7.1 step 6): the next 72 hours rescaled by how today has gone
|
|
6
|
+
* so far — completed hours only, ignored under five expected, shrunk towards 1
|
|
7
|
+
* by `NOWCAST_PRIOR` tasks on each side, held to [0.5, 3] — fading linearly to
|
|
8
|
+
* nothing over 72 hours, because a busy morning says a lot about this
|
|
9
|
+
* afternoon and little about Thursday.
|
|
10
|
+
*/
|
|
11
|
+
const calendar_1 = require("./calendar");
|
|
12
|
+
const stats_1 = require("./stats");
|
|
13
|
+
exports.NOWCAST_HOURS = 72;
|
|
14
|
+
/**
|
|
15
|
+
* Pseudo-tasks added to both sides of the ratio: twelve seen over six expected at 7am is Poisson noise, not
|
|
16
|
+
* a doubling, and must not double the afternoon. Near-neutral early in a quiet day, full strength once
|
|
17
|
+
* hundreds of tasks back it.
|
|
18
|
+
*/
|
|
19
|
+
exports.NOWCAST_PRIOR = 25;
|
|
20
|
+
/** The latest drained hour may start at most this many hours before the current one, or there is no nowcast. */
|
|
21
|
+
exports.NOWCAST_MAX_LAG_HOURS = 3;
|
|
22
|
+
function nowcastRatio(actualSoFar, expectedSoFar) {
|
|
23
|
+
if (expectedSoFar < 5)
|
|
24
|
+
return 1;
|
|
25
|
+
return (0, stats_1.clamp)((actualSoFar + exports.NOWCAST_PRIOR) / (expectedSoFar + exports.NOWCAST_PRIOR), 0.5, 3);
|
|
26
|
+
}
|
|
27
|
+
exports.nowcastRatio = nowcastRatio;
|
|
28
|
+
/**
|
|
29
|
+
* Re-applies the nowcast to a stored forecast. `hours` must start at local
|
|
30
|
+
* midnight today (as `forecastWorkspace` writes them); any nowcast already on
|
|
31
|
+
* them is taken off first, so calling this every few minutes with fresh
|
|
32
|
+
* actuals is safe.
|
|
33
|
+
*
|
|
34
|
+
* `actual` lags the clock (hours are drained, not streamed), so only hours up
|
|
35
|
+
* to the latest one present in it are counted: an hour after it has not been
|
|
36
|
+
* drained yet and is never read as zero, while a missing hour before it is a
|
|
37
|
+
* zero. With nothing drained, or a latest hour that starts more than
|
|
38
|
+
* `NOWCAST_MAX_LAG_HOURS` before the current one, there is no nowcast (`[]`
|
|
39
|
+
* returns the forecast without one).
|
|
40
|
+
*/
|
|
41
|
+
function applyNowcast(hours, actual, now) {
|
|
42
|
+
const current = Math.floor(now / calendar_1.HOUR_MS) * calendar_1.HOUR_MS;
|
|
43
|
+
const plain = hours.map((h) => {
|
|
44
|
+
const n = h.components.nowcast || 1;
|
|
45
|
+
if (n === 1)
|
|
46
|
+
return h.components.nowcast === 1 ? h : { ...h, components: { ...h.components, nowcast: 1 } };
|
|
47
|
+
// Back to the stored precision, so taking a nowcast off and not putting one back is exact.
|
|
48
|
+
return {
|
|
49
|
+
...h,
|
|
50
|
+
likely: (0, stats_1.round)(h.likely / n),
|
|
51
|
+
low: (0, stats_1.round)(h.low / n),
|
|
52
|
+
high: (0, stats_1.round)(h.high / n),
|
|
53
|
+
components: { ...h.components, nowcast: 1 },
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
const start = plain.length > 0 ? plain[0].at : current;
|
|
57
|
+
let latest = -Infinity;
|
|
58
|
+
for (const p of actual)
|
|
59
|
+
if (p.at < current && p.at > latest)
|
|
60
|
+
latest = p.at;
|
|
61
|
+
if (latest < current - exports.NOWCAST_MAX_LAG_HOURS * calendar_1.HOUR_MS)
|
|
62
|
+
return plain;
|
|
63
|
+
// Completed and drained: from the first stored hour through the latest hour the feed has delivered.
|
|
64
|
+
const upTo = latest + calendar_1.HOUR_MS;
|
|
65
|
+
let expected = 0;
|
|
66
|
+
for (const h of plain)
|
|
67
|
+
if (h.at >= start && h.at < upTo)
|
|
68
|
+
expected += h.likely;
|
|
69
|
+
let seen = 0;
|
|
70
|
+
for (const p of actual)
|
|
71
|
+
if (p.at >= start && p.at < upTo)
|
|
72
|
+
seen += p.value;
|
|
73
|
+
const ratio = nowcastRatio(seen, expected);
|
|
74
|
+
if (ratio === 1)
|
|
75
|
+
return plain;
|
|
76
|
+
return plain.map((h) => {
|
|
77
|
+
if (h.at < current)
|
|
78
|
+
return h;
|
|
79
|
+
const lead = (h.at - current) / calendar_1.HOUR_MS;
|
|
80
|
+
const factor = 1 + (ratio - 1) * Math.max(0, 1 - lead / exports.NOWCAST_HOURS);
|
|
81
|
+
if (factor === 1)
|
|
82
|
+
return h;
|
|
83
|
+
return {
|
|
84
|
+
...h,
|
|
85
|
+
likely: (0, stats_1.round)(h.likely * factor),
|
|
86
|
+
low: (0, stats_1.round)(h.low * factor),
|
|
87
|
+
high: (0, stats_1.round)(h.high * factor),
|
|
88
|
+
components: { ...h.components, nowcast: (0, stats_1.round)(factor, 4) },
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
exports.applyNowcast = applyNowcast;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { PoolMember, TagProfile } from './types';
|
|
2
|
+
export interface SimTag {
|
|
3
|
+
profile: TagProfile;
|
|
4
|
+
/** Units of a person one task takes: 1 / concurrency. */
|
|
5
|
+
cost: number;
|
|
6
|
+
priority: number;
|
|
7
|
+
}
|
|
8
|
+
/** One seed's arrivals for one interval, as parallel arrays sorted by arrival time. */
|
|
9
|
+
export interface SimTasks {
|
|
10
|
+
tag: Int32Array;
|
|
11
|
+
/** When the task enters the run (a carried task: the run's start). */
|
|
12
|
+
at: Float64Array;
|
|
13
|
+
/** When the task arrived: waits and oldest-first are measured from here. */
|
|
14
|
+
born: Float64Array;
|
|
15
|
+
handle: Float64Array;
|
|
16
|
+
leaveAt: Float64Array;
|
|
17
|
+
/** 1 when the task arrived inside the interval (the ones the service level is judged on). */
|
|
18
|
+
measured: Uint8Array;
|
|
19
|
+
/** Measured arrivals per tag. */
|
|
20
|
+
measuredByTag: Int32Array;
|
|
21
|
+
/** This seed's arrival rate per tag (per ms) after the overdispersion draw. */
|
|
22
|
+
rate: Float64Array;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Work still on the floor when an interval ends, in the order it is picked up again: tasks in progress
|
|
26
|
+
* first (their remaining handle time, never leaving), then the queue oldest first (time left before each
|
|
27
|
+
* gives up).
|
|
28
|
+
*/
|
|
29
|
+
export interface Carry {
|
|
30
|
+
tag: Int32Array;
|
|
31
|
+
handle: Float64Array;
|
|
32
|
+
leaveIn: Float64Array;
|
|
33
|
+
/** How long each queued task has already waited (0 for work in progress). */
|
|
34
|
+
waited: Float64Array;
|
|
35
|
+
/** 1 = still inside its target and handed over: judged in the interval that picks it up. */
|
|
36
|
+
judge: Uint8Array;
|
|
37
|
+
/** The first `inProgress` entries are in someone's hands; the rest are queued. */
|
|
38
|
+
inProgress: number;
|
|
39
|
+
}
|
|
40
|
+
/** What `simulate` hands back about the interval's end. */
|
|
41
|
+
export interface SimOut {
|
|
42
|
+
/** Filled with what is still queued or in progress when the interval ends. */
|
|
43
|
+
carry: Carry | null;
|
|
44
|
+
/**
|
|
45
|
+
* The next interval follows on: measured work still queued and inside its target at the end is handed
|
|
46
|
+
* over to be judged there, not here (`handedOver` counts it per tag; take it off this interval's arrivals).
|
|
47
|
+
*/
|
|
48
|
+
handOver?: boolean;
|
|
49
|
+
handedOver?: Int32Array;
|
|
50
|
+
}
|
|
51
|
+
/** A carry `ms` later with nobody working it: work in progress runs down, the queue loses whoever gave up. */
|
|
52
|
+
export declare function ageCarry(carry: Carry, ms: number): Carry;
|
|
53
|
+
export interface SimWindow {
|
|
54
|
+
warmMs: number;
|
|
55
|
+
lengthMs: number;
|
|
56
|
+
tailMs: number;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* What a run did besides serving, for validating the simulation against queueing theory. Counts are of
|
|
60
|
+
* measured arrivals only.
|
|
61
|
+
*/
|
|
62
|
+
export interface SimStats {
|
|
63
|
+
/** Arrivals that found nobody free to take them and queued. */
|
|
64
|
+
delayed: Int32Array;
|
|
65
|
+
/** Queued arrivals whose patience ran out before anybody took them (by the end of the tail). */
|
|
66
|
+
abandoned: Int32Array;
|
|
67
|
+
/** The most work any one person held at once, in units of a person (≤ 1). */
|
|
68
|
+
maxUsed: number;
|
|
69
|
+
}
|
|
70
|
+
export declare function createSimStats(nTags: number): SimStats;
|
|
71
|
+
/** The router's score per tag for one declared profile (`calculateMatchScore`); -1 = never offered. */
|
|
72
|
+
export declare function profileScores(member: Pick<PoolMember, 'tags' | 'weights'>, tags: readonly SimTag[]): Float64Array;
|
|
73
|
+
/**
|
|
74
|
+
* Draws one seed's arrivals. Deterministic in `rand`. `factors` (one per tag) replaces the Gamma draw of the
|
|
75
|
+
* rate's forecast error, for callers that stratify it. Each tag's arrivals are a Poisson process (exponential
|
|
76
|
+
* gaps, so already in time order) at a rate Gamma-noised around the forecast; the tags' streams are then
|
|
77
|
+
* merged, earliest first and the lower tag index on a tie.
|
|
78
|
+
*/
|
|
79
|
+
export declare function drawTasks(expected: readonly number[], tags: readonly SimTag[], window: SimWindow, overdispersion: number, rand: () => number, carry?: Carry | null, factors?: ArrayLike<number>): SimTasks;
|
|
80
|
+
/**
|
|
81
|
+
* Runs one interval with `team[p]` people of each pool profile `pool[p]` and
|
|
82
|
+
* returns, per tag, how many measured arrivals were picked up within target.
|
|
83
|
+
*
|
|
84
|
+
* An arriving task goes to the highest-scoring profile with somebody who can
|
|
85
|
+
* fit it — a part-busy person before an idle one — and waits in its tag's
|
|
86
|
+
* queue otherwise. A person who frees up takes the best waiting task they can
|
|
87
|
+
* fit. With `out`, what is still queued or in progress at the interval's end
|
|
88
|
+
* comes back as `out.carry`.
|
|
89
|
+
*/
|
|
90
|
+
export declare function simulate(tasks: SimTasks, pool: readonly Float64Array[], team: readonly number[], tags: readonly SimTag[], window: SimWindow, stats?: SimStats, out?: SimOut): Int32Array;
|
|
91
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"","sources":["../../src/forecasting/simulation.ts"],"names":[],"mappings":"AAkCA,OAAO,KAAK,EAAE,UAAU,EAAwB,UAAU,EAAE,MAAM,SAAS,CAAC;AAI5E,MAAM,WAAW,MAAM;IACnB,OAAO,EAAE,UAAU,CAAC;IACpB,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;CACpB;AAED,uFAAuF;AACvF,MAAM,WAAW,QAAQ;IACrB,GAAG,EAAE,UAAU,CAAC;IAChB,sEAAsE;IACtE,EAAE,EAAE,YAAY,CAAC;IACjB,4EAA4E;IAC5E,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,YAAY,CAAC;IACtB,6FAA6F;IAC7F,QAAQ,EAAE,UAAU,CAAC;IACrB,iCAAiC;IACjC,aAAa,EAAE,UAAU,CAAC;IAC1B,+EAA+E;IAC/E,IAAI,EAAE,YAAY,CAAC;CACtB;AAED;;;;GAIG;AACH,MAAM,WAAW,KAAK;IAClB,GAAG,EAAE,UAAU,CAAC;IAChB,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,YAAY,CAAC;IACtB,6EAA6E;IAC7E,MAAM,EAAE,YAAY,CAAC;IACrB,4FAA4F;IAC5F,KAAK,EAAE,UAAU,CAAC;IAClB,kFAAkF;IAClF,UAAU,EAAE,MAAM,CAAC;CACtB;AAED,2DAA2D;AAC3D,MAAM,WAAW,MAAM;IACnB,8EAA8E;IAC9E,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,UAAU,CAAC;CAC3B;AAED,8GAA8G;AAC9G,wBAAgB,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,MAAM,GAAG,KAAK,CAcxD;AAED,MAAM,WAAW,SAAS;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACrB,+DAA+D;IAC/D,OAAO,EAAE,UAAU,CAAC;IACpB,gGAAgG;IAChG,SAAS,EAAE,UAAU,CAAC;IACtB,6EAA6E;IAC7E,OAAO,EAAE,MAAM,CAAC;CACnB;AAED,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,QAAQ,CAEtD;AAED,uGAAuG;AACvG,wBAAgB,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,YAAY,CAQjH;AAeD;;;;;GAKG;AACH,wBAAgB,SAAS,CACrB,QAAQ,EAAE,SAAS,MAAM,EAAE,EAC3B,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,MAAM,EAAE,SAAS,EACjB,cAAc,EAAE,MAAM,EACtB,IAAI,EAAE,MAAM,MAAM,EAClB,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,EACpB,OAAO,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,GAC5B,QAAQ,CAyEV;AAiFD;;;;;;;;;GASG;AACH,wBAAgB,QAAQ,CACpB,KAAK,EAAE,QAAQ,EACf,IAAI,EAAE,SAAS,YAAY,EAAE,EAC7B,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,MAAM,EAAE,SAAS,EACjB,KAAK,CAAC,EAAE,QAAQ,EAChB,GAAG,CAAC,EAAE,MAAM,GACb,UAAU,CAoMZ"}
|
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.simulate = exports.drawTasks = exports.profileScores = exports.createSimStats = exports.ageCarry = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Discrete-event simulation of one staffing interval under the router's own
|
|
6
|
+
* rules, simplified but faithful:
|
|
7
|
+
*
|
|
8
|
+
* - A person can take a tag, and ranks it, exactly as the router scores it:
|
|
9
|
+
* `calculateMatchScore(user, tag, 0, false)` — summed positive routing
|
|
10
|
+
* weights (exact and wildcard), any matching weight 0 a veto, and without
|
|
11
|
+
* weights the share of the person's tags the task matches. Score 0 = never
|
|
12
|
+
* offered. A free person picks the waiting task with the highest
|
|
13
|
+
* `priority + score` (the router's combined priority), oldest first on a
|
|
14
|
+
* tie. An arriving task goes to the best-scoring people who can fit it,
|
|
15
|
+
* onto somebody part-busy before somebody idle (packing keeps whole people
|
|
16
|
+
* free for work that needs a whole person).
|
|
17
|
+
* - Each person holds up to one unit of work; a task of a tag with
|
|
18
|
+
* concurrency c takes 1/c of it.
|
|
19
|
+
* - Handle times and patience are drawn from the tag's measured distribution,
|
|
20
|
+
* never a mean. A task still queued when its patience runs out leaves, and
|
|
21
|
+
* counts as not served — also when it leaves before the target wait.
|
|
22
|
+
* - Arrivals are Poisson around a rate that is itself Gamma-noised around the
|
|
23
|
+
* forecast (overdispersion), over a warm-up, the interval, and a tail as long
|
|
24
|
+
* as the longest target, so the interval starts busy and its last arrivals
|
|
25
|
+
* are judged on a full wait. The warm-up opens with the work a floor at that
|
|
26
|
+
* rate already has in hand, so a short warm-up still starts near steady state.
|
|
27
|
+
*
|
|
28
|
+
* One person is one person: a multi-skilled worker is a single head serving
|
|
29
|
+
* every tag they cover, never counted once per tag.
|
|
30
|
+
*
|
|
31
|
+
* Consecutive intervals can be chained: `simulate` hands back what is still
|
|
32
|
+
* queued or in progress when the interval ends (a `Carry`), and `drawTasks`
|
|
33
|
+
* opens the next interval with it instead of a warm-up.
|
|
34
|
+
*/
|
|
35
|
+
const match_score_1 = require("../scoring/match-score");
|
|
36
|
+
const distributions_1 = require("./distributions");
|
|
37
|
+
const EPS = 1e-9;
|
|
38
|
+
/** A carry `ms` later with nobody working it: work in progress runs down, the queue loses whoever gave up. */
|
|
39
|
+
function ageCarry(carry, ms) {
|
|
40
|
+
const busy = (i) => i < carry.inProgress;
|
|
41
|
+
const keep = [];
|
|
42
|
+
for (let i = 0; i < carry.tag.length; i++) {
|
|
43
|
+
if ((busy(i) ? carry.handle[i] : carry.leaveIn[i]) - ms > 0)
|
|
44
|
+
keep.push(i);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
tag: Int32Array.from(keep, (i) => carry.tag[i]),
|
|
48
|
+
handle: Float64Array.from(keep, (i) => (busy(i) ? carry.handle[i] - ms : carry.handle[i])),
|
|
49
|
+
leaveIn: Float64Array.from(keep, (i) => (busy(i) ? Infinity : carry.leaveIn[i] - ms)),
|
|
50
|
+
waited: Float64Array.from(keep, (i) => (busy(i) ? 0 : carry.waited[i] + ms)),
|
|
51
|
+
judge: Uint8Array.from(keep, (i) => carry.judge[i]),
|
|
52
|
+
inProgress: keep.filter(busy).length,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
exports.ageCarry = ageCarry;
|
|
56
|
+
function createSimStats(nTags) {
|
|
57
|
+
return { delayed: new Int32Array(nTags), abandoned: new Int32Array(nTags), maxUsed: 0 };
|
|
58
|
+
}
|
|
59
|
+
exports.createSimStats = createSimStats;
|
|
60
|
+
/** The router's score per tag for one declared profile (`calculateMatchScore`); -1 = never offered. */
|
|
61
|
+
function profileScores(member, tags) {
|
|
62
|
+
const user = { id: 'sim', tags: member.tags, routingWeights: member.weights };
|
|
63
|
+
const out = new Float64Array(tags.length);
|
|
64
|
+
tags.forEach((t, i) => {
|
|
65
|
+
const [score] = (0, match_score_1.calculateMatchScore)(user, t.profile.tag, 0, false);
|
|
66
|
+
out[i] = score > 0 ? score : -1;
|
|
67
|
+
});
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
exports.profileScores = profileScores;
|
|
71
|
+
/**
|
|
72
|
+
* Time left on a task found in progress at a random moment: a length-biased draw (a long task is more likely
|
|
73
|
+
* to be the one in progress), cut at a uniform point. Rejection-sampled against the distribution's maximum.
|
|
74
|
+
*/
|
|
75
|
+
function residualHandle(d, rand) {
|
|
76
|
+
const top = d.q[d.q.length - 1];
|
|
77
|
+
for (let i = 0; i < 64 && top > 0; i++) {
|
|
78
|
+
const h = (0, distributions_1.sampleQuantiles)(d, rand);
|
|
79
|
+
if (rand() * top <= h)
|
|
80
|
+
return rand() * h;
|
|
81
|
+
}
|
|
82
|
+
return rand() * (0, distributions_1.sampleQuantiles)(d, rand);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Draws one seed's arrivals. Deterministic in `rand`. `factors` (one per tag) replaces the Gamma draw of the
|
|
86
|
+
* rate's forecast error, for callers that stratify it. Each tag's arrivals are a Poisson process (exponential
|
|
87
|
+
* gaps, so already in time order) at a rate Gamma-noised around the forecast; the tags' streams are then
|
|
88
|
+
* merged, earliest first and the lower tag index on a tie.
|
|
89
|
+
*/
|
|
90
|
+
function drawTasks(expected, tags, window, overdispersion, rand, carry, factors) {
|
|
91
|
+
const from = -window.warmMs;
|
|
92
|
+
const until = window.lengthMs + window.tailMs;
|
|
93
|
+
const measuredByTag = new Int32Array(tags.length);
|
|
94
|
+
const rates = new Float64Array(tags.length);
|
|
95
|
+
const streams = [];
|
|
96
|
+
let total = 0;
|
|
97
|
+
tags.forEach((t, g) => {
|
|
98
|
+
const stream = { at: [], handle: [], leaveAt: [] };
|
|
99
|
+
streams.push(stream);
|
|
100
|
+
const mean = expected[g] ?? 0;
|
|
101
|
+
if (mean <= 0)
|
|
102
|
+
return;
|
|
103
|
+
const factor = factors ? factors[g] : (0, distributions_1.sampleGammaMeanOne)(overdispersion, rand);
|
|
104
|
+
const rate = (mean * factor) / window.lengthMs;
|
|
105
|
+
rates[g] = rate;
|
|
106
|
+
const patience = () => (t.profile.patienceMs ? (0, distributions_1.sampleQuantiles)(t.profile.patienceMs, rand) : Infinity);
|
|
107
|
+
// Already in hand when the warm-up starts: the tasks a busy floor holds at this rate (Little's law),
|
|
108
|
+
// each with the rest of its handle time. Without them every run starts from an empty floor and the
|
|
109
|
+
// warm-up is too short to fill it, which reads a near-critical hour as better served than it is.
|
|
110
|
+
// A chained interval opens with the previous one's actual leftovers instead.
|
|
111
|
+
const inHand = carry ? 0 : (0, distributions_1.samplePoisson)(rate * (0, distributions_1.meanOf)(t.profile.handleMs), rand);
|
|
112
|
+
for (let i = 0; i < inHand; i++) {
|
|
113
|
+
stream.at.push(from);
|
|
114
|
+
stream.handle.push(Math.max(1, residualHandle(t.profile.handleMs, rand)));
|
|
115
|
+
stream.leaveAt.push(from + patience());
|
|
116
|
+
}
|
|
117
|
+
for (let at = from - Math.log(1 - rand()) / rate; at < until; at -= Math.log(1 - rand()) / rate) {
|
|
118
|
+
stream.at.push(at);
|
|
119
|
+
stream.handle.push(Math.max(1, (0, distributions_1.sampleQuantiles)(t.profile.handleMs, rand)));
|
|
120
|
+
stream.leaveAt.push(at + patience());
|
|
121
|
+
if (at >= 0 && at < window.lengthMs)
|
|
122
|
+
measuredByTag[g] += 1;
|
|
123
|
+
}
|
|
124
|
+
total += stream.at.length;
|
|
125
|
+
});
|
|
126
|
+
const carried = carry ? carry.tag.length : 0;
|
|
127
|
+
total += carried;
|
|
128
|
+
const out = {
|
|
129
|
+
tag: new Int32Array(total),
|
|
130
|
+
at: new Float64Array(total),
|
|
131
|
+
born: new Float64Array(total),
|
|
132
|
+
handle: new Float64Array(total),
|
|
133
|
+
leaveAt: new Float64Array(total),
|
|
134
|
+
measured: new Uint8Array(total),
|
|
135
|
+
measuredByTag,
|
|
136
|
+
rate: rates,
|
|
137
|
+
};
|
|
138
|
+
for (let i = 0; i < carried; i++) {
|
|
139
|
+
out.tag[i] = carry.tag[i];
|
|
140
|
+
out.at[i] = from;
|
|
141
|
+
out.born[i] = from - carry.waited[i];
|
|
142
|
+
out.handle[i] = Math.max(1, carry.handle[i]);
|
|
143
|
+
out.leaveAt[i] = from + carry.leaveIn[i];
|
|
144
|
+
if (carry.judge[i]) {
|
|
145
|
+
out.measured[i] = 1;
|
|
146
|
+
measuredByTag[carry.tag[i]] += 1;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const next = new Int32Array(tags.length);
|
|
150
|
+
for (let i = carried; i < total; i++) {
|
|
151
|
+
let g = -1;
|
|
152
|
+
for (let k = 0; k < streams.length; k++) {
|
|
153
|
+
if (next[k] < streams[k].at.length && (g < 0 || streams[k].at[next[k]] < streams[g].at[next[g]]))
|
|
154
|
+
g = k;
|
|
155
|
+
}
|
|
156
|
+
const j = next[g]++;
|
|
157
|
+
const at = streams[g].at[j];
|
|
158
|
+
out.tag[i] = g;
|
|
159
|
+
out.at[i] = at;
|
|
160
|
+
out.born[i] = at;
|
|
161
|
+
out.handle[i] = streams[g].handle[j];
|
|
162
|
+
out.leaveAt[i] = streams[g].leaveAt[j];
|
|
163
|
+
out.measured[i] = at >= 0 && at < window.lengthMs ? 1 : 0;
|
|
164
|
+
}
|
|
165
|
+
return out;
|
|
166
|
+
}
|
|
167
|
+
exports.drawTasks = drawTasks;
|
|
168
|
+
/** A min-heap of completion times with the worker and the units they free, on typed arrays. */
|
|
169
|
+
class Completions {
|
|
170
|
+
constructor() {
|
|
171
|
+
this.time = new Float64Array(64);
|
|
172
|
+
this.worker = new Int32Array(64);
|
|
173
|
+
this.cost = new Float64Array(64);
|
|
174
|
+
this.task = new Int32Array(64);
|
|
175
|
+
this.size = 0;
|
|
176
|
+
this.lastWorker = -1;
|
|
177
|
+
this.lastCost = 0;
|
|
178
|
+
}
|
|
179
|
+
/** Task and completion time of the k-th entry (heap order), for a snapshot. */
|
|
180
|
+
taskAt(k) {
|
|
181
|
+
return this.task[k];
|
|
182
|
+
}
|
|
183
|
+
timeAt(k) {
|
|
184
|
+
return this.time[k];
|
|
185
|
+
}
|
|
186
|
+
peek() {
|
|
187
|
+
return this.size > 0 ? this.time[0] : Infinity;
|
|
188
|
+
}
|
|
189
|
+
push(t, w, c, task) {
|
|
190
|
+
if (this.size === this.time.length)
|
|
191
|
+
this.grow();
|
|
192
|
+
let i = this.size++;
|
|
193
|
+
while (i > 0) {
|
|
194
|
+
const p = (i - 1) >> 1;
|
|
195
|
+
if (this.time[p] <= t)
|
|
196
|
+
break;
|
|
197
|
+
this.move(p, i);
|
|
198
|
+
i = p;
|
|
199
|
+
}
|
|
200
|
+
this.time[i] = t;
|
|
201
|
+
this.worker[i] = w;
|
|
202
|
+
this.cost[i] = c;
|
|
203
|
+
this.task[i] = task;
|
|
204
|
+
}
|
|
205
|
+
move(from, to) {
|
|
206
|
+
this.time[to] = this.time[from];
|
|
207
|
+
this.worker[to] = this.worker[from];
|
|
208
|
+
this.cost[to] = this.cost[from];
|
|
209
|
+
this.task[to] = this.task[from];
|
|
210
|
+
}
|
|
211
|
+
/** Removes the earliest completion; read `lastWorker` / `lastCost` for what it freed. */
|
|
212
|
+
pop() {
|
|
213
|
+
this.lastWorker = this.worker[0];
|
|
214
|
+
this.lastCost = this.cost[0];
|
|
215
|
+
const n = --this.size;
|
|
216
|
+
const t = this.time[n];
|
|
217
|
+
const w = this.worker[n];
|
|
218
|
+
const c = this.cost[n];
|
|
219
|
+
const task = this.task[n];
|
|
220
|
+
let i = 0;
|
|
221
|
+
for (;;) {
|
|
222
|
+
const l = 2 * i + 1;
|
|
223
|
+
if (l >= n)
|
|
224
|
+
break;
|
|
225
|
+
const r = l + 1;
|
|
226
|
+
const m = r < n && this.time[r] < this.time[l] ? r : l;
|
|
227
|
+
if (this.time[m] >= t)
|
|
228
|
+
break;
|
|
229
|
+
this.move(m, i);
|
|
230
|
+
i = m;
|
|
231
|
+
}
|
|
232
|
+
this.time[i] = t;
|
|
233
|
+
this.worker[i] = w;
|
|
234
|
+
this.cost[i] = c;
|
|
235
|
+
this.task[i] = task;
|
|
236
|
+
}
|
|
237
|
+
grow() {
|
|
238
|
+
const time = new Float64Array(this.time.length * 2);
|
|
239
|
+
const worker = new Int32Array(this.worker.length * 2);
|
|
240
|
+
const cost = new Float64Array(this.cost.length * 2);
|
|
241
|
+
const task = new Int32Array(this.task.length * 2);
|
|
242
|
+
time.set(this.time);
|
|
243
|
+
worker.set(this.worker);
|
|
244
|
+
cost.set(this.cost);
|
|
245
|
+
task.set(this.task);
|
|
246
|
+
this.time = time;
|
|
247
|
+
this.worker = worker;
|
|
248
|
+
this.cost = cost;
|
|
249
|
+
this.task = task;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Runs one interval with `team[p]` people of each pool profile `pool[p]` and
|
|
254
|
+
* returns, per tag, how many measured arrivals were picked up within target.
|
|
255
|
+
*
|
|
256
|
+
* An arriving task goes to the highest-scoring profile with somebody who can
|
|
257
|
+
* fit it — a part-busy person before an idle one — and waits in its tag's
|
|
258
|
+
* queue otherwise. A person who frees up takes the best waiting task they can
|
|
259
|
+
* fit. With `out`, what is still queued or in progress at the interval's end
|
|
260
|
+
* comes back as `out.carry`.
|
|
261
|
+
*/
|
|
262
|
+
function simulate(tasks, pool, team, tags, window, stats, out) {
|
|
263
|
+
const nTags = tags.length;
|
|
264
|
+
const end = window.lengthMs + window.tailMs;
|
|
265
|
+
const profileOf = [];
|
|
266
|
+
team.forEach((k, p) => {
|
|
267
|
+
for (let i = 0; i < k; i++)
|
|
268
|
+
profileOf.push(p);
|
|
269
|
+
});
|
|
270
|
+
const people = profileOf.length;
|
|
271
|
+
const used = new Float64Array(people);
|
|
272
|
+
const eligible = pool.map((scores) => {
|
|
273
|
+
const out = [];
|
|
274
|
+
for (let g = 0; g < nTags; g++)
|
|
275
|
+
if (scores[g] >= 0)
|
|
276
|
+
out.push(g);
|
|
277
|
+
return out;
|
|
278
|
+
});
|
|
279
|
+
// Per tag: groups of profiles that score it equally, best first.
|
|
280
|
+
const tiers = tags.map((_, g) => {
|
|
281
|
+
const able = pool.map((_s, p) => p).filter((p) => pool[p][g] >= 0 && team[p] > 0);
|
|
282
|
+
able.sort((a, b) => pool[b][g] - pool[a][g] || a - b);
|
|
283
|
+
const out = [];
|
|
284
|
+
for (const p of able) {
|
|
285
|
+
const last = out[out.length - 1];
|
|
286
|
+
if (last && pool[last[0]][g] === pool[p][g])
|
|
287
|
+
last.push(p);
|
|
288
|
+
else
|
|
289
|
+
out.push([p]);
|
|
290
|
+
}
|
|
291
|
+
return out;
|
|
292
|
+
});
|
|
293
|
+
// Idle people per profile (a stack), part-busy people per profile (swap-remove list).
|
|
294
|
+
const idle = pool.map(() => []);
|
|
295
|
+
const partial = pool.map(() => []);
|
|
296
|
+
const partialPos = new Int32Array(people).fill(-1);
|
|
297
|
+
for (let w = people - 1; w >= 0; w--)
|
|
298
|
+
idle[profileOf[w]].push(w);
|
|
299
|
+
const leavePartial = (w) => {
|
|
300
|
+
const pos = partialPos[w];
|
|
301
|
+
if (pos < 0)
|
|
302
|
+
return;
|
|
303
|
+
const list = partial[profileOf[w]];
|
|
304
|
+
const last = list.pop();
|
|
305
|
+
if (last !== w) {
|
|
306
|
+
list[pos] = last;
|
|
307
|
+
partialPos[last] = pos;
|
|
308
|
+
}
|
|
309
|
+
partialPos[w] = -1;
|
|
310
|
+
};
|
|
311
|
+
/** Files a person under part-busy, or takes them off it, after their load changed (idle is the caller's). */
|
|
312
|
+
const refile = (w) => {
|
|
313
|
+
if (used[w] > EPS && used[w] < 1 - EPS) {
|
|
314
|
+
if (partialPos[w] < 0) {
|
|
315
|
+
const list = partial[profileOf[w]];
|
|
316
|
+
partialPos[w] = list.length;
|
|
317
|
+
list.push(w);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
else
|
|
321
|
+
leavePartial(w);
|
|
322
|
+
};
|
|
323
|
+
const queues = Array.from({ length: nTags }, () => []);
|
|
324
|
+
const heads = new Int32Array(nTags);
|
|
325
|
+
const served = new Int32Array(nTags);
|
|
326
|
+
const started = stats ? new Uint8Array(tasks.at.length) : null;
|
|
327
|
+
const completions = new Completions();
|
|
328
|
+
const start = (i, w, t) => {
|
|
329
|
+
const g = tasks.tag[i];
|
|
330
|
+
if (tasks.measured[i] && t - tasks.born[i] <= tags[g].profile.targetWaitMs + EPS)
|
|
331
|
+
served[g] += 1;
|
|
332
|
+
used[w] += tags[g].cost;
|
|
333
|
+
if (stats) {
|
|
334
|
+
started[i] = 1;
|
|
335
|
+
if (used[w] > stats.maxUsed)
|
|
336
|
+
stats.maxUsed = used[w];
|
|
337
|
+
}
|
|
338
|
+
refile(w);
|
|
339
|
+
completions.push(t + tasks.handle[i], w, tags[g].cost, i);
|
|
340
|
+
};
|
|
341
|
+
const headOf = (g, t) => {
|
|
342
|
+
const q = queues[g];
|
|
343
|
+
while (heads[g] < q.length && tasks.leaveAt[q[heads[g]]] <= t)
|
|
344
|
+
heads[g] += 1;
|
|
345
|
+
return heads[g] < q.length ? q[heads[g]] : -1;
|
|
346
|
+
};
|
|
347
|
+
const serve = (w, t) => {
|
|
348
|
+
const scores = pool[profileOf[w]];
|
|
349
|
+
for (;;) {
|
|
350
|
+
let best = -1;
|
|
351
|
+
let bestKey = -Infinity;
|
|
352
|
+
for (const g of eligible[profileOf[w]]) {
|
|
353
|
+
if (used[w] + tags[g].cost > 1 + EPS)
|
|
354
|
+
continue;
|
|
355
|
+
const i = headOf(g, t);
|
|
356
|
+
if (i < 0)
|
|
357
|
+
continue;
|
|
358
|
+
const key = tags[g].priority + scores[g];
|
|
359
|
+
if (best < 0 || key > bestKey || (key === bestKey && tasks.born[i] < tasks.born[best])) {
|
|
360
|
+
best = i;
|
|
361
|
+
bestKey = key;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
if (best < 0)
|
|
365
|
+
return;
|
|
366
|
+
heads[tasks.tag[best]] += 1;
|
|
367
|
+
start(best, w, t);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const assign = (i, t) => {
|
|
371
|
+
const g = tasks.tag[i];
|
|
372
|
+
const cost = tags[g].cost;
|
|
373
|
+
for (const tier of tiers[g]) {
|
|
374
|
+
if (cost < 1 - EPS) {
|
|
375
|
+
for (const p of tier) {
|
|
376
|
+
for (const w of partial[p]) {
|
|
377
|
+
if (used[w] + cost <= 1 + EPS) {
|
|
378
|
+
start(i, w, t);
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
for (const p of tier) {
|
|
385
|
+
const w = idle[p].pop();
|
|
386
|
+
if (w !== undefined) {
|
|
387
|
+
start(i, w, t);
|
|
388
|
+
return true;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return false;
|
|
393
|
+
};
|
|
394
|
+
const snapshotAt = window.lengthMs;
|
|
395
|
+
/** What is on the floor at the interval's end: in progress first, then the queue oldest first. */
|
|
396
|
+
const snapshot = () => {
|
|
397
|
+
const inProgress = [];
|
|
398
|
+
for (let k = 0; k < completions.size; k++)
|
|
399
|
+
inProgress.push(k);
|
|
400
|
+
inProgress.sort((a, b) => completions.taskAt(a) - completions.taskAt(b));
|
|
401
|
+
const waiting = [];
|
|
402
|
+
for (let g = 0; g < nTags; g++) {
|
|
403
|
+
for (let j = heads[g]; j < queues[g].length; j++) {
|
|
404
|
+
if (tasks.leaveAt[queues[g][j]] > snapshotAt)
|
|
405
|
+
waiting.push(queues[g][j]);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
waiting.sort((a, b) => tasks.born[a] - tasks.born[b] || a - b);
|
|
409
|
+
const count = inProgress.length + waiting.length;
|
|
410
|
+
const carry = {
|
|
411
|
+
tag: new Int32Array(count),
|
|
412
|
+
handle: new Float64Array(count),
|
|
413
|
+
leaveIn: new Float64Array(count),
|
|
414
|
+
waited: new Float64Array(count),
|
|
415
|
+
judge: new Uint8Array(count),
|
|
416
|
+
inProgress: inProgress.length,
|
|
417
|
+
};
|
|
418
|
+
inProgress.forEach((k, j) => {
|
|
419
|
+
carry.tag[j] = tasks.tag[completions.taskAt(k)];
|
|
420
|
+
carry.handle[j] = completions.timeAt(k) - snapshotAt;
|
|
421
|
+
carry.leaveIn[j] = Infinity;
|
|
422
|
+
});
|
|
423
|
+
const handedOver = new Int32Array(nTags);
|
|
424
|
+
waiting.forEach((i, j) => {
|
|
425
|
+
const at = inProgress.length + j;
|
|
426
|
+
const g = tasks.tag[i];
|
|
427
|
+
carry.tag[at] = g;
|
|
428
|
+
carry.handle[at] = tasks.handle[i];
|
|
429
|
+
carry.leaveIn[at] = tasks.leaveAt[i] - snapshotAt;
|
|
430
|
+
carry.waited[at] = snapshotAt - tasks.born[i];
|
|
431
|
+
// Still able to make its target: the next interval judges it, on that interval's people.
|
|
432
|
+
if (out.handOver && tasks.measured[i] && carry.waited[at] <= tags[g].profile.targetWaitMs) {
|
|
433
|
+
carry.judge[at] = 1;
|
|
434
|
+
handedOver[g] += 1;
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
out.handedOver = handedOver;
|
|
438
|
+
return carry;
|
|
439
|
+
};
|
|
440
|
+
let next = 0;
|
|
441
|
+
const n = tasks.at.length;
|
|
442
|
+
for (;;) {
|
|
443
|
+
const tA = next < n ? tasks.at[next] : Infinity;
|
|
444
|
+
const tC = completions.peek();
|
|
445
|
+
const t = tA < tC ? tA : tC;
|
|
446
|
+
if (out && !out.carry && t > snapshotAt)
|
|
447
|
+
out.carry = snapshot();
|
|
448
|
+
if (t > end || t === Infinity)
|
|
449
|
+
break;
|
|
450
|
+
if (tC <= tA) {
|
|
451
|
+
completions.pop();
|
|
452
|
+
const w = completions.lastWorker;
|
|
453
|
+
used[w] -= completions.lastCost;
|
|
454
|
+
if (used[w] <= EPS)
|
|
455
|
+
used[w] = 0;
|
|
456
|
+
// Freed capacity goes to the queue first; somebody it leaves with nothing is idle again.
|
|
457
|
+
serve(w, t);
|
|
458
|
+
refile(w);
|
|
459
|
+
if (used[w] === 0)
|
|
460
|
+
idle[profileOf[w]].push(w);
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
const i = next++;
|
|
464
|
+
if (!assign(i, t)) {
|
|
465
|
+
queues[tasks.tag[i]].push(i);
|
|
466
|
+
if (stats && tasks.measured[i])
|
|
467
|
+
stats.delayed[tasks.tag[i]] += 1;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (stats) {
|
|
471
|
+
for (let i = 0; i < n; i++) {
|
|
472
|
+
if (tasks.measured[i] && !started[i] && tasks.leaveAt[i] <= end)
|
|
473
|
+
stats.abandoned[tasks.tag[i]] += 1;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return served;
|
|
477
|
+
}
|
|
478
|
+
exports.simulate = simulate;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type FittedSeries } from './model';
|
|
2
|
+
import type { SpikeSpan } from './types';
|
|
3
|
+
/** How far back spikes are looked for: the hourly table's 16 weeks. */
|
|
4
|
+
export declare const SPIKE_LOOKBACK_DAYS = 112;
|
|
5
|
+
export declare function detectSpikes(total: FittedSeries, others: readonly FittedSeries[]): SpikeSpan[];
|
|
6
|
+
//# sourceMappingURL=spikes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spikes.d.ts","sourceRoot":"","sources":["../../src/forecasting/spikes.ts"],"names":[],"mappings":"AAWA,OAAO,EAAqC,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC,uEAAuE;AACvE,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAWvC,wBAAgB,YAAY,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,YAAY,EAAE,GAAG,SAAS,EAAE,CAiD9F"}
|