@ovok/core 0.4.49 → 0.4.50
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/dist/client/observation/fhir-systems.d.ts +12 -0
- package/dist/client/observation/fhir-systems.js +13 -1
- package/dist/client/observation/observation-groups.d.ts +4 -3
- package/dist/client/observation/observation-groups.js +8 -4
- package/dist/client/observation/series.d.ts +21 -3
- package/dist/client/observation/series.js +14 -4
- package/dist/client/observation/services/ecg-observation-fragments.js +1 -0
- package/dist/client/offline/offline-measurement-queue.d.ts +3 -1
- package/dist/client/offline/offline-measurement-queue.js +2 -1
- package/dist/conformance/capability-requirements.js +1 -1
- package/dist/next/config/ovok-config.d.ts +18 -0
- package/dist/next/config/settings.js +10 -1
- package/dist/next/errors/ovok-errors.d.ts +7 -0
- package/dist/next/errors/ovok-errors.js +9 -1
- package/dist/next/http/transmit.js +4 -2
- package/dist/next/index.d.ts +15 -1
- package/dist/next/index.js +18 -1
- package/dist/next/logging/ovok-logger.d.ts +26 -0
- package/dist/next/logging/ovok-logger.js +10 -0
- package/dist/next/outbox/entry-observation.d.ts +3 -0
- package/dist/next/outbox/entry-observation.js +6 -0
- package/dist/next/outbox/failure-policy.d.ts +45 -0
- package/dist/next/outbox/failure-policy.js +92 -0
- package/dist/next/outbox/group-links.d.ts +19 -0
- package/dist/next/outbox/group-links.js +65 -0
- package/dist/next/outbox/indexed-db-outbox-store.d.ts +38 -0
- package/dist/next/outbox/indexed-db-outbox-store.js +90 -0
- package/dist/next/outbox/item-settler.d.ts +37 -0
- package/dist/next/outbox/item-settler.js +49 -0
- package/dist/next/outbox/legacy-queue-import.d.ts +7 -0
- package/dist/next/outbox/legacy-queue-import.js +50 -0
- package/dist/next/outbox/memory-outbox-store.d.ts +22 -0
- package/dist/next/outbox/memory-outbox-store.js +40 -0
- package/dist/next/outbox/outbox-items.d.ts +17 -0
- package/dist/next/outbox/outbox-items.js +89 -0
- package/dist/next/outbox/outbox-queries.d.ts +4 -0
- package/dist/next/outbox/outbox-queries.js +9 -0
- package/dist/next/outbox/outbox-resource.d.ts +73 -0
- package/dist/next/outbox/outbox-resource.js +170 -0
- package/dist/next/outbox/outbox-runner.d.ts +56 -0
- package/dist/next/outbox/outbox-runner.js +179 -0
- package/dist/next/outbox/outbox-snapshot.d.ts +3 -0
- package/dist/next/outbox/outbox-snapshot.js +40 -0
- package/dist/next/outbox/outbox-store.d.ts +21 -0
- package/dist/next/outbox/outbox-store.js +2 -0
- package/dist/next/outbox/outbox-triggers.d.ts +30 -0
- package/dist/next/outbox/outbox-triggers.js +38 -0
- package/dist/next/outbox/outbox-types.d.ts +138 -0
- package/dist/next/outbox/outbox-types.js +2 -0
- package/dist/next/outbox/outcome-handlers.d.ts +30 -0
- package/dist/next/outbox/outcome-handlers.js +50 -0
- package/dist/next/outbox/pending-saves.d.ts +6 -0
- package/dist/next/outbox/pending-saves.js +18 -0
- package/dist/next/outbox/request-budget.d.ts +10 -0
- package/dist/next/outbox/request-budget.js +22 -0
- package/dist/next/outbox/request-planner.d.ts +5 -0
- package/dist/next/outbox/request-planner.js +39 -0
- package/dist/next/outbox/runner-state.d.ts +34 -0
- package/dist/next/outbox/runner-state.js +61 -0
- package/dist/next/outbox/store-contract.d.ts +19 -0
- package/dist/next/outbox/store-contract.js +108 -0
- package/dist/next/outbox/wakeup.d.ts +11 -0
- package/dist/next/outbox/wakeup.js +34 -0
- package/dist/next/ovok.d.ts +3 -0
- package/dist/next/ovok.js +13 -2
- package/dist/next/react/index.d.ts +11 -1
- package/dist/next/react/index.js +15 -1
- package/dist/next/resources/measurements/measurement-sender.d.ts +27 -0
- package/dist/next/resources/measurements/measurement-sender.js +66 -0
- package/dist/next/resources/measurements/measurements-resource.d.ts +36 -25
- package/dist/next/resources/measurements/measurements-resource.js +62 -65
- package/dist/next/resources/measurements/offline-measurement-queue-types.d.ts +12 -14
- package/dist/next/resources/measurements/offline-measurement-queue.d.ts +4 -23
- package/dist/next/resources/measurements/offline-measurement-queue.js +18 -143
- package/package.json +1 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { BundleEntry } from "@medplum/fhirtypes";
|
|
2
|
+
/**
|
|
3
|
+
* What an outbox item holds: a whole reading, a growing series, one piece of a long recording, or a deletion.
|
|
4
|
+
*
|
|
5
|
+
* @category outbox
|
|
6
|
+
*/
|
|
7
|
+
export type OutboxKind = "measurement" | "series" | "segment" | "delete";
|
|
8
|
+
/**
|
|
9
|
+
* Whether an item is still being sent, or the server refused it and it waits for `resend` or `discard`.
|
|
10
|
+
*
|
|
11
|
+
* @category outbox
|
|
12
|
+
*/
|
|
13
|
+
export type OutboxState = "pending" | "rejected";
|
|
14
|
+
/**
|
|
15
|
+
* Why the last attempt to send an item failed. It never holds a message or a value.
|
|
16
|
+
*
|
|
17
|
+
* @category outbox
|
|
18
|
+
*/
|
|
19
|
+
export type OutboxFailure = {
|
|
20
|
+
/** Error name. */
|
|
21
|
+
name: string;
|
|
22
|
+
/** Error code, when the error had one. */
|
|
23
|
+
code?: string;
|
|
24
|
+
/** HTTP status, when the failure was a response. */
|
|
25
|
+
status?: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Without its entries: what the outbox lists and counts.
|
|
29
|
+
*
|
|
30
|
+
* @category outbox
|
|
31
|
+
*/
|
|
32
|
+
export type OutboxItemSummary = {
|
|
33
|
+
/** Unique id; a newer version of a series or segment keeps the id of the one it replaces. */
|
|
34
|
+
id: string;
|
|
35
|
+
/** Account that owns the item; only that account's session sends it. */
|
|
36
|
+
ownerKey: string;
|
|
37
|
+
/** What the item holds. */
|
|
38
|
+
kind: OutboxKind;
|
|
39
|
+
/** Recording the item belongs to, for the pieces of a long recording and the group that closes it. */
|
|
40
|
+
group?: string;
|
|
41
|
+
/** Position in the group. The group's own item comes after its last segment, so its `seq` is the segment count. */
|
|
42
|
+
seq?: number;
|
|
43
|
+
/** Whether the item is still being sent. */
|
|
44
|
+
state: OutboxState;
|
|
45
|
+
/** Attempts that failed for a reason of the item itself. */
|
|
46
|
+
attempts: number;
|
|
47
|
+
/** Earliest time, in epoch milliseconds, to try again. */
|
|
48
|
+
nextAttemptAt: number;
|
|
49
|
+
/** When the item entered the outbox, in epoch milliseconds. */
|
|
50
|
+
createdAt: number;
|
|
51
|
+
/** When the reading was taken, in epoch milliseconds; the outbox sends the newest first. */
|
|
52
|
+
recordedAt: number;
|
|
53
|
+
/** The last failure, when there was one. */
|
|
54
|
+
lastFailure?: OutboxFailure;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* One unit of sending: the bundle entries that save a reading, a piece of one, or a deletion, with their state.
|
|
58
|
+
* Items are never changed in place; a new version is stored under the same id.
|
|
59
|
+
*
|
|
60
|
+
* @category outbox
|
|
61
|
+
*/
|
|
62
|
+
export type OutboxItem = OutboxItemSummary & {
|
|
63
|
+
/** The bundle entries to send. */
|
|
64
|
+
entries: BundleEntry[];
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* An item the outbox has sent and removed.
|
|
68
|
+
*
|
|
69
|
+
* @category outbox
|
|
70
|
+
*/
|
|
71
|
+
export type OutboxSentItem = Pick<OutboxItemSummary, "id" | "kind" | "group" | "seq" | "recordedAt"> & {
|
|
72
|
+
/** When the server accepted it, in epoch milliseconds. */
|
|
73
|
+
sentAt: number;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* How far a long recording has got.
|
|
77
|
+
*
|
|
78
|
+
* @category outbox
|
|
79
|
+
*/
|
|
80
|
+
export type OutboxGroupProgress = {
|
|
81
|
+
/** The recording's id. */
|
|
82
|
+
id: string;
|
|
83
|
+
/** Segments the server has accepted. */
|
|
84
|
+
segmentsSent: number;
|
|
85
|
+
/** Segments the recording has. */
|
|
86
|
+
segmentsTotal: number;
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* What the outbox holds and what it is doing now, for the signed-in account.
|
|
90
|
+
*
|
|
91
|
+
* @category outbox
|
|
92
|
+
*/
|
|
93
|
+
export type OutboxSnapshot = {
|
|
94
|
+
/** Items not yet saved and not refused, including those being sent. */
|
|
95
|
+
pending: number;
|
|
96
|
+
/** Items in the request being sent now. */
|
|
97
|
+
sending: number;
|
|
98
|
+
/** Items the server refused; they wait for `resend` or `discard`. */
|
|
99
|
+
rejected: number;
|
|
100
|
+
/** Whether the last request could not reach the server. */
|
|
101
|
+
offline: boolean;
|
|
102
|
+
/** When sending resumes after a pause the server asked for, in epoch milliseconds, or null. */
|
|
103
|
+
pausedUntil: number | null;
|
|
104
|
+
/** When the oldest pending item entered the outbox, in epoch milliseconds, or null. */
|
|
105
|
+
oldestPendingAt: number | null;
|
|
106
|
+
/** The most recent failure since an item last went through, or null. */
|
|
107
|
+
lastError: OutboxFailure | null;
|
|
108
|
+
/** Pending items by kind. */
|
|
109
|
+
byKind: Record<OutboxKind, number>;
|
|
110
|
+
/** Progress of each long recording that is not yet saved. */
|
|
111
|
+
groups: OutboxGroupProgress[];
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Counts from one flush.
|
|
115
|
+
*
|
|
116
|
+
* @category outbox
|
|
117
|
+
*/
|
|
118
|
+
export type OutboxFlushResult = {
|
|
119
|
+
/** Items the server accepted. */
|
|
120
|
+
sent: number;
|
|
121
|
+
/** Items the server refused. */
|
|
122
|
+
rejected: number;
|
|
123
|
+
/** Items still waiting. */
|
|
124
|
+
pending: number;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* The observation to delete, found by its identifier.
|
|
128
|
+
*
|
|
129
|
+
* @category outbox
|
|
130
|
+
*/
|
|
131
|
+
export type OutboxDeleteTarget = {
|
|
132
|
+
/** Identifier system. */
|
|
133
|
+
system: string;
|
|
134
|
+
/** Identifier value. */
|
|
135
|
+
value: string;
|
|
136
|
+
/** Patient the observation belongs to; defaults to the signed-in profile. */
|
|
137
|
+
patientId?: string;
|
|
138
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Outcome } from "./failure-policy";
|
|
2
|
+
import type { FlushReport, ItemSettler } from "./item-settler";
|
|
3
|
+
import type { OutboxItem } from "./outbox-types";
|
|
4
|
+
import type { RunnerState } from "./runner-state";
|
|
5
|
+
import type { RequestOptions } from "../http/transmit";
|
|
6
|
+
import type { ScopedLogger } from "../logging/ovok-logger";
|
|
7
|
+
import type { Bundle } from "@medplum/fhirtypes";
|
|
8
|
+
export type RequestContext = {
|
|
9
|
+
ownerKey: string;
|
|
10
|
+
options: RequestOptions | undefined;
|
|
11
|
+
};
|
|
12
|
+
export type SentOutcome = {
|
|
13
|
+
type: "sent";
|
|
14
|
+
response: Bundle;
|
|
15
|
+
};
|
|
16
|
+
export type AnyOutcome = Outcome | SentOutcome;
|
|
17
|
+
/** What to do with the items of a request once it ended as `T`; the answer is whether the flush goes on. */
|
|
18
|
+
export type Handlers = {
|
|
19
|
+
[T in AnyOutcome["type"]]: (outcome: Extract<AnyOutcome, {
|
|
20
|
+
type: T;
|
|
21
|
+
}>, items: OutboxItem[], report: FlushReport, request: RequestContext) => Promise<boolean>;
|
|
22
|
+
};
|
|
23
|
+
export type HandlerDeps = {
|
|
24
|
+
settler: ItemSettler;
|
|
25
|
+
state: RunnerState;
|
|
26
|
+
logger: ScopedLogger;
|
|
27
|
+
now: () => number;
|
|
28
|
+
sendEach: (items: OutboxItem[], report: FlushReport, request: RequestContext) => Promise<boolean>;
|
|
29
|
+
};
|
|
30
|
+
export declare const outcomeHandlers: (deps: HandlerDeps) => Handlers;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.outcomeHandlers = void 0;
|
|
4
|
+
const ALREADY_APPLIED = { resourceType: "Bundle", type: "batch-response", entry: [] };
|
|
5
|
+
/** A failure of one item settles it; a failure of a request of several sends each of them alone to find out whose. */
|
|
6
|
+
const failEach = ({ settler, sendEach }) => async (items, outcome, report, request) => {
|
|
7
|
+
if (items.length > 1) {
|
|
8
|
+
return sendEach(items, report, request);
|
|
9
|
+
}
|
|
10
|
+
await settler.failed(items[0], outcome, report);
|
|
11
|
+
return true;
|
|
12
|
+
};
|
|
13
|
+
const outcomeHandlers = (deps) => {
|
|
14
|
+
const { settler, state, logger, now } = deps;
|
|
15
|
+
const fail = failEach(deps);
|
|
16
|
+
return {
|
|
17
|
+
sent: async ({ response }, items, report) => {
|
|
18
|
+
await settler.sent(items, response, report);
|
|
19
|
+
return true;
|
|
20
|
+
},
|
|
21
|
+
"already-applied": async (_outcome, items, report) => {
|
|
22
|
+
await settler.sent(items, ALREADY_APPLIED, report);
|
|
23
|
+
return true;
|
|
24
|
+
},
|
|
25
|
+
interrupted: async () => false,
|
|
26
|
+
offline: async ({ failure }) => {
|
|
27
|
+
state.offline = true;
|
|
28
|
+
state.lastError = failure;
|
|
29
|
+
return false;
|
|
30
|
+
},
|
|
31
|
+
"owner-unavailable": async ({ failure }) => {
|
|
32
|
+
state.lastError = failure;
|
|
33
|
+
return false;
|
|
34
|
+
},
|
|
35
|
+
pause: async ({ reason, retryAfterMs, failure }) => {
|
|
36
|
+
const until = now() + state.pauses.durationMs(reason, retryAfterMs);
|
|
37
|
+
state.pauseUntil(until);
|
|
38
|
+
state.lastError = failure;
|
|
39
|
+
logger.warn("outbox.paused", { reason, until });
|
|
40
|
+
return false;
|
|
41
|
+
},
|
|
42
|
+
timeout: async (outcome, items, report, request) => {
|
|
43
|
+
await fail(items, outcome, report, request);
|
|
44
|
+
return false;
|
|
45
|
+
},
|
|
46
|
+
retry: (outcome, items, report, request) => fail(items, outcome, report, request),
|
|
47
|
+
reject: (outcome, items, report, request) => fail(items, outcome, report, request),
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
exports.outcomeHandlers = outcomeHandlers;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { OutboxStore } from "./outbox-store";
|
|
2
|
+
import type { OutboxDeleteTarget, OutboxItem } from "./outbox-types";
|
|
3
|
+
/** Whether an item would save an observation carrying the target's identifier. */
|
|
4
|
+
export declare const savesIdentifier: (item: OutboxItem, target: OutboxDeleteTarget) => boolean;
|
|
5
|
+
/** Removes pending saves of the target, so a delete sent first is never undone by a save sent after it. */
|
|
6
|
+
export declare const cancelPendingSaves: (store: OutboxStore, ownerKey: string, target: OutboxDeleteTarget, cancelled: (id: string) => void) => Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cancelPendingSaves = exports.savesIdentifier = void 0;
|
|
4
|
+
const carriesIdentifier = (resource, { system, value }) => { var _a, _b; return (_b = (_a = resource === null || resource === void 0 ? void 0 : resource.identifier) === null || _a === void 0 ? void 0 : _a.some((id) => id.system === system && id.value === value)) !== null && _b !== void 0 ? _b : false; };
|
|
5
|
+
/** Whether an item would save an observation carrying the target's identifier. */
|
|
6
|
+
const savesIdentifier = (item, target) => item.entries.some(({ resource }) => carriesIdentifier(resource, target));
|
|
7
|
+
exports.savesIdentifier = savesIdentifier;
|
|
8
|
+
/** Removes pending saves of the target, so a delete sent first is never undone by a save sent after it. */
|
|
9
|
+
const cancelPendingSaves = async (store, ownerKey, target, cancelled) => {
|
|
10
|
+
for (const { id, kind, state } of await store.summaries(ownerKey)) {
|
|
11
|
+
const item = kind !== "delete" && state === "pending" ? await store.get(id) : null;
|
|
12
|
+
if (item && (0, exports.savesIdentifier)(item, target)) {
|
|
13
|
+
cancelled(id);
|
|
14
|
+
await store.remove(id);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
exports.cancelPendingSaves = cancelPendingSaves;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** How many requests the outbox may make in a sliding window, so it leaves room under the patient's rate limit. */
|
|
2
|
+
export declare class RequestBudget {
|
|
3
|
+
private readonly limit;
|
|
4
|
+
private readonly windowMs;
|
|
5
|
+
private readonly times;
|
|
6
|
+
constructor(limit: number, windowMs?: number);
|
|
7
|
+
record(now: number): void;
|
|
8
|
+
/** Milliseconds until a request fits, 0 when one fits now. */
|
|
9
|
+
waitMs(now: number): number;
|
|
10
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestBudget = void 0;
|
|
4
|
+
const WINDOW_MS = 30000;
|
|
5
|
+
/** How many requests the outbox may make in a sliding window, so it leaves room under the patient's rate limit. */
|
|
6
|
+
class RequestBudget {
|
|
7
|
+
constructor(limit, windowMs = WINDOW_MS) {
|
|
8
|
+
this.limit = limit;
|
|
9
|
+
this.windowMs = windowMs;
|
|
10
|
+
this.times = [];
|
|
11
|
+
}
|
|
12
|
+
record(now) {
|
|
13
|
+
this.times.push(now);
|
|
14
|
+
}
|
|
15
|
+
/** Milliseconds until a request fits, 0 when one fits now. */
|
|
16
|
+
waitMs(now) {
|
|
17
|
+
const recent = this.times.filter((time) => time > now - this.windowMs);
|
|
18
|
+
this.times.splice(0, this.times.length, ...recent);
|
|
19
|
+
return recent.length < this.limit ? 0 : recent[0] + this.windowMs - now;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.RequestBudget = RequestBudget;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { OutboxItem, OutboxItemSummary } from "./outbox-types";
|
|
2
|
+
/** Items of a recording wait their turn: only the lowest `seq` still stored may go, so the group closes it. */
|
|
3
|
+
export declare const readyItems: (due: OutboxItem[], stored: OutboxItemSummary[]) => OutboxItem[];
|
|
4
|
+
/** Items of a recording go one request each; the rest are packed, newest first, into as few requests as fit. */
|
|
5
|
+
export declare const planRequests: (ready: OutboxItem[]) => OutboxItem[][];
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.planRequests = exports.readyItems = void 0;
|
|
4
|
+
const observation_groups_1 = require("../../client/observation/observation-groups");
|
|
5
|
+
const MAX_ENTRIES_PER_REQUEST = 50;
|
|
6
|
+
const AFTER_ANY_SEGMENT = Number.MAX_SAFE_INTEGER;
|
|
7
|
+
const positionIn = (item) => { var _a; return (_a = item.seq) !== null && _a !== void 0 ? _a : AFTER_ANY_SEGMENT; };
|
|
8
|
+
const isNextInGroup = (item, stored) => !stored.some((other) => other.group === item.group && other.id !== item.id && positionIn(other) < positionIn(item));
|
|
9
|
+
/** Items of a recording wait their turn: only the lowest `seq` still stored may go, so the group closes it. */
|
|
10
|
+
const readyItems = (due, stored) => due.filter((item) => item.group === undefined || isNextInGroup(item, stored));
|
|
11
|
+
exports.readyItems = readyItems;
|
|
12
|
+
const packPlain = (items) => {
|
|
13
|
+
const requests = (0, observation_groups_1.packObservationGroups)(items.map(({ entries }) => entries), MAX_ENTRIES_PER_REQUEST);
|
|
14
|
+
return requests.reduce((packed, request) => {
|
|
15
|
+
const taken = packed.reduce((count, group) => count + group.length, 0);
|
|
16
|
+
return [...packed, items.slice(taken, taken + request.length)];
|
|
17
|
+
}, []);
|
|
18
|
+
};
|
|
19
|
+
/** Items of a recording go one request each; the rest are packed, newest first, into as few requests as fit. */
|
|
20
|
+
const planRequests = (ready) => {
|
|
21
|
+
const requests = [];
|
|
22
|
+
let plain = [];
|
|
23
|
+
const flushPlain = () => {
|
|
24
|
+
requests.push(...packPlain(plain));
|
|
25
|
+
plain = [];
|
|
26
|
+
};
|
|
27
|
+
for (const item of ready) {
|
|
28
|
+
if (item.group === undefined) {
|
|
29
|
+
plain.push(item);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
flushPlain();
|
|
33
|
+
requests.push([item]);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
flushPlain();
|
|
37
|
+
return requests;
|
|
38
|
+
};
|
|
39
|
+
exports.planRequests = planRequests;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { OutboxFailure, OutboxItem, OutboxSentItem } from "./outbox-types";
|
|
2
|
+
import { PauseLadder } from "./failure-policy";
|
|
3
|
+
/** What the outbox is doing, as `ovok.outbox.snapshot` reports it. */
|
|
4
|
+
export type RunnerView = {
|
|
5
|
+
sending: number;
|
|
6
|
+
offline: boolean;
|
|
7
|
+
pausedUntil: number | null;
|
|
8
|
+
lastError: OutboxFailure | null;
|
|
9
|
+
};
|
|
10
|
+
/** What the runner remembers between requests: pauses, connectivity, what is planned or sending, what was sent. */
|
|
11
|
+
export declare class RunnerState {
|
|
12
|
+
private readonly now;
|
|
13
|
+
readonly pauses: PauseLadder;
|
|
14
|
+
readonly claimed: Set<string>;
|
|
15
|
+
readonly sending: Set<string>;
|
|
16
|
+
offline: boolean;
|
|
17
|
+
lastError: OutboxFailure | null;
|
|
18
|
+
private pausedUntil;
|
|
19
|
+
private readonly superseded;
|
|
20
|
+
private readonly recent;
|
|
21
|
+
constructor(now: () => number);
|
|
22
|
+
view(): RunnerView;
|
|
23
|
+
isPaused(): boolean;
|
|
24
|
+
pausedUntilTime(): number | null;
|
|
25
|
+
pauseUntil(time: number): void;
|
|
26
|
+
/** A request went through: the outbox is online, unpaused and its pauses start over. */
|
|
27
|
+
succeeded(): void;
|
|
28
|
+
/** A stored item was replaced or deleted: whatever is planned or sending for it is stale. */
|
|
29
|
+
supersede(id: string): void;
|
|
30
|
+
/** True once if the item was superseded since it was planned. */
|
|
31
|
+
wasSuperseded(id: string): boolean;
|
|
32
|
+
remember({ id, kind, group, seq, recordedAt }: OutboxItem): void;
|
|
33
|
+
recentlySent(): OutboxSentItem[];
|
|
34
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RunnerState = void 0;
|
|
4
|
+
const failure_policy_1 = require("./failure-policy");
|
|
5
|
+
const RECENTLY_SENT_LIMIT = 50;
|
|
6
|
+
/** What the runner remembers between requests: pauses, connectivity, what is planned or sending, what was sent. */
|
|
7
|
+
class RunnerState {
|
|
8
|
+
constructor(now) {
|
|
9
|
+
this.now = now;
|
|
10
|
+
this.pauses = new failure_policy_1.PauseLadder();
|
|
11
|
+
this.claimed = new Set();
|
|
12
|
+
this.sending = new Set();
|
|
13
|
+
this.offline = false;
|
|
14
|
+
this.lastError = null;
|
|
15
|
+
this.pausedUntil = null;
|
|
16
|
+
this.superseded = new Set();
|
|
17
|
+
this.recent = [];
|
|
18
|
+
}
|
|
19
|
+
view() {
|
|
20
|
+
return {
|
|
21
|
+
sending: this.sending.size,
|
|
22
|
+
offline: this.offline,
|
|
23
|
+
pausedUntil: this.pausedUntilTime(),
|
|
24
|
+
lastError: this.lastError,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
isPaused() {
|
|
28
|
+
return this.pausedUntilTime() !== null;
|
|
29
|
+
}
|
|
30
|
+
pausedUntilTime() {
|
|
31
|
+
return this.pausedUntil !== null && this.now() < this.pausedUntil ? this.pausedUntil : null;
|
|
32
|
+
}
|
|
33
|
+
pauseUntil(time) {
|
|
34
|
+
this.pausedUntil = time;
|
|
35
|
+
}
|
|
36
|
+
/** A request went through: the outbox is online, unpaused and its pauses start over. */
|
|
37
|
+
succeeded() {
|
|
38
|
+
this.offline = false;
|
|
39
|
+
this.lastError = null;
|
|
40
|
+
this.pauses.reset();
|
|
41
|
+
}
|
|
42
|
+
/** A stored item was replaced or deleted: whatever is planned or sending for it is stale. */
|
|
43
|
+
supersede(id) {
|
|
44
|
+
if (this.claimed.has(id)) {
|
|
45
|
+
this.superseded.add(id);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** True once if the item was superseded since it was planned. */
|
|
49
|
+
wasSuperseded(id) {
|
|
50
|
+
return this.superseded.delete(id);
|
|
51
|
+
}
|
|
52
|
+
remember({ id, kind, group, seq, recordedAt }) {
|
|
53
|
+
const position = { ...(group === undefined ? {} : { group }), ...(seq === undefined ? {} : { seq }) };
|
|
54
|
+
this.recent.unshift({ id, kind, recordedAt, sentAt: this.now(), ...position });
|
|
55
|
+
this.recent.length = Math.min(this.recent.length, RECENTLY_SENT_LIMIT);
|
|
56
|
+
}
|
|
57
|
+
recentlySent() {
|
|
58
|
+
return [...this.recent];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.RunnerState = RunnerState;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { OutboxStore } from "./outbox-store";
|
|
2
|
+
/**
|
|
3
|
+
* One behaviour every `OutboxStore` must have: a name and a function that throws when the store lacks it.
|
|
4
|
+
*
|
|
5
|
+
* @category outbox
|
|
6
|
+
*/
|
|
7
|
+
export type OutboxStoreCheck = {
|
|
8
|
+
/** What the store must do. */
|
|
9
|
+
name: string;
|
|
10
|
+
/** Runs the check on a fresh store; it throws when the store does not behave. */
|
|
11
|
+
run: () => Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The checks every `OutboxStore` adapter must pass. Run each against a store of your own:
|
|
15
|
+
* `outboxStoreContract(() => new MyStore()).forEach(({ name, run }) => it(name, run))`.
|
|
16
|
+
*
|
|
17
|
+
* @category outbox
|
|
18
|
+
*/
|
|
19
|
+
export declare const outboxStoreContract: (createStore: () => OutboxStore | Promise<OutboxStore>) => OutboxStoreCheck[];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.outboxStoreContract = void 0;
|
|
4
|
+
const OWNER = "user:Project/1|Patient/1";
|
|
5
|
+
const OTHER_OWNER = "user:Project/1|Patient/2";
|
|
6
|
+
const NOW = 1000000;
|
|
7
|
+
const NEVER_RETRIED_YET = NOW + 1;
|
|
8
|
+
const item = (id, overrides = {}) => ({
|
|
9
|
+
id,
|
|
10
|
+
ownerKey: OWNER,
|
|
11
|
+
kind: "measurement",
|
|
12
|
+
entries: [{ fullUrl: `urn:uuid:${id}`, request: { method: "POST", url: "Observation" } }],
|
|
13
|
+
state: "pending",
|
|
14
|
+
attempts: 0,
|
|
15
|
+
nextAttemptAt: 0,
|
|
16
|
+
createdAt: NOW,
|
|
17
|
+
recordedAt: NOW,
|
|
18
|
+
...overrides,
|
|
19
|
+
});
|
|
20
|
+
const canonical = (value) => JSON.stringify(value, (_key, nested) => nested && typeof nested === "object" && !Array.isArray(nested)
|
|
21
|
+
? Object.fromEntries(Object.entries(nested).sort(([left], [right]) => left.localeCompare(right)))
|
|
22
|
+
: nested);
|
|
23
|
+
const expectSame = (actual, expected) => {
|
|
24
|
+
if (canonical(actual) !== canonical(expected)) {
|
|
25
|
+
throw new Error(`Expected ${canonical(expected)} but got ${canonical(actual)}.`);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const idsOf = (items) => items.map(({ id }) => id);
|
|
29
|
+
const checks = {
|
|
30
|
+
"gives back an item it was given": async (store) => {
|
|
31
|
+
await store.put(item("a", { group: "recording", seq: 2, lastFailure: { name: "Failure", status: 422 } }));
|
|
32
|
+
expectSame(await store.get("a"), item("a", { group: "recording", seq: 2, lastFailure: { name: "Failure", status: 422 } }));
|
|
33
|
+
},
|
|
34
|
+
"gives null for an id it does not have": async (store) => {
|
|
35
|
+
expectSame(await store.get("missing"), null);
|
|
36
|
+
},
|
|
37
|
+
"replaces the item with the same id": async (store) => {
|
|
38
|
+
var _a;
|
|
39
|
+
await store.put(item("a"));
|
|
40
|
+
await store.put(item("a", { attempts: 3 }));
|
|
41
|
+
expectSame((_a = (await store.get("a"))) === null || _a === void 0 ? void 0 : _a.attempts, 3);
|
|
42
|
+
},
|
|
43
|
+
"removes an item": async (store) => {
|
|
44
|
+
await store.put(item("a"));
|
|
45
|
+
await store.remove("a");
|
|
46
|
+
expectSame(await store.get("a"), null);
|
|
47
|
+
},
|
|
48
|
+
"removes an unknown id without failing": async (store) => {
|
|
49
|
+
await store.remove("missing");
|
|
50
|
+
expectSame(await store.count(OWNER, "pending"), 0);
|
|
51
|
+
},
|
|
52
|
+
"lists due items newest recorded first": async (store) => {
|
|
53
|
+
await store.put(item("old", { recordedAt: 1 }));
|
|
54
|
+
await store.put(item("new", { recordedAt: 3 }));
|
|
55
|
+
await store.put(item("middle", { recordedAt: 2 }));
|
|
56
|
+
expectSame(idsOf(await store.due(OWNER, NOW, 10)), ["new", "middle", "old"]);
|
|
57
|
+
},
|
|
58
|
+
"stops due items at the limit": async (store) => {
|
|
59
|
+
await store.put(item("old", { recordedAt: 1 }));
|
|
60
|
+
await store.put(item("new", { recordedAt: 3 }));
|
|
61
|
+
expectSame(idsOf(await store.due(OWNER, NOW, 1)), ["new"]);
|
|
62
|
+
},
|
|
63
|
+
"leaves out items that are not yet due": async (store) => {
|
|
64
|
+
await store.put(item("later", { nextAttemptAt: NEVER_RETRIED_YET }));
|
|
65
|
+
expectSame(await store.due(OWNER, NOW, 10), []);
|
|
66
|
+
},
|
|
67
|
+
"leaves out rejected items from due": async (store) => {
|
|
68
|
+
await store.put(item("refused", { state: "rejected" }));
|
|
69
|
+
expectSame(await store.due(OWNER, NOW, 10), []);
|
|
70
|
+
},
|
|
71
|
+
"leaves out another owner's items from due": async (store) => {
|
|
72
|
+
await store.put(item("theirs", { ownerKey: OTHER_OWNER }));
|
|
73
|
+
expectSame(await store.due(OWNER, NOW, 10), []);
|
|
74
|
+
},
|
|
75
|
+
"returns the entries of due items": async (store) => {
|
|
76
|
+
var _a;
|
|
77
|
+
await store.put(item("a"));
|
|
78
|
+
expectSame((_a = (await store.due(OWNER, NOW, 10))[0]) === null || _a === void 0 ? void 0 : _a.entries, item("a").entries);
|
|
79
|
+
},
|
|
80
|
+
"lists every state of one owner in summaries": async (store) => {
|
|
81
|
+
await store.put(item("a"));
|
|
82
|
+
await store.put(item("b", { state: "rejected" }));
|
|
83
|
+
await store.put(item("c", { ownerKey: OTHER_OWNER }));
|
|
84
|
+
expectSame(idsOf(await store.summaries(OWNER)).sort(), ["a", "b"]);
|
|
85
|
+
},
|
|
86
|
+
"leaves the entries out of summaries": async (store) => {
|
|
87
|
+
var _a;
|
|
88
|
+
await store.put(item("a"));
|
|
89
|
+
expectSame("entries" in ((_a = (await store.summaries(OWNER))[0]) !== null && _a !== void 0 ? _a : {}), false);
|
|
90
|
+
},
|
|
91
|
+
"counts one state of one owner": async (store) => {
|
|
92
|
+
await store.put(item("a"));
|
|
93
|
+
await store.put(item("b", { state: "rejected" }));
|
|
94
|
+
await store.put(item("c", { ownerKey: OTHER_OWNER }));
|
|
95
|
+
expectSame(await store.count(OWNER, "pending"), 1);
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* The checks every `OutboxStore` adapter must pass. Run each against a store of your own:
|
|
100
|
+
* `outboxStoreContract(() => new MyStore()).forEach(({ name, run }) => it(name, run))`.
|
|
101
|
+
*
|
|
102
|
+
* @category outbox
|
|
103
|
+
*/
|
|
104
|
+
const outboxStoreContract = (createStore) => Object.entries(checks).map(([name, scenario]) => ({
|
|
105
|
+
name,
|
|
106
|
+
run: async () => scenario(await createStore()),
|
|
107
|
+
}));
|
|
108
|
+
exports.outboxStoreContract = outboxStoreContract;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** One timer that fires `fire` at the earliest time anyone asked for. */
|
|
2
|
+
export declare class Wakeup {
|
|
3
|
+
private readonly now;
|
|
4
|
+
private readonly fire;
|
|
5
|
+
private timer;
|
|
6
|
+
private dueAt;
|
|
7
|
+
constructor(now: () => number, fire: () => void);
|
|
8
|
+
at(delayMs: number): void;
|
|
9
|
+
private ring;
|
|
10
|
+
private cancel;
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Wakeup = void 0;
|
|
4
|
+
/** One timer that fires `fire` at the earliest time anyone asked for. */
|
|
5
|
+
class Wakeup {
|
|
6
|
+
constructor(now, fire) {
|
|
7
|
+
this.now = now;
|
|
8
|
+
this.fire = fire;
|
|
9
|
+
this.timer = null;
|
|
10
|
+
this.dueAt = Infinity;
|
|
11
|
+
}
|
|
12
|
+
at(delayMs) {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
const dueAt = this.now() + delayMs;
|
|
15
|
+
if (this.timer !== null && this.dueAt <= dueAt) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.cancel();
|
|
19
|
+
this.dueAt = dueAt;
|
|
20
|
+
this.timer = setTimeout(() => this.ring(), delayMs);
|
|
21
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
22
|
+
}
|
|
23
|
+
ring() {
|
|
24
|
+
this.timer = null;
|
|
25
|
+
this.dueAt = Infinity;
|
|
26
|
+
this.fire();
|
|
27
|
+
}
|
|
28
|
+
cancel() {
|
|
29
|
+
if (this.timer !== null) {
|
|
30
|
+
clearTimeout(this.timer);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.Wakeup = Wakeup;
|
package/dist/next/ovok.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OvokConfig } from "./config/ovok-config";
|
|
2
|
+
import { OutboxResource } from "./outbox/outbox-resource";
|
|
2
3
|
import { AccountsResource } from "./resources/accounts/accounts-resource";
|
|
3
4
|
import { AiResource } from "./resources/ai/ai-resource";
|
|
4
5
|
import { AuthResource } from "./resources/auth-resource";
|
|
@@ -42,6 +43,8 @@ export declare class Ovok {
|
|
|
42
43
|
readonly patient: PatientResource;
|
|
43
44
|
/** Saving and reading measurements. */
|
|
44
45
|
readonly measurements: MeasurementsResource;
|
|
46
|
+
/** Measurements waiting to be sent, and what the outbox is doing. */
|
|
47
|
+
readonly outbox: OutboxResource;
|
|
45
48
|
/** Questionnaires. */
|
|
46
49
|
readonly questionnaires: QuestionnairesResource;
|
|
47
50
|
/** Questionnaire responses. */
|
package/dist/next/ovok.js
CHANGED
|
@@ -56,10 +56,21 @@ class Ovok {
|
|
|
56
56
|
this.health = new health_resource_1.HealthResource(http.anonymous());
|
|
57
57
|
this.patient = new patient_resource_1.PatientResource(http, fhir);
|
|
58
58
|
this.measurements = new measurements_resource_1.MeasurementsResource(http, fhir, session, {
|
|
59
|
-
storage: settings.storage,
|
|
60
|
-
queueKey: (0, offline_measurement_queue_1.offlineMeasurementStorageKey)(settings.tenantCode, settings.baseUrl),
|
|
61
59
|
machineClientId: (_b = (_a = settings.machineClient) === null || _a === void 0 ? void 0 : _a.clientId) !== null && _b !== void 0 ? _b : null,
|
|
60
|
+
outbox: {
|
|
61
|
+
store: settings.outboxStore,
|
|
62
|
+
legacyQueue: {
|
|
63
|
+
storage: settings.storage,
|
|
64
|
+
key: (0, offline_measurement_queue_1.offlineMeasurementStorageKey)(settings.tenantCode, settings.baseUrl),
|
|
65
|
+
},
|
|
66
|
+
connectivity: settings.connectivity,
|
|
67
|
+
lifecycle: settings.lifecycle,
|
|
68
|
+
requestBudget: settings.outboxRequestBudget,
|
|
69
|
+
logger: settings.logger,
|
|
70
|
+
onSessionChange: (listener) => this.session.onChange(listener),
|
|
71
|
+
},
|
|
62
72
|
});
|
|
73
|
+
this.outbox = this.measurements.outbox;
|
|
63
74
|
this.questionnaires = new questionnaires_resource_1.QuestionnairesResource(http);
|
|
64
75
|
this.questionnaireResponses = new questionnaires_resource_1.QuestionnaireResponsesResource(http, fhir);
|
|
65
76
|
this.consent = new consent_resource_1.ConsentResource(http);
|