signalk-chiplog 2.7.0 → 2.9.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.
Files changed (50) hide show
  1. package/CHANGELOG.md +74 -1
  2. package/README.md +152 -45
  3. package/docs/screenshots/05-stats.png +0 -0
  4. package/docs/screenshots/06-3d-animation.png +0 -0
  5. package/index.js +241 -4
  6. package/lib/api.js +123 -2
  7. package/lib/cloud-pairing.js +172 -0
  8. package/lib/cloud-restore.js +416 -0
  9. package/lib/cloud-sync.js +455 -0
  10. package/lib/database.js +50 -1
  11. package/lib/detection.js +4 -0
  12. package/lib/events.js +84 -20
  13. package/lib/export.js +1 -1
  14. package/lib/mail.js +162 -0
  15. package/lib/passage-map.js +342 -0
  16. package/lib/passage-summary.js +169 -0
  17. package/lib/places.js +6 -0
  18. package/lib/png.js +295 -0
  19. package/lib/qr.js +266 -0
  20. package/lib/smtp.js +242 -0
  21. package/lib/summary-mailer.js +286 -0
  22. package/package.json +2 -1
  23. package/public/app.css +94 -0
  24. package/public/entry/entry.css +94 -1
  25. package/public/entry/icons/apple-touch-icon.png +0 -0
  26. package/public/entry/icons/icon-192.png +0 -0
  27. package/public/entry/icons/icon-512.png +0 -0
  28. package/public/entry/index.html +2 -2
  29. package/public/entry/js/components/Dialogs.mjs +82 -16
  30. package/public/entry/js/components/Icons.mjs +33 -0
  31. package/public/entry/js/components/NotePanel.mjs +21 -3
  32. package/public/entry/js/components/RecentList.mjs +2 -3
  33. package/public/entry/js/journal.mjs +24 -7
  34. package/public/entry/js/main.mjs +27 -18
  35. package/public/entry/manifest.webmanifest +1 -1
  36. package/public/entry/sw.js +2 -0
  37. package/public/icon-small.svg +37 -0
  38. package/public/icon.svg +82 -6
  39. package/public/index.html +1 -1
  40. package/public/js/components/CloudBackup.mjs +298 -0
  41. package/public/js/components/ExportView.mjs +3 -0
  42. package/public/js/components/LogView.mjs +63 -6
  43. package/public/js/components/PassageView.mjs +4 -3
  44. package/public/js/components/Timeline.mjs +40 -14
  45. package/public/js/context.mjs +22 -3
  46. package/public/js/format.mjs +34 -0
  47. package/public/js/i18n.mjs +107 -2
  48. package/public/js/log-lines.mjs +5 -0
  49. package/public/js/main.mjs +1 -1
  50. package/public/js/view-memory.mjs +39 -0
@@ -0,0 +1,455 @@
1
+ const crypto = require('node:crypto');
2
+ const zlib = require('node:zlib');
3
+ const { listEntryCrew } = require('./crew');
4
+ const { toEntry } = require('./entries');
5
+ const { allEvents } = require('./events');
6
+ const { passageFingerprint } = require('./export');
7
+ const { allPlaces } = require('./places');
8
+ const { allSegments } = require('./propulsion');
9
+ const { getTideForecast } = require('./tide-forecaster');
10
+ const { allObservations, allTrackPoints } = require('./track');
11
+ const { getWeatherForecast } = require('./weather-forecaster');
12
+
13
+ const CLOUD_SYNC_DEFAULTS = {
14
+ cloudSyncEnabled: false,
15
+ cloudSyncIntervalMinutes: 15,
16
+ cloudSyncCostlyLink: false
17
+ };
18
+
19
+ // The exchange format the service is sent, in step with its SUPPORTED_FORMAT_VERSIONS.
20
+ const FORMAT_VERSION = 1;
21
+ // About 300 kB of JSON before compression: a chunk lost to a dropped link is cheap to resend.
22
+ const TRACK_CHUNK_POINTS = 2000;
23
+ // On a costly link (SPEC §4.17), a passage goes as a light copy: one track point per ten
24
+ // minutes, in small chunks so that a passage under way only sends its last few points again.
25
+ const LIGHT_POINT_EVERY_MS = 10 * 60 * 1000;
26
+ const LIGHT_CHUNK_POINTS = 100;
27
+ // Each run costs a connection and the service's list of what it holds: on a costly link the
28
+ // backup runs at departures and arrivals, and otherwise only this often.
29
+ const COSTLY_INTERVAL_MS = 6 * 60 * 60 * 1000;
30
+ const REQUEST_TIMEOUT_MS = 2 * 60 * 1000;
31
+ const IDLE_MS = 15 * 60 * 1000;
32
+ const FIRST_RETRY_MS = 60 * 1000;
33
+ const MAX_RETRY_MS = 60 * 60 * 1000;
34
+
35
+ class CloudSyncError extends Error {
36
+ constructor(message, { status = null, code = null } = {}) {
37
+ super(message);
38
+ this.status = status;
39
+ this.code = code;
40
+ }
41
+ }
42
+
43
+ // Objects with their keys in order, at every depth.
44
+ function sorted(value) {
45
+ if (Array.isArray(value)) {
46
+ return value.map(sorted);
47
+ }
48
+ if (value !== null && typeof value === 'object') {
49
+ return Object.fromEntries(
50
+ Object.keys(value)
51
+ .sort()
52
+ .map((key) => [key, sorted(value[key])])
53
+ );
54
+ }
55
+ return value;
56
+ }
57
+
58
+ // SHA-256 of the value's JSON with every object's keys sorted: the service computes the same
59
+ // over what it receives. Sorted, so that a passage hashes the same once it has been through
60
+ // the service and back -- its database keeps no key order -- and a logbook restored from it
61
+ // (lib/cloud-restore.js) is not sent again whole.
62
+ const jsonHash = (value) =>
63
+ crypto
64
+ .createHash('sha256')
65
+ .update(JSON.stringify(sorted(value)))
66
+ .digest('hex');
67
+
68
+ function getLogbookId(db) {
69
+ return db.prepare('SELECT id FROM logbook_identity').get().id;
70
+ }
71
+
72
+ // Whether an earlier logbook is still being read back from the service (lib/cloud-restore.js).
73
+ function isRestoring(db) {
74
+ return db.prepare('SELECT restoring FROM logbook_identity').get().restoring === 1;
75
+ }
76
+
77
+ // Everything the service keeps of a passage but its track, which goes in chunks.
78
+ function passageContent(db, row) {
79
+ return {
80
+ entry: toEntry(row),
81
+ events: allEvents(db, row.id),
82
+ observations: allObservations(db, row.id),
83
+ propulsion: allSegments(db, row.id),
84
+ crew: listEntryCrew(db, row.id),
85
+ tide: getTideForecast(db, row.id),
86
+ weather: getWeatherForecast(db, row.id)
87
+ };
88
+ }
89
+
90
+ function trackChunks(points, size = TRACK_CHUNK_POINTS) {
91
+ const chunks = [];
92
+ for (let start = 0; start < points.length; start += size) {
93
+ chunks.push(points.slice(start, start + size));
94
+ }
95
+ return chunks;
96
+ }
97
+
98
+ // The passage in a few kilobytes: what happened and who was aboard, without the instrument
99
+ // snapshots, the forecasts and the strokes of handwritten notes, which wait for a cheap link.
100
+ function lightContent(db, row) {
101
+ return {
102
+ entry: toEntry(row),
103
+ events: allEvents(db, row.id).map((event) =>
104
+ event.type === 'handwritten_annotation' ? { ...event, payload: null } : event
105
+ ),
106
+ observations: [],
107
+ propulsion: allSegments(db, row.id),
108
+ crew: listEntryCrew(db, row.id),
109
+ tide: null,
110
+ weather: null
111
+ };
112
+ }
113
+
114
+ // The track thinned out: the first point of every ten minutes of the clock, and the last one,
115
+ // with position and speed only. Cut on the clock rather than evenly so that a track that grew
116
+ // keeps the points it already had -- only its last chunk changes.
117
+ function lightTrack(points) {
118
+ const kept = [];
119
+ let slot = null;
120
+ points.forEach((point, index) => {
121
+ const current = Math.floor(Date.parse(point.time) / LIGHT_POINT_EVERY_MS);
122
+ if (current !== slot || index === points.length - 1) {
123
+ slot = current;
124
+ kept.push({ time: point.time, lat: point.lat, lon: point.lon, sog: point.sog });
125
+ }
126
+ });
127
+ return kept;
128
+ }
129
+
130
+ function configurationProblem(settings) {
131
+ if (!settings.cloudSyncUrl) {
132
+ return 'no service address';
133
+ }
134
+ if (!settings.cloudSyncToken) {
135
+ return 'no device token';
136
+ }
137
+ return null;
138
+ }
139
+
140
+ // Pushes the logbook to the online service (SPEC §4.17): the passages new or changed since
141
+ // the service last heard of them, newest first, and the deletion of those gone from board.
142
+ // The service's sync state is the record of what it holds, so nothing is stored here: a
143
+ // restart only costs working the hashes out again. Shaped like the forecasters: a
144
+ // `resolveNext()` run by its own background schedule.
145
+ function createCloudSync({
146
+ db,
147
+ settings,
148
+ userAgent,
149
+ log,
150
+ fetch = globalThis.fetch,
151
+ clock = Date.now
152
+ }) {
153
+ // Per passage, what its hashes were last worked out from, so an unchanged passage is not
154
+ // read again in full every run.
155
+ const cache = new Map();
156
+ let failures = 0;
157
+ let stopped = false;
158
+ let lastSuccess = null;
159
+ let lastError = null;
160
+ let progress = null;
161
+ // Set by a backup asked for by hand: the next run sends everything in full, costly link or not.
162
+ let fullRequested = false;
163
+
164
+ const iso = (ms) => new Date(ms).toISOString();
165
+ const base = () => `${settings.cloudSyncUrl.replace(/\/+$/, '')}/v1/logbooks/${getLogbookId(db)}`;
166
+
167
+ async function call(method, path, body, extraHeaders = {}) {
168
+ const headers = {
169
+ authorization: `Bearer ${settings.cloudSyncToken}`,
170
+ 'user-agent': userAgent,
171
+ 'x-chiplog-version': userAgent.split('/')[1] ?? '',
172
+ ...extraHeaders
173
+ };
174
+ let payload;
175
+ if (body !== undefined) {
176
+ headers['content-type'] = 'application/json';
177
+ headers['content-encoding'] = 'gzip';
178
+ payload = zlib.gzipSync(JSON.stringify(body));
179
+ }
180
+ const response = await fetch(`${base()}${path}`, {
181
+ method,
182
+ headers,
183
+ body: payload,
184
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS)
185
+ });
186
+ if (!response.ok) {
187
+ let detail = {};
188
+ try {
189
+ detail = await response.json();
190
+ } catch {
191
+ // Not every failure comes from the service itself: a proxy, a captive portal.
192
+ }
193
+ throw new CloudSyncError(
194
+ `${method} ${path} answered ${response.status}${detail.message ? `: ${detail.message}` : ''}`,
195
+ { status: response.status, code: detail.error ?? null }
196
+ );
197
+ }
198
+ return response.status === 204 ? null : response.json();
199
+ }
200
+
201
+ function localPassage(row) {
202
+ const fingerprint = passageFingerprint(db, row, {});
203
+ const cached = cache.get(row.id);
204
+ if (cached?.fingerprint === fingerprint) {
205
+ return cached;
206
+ }
207
+ const content = passageContent(db, row);
208
+ const chunks = trackChunks(allTrackPoints(db, row.id));
209
+ const chunkHashes = chunks.map(jsonHash);
210
+ const local = {
211
+ fingerprint,
212
+ contentHash: jsonHash(content),
213
+ trackHash: jsonHash(chunkHashes),
214
+ chunkHashes,
215
+ pointCount: chunks.reduce((sum, chunk) => sum + chunk.length, 0)
216
+ };
217
+ cache.set(row.id, local);
218
+ return local;
219
+ }
220
+
221
+ // The hashes of the passage's light copy, worked out when first needed and kept with the
222
+ // others for as long as the passage does not change.
223
+ function lightOf(row, local) {
224
+ if (!local.light) {
225
+ const chunkHashes = trackChunks(
226
+ lightTrack(allTrackPoints(db, row.id)),
227
+ LIGHT_CHUNK_POINTS
228
+ ).map(jsonHash);
229
+ local.light = {
230
+ contentHash: jsonHash(lightContent(db, row)),
231
+ trackHash: jsonHash(chunkHashes)
232
+ };
233
+ }
234
+ return local.light;
235
+ }
236
+
237
+ async function upload(row, light) {
238
+ // Read again rather than kept: a long track is only held in memory while it is sent.
239
+ const content = light ? lightContent(db, row) : passageContent(db, row);
240
+ const points = allTrackPoints(db, row.id);
241
+ const chunks = light
242
+ ? trackChunks(lightTrack(points), LIGHT_CHUNK_POINTS)
243
+ : trackChunks(points);
244
+ const { missingChunks } = await call('PUT', `/passages/${row.id}`, {
245
+ formatVersion: FORMAT_VERSION,
246
+ contentHash: jsonHash(content),
247
+ passage: content,
248
+ track: {
249
+ pointCount: chunks.reduce((sum, chunk) => sum + chunk.length, 0),
250
+ chunkHashes: chunks.map(jsonHash)
251
+ },
252
+ ...(light ? { light: true } : {})
253
+ });
254
+ for (const index of missingChunks) {
255
+ if (stopped) {
256
+ return;
257
+ }
258
+ const points = chunks[index];
259
+ await call('PUT', `/passages/${row.id}/track/${index}`, { hash: jsonHash(points), points });
260
+ }
261
+ }
262
+
263
+ async function synchronise(costly) {
264
+ const rows = db.prepare('SELECT * FROM log_entries ORDER BY start_time DESC, id DESC').all();
265
+ // How many passages are on board: the service tells its owner how far the backup is.
266
+ const state = await call('GET', '/sync-state', undefined, {
267
+ 'x-chiplog-passages': String(rows.length)
268
+ });
269
+ const remote = state.passages;
270
+ const held = new Map(remote.map((item) => [item.originId, item]));
271
+ const onBoard = new Set(rows.map((row) => row.id));
272
+ for (const id of cache.keys()) {
273
+ if (!onBoard.has(id)) {
274
+ cache.delete(id);
275
+ }
276
+ }
277
+
278
+ // What to send, and how: in full, or as a light copy on a costly link. `waiting` counts the
279
+ // passages the service does not hold in full yet, for the page to say so.
280
+ const due = [];
281
+ let waiting = 0;
282
+ for (const row of rows) {
283
+ const local = localPassage(row);
284
+ const remoteItem = held.get(row.id);
285
+ const current =
286
+ remoteItem &&
287
+ remoteItem.complete &&
288
+ remoteItem.contentHash === local.contentHash &&
289
+ remoteItem.trackHash === local.trackHash;
290
+ if (current) {
291
+ continue;
292
+ }
293
+ if (!costly) {
294
+ due.push([row, false]);
295
+ continue;
296
+ }
297
+ waiting += 1;
298
+ // A full copy the service already holds, though out of date, is worth more than a light
299
+ // one up to date: it stays until the full passage can replace it.
300
+ if (remoteItem && !remoteItem.light) {
301
+ continue;
302
+ }
303
+ const light = lightOf(row, local);
304
+ const lightCurrent =
305
+ remoteItem &&
306
+ remoteItem.complete &&
307
+ remoteItem.contentHash === light.contentHash &&
308
+ remoteItem.trackHash === light.trackHash;
309
+ if (!lightCurrent) {
310
+ due.push([row, true]);
311
+ }
312
+ }
313
+ const gone = remote.filter((item) => !onBoard.has(item.originId));
314
+
315
+ progress = { total: due.length + gone.length, done: 0 };
316
+ let sent = 0;
317
+ let deleted = 0;
318
+ let sentLight = 0;
319
+ for (const [row, light] of due) {
320
+ if (stopped) {
321
+ return null;
322
+ }
323
+ await upload(row, light);
324
+ sent += 1;
325
+ sentLight += light ? 1 : 0;
326
+ progress.done += 1;
327
+ }
328
+ for (const item of gone) {
329
+ if (stopped) {
330
+ return null;
331
+ }
332
+ await call('DELETE', `/passages/${item.originId}`);
333
+ deleted += 1;
334
+ progress.done += 1;
335
+ }
336
+
337
+ // The places -- names, positions and countries -- go whole, when the list differs from
338
+ // the one the service holds: there are few of them, and a place renamed, deleted or given
339
+ // its country then needs no message of its own. An older service says nothing of places
340
+ // and is sent none; an empty list is not worth sending to a service that has none either.
341
+ let places = false;
342
+ // Not on a costly link: the list goes whole each time it changes, and can wait.
343
+ if ('placesHash' in state && !costly) {
344
+ const list = allPlaces(db);
345
+ const hash = jsonHash(list);
346
+ if (hash !== state.placesHash && (list.length > 0 || state.placesHash !== null)) {
347
+ if (stopped) {
348
+ return null;
349
+ }
350
+ await call('PUT', '/places', { hash, places: list });
351
+ places = true;
352
+ }
353
+ }
354
+ return { sent, light: sentLight, waiting, deleted, held: rows.length, places };
355
+ }
356
+
357
+ return {
358
+ async resolveNext() {
359
+ if (!settings.cloudSyncEnabled || configurationProblem(settings)) {
360
+ return { outcome: 'disabled', retryInMs: IDLE_MS };
361
+ }
362
+ // A logbook half restored lacks passages the service holds: sending now would report
363
+ // them deleted on board.
364
+ if (isRestoring(db)) {
365
+ return { outcome: 'restoring', retryInMs: FIRST_RETRY_MS };
366
+ }
367
+ // A run asked for by hand is a full one, whatever the link: asking is saying it is worth it.
368
+ const costly = Boolean(settings.cloudSyncCostlyLink) && !fullRequested;
369
+ const intervalMs = Math.max(
370
+ settings.cloudSyncIntervalMinutes * 60 * 1000,
371
+ settings.cloudSyncCostlyLink ? COSTLY_INTERVAL_MS : 0
372
+ );
373
+ let result;
374
+ try {
375
+ result = await synchronise(costly);
376
+ } catch (error) {
377
+ progress = null;
378
+ if (stopped) {
379
+ return { outcome: 'stopped' };
380
+ }
381
+ failures += 1;
382
+ lastError = { at: iso(clock()), message: error.message, code: error.code ?? null };
383
+ // A token the service will keep refusing is worth a line in the log, not a retry a
384
+ // minute: it only changes when someone pastes a new one.
385
+ if (error.status === 401 || error.status === 403) {
386
+ log('error', `Online backup refused: ${error.message}`);
387
+ return { outcome: 'refused', retryInMs: MAX_RETRY_MS };
388
+ }
389
+ return {
390
+ outcome: 'failed',
391
+ error,
392
+ retryInMs: Math.min(FIRST_RETRY_MS * 2 ** (failures - 1), MAX_RETRY_MS)
393
+ };
394
+ }
395
+ progress = null;
396
+ if (stopped || result === null) {
397
+ return { outcome: 'stopped' };
398
+ }
399
+ failures = 0;
400
+ fullRequested = false;
401
+ lastSuccess = { at: iso(clock()), ...result };
402
+ lastError = null;
403
+ const changed = result.sent > 0 || result.deleted > 0 || result.places;
404
+ if (changed) {
405
+ log(
406
+ 'info',
407
+ `Online backup: ${result.sent} passage(s) sent${result.light ? ` (${result.light} as light copies)` : ''}, ${result.deleted} deleted${result.places ? ', places sent' : ''}`
408
+ );
409
+ }
410
+ return {
411
+ outcome: changed ? 'sent' : 'idle',
412
+ retryInMs: intervalMs
413
+ };
414
+ },
415
+
416
+ status() {
417
+ const problem = configurationProblem(settings);
418
+ return {
419
+ enabled: Boolean(settings.cloudSyncEnabled),
420
+ configured: problem === null,
421
+ problem,
422
+ url: settings.cloudSyncUrl || null,
423
+ costlyLink: Boolean(settings.cloudSyncCostlyLink),
424
+ logbookId: getLogbookId(db),
425
+ inProgress: progress,
426
+ lastSuccess,
427
+ lastError
428
+ };
429
+ },
430
+
431
+ // The next run sends every passage in full, even on a costly link.
432
+ requestFull() {
433
+ fullRequested = true;
434
+ },
435
+
436
+ stop() {
437
+ stopped = true;
438
+ }
439
+ };
440
+ }
441
+
442
+ module.exports = {
443
+ createCloudSync,
444
+ CLOUD_SYNC_DEFAULTS,
445
+ FORMAT_VERSION,
446
+ TRACK_CHUNK_POINTS,
447
+ LIGHT_CHUNK_POINTS,
448
+ COSTLY_INTERVAL_MS,
449
+ lightContent,
450
+ lightTrack,
451
+ getLogbookId,
452
+ isRestoring,
453
+ jsonHash,
454
+ passageContent
455
+ };
package/lib/database.js CHANGED
@@ -374,7 +374,56 @@ const MIGRATIONS = [
374
374
  `,
375
375
  // Another CHECK-widening rebuild, same reason and same shape as
376
376
  // addPropulsionChangeEventType.
377
- addHeadingChangeEventType
377
+ addHeadingChangeEventType,
378
+ `
379
+ -- The summary email sent once a passage is definitively closed (SPEC
380
+ -- §4.16). One row per passage, written whatever the outcome: the row is what
381
+ -- stops a summary going out twice, and its absence is what makes a passage
382
+ -- due one. sent_at is null when it never went -- the server refused it for
383
+ -- good, or the passage aged out of the window worth mailing -- with the
384
+ -- reason in \`error\`.
385
+ CREATE TABLE passage_summary_mails (
386
+ entry_id INTEGER PRIMARY KEY REFERENCES log_entries (id) ON DELETE CASCADE,
387
+ sent_at TEXT,
388
+ recipients TEXT,
389
+ error TEXT,
390
+ created_at TEXT NOT NULL
391
+ );
392
+
393
+ -- Every passage already on record counts as dealt with: switching the option
394
+ -- on is not a request for the whole logbook by email.
395
+ INSERT INTO passage_summary_mails (entry_id, error, created_at)
396
+ SELECT id, 'recorded before summary emails were available', end_time
397
+ FROM log_entries WHERE state = 'closed';
398
+ `,
399
+ `
400
+ -- The logbook's own identity, for the online backup (SPEC §4.17): the
401
+ -- service files passages under it, so a reinstalled plugin, whose entry ids
402
+ -- start again from 1, can never overwrite or delete the passages an earlier
403
+ -- logbook sent. A random UUID, made once; a copy of this database file is
404
+ -- the same logbook.
405
+ CREATE TABLE logbook_identity (
406
+ id TEXT NOT NULL,
407
+ created_at TEXT NOT NULL
408
+ );
409
+ INSERT INTO logbook_identity (id, created_at) VALUES (
410
+ lower(
411
+ hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' ||
412
+ substr(hex(randomblob(2)), 2) || '-' ||
413
+ substr('89ab', 1 + abs(random()) % 4, 1) || substr(hex(randomblob(2)), 2) || '-' ||
414
+ hex(randomblob(6))
415
+ ),
416
+ strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
417
+ );
418
+ `,
419
+ `
420
+ -- Set while an earlier logbook is being read back from the online service
421
+ -- (SPEC §4.17): this logbook has already taken its identity but not yet all
422
+ -- its passages, so the backup must hold off -- it would otherwise report the
423
+ -- missing ones as deleted on board.
424
+ ALTER TABLE logbook_identity ADD COLUMN restoring INTEGER NOT NULL DEFAULT 0
425
+ CHECK (restoring IN (0, 1));
426
+ `
378
427
  ];
379
428
 
380
429
  function addPropulsionChangeEventType(db, version) {
package/lib/detection.js CHANGED
@@ -299,6 +299,10 @@ function createPassageDetector({ db, readSelfPath, settings, clock = Date.now })
299
299
  end_place_name = NULL, end_place_pending = 0, updated_at = ?
300
300
  WHERE id = ?`
301
301
  ).run(iso(now), entry.id);
302
+ // A cast-off can reopen a passage later than the mail window (SPEC §4.3),
303
+ // so a summary may already have gone out. Forget it: the passage is not
304
+ // over, and the one that closes it is the one worth sending (SPEC §4.16).
305
+ db.prepare('DELETE FROM passage_summary_mails WHERE entry_id = ?').run(entry.id);
302
306
  recordMovement(activeEntry(), now, position, { force: true });
303
307
  }
304
308
 
package/lib/events.js CHANGED
@@ -138,28 +138,44 @@ function allEvents(db, entryId) {
138
138
  .map(toEvent);
139
139
  }
140
140
 
141
- function positionAt(db, time, { now, vesselPosition }) {
142
- if (time === undefined) {
143
- return vesselPosition;
144
- }
141
+ // Where the track says the vessel was at `time`, or null when it recorded
142
+ // nothing close enough. `entryId` narrows the search to one passage, which only
143
+ // an edit can do: a new entry does not know its passage yet.
144
+ function trackPositionAt(db, time, entryId = null) {
145
145
  const target = Date.parse(time);
146
+ const scope = entryId === null ? '' : ' AND entry_id = ?';
147
+ const bounds = entryId === null ? [time] : [time, entryId];
146
148
  const nearest = [
147
149
  db.prepare(
148
- 'SELECT time, lat, lon FROM track_points WHERE time <= ? ORDER BY time DESC LIMIT 1'
150
+ `SELECT time, lat, lon FROM track_points WHERE time <= ?${scope} ORDER BY time DESC LIMIT 1`
149
151
  ),
150
- db.prepare('SELECT time, lat, lon FROM track_points WHERE time >= ? ORDER BY time LIMIT 1')
152
+ db.prepare(
153
+ `SELECT time, lat, lon FROM track_points WHERE time >= ?${scope} ORDER BY time LIMIT 1`
154
+ )
151
155
  ]
152
- .map((statement) => statement.get(time))
156
+ .map((statement) => statement.get(...bounds))
153
157
  .filter(
154
158
  (point) => point && Math.abs(Date.parse(point.time) - target) <= TRACK_POSITION_WINDOW_MS
155
159
  )
156
160
  .sort(
157
161
  (a, b) => Math.abs(Date.parse(a.time) - target) - Math.abs(Date.parse(b.time) - target)
158
162
  )[0];
159
- if (nearest) {
160
- return { lat: nearest.lat, lon: nearest.lon };
163
+ return nearest ? { lat: nearest.lat, lon: nearest.lon } : null;
164
+ }
165
+
166
+ function positionAt(db, time, { now, vesselPosition }) {
167
+ if (time === undefined) {
168
+ return vesselPosition;
169
+ }
170
+ const recorded = trackPositionAt(db, time);
171
+ if (recorded) {
172
+ return recorded;
161
173
  }
162
- return Math.abs(Date.parse(now) - target) <= CURRENT_POSITION_WINDOW_MS ? vesselPosition : null;
174
+ // Still the vessel's own position when the entry was made moments ago — a
175
+ // replay from the tablet's queue, where the track may hold nothing yet.
176
+ return Math.abs(Date.parse(now) - Date.parse(time)) <= CURRENT_POSITION_WINDOW_MS
177
+ ? vesselPosition
178
+ : null;
163
179
  }
164
180
 
165
181
  function findByClientRef(db, clientRef) {
@@ -259,7 +275,39 @@ function logCrewEvent(db, input, context) {
259
275
  });
260
276
  }
261
277
 
262
- function updateEvent(db, id, patch) {
278
+ // A hand-written time has to stay a time the passage could have been logged at,
279
+ // and must not contradict the departure it opened.
280
+ function checkRetiming(db, event, time, now) {
281
+ if (Date.parse(time) > Date.parse(now)) {
282
+ throw badRequest('time cannot be in the future');
283
+ }
284
+ const entry = requireEntryRow(db, event.entryId);
285
+ const until = entry.end_time ?? now;
286
+ if (Date.parse(time) < Date.parse(entry.start_time) || Date.parse(time) > Date.parse(until)) {
287
+ throw badRequest('time must fall within the passage');
288
+ }
289
+ const opens = db.prepare('SELECT 1 FROM log_entries WHERE opened_by_event_id = ?').get(event.id);
290
+ if (opens) {
291
+ throw badRequest('This event opened the passage; change the passage departure time instead');
292
+ }
293
+ }
294
+
295
+ // The instrument snapshot taken when the event was logged (SPEC §4.1) belongs to
296
+ // the moment the button was pressed, not to the moment the event is redated to:
297
+ // it goes, unless another event of the passage still stands at that time.
298
+ function dropEventSnapshot(db, event) {
299
+ const others = db
300
+ .prepare('SELECT 1 FROM events WHERE entry_id = ? AND time = ? AND id <> ?')
301
+ .get(event.entryId, event.time, event.id);
302
+ if (!others) {
303
+ db.prepare("DELETE FROM observations WHERE entry_id = ? AND time = ? AND reason = 'event'").run(
304
+ event.entryId,
305
+ event.time
306
+ );
307
+ }
308
+ }
309
+
310
+ function updateEvent(db, id, patch, context = {}) {
263
311
  const current = toEvent(requireEventRow(db, id));
264
312
 
265
313
  if (!CLIENT_EVENT_TYPES.includes(current.type)) {
@@ -271,16 +319,32 @@ function updateEvent(db, id, patch) {
271
319
 
272
320
  const next = { ...current, ...patch };
273
321
  validateContent(db, next);
322
+ const retimed = next.time !== current.time;
323
+ if (retimed) {
324
+ checkRetiming(db, current, next.time, context.now);
325
+ }
274
326
 
275
- db.prepare('UPDATE events SET time = ?, subtype = ?, comment = ?, payload = ? WHERE id = ?').run(
276
- next.time,
277
- next.subtype,
278
- next.comment,
279
- serializePayload(next.payload),
280
- id
281
- );
282
-
283
- return toEvent(requireEventRow(db, id));
327
+ return withTransaction(db, () => {
328
+ // Redating an event unmoors it from where the crew was when they logged it:
329
+ // its position is the track's at the new time, or none at all.
330
+ const position = retimed ? trackPositionAt(db, next.time, current.entryId) : current.position;
331
+ db.prepare(
332
+ `UPDATE events SET time = ?, subtype = ?, comment = ?, payload = ?, lat = ?, lon = ?
333
+ WHERE id = ?`
334
+ ).run(
335
+ next.time,
336
+ next.subtype,
337
+ next.comment,
338
+ serializePayload(next.payload),
339
+ position ? position.lat : null,
340
+ position ? position.lon : null,
341
+ id
342
+ );
343
+ if (retimed) {
344
+ dropEventSnapshot(db, current);
345
+ }
346
+ return toEvent(requireEventRow(db, id));
347
+ });
284
348
  }
285
349
 
286
350
  // An alarm is logged as two lines — the notification reaching `alarm` or
package/lib/export.js CHANGED
@@ -248,4 +248,4 @@ async function writeUsbExport(db, directory, now, pdf = {}) {
248
248
  };
249
249
  }
250
250
 
251
- module.exports = { EXPORT_FORMATS, renderExport, writeUsbExport };
251
+ module.exports = { EXPORT_FORMATS, renderExport, writeUsbExport, passageFingerprint };