@aeolus-fleet/core 0.36.1 → 0.37.1
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 +1 -1
- package/dist/adapters/http/server.js +9 -1
- package/dist/adapters/http/server.js.map +1 -1
- package/dist/adapters/prisma/fleet-event-wakeups.js +2 -1
- package/dist/adapters/prisma/fleet-event-wakeups.js.map +1 -1
- package/dist/adapters/prisma/harbour.d.ts +37 -0
- package/dist/adapters/prisma/harbour.js +119 -0
- package/dist/adapters/prisma/harbour.js.map +1 -0
- package/dist/adapters/prisma/receiver-wakeups.d.ts +11 -9
- package/dist/adapters/prisma/receiver-wakeups.js +71 -2
- package/dist/adapters/prisma/receiver-wakeups.js.map +1 -1
- package/dist/adapters/prisma/registry.d.ts +13 -1
- package/dist/adapters/prisma/registry.js +6 -4
- package/dist/adapters/prisma/registry.js.map +1 -1
- package/dist/adapters/rest/openapi.d.ts +3 -3
- package/dist/adapters/rest/openapi.js +30 -3
- package/dist/adapters/rest/openapi.js.map +1 -1
- package/dist/adapters/rest/rest-api.d.ts +4 -0
- package/dist/adapters/rest/rest-api.js +107 -3
- package/dist/adapters/rest/rest-api.js.map +1 -1
- package/dist/adapters/telemetry/calls.d.ts +8 -0
- package/dist/adapters/telemetry/calls.js +39 -3
- package/dist/adapters/telemetry/calls.js.map +1 -1
- package/dist/adapters/telemetry/wake-streams.d.ts +6 -0
- package/dist/adapters/telemetry/wake-streams.js +28 -0
- package/dist/adapters/telemetry/wake-streams.js.map +1 -0
- package/dist/adapters/trpc/context.d.ts +6 -1
- package/dist/adapters/trpc/fleet.d.ts +31 -0
- package/dist/adapters/trpc/fleet.js +23 -1
- package/dist/adapters/trpc/fleet.js.map +1 -1
- package/dist/adapters/trpc/installation.js +7 -1
- package/dist/adapters/trpc/installation.js.map +1 -1
- package/dist/adapters/trpc/router.d.ts +40 -0
- package/dist/adapters/trpc/ship-contract.d.ts +68 -0
- package/dist/adapters/trpc/ship-contract.js +99 -1
- package/dist/adapters/trpc/ship-contract.js.map +1 -1
- package/dist/adapters/trpc/ship-protocol.js +1 -0
- package/dist/adapters/trpc/ship-protocol.js.map +1 -1
- package/dist/adapters/trpc/ship.d.ts +50 -13
- package/dist/adapters/trpc/ship.js +50 -0
- package/dist/adapters/trpc/ship.js.map +1 -1
- package/dist/adapters/trpc/trpc.js +2 -0
- package/dist/adapters/trpc/trpc.js.map +1 -1
- package/dist/app.js +2 -1
- package/dist/app.js.map +1 -1
- package/dist/domain/messaging/ports.d.ts +25 -0
- package/dist/domain/messaging/watch-wakeups.d.ts +41 -0
- package/dist/domain/messaging/watch-wakeups.js +54 -0
- package/dist/domain/messaging/watch-wakeups.js.map +1 -0
- package/dist/domain/registry/get-installation-fleet.d.ts +5 -1
- package/dist/domain/registry/get-installation-fleet.js +12 -3
- package/dist/domain/registry/get-installation-fleet.js.map +1 -1
- package/dist/domain/registry/harbour.d.ts +36 -0
- package/dist/domain/registry/harbour.js +27 -0
- package/dist/domain/registry/harbour.js.map +1 -0
- package/dist/domain/registry/ports.d.ts +25 -0
- package/dist/domain/registry/read-harbour.d.ts +29 -0
- package/dist/domain/registry/read-harbour.js +27 -0
- package/dist/domain/registry/read-harbour.js.map +1 -0
- package/dist/domain/shared/errors.d.ts +1 -1
- package/dist/domain/shared/errors.js.map +1 -1
- package/dist/wiring.d.ts +7 -1
- package/dist/wiring.js +5 -0
- package/dist/wiring.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -65,7 +65,7 @@ The server and the console send traces and metrics over OpenTelemetry (OTLP), sw
|
|
|
65
65
|
Every other `OTEL_*` setting the OpenTelemetry Node SDK reads works as its specification says. What is sent:
|
|
66
66
|
|
|
67
67
|
- Every HTTP request is a point in the histogram `http.server.request.duration` (seconds) with `http.request.method`, `http.route` (the route template, such as `/trpc/:path`) and `http.response.status_code`; a 5xx adds `error.type`. A tRPC batch is one request for several procedures, so each procedure is measured on its own as well.
|
|
68
|
-
- Every tRPC, REST and MCP call is a server span named by its procedure (`system.ping`, `ship.receive`), and a point in the histogram `rpc.server.call.duration` (seconds) with `rpc.system` (`trpc`), `rpc.method` and `rpc.response.status_code` (`OK` or the refusal code), so p95, p99 and the error rate are read per procedure; a server failure adds `error.type`. Subscriptions are not measured.
|
|
68
|
+
- Every tRPC, REST and MCP call is a server span named by its procedure (`system.ping`, `ship.receive`), and a point in the histogram `rpc.server.call.duration` (seconds) with `rpc.system` (`trpc`), `rpc.method` and `rpc.response.status_code` (`OK` or the refusal code), so p95, p99 and the error rate are read per procedure; a server failure adds `error.type`. The time a call waits for a message (receive, pushReceive, inbox, receiveReply, fleet.follow) is left out of that duration and recorded apart in `rpc.server.call.wait.duration`, by `rpc.method`. Subscriptions are not measured.
|
|
69
69
|
- Every database query is traced under the call that made it (Prisma's instrumentation, with its SQL), and each Prisma operation is a point in `db.client.operation.duration` (seconds) with `db.operation.name`, such as `Ship.findMany` or `$queryRaw`.
|
|
70
70
|
- The console sends Next.js's own spans and a point in `http.server.request.duration` for every request, and each console read as a span and a point in `rpc.server.call.duration`, named `reads.<read>`.
|
|
71
71
|
|
|
@@ -6,7 +6,7 @@ import Fastify, {} from 'fastify';
|
|
|
6
6
|
import { registerMcpEndpoint } from '../mcp/mcp-endpoint.js';
|
|
7
7
|
import { createFleetEventWakeups } from '../prisma/fleet-event-wakeups.js';
|
|
8
8
|
import { failureForLog } from '../prisma/failure-log.js';
|
|
9
|
-
import { registerRestApi } from '../rest/rest-api.js';
|
|
9
|
+
import { FLEET_EVENTS_STREAM_ROUTE, registerRestApi, WAKE_STREAM_ROUTE } from '../rest/rest-api.js';
|
|
10
10
|
import { appRouter } from '../trpc/router.js';
|
|
11
11
|
import { refusalBody, unexpectedFailure } from '../trpc/ship-contract.js';
|
|
12
12
|
import { createRateLimiter } from './rate-limiter.js';
|
|
@@ -89,6 +89,14 @@ export function buildHttpServer(options) {
|
|
|
89
89
|
const fleetEvents = options.fleetEvents ?? createFleetEventWakeups();
|
|
90
90
|
// Every answered request, measured (telemetry/requests.ts): a no-op until telemetry starts.
|
|
91
91
|
server.addHook('onResponse', async (request, reply) => {
|
|
92
|
+
if (request.routeOptions.url === WAKE_STREAM_ROUTE) {
|
|
93
|
+
// A wake stream lasts as long as its crew keeps it: measured apart (telemetry/wake-streams.ts).
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (request.routeOptions.url === FLEET_EVENTS_STREAM_ROUTE) {
|
|
97
|
+
// A fleet events stream lasts as long as its follower keeps it.
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
92
100
|
measureRequest({ method: request.method, route: request.routeOptions.url, statusCode: reply.statusCode, durationMs: reply.elapsedTime });
|
|
93
101
|
});
|
|
94
102
|
// The console may run on another host under the cookie's domain (ADR 0012).
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/adapters/http/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAiC,MAAM,+BAA+B,CAAC;AACjG,OAAO,EAAE,sBAAsB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AACnF,OAAO,OAAO,EAAE,EAA2F,MAAM,SAAS,CAAC;AAG3H,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAC7D,OAAO,EAAE,uBAAuB,EAAE,MAAM,kCAAkC,CAAC;AAC3E,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/adapters/http/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,gBAAgB,MAAM,oBAAoB,CAAC;AAClD,OAAO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ded = false;
|
|
@@ -75,7 +76,7 @@ export function fleetEventWakeupsOf(hub) {
|
|
|
75
76
|
watch: (fleetId) => {
|
|
76
77
|
const watch = hub.watch(fleetId);
|
|
77
78
|
return {
|
|
78
|
-
next: async (waitMs) => ((await watch.next(AbortSignal.timeout(waitMs))) ? 'woken' : 'timedOut'),
|
|
79
|
+
next: async (waitMs) => ((await measureWait(() => watch.next(AbortSignal.timeout(waitMs)))) ? 'woken' : 'timedOut'),
|
|
79
80
|
stop: () => {
|
|
80
81
|
watch.stop();
|
|
81
82
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fleet-event-wakeups.js","sourceRoot":"","sources":["../../../src/adapters/prisma/fleet-event-wakeups.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"fleet-event-wakeups.js","sourceRoot":"","sources":["../../../src/adapters/prisma/fleet-event-wakeups.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AA2BpD,MAAM,UAAU,uBAAuB;IACrC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAC;IAClD,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,MAAM,IAAI,GAAG,CAAC,OAAgB,EAAE,EAAE;QAChC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACvB,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;QACzB,CAAC;IACH,CAAC,CAAC;IAEF,MAAM,KAAK,GAAG,GAAc,EAAE,CAAC,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;IAEjF,OAAO;QACL,KAAK,EAAE,CAAC,OAAO,EAAE,EAAE;YACjB,MAAM,OAAO,GAAY,EAAE,OAAO,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oBACT,KAAK,CAAC,IAAI,EAAE,CAAC;gBACf,CAAC;aACF,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harbour's read (docs/design/fleet-overview, "The read Harbour needs"): the
|
|
3
|
+
* active ships in read reach, the viewer ship left out, each by id, name,
|
|
4
|
+
* kind, its state, note and last activity, a page at a time in the listing's default order
|
|
5
|
+
* through `ships_listing_idx`; and the numbers over the same ships. The
|
|
6
|
+
* state is one CASE, held to `harbourStateOf` by harbour.integration.test.ts.
|
|
7
|
+
*/
|
|
8
|
+
import { type FleetId, type ShipPosition } from '@aeolus-fleet/common';
|
|
9
|
+
import type { HarbourCounts, HarbourShip } from '../../domain/registry/harbour.js';
|
|
10
|
+
import type { HarbourRead } from '../../domain/registry/ports.js';
|
|
11
|
+
import type { ReadReach } from '../../domain/registry/read-reach.js';
|
|
12
|
+
import type { Db } from './client.js';
|
|
13
|
+
import { Prisma } from './generated/client.js';
|
|
14
|
+
/**
|
|
15
|
+
* A page of Harbour's ships: ordered and cut by the default order's keys
|
|
16
|
+
* alone, argo first and then by name, so the page reads only its own ships
|
|
17
|
+
* from the index; then only the page's ships read their lease and alerts.
|
|
18
|
+
* With ship ids, only those ships, in the same order.
|
|
19
|
+
*/
|
|
20
|
+
export declare function harbourPageStatement(fleetId: FleetId, read: HarbourRead): Prisma.Sql;
|
|
21
|
+
/** Harbour's numbers: the ships it shows, counted per state with the same CASE, without reading a retired ship. */
|
|
22
|
+
export declare function harbourCountsStatement(fleetId: FleetId, reach: ReadReach): Prisma.Sql;
|
|
23
|
+
/**
|
|
24
|
+
* Reads Harbour's ships, and on a page from the start its numbers, in one
|
|
25
|
+
* repeatable-read transaction so the numbers count the ships the page
|
|
26
|
+
* shows; inside a unit of work, in its transaction.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readHarbourOf(db: Db, { fleetId, read }: {
|
|
29
|
+
fleetId: FleetId;
|
|
30
|
+
read: HarbourRead;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
ships: {
|
|
33
|
+
ship: HarbourShip;
|
|
34
|
+
position: ShipPosition;
|
|
35
|
+
}[];
|
|
36
|
+
counts: HarbourCounts | null;
|
|
37
|
+
}>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Harbour's read (docs/design/fleet-overview, "The read Harbour needs"): the
|
|
3
|
+
* active ships in read reach, the viewer ship left out, each by id, name,
|
|
4
|
+
* kind, its state, note and last activity, a page at a time in the listing's default order
|
|
5
|
+
* through `ships_listing_idx`; and the numbers over the same ships. The
|
|
6
|
+
* state is one CASE, held to `harbourStateOf` by harbour.integration.test.ts.
|
|
7
|
+
*/
|
|
8
|
+
import { idSchema, SHIP_KINDS, shipKindSchema } from '@aeolus-fleet/common';
|
|
9
|
+
import { HARBOUR_STATES } from '@aeolus-fleet/common/rules';
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { Prisma } from './generated/client.js';
|
|
12
|
+
import { afterPosition, isInReachOfShip, orderOfPage, shipPositionOfRow } from './registry.js';
|
|
13
|
+
/**
|
|
14
|
+
* The state of the ship `s` of the outer query, with `l` its open lease,
|
|
15
|
+
* first match wins: an open alert, awaiting crew (no open lease; argo is
|
|
16
|
+
* always crewed) or a blocked report, a working report, else idle.
|
|
17
|
+
*/
|
|
18
|
+
function harbourStateOfShip(hasOpenAlert) {
|
|
19
|
+
return Prisma.sql `
|
|
20
|
+
CASE
|
|
21
|
+
WHEN ${hasOpenAlert} THEN 'alert'
|
|
22
|
+
WHEN (l.id IS NULL AND s.kind <> 'operator') OR l.report_state = 'blocked' THEN 'waiting'
|
|
23
|
+
WHEN l.report_state = 'working' THEN 'working'
|
|
24
|
+
ELSE 'idle'
|
|
25
|
+
END`;
|
|
26
|
+
}
|
|
27
|
+
/** Whether the ship `s` has an open alert, looked up for it alone through alerts_open_on_ship_idx: for a page's ships. */
|
|
28
|
+
const hasOpenAlertOfShip = Prisma.sql `EXISTS (SELECT 1 FROM alerts a WHERE a.fleet_id = s.fleet_id AND a.ship_id = s.id AND a.cleared_at IS NULL)`;
|
|
29
|
+
/** Every kind but the viewer ship, the console's own. */
|
|
30
|
+
const SHOWN_KINDS = SHIP_KINDS.filter((kind) => kind !== 'viewer');
|
|
31
|
+
/** The ships Harbour shows, as conditions on the ship `s` of the outer query: active, not the viewer ship, in reach. */
|
|
32
|
+
function isShownOfShip(fleetId, reach) {
|
|
33
|
+
// The kinds named, not "not the viewer", so ships_kind_listing_idx serves it.
|
|
34
|
+
return Prisma.sql `s.fleet_id = ${fleetId} AND s.retired_at IS NULL AND s.removed_at IS NULL AND s.kind = ANY(${[...SHOWN_KINDS]}::ship_kind[]) AND ${isInReachOfShip(reach)}`;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A page of Harbour's ships: ordered and cut by the default order's keys
|
|
38
|
+
* alone, argo first and then by name, so the page reads only its own ships
|
|
39
|
+
* from the index; then only the page's ships read their lease and alerts.
|
|
40
|
+
* With ship ids, only those ships, in the same order.
|
|
41
|
+
*/
|
|
42
|
+
export function harbourPageStatement(fleetId, read) {
|
|
43
|
+
const selected = 'shipIds' in read ? Prisma.sql `AND s.id = ANY(${[...read.shipIds]}::text[])` : Prisma.empty;
|
|
44
|
+
const after = 'shipIds' in read ? Prisma.empty : afterPosition(read.after, undefined);
|
|
45
|
+
const limit = 'shipIds' in read ? Prisma.empty : Prisma.sql `LIMIT ${read.limit}`;
|
|
46
|
+
return Prisma.sql `
|
|
47
|
+
SELECT s.id, s.name, s.kind::text AS kind, l.report_note, ${harbourStateOfShip(hasOpenAlertOfShip)} AS state,
|
|
48
|
+
GREATEST(sent_or_acked.occurred_at, l.activity_at) AS last_activity_at,
|
|
49
|
+
pg.sort_is_empty, pg.sort_value, pg.sort_name
|
|
50
|
+
FROM (
|
|
51
|
+
SELECT ranked.* FROM (
|
|
52
|
+
SELECT s.id, s.kind <> 'operator' AS sort_is_empty, ''::text AS sort_value, lower(s.name) AS sort_name
|
|
53
|
+
FROM ships s
|
|
54
|
+
WHERE ${isShownOfShip(fleetId, read.reach)} ${selected}
|
|
55
|
+
) ranked
|
|
56
|
+
${after}
|
|
57
|
+
${orderOfPage('ranked', undefined)}
|
|
58
|
+
${limit}
|
|
59
|
+
) pg
|
|
60
|
+
JOIN ships s ON s.fleet_id = ${fleetId} AND s.id = pg.id
|
|
61
|
+
LEFT JOIN leases l ON l.fleet_id = s.fleet_id AND l.ship_id = s.id AND l.ended_at IS NULL
|
|
62
|
+
-- The ship's newest send or acknowledgement, one step back along its own events (events_fleet_id_actor_ship_id_seq_idx), for the page's ships only.
|
|
63
|
+
LEFT JOIN LATERAL (
|
|
64
|
+
SELECT e.occurred_at FROM events e
|
|
65
|
+
WHERE e.fleet_id = s.fleet_id AND e.actor_ship_id = s.id AND e.type IN ('MessageAccepted', 'DeliveryAcknowledged')
|
|
66
|
+
ORDER BY e.seq DESC
|
|
67
|
+
LIMIT 1
|
|
68
|
+
) sent_or_acked ON true
|
|
69
|
+
${orderOfPage('pg', undefined)}`;
|
|
70
|
+
}
|
|
71
|
+
/** Harbour's numbers: the ships it shows, counted per state with the same CASE, without reading a retired ship. */
|
|
72
|
+
export function harbourCountsStatement(fleetId, reach) {
|
|
73
|
+
return Prisma.sql `
|
|
74
|
+
SELECT count(*)::int AS ships,
|
|
75
|
+
count(*) FILTER (WHERE state = 'alert')::int AS alert,
|
|
76
|
+
count(*) FILTER (WHERE state = 'waiting')::int AS waiting,
|
|
77
|
+
count(*) FILTER (WHERE state = 'working')::int AS working,
|
|
78
|
+
count(*) FILTER (WHERE state = 'idle')::int AS idle
|
|
79
|
+
FROM (
|
|
80
|
+
SELECT ${harbourStateOfShip(Prisma.sql `oa.ship_id IS NOT NULL`)} AS state
|
|
81
|
+
FROM ships s
|
|
82
|
+
LEFT JOIN leases l ON l.fleet_id = s.fleet_id AND l.ship_id = s.id AND l.ended_at IS NULL
|
|
83
|
+
-- The fleet's open alerts read once, each ship with one at most once, not a lookup per ship and per count.
|
|
84
|
+
LEFT JOIN (SELECT DISTINCT a.ship_id FROM alerts a WHERE a.fleet_id = ${fleetId} AND a.cleared_at IS NULL) oa ON oa.ship_id = s.id
|
|
85
|
+
WHERE ${isShownOfShip(fleetId, reach)}
|
|
86
|
+
) shown`;
|
|
87
|
+
}
|
|
88
|
+
const harbourRowSchema = z.object({
|
|
89
|
+
id: idSchema('ship'),
|
|
90
|
+
name: z.string(),
|
|
91
|
+
kind: shipKindSchema,
|
|
92
|
+
report_note: z.string().nullable(),
|
|
93
|
+
state: z.enum(HARBOUR_STATES),
|
|
94
|
+
last_activity_at: z.date().nullable(),
|
|
95
|
+
});
|
|
96
|
+
const countsRowSchema = z.object({ ships: z.int(), alert: z.int(), waiting: z.int(), working: z.int(), idle: z.int() });
|
|
97
|
+
function harbourShipOfRow(row) {
|
|
98
|
+
const { id, name, kind, report_note, state, last_activity_at } = harbourRowSchema.parse(row);
|
|
99
|
+
return { ship: { id, name, kind, state, note: report_note, lastActivityAt: last_activity_at }, position: shipPositionOfRow(row) };
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Reads Harbour's ships, and on a page from the start its numbers, in one
|
|
103
|
+
* repeatable-read transaction so the numbers count the ships the page
|
|
104
|
+
* shows; inside a unit of work, in its transaction.
|
|
105
|
+
*/
|
|
106
|
+
export async function readHarbourOf(db, { fleetId, read }) {
|
|
107
|
+
const isFromStart = !('shipIds' in read) && read.after === undefined;
|
|
108
|
+
if (!isFromStart) {
|
|
109
|
+
const rows = await db.$queryRaw(harbourPageStatement(fleetId, read));
|
|
110
|
+
return { ships: rows.map(harbourShipOfRow), counts: null };
|
|
111
|
+
}
|
|
112
|
+
const readBoth = async (tx) => [
|
|
113
|
+
await tx.$queryRaw(harbourPageStatement(fleetId, read)),
|
|
114
|
+
await tx.$queryRaw(harbourCountsStatement(fleetId, read.reach)),
|
|
115
|
+
];
|
|
116
|
+
const [rows, [counts]] = '$transaction' in db ? await db.$transaction(readBoth, { isolationLevel: 'RepeatableRead' }) : await readBoth(db);
|
|
117
|
+
return { ships: rows.map(harbourShipOfRow), counts: countsRowSchema.parse(counts) };
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=harbour.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"harbour.js","sourceRoot":"","sources":["../../../src/adapters/prisma/harbour.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,cAAc,EAAmC,MAAM,sBAAsB,CAAC;AAC7G,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE/F;;;;GAIG;AACH,SAAS,kBAAkB,CAAC,YAAwB;IAClD,OAAO,MAAM,CAAC,GAAG,CAAA;;aAEN,YAAY;;;;QAIjB,CAAC;AACT,CAAC;AAED,0HAA0H;AAC1H,MAAM,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAA,6GAA6G,CAAC;AAEnJ,yDAAyD;AACzD,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC;AAEnE,wHAAwH;AACxH,SAAS,aAAa,CAAC,OAAgB,EAAE,KAAgB;IACvD,8EAA8E;IAC9E,OAAO,MAAM,CAAC,GAAG,CAAA,gBAAgB,OAAO,uEAAuE,CAAC,GAAG,WAAW,CAAC,sBAAsB,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;AAChL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,OAAgB,EAAE,IAAiB;IACtE,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AAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAC7D,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,EAAE,EAAM,EAAmC,EAAE,CAAC;QAClE,MAAM,EAAE,CAAC,SAAS,CAAY,oBAAoB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAClE,MAAM,EAAE,CAAC,SAAS,CAAY,sBAAsB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;KAC3E,CAAC;IACF,MAAM,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,CAAC,GAAG,cAAc,IAAI,EAAE,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,YAAY,CAAC,QAAQ,EAAE,EAAE,cAAc,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,QAAQ,CAAC,EAAE,CAAC,CAAC;IAC3I,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,gBAAgB,CAAC,EAAE,MAAM,EAAE,eAAe,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;AACtF,CAAC"}
|
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Wakes waiting receives from the notices the delivery
|
|
3
|
-
* 0003). Each
|
|
2
|
+
* Wakes waiting receives and open wake streams from the notices the delivery
|
|
3
|
+
* listener hears (ADR 0003). Each watches its ship and its type; a notice of a
|
|
4
4
|
* delivery pending for either wakes it, and it looks in the database again.
|
|
5
5
|
* Held in the server's memory only: a wake-up is a hint, and a receive that
|
|
6
|
-
* misses one still returns within its wait,
|
|
7
|
-
*
|
|
6
|
+
* misses one still returns within its wait, as a stream's crew still looks at
|
|
7
|
+
* its next wake, reopen or safety look, so nothing the guarantee depends on
|
|
8
|
+
* lives here.
|
|
8
9
|
*/
|
|
9
|
-
import type { ReceiverWakeups } from '../../domain/messaging/ports.js';
|
|
10
|
+
import type { ReceiverWakeups, WakeStreams } from '../../domain/messaging/ports.js';
|
|
10
11
|
import type { DeliveryNotice } from '../../domain/shared/notifier.js';
|
|
11
|
-
export interface ReceiverWakeupHub extends ReceiverWakeups {
|
|
12
|
-
/** Wakes the receives watching the ship or the type the delivery is pending for. */
|
|
12
|
+
export interface ReceiverWakeupHub extends ReceiverWakeups, WakeStreams {
|
|
13
|
+
/** Wakes the receives and streams watching the ship or the type the delivery is pending for. */
|
|
13
14
|
deliveryPending: (notice: DeliveryNotice) => void;
|
|
14
15
|
/**
|
|
15
|
-
* Wakes every waiting receive, so each looks again. For when the listener
|
|
16
|
+
* Wakes every waiting receive and every open stream (reason `reconnect`), so each looks again. For when the listener
|
|
16
17
|
* starts listening again: notices sent while it did not listen are gone.
|
|
17
18
|
*/
|
|
18
19
|
wakeAll: () => void;
|
|
19
20
|
/**
|
|
20
21
|
* Ends every wait at once, as if its time had passed, and every later one:
|
|
21
22
|
* for a stopping server, so no receive holds the stop up. Each answers
|
|
22
|
-
* what it would at the end of its wait: no deliveries.
|
|
23
|
+
* what it would at the end of its wait: no deliveries. Every open stream
|
|
24
|
+
* ends too, and no stream opens after.
|
|
23
25
|
*/
|
|
24
26
|
endAll: () => void;
|
|
25
27
|
}
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { measureWait } from '../telemetry/calls.js';
|
|
2
|
+
/** Each notice wakes a stream once (long-poll design, W1): what comes while it is not waiting is kept, never merged. */
|
|
3
|
+
function wakeStream(stream, reason) {
|
|
4
|
+
if (stream.settle) {
|
|
5
|
+
stream.settle(reason);
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
stream.kept.push(reason);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
1
11
|
function shipKey(fleetId, shipId) {
|
|
2
12
|
return `${fleetId} ship ${shipId}`;
|
|
3
13
|
}
|
|
@@ -6,6 +16,7 @@ function typeKey(fleetId, type) {
|
|
|
6
16
|
}
|
|
7
17
|
export function createReceiverWakeups() {
|
|
8
18
|
const watchers = new Map();
|
|
19
|
+
const streams = new Map();
|
|
9
20
|
let isEnded = false;
|
|
10
21
|
const wake = (watcher) => {
|
|
11
22
|
if (watcher.settle) {
|
|
@@ -16,6 +27,7 @@ export function createReceiverWakeups() {
|
|
|
16
27
|
}
|
|
17
28
|
};
|
|
18
29
|
const every = () => [...new Set([...watchers.values()].flatMap((set) => [...set]))];
|
|
30
|
+
const everyStream = () => [...new Set([...streams.values()].flatMap((set) => [...set]))];
|
|
19
31
|
return {
|
|
20
32
|
watch: (address) => {
|
|
21
33
|
const watcher = { isWoken: false };
|
|
@@ -34,7 +46,7 @@ export function createReceiverWakeups() {
|
|
|
34
46
|
watcher.isWoken = false;
|
|
35
47
|
return Promise.resolve('woken');
|
|
36
48
|
}
|
|
37
|
-
return new Promise((resolve) => {
|
|
49
|
+
return measureWait(() => new Promise((resolve) => {
|
|
38
50
|
const timer = setTimeout(() => {
|
|
39
51
|
watcher.settle?.('timedOut');
|
|
40
52
|
}, waitMs);
|
|
@@ -43,7 +55,7 @@ export function createReceiverWakeups() {
|
|
|
43
55
|
watcher.settle = undefined;
|
|
44
56
|
resolve(outcome);
|
|
45
57
|
};
|
|
46
|
-
});
|
|
58
|
+
}));
|
|
47
59
|
},
|
|
48
60
|
stop: () => {
|
|
49
61
|
watcher.settle?.('timedOut');
|
|
@@ -57,22 +69,79 @@ export function createReceiverWakeups() {
|
|
|
57
69
|
},
|
|
58
70
|
};
|
|
59
71
|
},
|
|
72
|
+
open: (address) => {
|
|
73
|
+
if (isEnded) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const stream = { kept: [] };
|
|
77
|
+
const keys = [shipKey(address.fleetId, address.shipId), typeKey(address.fleetId, address.type)];
|
|
78
|
+
for (const key of keys) {
|
|
79
|
+
const set = streams.get(key) ?? new Set();
|
|
80
|
+
set.add(stream);
|
|
81
|
+
streams.set(key, set);
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
next: (waitMs, signal) => {
|
|
85
|
+
if (isEnded || signal.aborted) {
|
|
86
|
+
return Promise.resolve('ended');
|
|
87
|
+
}
|
|
88
|
+
const kept = stream.kept.shift();
|
|
89
|
+
if (kept !== undefined) {
|
|
90
|
+
return Promise.resolve(kept);
|
|
91
|
+
}
|
|
92
|
+
return new Promise((resolve) => {
|
|
93
|
+
const onAbort = () => {
|
|
94
|
+
stream.settle?.('ended');
|
|
95
|
+
};
|
|
96
|
+
const timer = setTimeout(() => {
|
|
97
|
+
stream.settle?.('timedOut');
|
|
98
|
+
}, waitMs);
|
|
99
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
100
|
+
stream.settle = (outcome) => {
|
|
101
|
+
clearTimeout(timer);
|
|
102
|
+
signal.removeEventListener('abort', onAbort);
|
|
103
|
+
stream.settle = undefined;
|
|
104
|
+
resolve(outcome);
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
stop: () => {
|
|
109
|
+
stream.settle?.('ended');
|
|
110
|
+
for (const key of keys) {
|
|
111
|
+
const set = streams.get(key);
|
|
112
|
+
set?.delete(stream);
|
|
113
|
+
if (set?.size === 0) {
|
|
114
|
+
streams.delete(key);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
},
|
|
60
120
|
deliveryPending: ({ fleetId, recipient }) => {
|
|
61
121
|
const key = recipient.kind === 'ship' ? shipKey(fleetId, recipient.shipId) : typeKey(fleetId, recipient.type);
|
|
62
122
|
for (const watcher of watchers.get(key) ?? []) {
|
|
63
123
|
wake(watcher);
|
|
64
124
|
}
|
|
125
|
+
for (const stream of streams.get(key) ?? []) {
|
|
126
|
+
wakeStream(stream, 'notice');
|
|
127
|
+
}
|
|
65
128
|
},
|
|
66
129
|
wakeAll: () => {
|
|
67
130
|
for (const watcher of every()) {
|
|
68
131
|
wake(watcher);
|
|
69
132
|
}
|
|
133
|
+
for (const stream of everyStream()) {
|
|
134
|
+
wakeStream(stream, 'reconnect');
|
|
135
|
+
}
|
|
70
136
|
},
|
|
71
137
|
endAll: () => {
|
|
72
138
|
isEnded = true;
|
|
73
139
|
for (const watcher of every()) {
|
|
74
140
|
watcher.settle?.('timedOut');
|
|
75
141
|
}
|
|
142
|
+
for (const stream of everyStream()) {
|
|
143
|
+
stream.settle?.('ended');
|
|
144
|
+
}
|
|
76
145
|
},
|
|
77
146
|
};
|
|
78
147
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"receiver-wakeups.js","sourceRoot":"","sources":["../../../src/adapters/prisma/receiver-wakeups.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"receiver-wakeups.js","sourceRoot":"","sources":["../../../src/adapters/prisma/receiver-wakeups.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAkCpD,wHAAwH;AACxH,SAAS,UAAU,CAAC,MAAc,EAAE,MAA8B;IAChE,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;QAClB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;SAAM,CAAC;QACN,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CAAC,OAAe,EAAE,MAAc;IAC9C,OAAO,GAAG,OAAO,SAAS,MAAM,EAAE,CAAC;AACrC,CAAC;AAED,SAAS,OAAO,CAAC,OAAe,EAAE,IAAY;IAC5C,OAAO,GAAG,OAAO,SAAS,IAAI,EAAE,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,qBAAqB;IACnC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAwB,CAAC;IACjD,MAAM,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAC;IAC/C,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,MAAM,IAAI,GAAG,CAAC,OAAgB,EAAE,EAAE;QAChC,IAAI,OAAO,CAAC,MAAM,EAAE,CAAC;YACnB,OAAO,CAAC,MAAM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CAAC;QACH,CAAC;QACD,OAAO,EAAE,GAAG,EAAE;YACZ,KAAK,MAAM,OAAO,IAAI,KAAK,EAAE,EAAE,CAAC;gBAC9B,IAAI,CAAC,OAAO,CAAC,CAAC;YAChB,CAAC;YACD,KAAK,MAAM,MAAM,IAAI,WAAW,EAAE,EAAE,CAAC;gBACnC,UAAU,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;QACD,MAAM,EAAE,GAAG,EAAE;YACX,OAAO,GAAG,IAAI,CAAC;YACf,KAAK,MAAM,OAAO,IAAI,KAAK,EAAE,EAAE,CAAC;gBAC9B,OAAO,CAAC,MAAM,EAAE,CAAC,UAAU,CAAC,CAAC;YAC/B,CAAC;YACD,KAAK,MAAM,MAAM,IAAI,WAAW,EAAE,EAAE,CAAC;gBACnC,MAAM,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC;YAC3B,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { type FleetId, type ShipStatus } from '@aeolus-fleet/common';
|
|
1
|
+
import { type FleetId, type ShipPosition, type ShipStatus } from '@aeolus-fleet/common';
|
|
2
2
|
import type { ClearRequestRepository, CrewRequestRepository, FleetListing, ShipPage, LabelRepository, FleetRepository, InFlightDeliveries, LeaseRepository, ShipRepository } from '../../domain/registry/ports.js';
|
|
3
3
|
import type { ShipFilter } from '@aeolus-fleet/common/rules';
|
|
4
|
+
import { type ShipSort } from '../../domain/registry/ship-order.js';
|
|
4
5
|
import type { ReadReach } from '../../domain/registry/read-reach.js';
|
|
5
6
|
import type { Db } from './client.js';
|
|
6
7
|
import { Prisma } from './generated/client.js';
|
|
@@ -11,6 +12,8 @@ export declare function createPrismaLabelRepository(db: Db): LabelRepository;
|
|
|
11
12
|
export declare function createPrismaShipRepository(db: Db): ShipRepository;
|
|
12
13
|
export declare function createPrismaLeaseRepository(db: Db): LeaseRepository;
|
|
13
14
|
export declare function createPrismaInFlightDeliveries(db: Db): InFlightDeliveries;
|
|
15
|
+
/** Whether the caller reads the ship `s` of the outer query (decision 0034). */
|
|
16
|
+
export declare function isInReachOfShip(reach: ReadReach): Prisma.Sql;
|
|
14
17
|
/**
|
|
15
18
|
* The filter (common/src/rules/ship-filter.ts) as conditions on the ship `s`
|
|
16
19
|
* of the outer query, each starting with AND: one statement selects the page
|
|
@@ -18,6 +21,13 @@ export declare function createPrismaInFlightDeliveries(db: Db): InFlightDeliveri
|
|
|
18
21
|
* is never selected: nothing lists a remnant (decision 0070, D4).
|
|
19
22
|
*/
|
|
20
23
|
export declare function shipFilterCondition({ query, name, status, type, shipKind, crewRequest, assignedTo, needsCrew, valueIds, shipIds, kind, pluginShipIds }: ShipFilter, reach: ReadReach): Prisma.Sql;
|
|
24
|
+
/**
|
|
25
|
+
* The ships after the position, in the order of the terms. Terms all
|
|
26
|
+
* ascending compare as one row, which a listing index seeks to; with a
|
|
27
|
+
* descending first term, its bound comes first, so the index seeks to it.
|
|
28
|
+
*/
|
|
29
|
+
export declare function afterPosition(position: ShipPosition | undefined, sort: ShipSort | undefined): Prisma.Sql;
|
|
30
|
+
export declare function orderOfPage(alias: string, sort: ShipSort | undefined): Prisma.Sql;
|
|
21
31
|
/**
|
|
22
32
|
* The listing's one statement: a page of the fleet's ships with what the
|
|
23
33
|
* snapshot shows of each (decision 4 of the performance design). The ships
|
|
@@ -28,6 +38,8 @@ export declare function shipFilterCondition({ query, name, status, type, shipKin
|
|
|
28
38
|
* indexes), so neither join multiplies rows.
|
|
29
39
|
*/
|
|
30
40
|
export declare function fleetListingStatement(fleetId: FleetId, { after, limit, filter, sort, reach }: ShipPage): Prisma.Sql;
|
|
41
|
+
/** The ship's place in the page's order, as the listing statement read it. */
|
|
42
|
+
export declare function shipPositionOfRow(row: unknown): ShipPosition;
|
|
31
43
|
/**
|
|
32
44
|
* The types of the fleet's ships in `reach` of any of the statuses, each
|
|
33
45
|
* once: for active ships alone, read from the type index without a ship row.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PING_CONTENT_TYPE, idSchema, scopeNameSchema } from '@aeolus-fleet/common';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { COLUMNS_SHOWN_OUTSIDE_READ_REACH, isValueDescending } from '../../domain/registry/ship-order.js';
|
|
4
|
+
import { readHarbourOf } from './harbour.js';
|
|
4
5
|
import { Prisma } from './generated/client.js';
|
|
5
6
|
import { commissionerColumns, toAbandonedDelivery, toClearRequest, toCrewRequest, toDeliveryFromSql, toFleet, toLastSeen, toLabelFromSql, toLease, toListedLabel, toLeaseShipId, toShip, toShipFacts, toShipFromSql, toShipLabel, toShipReport, toStatedStatement, toTurn, } from './rows.js';
|
|
6
7
|
import { limitSettingsOf } from './fleet-limit-settings.js';
|
|
@@ -684,7 +685,7 @@ const statusOfShip = Prisma.sql `
|
|
|
684
685
|
ELSE 'awaitingCrew'
|
|
685
686
|
END`;
|
|
686
687
|
/** Whether the caller reads the ship `s` of the outer query (decision 0034). */
|
|
687
|
-
function isInReachOfShip(reach) {
|
|
688
|
+
export function isInReachOfShip(reach) {
|
|
688
689
|
return reach.kind === 'every' ? Prisma.sql `true` : Prisma.sql `s.id = ANY(${[...reach.shipIds]}::text[])`;
|
|
689
690
|
}
|
|
690
691
|
/**
|
|
@@ -841,7 +842,7 @@ function positionValueOf(position, { column }) {
|
|
|
841
842
|
* ascending compare as one row, which a listing index seeks to; with a
|
|
842
843
|
* descending first term, its bound comes first, so the index seeks to it.
|
|
843
844
|
*/
|
|
844
|
-
function afterPosition(position, sort) {
|
|
845
|
+
export function afterPosition(position, sort) {
|
|
845
846
|
if (position === undefined) {
|
|
846
847
|
return Prisma.empty;
|
|
847
848
|
}
|
|
@@ -866,7 +867,7 @@ function beyond(terms, position) {
|
|
|
866
867
|
const after = term.isDescending ? Prisma.sql `${ranked} < ${value}` : Prisma.sql `${ranked} > ${value}`;
|
|
867
868
|
return rest.length === 0 ? after : Prisma.sql `${after} OR (${ranked} = ${value} AND (${beyond(rest, position)}))`;
|
|
868
869
|
}
|
|
869
|
-
function orderOfPage(alias, sort) {
|
|
870
|
+
export function orderOfPage(alias, sort) {
|
|
870
871
|
const terms = orderTermsOf(sort).map((term) => Prisma.sql `${termOf(alias, term)} ${term.isDescending ? Prisma.sql `DESC` : Prisma.sql `ASC`}`);
|
|
871
872
|
return Prisma.sql `ORDER BY ${Prisma.join(terms)}`;
|
|
872
873
|
}
|
|
@@ -927,7 +928,7 @@ export function fleetListingStatement(fleetId, { after, limit, filter, sort, rea
|
|
|
927
928
|
}
|
|
928
929
|
const shipPositionRowSchema = z.object({ id: idSchema('ship'), sort_is_empty: z.boolean(), sort_value: z.string(), sort_name: z.string() });
|
|
929
930
|
/** The ship's place in the page's order, as the listing statement read it. */
|
|
930
|
-
function shipPositionOfRow(row) {
|
|
931
|
+
export function shipPositionOfRow(row) {
|
|
931
932
|
const { id, sort_is_empty, sort_value, sort_name } = shipPositionRowSchema.parse(row);
|
|
932
933
|
return { isEmpty: sort_is_empty, value: sort_value, name: sort_name, id };
|
|
933
934
|
}
|
|
@@ -942,6 +943,7 @@ export function shipTypesStatement(fleetId, { reach, status }) {
|
|
|
942
943
|
}
|
|
943
944
|
export function createPrismaFleetListing(db) {
|
|
944
945
|
return {
|
|
946
|
+
harbour: (fleetId, read) => readHarbourOf(db, { fleetId, read }),
|
|
945
947
|
ships: async (fleetId, page) => {
|
|
946
948
|
const rows = await db.$queryRaw(fleetListingStatement(fleetId, page));
|
|
947
949
|
return rows.map((row) => ({ facts: toShipFacts(row), position: shipPositionOfRow(row) }));
|