@yaks/journal 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +522 -2
- package/_dist/backed.d.ts +9 -0
- package/_dist/backed.d.ts.map +1 -0
- package/_dist/batch.d.ts +45 -0
- package/_dist/batch.d.ts.map +1 -0
- package/_dist/feed.d.ts +108 -0
- package/_dist/feed.d.ts.map +1 -0
- package/_dist/graph.d.ts +104 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/kept.d.ts +63 -0
- package/_dist/kept.d.ts.map +1 -0
- package/_dist/log.d.ts +142 -0
- package/_dist/log.d.ts.map +1 -0
- package/_dist/merge.d.ts +41 -0
- package/_dist/merge.d.ts.map +1 -0
- package/_dist/mod.d.ts +101 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/service.d.ts +68 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/tools.d.ts +5 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/undo.d.ts +48 -0
- package/_dist/undo.d.ts.map +1 -0
- package/_dist/value.d.ts +6 -0
- package/_dist/value.d.ts.map +1 -0
- package/_dist/views.d.ts +25 -0
- package/_dist/views.d.ts.map +1 -0
- package/_dist/vocab.d.ts +7 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/backed.js +225 -0
- package/backed.js.map +1 -0
- package/backed.ts +274 -0
- package/batch.js +12 -0
- package/batch.js.map +1 -0
- package/batch.ts +84 -0
- package/deno.json +1 -0
- package/feed.js +295 -0
- package/feed.js.map +1 -0
- package/feed.ts +368 -0
- package/graph.js +224 -0
- package/graph.js.map +1 -0
- package/graph.ts +329 -0
- package/held.js +25 -0
- package/held.js.map +1 -0
- package/held.ts +24 -0
- package/kept.js +61 -0
- package/kept.js.map +1 -0
- package/kept.ts +100 -0
- package/log.js +1161 -0
- package/log.js.map +1 -0
- package/log.ts +1299 -0
- package/merge.js +158 -0
- package/merge.js.map +1 -0
- package/merge.ts +185 -0
- package/mod.js +101 -0
- package/mod.js.map +1 -0
- package/mod.ts +102 -0
- package/names.js +206 -0
- package/names.js.map +1 -0
- package/names.ts +230 -0
- package/package.json +52 -4
- package/page.js +160 -0
- package/page.js.map +1 -0
- package/page.ts +233 -0
- package/service.js +118 -0
- package/service.js.map +1 -0
- package/service.ts +148 -0
- package/tools.js +110 -0
- package/tools.js.map +1 -0
- package/tools.ts +113 -0
- package/undo.js +187 -0
- package/undo.js.map +1 -0
- package/undo.ts +190 -0
- package/value.js +26 -0
- package/value.js.map +1 -0
- package/value.ts +35 -0
- package/views.js +85 -0
- package/views.js.map +1 -0
- package/views.ts +91 -0
- package/vocab.js +20 -0
- package/vocab.js.map +1 -0
- package/vocab.json +308 -0
- package/vocab.ts +22 -0
package/graph.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// `@yaks/journal/graph` — what a server composed from a config file imports to
|
|
2
|
+
// switch the journal on. Its schema is the journal's append-only tables and
|
|
3
|
+
// the feed's short-lived one, which a writing host adds to its store; its
|
|
4
|
+
// plugins write a row per component every transaction touched that the
|
|
5
|
+
// vocabulary keeps as history, and a row per commit to the feed.
|
|
6
|
+
//
|
|
7
|
+
// `logFor` and `feedFor` are here rather than inside `plugins` because
|
|
8
|
+
// `@yaks/journal/tools` reads the same tables this plugin writes, and the feed
|
|
9
|
+
// below follows its own: one host, one log and one feed, bound in one place.
|
|
10
|
+
// Both write as the same host, which is what lets the feed leave out what this
|
|
11
|
+
// host wrote itself.
|
|
12
|
+
|
|
13
|
+
import type { Bundle, News, Plugin } from 'jsr:@yaks/graph@0.2.36'
|
|
14
|
+
import {
|
|
15
|
+
as,
|
|
16
|
+
col,
|
|
17
|
+
type Derived,
|
|
18
|
+
fn,
|
|
19
|
+
type History,
|
|
20
|
+
NOW,
|
|
21
|
+
type Row,
|
|
22
|
+
select,
|
|
23
|
+
type Statements,
|
|
24
|
+
statements,
|
|
25
|
+
type Stmt,
|
|
26
|
+
table,
|
|
27
|
+
} from '@yaks/sql'
|
|
28
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
29
|
+
import {
|
|
30
|
+
ddl,
|
|
31
|
+
journal,
|
|
32
|
+
type Log,
|
|
33
|
+
log,
|
|
34
|
+
type Recording,
|
|
35
|
+
type Rows,
|
|
36
|
+
} from './mod.js'
|
|
37
|
+
import {
|
|
38
|
+
type Feed,
|
|
39
|
+
feedDdl,
|
|
40
|
+
feeder,
|
|
41
|
+
feedOf,
|
|
42
|
+
filled,
|
|
43
|
+
follow,
|
|
44
|
+
type Read,
|
|
45
|
+
type Wrote,
|
|
46
|
+
} from './feed.js'
|
|
47
|
+
|
|
48
|
+
let logs = new WeakMap<object, Log>()
|
|
49
|
+
let feeds = new WeakMap<object, Feed>()
|
|
50
|
+
|
|
51
|
+
/** What a log is bound to: the host's storage, its vocabulary, which names
|
|
52
|
+
* what the log's rows record (./log.ts `LogOpts.vocab`), and the read
|
|
53
|
+
* overrides its store was opened with, so a value the store keeps in another
|
|
54
|
+
* form reads back the way the graph reads it (./log.ts `LogOpts.derived`). */
|
|
55
|
+
export type Bound = {
|
|
56
|
+
storage: { statements?: Statements }
|
|
57
|
+
vocab: Vocab
|
|
58
|
+
derived?: Derived
|
|
59
|
+
reader?: {
|
|
60
|
+
statements?: { query: (statement: Stmt) => Row[] | Promise<Row[]> }
|
|
61
|
+
storage?: { get: Read }
|
|
62
|
+
tool?: (name: string, call: Bundle) => Promise<Bundle[]>
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let rowsOf = (host: Bound): Rows => {
|
|
67
|
+
let sql = statements(host.storage)
|
|
68
|
+
return (s: Stmt) => sql.query(s)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The log bound to a host: the three tables, read and written over that
|
|
72
|
+
* host's storage transaction, as that host. The same host gets the same log. */
|
|
73
|
+
export let logFor = (host: Bound): Log => {
|
|
74
|
+
let found = logs.get(host)
|
|
75
|
+
if (!found) {
|
|
76
|
+
logs.set(
|
|
77
|
+
host,
|
|
78
|
+
found = log({
|
|
79
|
+
rows: rowsOf(host),
|
|
80
|
+
vocab: host.vocab,
|
|
81
|
+
derived: host.derived,
|
|
82
|
+
}),
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
return found
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The feed bound to a host, written as the host its log writes as. The same
|
|
89
|
+
* host gets the same feed. */
|
|
90
|
+
export let feedFor = (host: Bound): Feed => {
|
|
91
|
+
let found = feeds.get(host)
|
|
92
|
+
if (!found) {
|
|
93
|
+
let rows = rowsOf(host)
|
|
94
|
+
let read = host.reader?.statements
|
|
95
|
+
feeds.set(
|
|
96
|
+
host,
|
|
97
|
+
found = feedOf({
|
|
98
|
+
rows,
|
|
99
|
+
read: read && ((s) => read.query(s)),
|
|
100
|
+
host: logFor(host).host,
|
|
101
|
+
}),
|
|
102
|
+
)
|
|
103
|
+
}
|
|
104
|
+
return found
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The journal's tables and the feed's, and the words of the host's
|
|
108
|
+
* vocabulary (./names.ts), which a writing host adds with the rest of its
|
|
109
|
+
* composed schema; a store an older journal made gains the columns it
|
|
110
|
+
* predates there (@yaks/sqlite `grow`). */
|
|
111
|
+
export let schema = (host: { vocab: Vocab }): Stmt[] => [
|
|
112
|
+
...ddl(host.vocab),
|
|
113
|
+
...feedDdl(),
|
|
114
|
+
]
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Which aspects of history a config switches on (D-130964), each covering the
|
|
118
|
+
* components not declared `journal: false`:
|
|
119
|
+
*
|
|
120
|
+
* - `attribution`: a `journal_change` row per component each transaction
|
|
121
|
+
* wrote or removed, under its `journal_tx` row (when, by whom, through what).
|
|
122
|
+
* - `versions`: the store keeps every version a write replaces (@yaks/sqlite
|
|
123
|
+
* `History`), each naming its transaction.
|
|
124
|
+
*
|
|
125
|
+
* Naming either turns history to them, each on unless set false:
|
|
126
|
+
* `{"use": "@yaks/journal", "with": {"versions": true}}`. Naming neither is
|
|
127
|
+
* the journal before versions, attribution with every value as a
|
|
128
|
+
* `journal_field` row, until the box's journal moves into versions.
|
|
129
|
+
* `{"with": {"components": ["space", "member"]}}` keeps the history of those
|
|
130
|
+
* components alone. `{"with": {"feed": false}}` writes no feed, for a store
|
|
131
|
+
* no other host writes to (a Durable Object is its store's only host).
|
|
132
|
+
*/
|
|
133
|
+
export type Aspects = {
|
|
134
|
+
attribution: boolean
|
|
135
|
+
versions: boolean
|
|
136
|
+
values: boolean
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export let aspects = (options: Record<string, unknown> = {}): Aspects =>
|
|
140
|
+
'attribution' in options || 'versions' in options
|
|
141
|
+
? {
|
|
142
|
+
attribution: options.attribution !== false,
|
|
143
|
+
versions: options.versions !== false,
|
|
144
|
+
values: false,
|
|
145
|
+
}
|
|
146
|
+
: { attribution: true, versions: false, values: true }
|
|
147
|
+
|
|
148
|
+
/** What a store keeps versions with, where `versions` is on: the statement
|
|
149
|
+
* that takes a transaction's id, as a `journal_tx` row whose writer the
|
|
150
|
+
* journal fills in as it records the transaction, and the newest one's. */
|
|
151
|
+
export let history = (options?: Record<string, unknown>): History | undefined =>
|
|
152
|
+
aspects(options).versions
|
|
153
|
+
? {
|
|
154
|
+
clock: {
|
|
155
|
+
t: 'insert',
|
|
156
|
+
into: 'journal_tx',
|
|
157
|
+
cols: ['ts'],
|
|
158
|
+
rows: [[NOW]],
|
|
159
|
+
returning: [col('id')],
|
|
160
|
+
},
|
|
161
|
+
tip: select({
|
|
162
|
+
cols: [as(fn('max', col('id')), 'id')],
|
|
163
|
+
from: table('journal_tx'),
|
|
164
|
+
}),
|
|
165
|
+
}
|
|
166
|
+
: undefined
|
|
167
|
+
|
|
168
|
+
/** A store keeping versions requires the journal from then on: a writer
|
|
169
|
+
* without it would change live rows and keep nothing they replaced. */
|
|
170
|
+
export let required = (options?: Record<string, unknown>): boolean =>
|
|
171
|
+
aspects(options).versions
|
|
172
|
+
|
|
173
|
+
/** Bind the hooks over the tables the installer prepared: the journal,
|
|
174
|
+
* recording what the host's vocabulary keeps as history, as its aspects say,
|
|
175
|
+
* and the feed, carrying every commit to the other hosts. */
|
|
176
|
+
export let plugins = (
|
|
177
|
+
host: Bound,
|
|
178
|
+
options?: Record<string, unknown>,
|
|
179
|
+
): Plugin[] => {
|
|
180
|
+
let a = aspects(options)
|
|
181
|
+
// A branch's commits are recorded as its own, and reach no other host: the
|
|
182
|
+
// feed carries main's.
|
|
183
|
+
let mode = host.storage.statements?.mode
|
|
184
|
+
let record: Recording = {
|
|
185
|
+
changes: a.attribution,
|
|
186
|
+
values: a.values,
|
|
187
|
+
...mode && 'branch' in mode ? { branch: mode.branch } : {},
|
|
188
|
+
}
|
|
189
|
+
let components = Array.isArray(options?.components)
|
|
190
|
+
? options.components.map(String)
|
|
191
|
+
: undefined
|
|
192
|
+
return [
|
|
193
|
+
journal(logFor(host), host.vocab, { record, components }),
|
|
194
|
+
...mode || options?.feed === false ? [] : [feeder(feedFor(host))],
|
|
195
|
+
]
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** How often a host looks for what other hosts committed, in ms, unless the
|
|
199
|
+
* config says (`{"use": "@yaks/journal", "with": {"every": 50}}`). */
|
|
200
|
+
export let EVERY = 200
|
|
201
|
+
|
|
202
|
+
/** What a feed hands what it heard to, all of it at once; it may take its
|
|
203
|
+
* time, and what is heard meanwhile waits for the next call. */
|
|
204
|
+
export type Each = (news: News) => void | Promise<void>
|
|
205
|
+
|
|
206
|
+
/** How many times news the consumer failed on is offered again before it is
|
|
207
|
+
* reported and passed over, so news that can never be taken does not hold
|
|
208
|
+
* back everything after it; the consumer is then told it missed some. */
|
|
209
|
+
export let TRIES = 5
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The commits other hosts made to this store, handed to `each` as bundles of
|
|
213
|
+
* what each wrote, its values read from the graph as they stand (./feed.ts
|
|
214
|
+
* `filled`), oldest first, from the moment `each` is given until the
|
|
215
|
+
* returned function is called or the host stops. What a subscription registry
|
|
216
|
+
* or a cache is fed so it also sees writes its own graph did not make: a `yak`
|
|
217
|
+
* command's beside `yak serve`, or a worker's serving the effects role.
|
|
218
|
+
* Whatever goes wrong on the way is the host's to report.
|
|
219
|
+
*
|
|
220
|
+
* The feed is read every `every` ms however long the consumer takes, so a
|
|
221
|
+
* slow consumer is handed more at once rather than left behind what the store
|
|
222
|
+
* keeps (./feed.ts `KEEP`). What still never reaches it (a process asleep past
|
|
223
|
+
* that, news passed over) comes with `missed`, and the consumer reads again
|
|
224
|
+
* what it holds: it ends up correct on its own.
|
|
225
|
+
*
|
|
226
|
+
* A look reads through the host's reader, and the consumer may take its time:
|
|
227
|
+
* stopping cancels the next look, and the host's release (`defer`) waits out
|
|
228
|
+
* the one under way, so no read outlives the plugins into a closed reader.
|
|
229
|
+
* A host whose options say `feed: false` is offered none.
|
|
230
|
+
*/
|
|
231
|
+
export let feed = (
|
|
232
|
+
host: Bound & {
|
|
233
|
+
storage: { get: Read }
|
|
234
|
+
stopping: AbortSignal
|
|
235
|
+
report?: (error: unknown) => void | Promise<void>
|
|
236
|
+
defer?: (release: () => void | Promise<void>) => void
|
|
237
|
+
},
|
|
238
|
+
options: { every?: number; timer?: Timer; feed?: boolean } = {},
|
|
239
|
+
): ((each: Each) => () => void) | undefined =>
|
|
240
|
+
options.feed === false ? undefined : (each: Each): () => void => {
|
|
241
|
+
let every = options.every ?? EVERY
|
|
242
|
+
let timer = options.timer ?? later
|
|
243
|
+
let told = (error: unknown) =>
|
|
244
|
+
host.report
|
|
245
|
+
? void host.report(error)
|
|
246
|
+
: console.error('@yaks/journal feed —', error)
|
|
247
|
+
let missed = false
|
|
248
|
+
let next = follow(feedFor(host), {
|
|
249
|
+
missed: (n) => {
|
|
250
|
+
missed = true
|
|
251
|
+
told(new Error(`the feed pruned ${n} commits before they were read`))
|
|
252
|
+
},
|
|
253
|
+
})
|
|
254
|
+
let heard: Wrote[][] = []
|
|
255
|
+
let tries = 0
|
|
256
|
+
let taking: Promise<void> | undefined
|
|
257
|
+
let cancel = () => {}
|
|
258
|
+
let stopped = false
|
|
259
|
+
// One call at a time, so the consumer is never handed a transaction out of
|
|
260
|
+
// order. News it fails on (a store busy for a moment) is offered again at
|
|
261
|
+
// the next look, ahead of everything heard after it.
|
|
262
|
+
let take = async () => {
|
|
263
|
+
while ((heard.length || missed) && !stopped) {
|
|
264
|
+
let [said, was] = [heard, missed]
|
|
265
|
+
heard = []
|
|
266
|
+
missed = false
|
|
267
|
+
try {
|
|
268
|
+
let applied = await filled(
|
|
269
|
+
said,
|
|
270
|
+
(e, c) =>
|
|
271
|
+
host.reader?.storage
|
|
272
|
+
? host.reader.storage.get(e, c)
|
|
273
|
+
: host.storage.get(e, c),
|
|
274
|
+
)
|
|
275
|
+
await each({ applied, missed: was })
|
|
276
|
+
tries = 0
|
|
277
|
+
} catch (err) {
|
|
278
|
+
if (++tries < TRIES) {
|
|
279
|
+
heard = [...said, ...heard]
|
|
280
|
+
missed ||= was
|
|
281
|
+
break
|
|
282
|
+
}
|
|
283
|
+
tries = 0
|
|
284
|
+
missed = true
|
|
285
|
+
told(new Error('the feed passed over what it heard', { cause: err }))
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
// A read that fails is tried again at the next look, since the cursor moves
|
|
290
|
+
// only past what was read.
|
|
291
|
+
let looking: Promise<unknown> | undefined
|
|
292
|
+
let look = () => looking = looked()
|
|
293
|
+
let looked = async () => {
|
|
294
|
+
try {
|
|
295
|
+
heard.push(...(await next()).map((h) => h.wrote))
|
|
296
|
+
} catch (err) {
|
|
297
|
+
told(err)
|
|
298
|
+
}
|
|
299
|
+
let started = !taking && (heard.length || missed)
|
|
300
|
+
? taking = take().finally(() => taking = undefined)
|
|
301
|
+
: undefined
|
|
302
|
+
if (!stopped) cancel = timer(look, every)
|
|
303
|
+
return started
|
|
304
|
+
}
|
|
305
|
+
let stop = () => {
|
|
306
|
+
stopped = true
|
|
307
|
+
cancel()
|
|
308
|
+
}
|
|
309
|
+
host.defer?.(async () => {
|
|
310
|
+
stop()
|
|
311
|
+
await looking
|
|
312
|
+
await taking
|
|
313
|
+
})
|
|
314
|
+
if (host.stopping.aborted) return stop
|
|
315
|
+
host.stopping.addEventListener('abort', stop, { once: true })
|
|
316
|
+
cancel = timer(look, every)
|
|
317
|
+
return stop
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** What a feed schedules its next look with, and how it cancels it. */
|
|
321
|
+
export type Timer = (
|
|
322
|
+
fn: () => void | Promise<void>,
|
|
323
|
+
ms: number,
|
|
324
|
+
) => () => void
|
|
325
|
+
|
|
326
|
+
let later: Timer = (fn, ms) => {
|
|
327
|
+
let id = setTimeout(fn, ms)
|
|
328
|
+
return () => clearTimeout(id)
|
|
329
|
+
}
|
package/held.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Count retained changes by component on the covering index. The daily file
|
|
2
|
+
// measurement can run this scan through its asynchronous statement reader.
|
|
3
|
+
import { as, col, fn, lit, select, table } from "@yaks/sql";
|
|
4
|
+
import { names } from "./names.js";
|
|
5
|
+
export let heldSql = ()=>select({
|
|
6
|
+
cols: [
|
|
7
|
+
as(col('component'), 'c'),
|
|
8
|
+
as(fn('count', lit('*')), 'n')
|
|
9
|
+
],
|
|
10
|
+
from: table('journal_change'),
|
|
11
|
+
group: [
|
|
12
|
+
col('component')
|
|
13
|
+
]
|
|
14
|
+
});
|
|
15
|
+
/** The counts by each component's label, where rows recorded it by label or
|
|
16
|
+
* by identity (./names.ts). */ export let heldRows = (rows, vocab)=>{
|
|
17
|
+
let { compLabel } = names(vocab);
|
|
18
|
+
let held = {};
|
|
19
|
+
for (let r of rows){
|
|
20
|
+
let comp = compLabel(String(r.c));
|
|
21
|
+
held[comp] = (held[comp] ?? 0) + Number(r.n ?? 0);
|
|
22
|
+
}
|
|
23
|
+
return held;
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=held.js.map
|
package/held.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"held.js","sources":["./held.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,2EAA2E;AAC3E,SAAS,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAY,MAAM,EAAa,KAAK,yBAA8B;AAE3F,SAAS,KAAK,qBAAoB;AAElC,OAAO,IAAI,UAAU,IACnB,OAAO;IACL,MAAM;MAAC,GAAG,IAAI,cAAc;MAAM,GAAG,GAAG,SAAS,IAAI,OAAO;KAAK;IACjE,MAAM,MAAM;IACZ,OAAO;MAAC,IAAI;KAAa;EAC3B,GAAE;AAEJ;6BAC6B,GAC7B,OAAO,IAAI,WAAW,CAAC,MAAa;EAClC,IAAI,EAAE,SAAS,EAAE,GAAG,MAAM;EAC1B,IAAI,OAA+B,CAAC;EACpC,KAAK,IAAI,KAAK,KAAM;IAClB,IAAI,OAAO,UAAU,OAAO,EAAE,CAAC;IAC/B,IAAI,CAAC,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI,CAAC,IAAI,OAAO,EAAE,CAAC,IAAI;EACjD;EACA,OAAO;AACT,EAAC"}
|
package/held.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Count retained changes by component on the covering index. The daily file
|
|
2
|
+
// measurement can run this scan through its asynchronous statement reader.
|
|
3
|
+
import { as, col, fn, lit, type Row, select, type Stmt, table } from '@yaks/sql'
|
|
4
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
5
|
+
import { names } from './names.js'
|
|
6
|
+
|
|
7
|
+
export let heldSql = (): Stmt =>
|
|
8
|
+
select({
|
|
9
|
+
cols: [as(col('component'), 'c'), as(fn('count', lit('*')), 'n')],
|
|
10
|
+
from: table('journal_change'),
|
|
11
|
+
group: [col('component')],
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
/** The counts by each component's label, where rows recorded it by label or
|
|
15
|
+
* by identity (./names.ts). */
|
|
16
|
+
export let heldRows = (rows: Row[], vocab: Vocab): Record<string, number> => {
|
|
17
|
+
let { compLabel } = names(vocab)
|
|
18
|
+
let held: Record<string, number> = {}
|
|
19
|
+
for (let r of rows) {
|
|
20
|
+
let comp = compLabel(String(r.c))
|
|
21
|
+
held[comp] = (held[comp] ?? 0) + Number(r.n ?? 0)
|
|
22
|
+
}
|
|
23
|
+
return held
|
|
24
|
+
}
|
package/kept.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a day's `kept` row says (./service.ts), as the History page reads it:
|
|
3
|
+
* which tables are history, what history kept, how many changes it held, and
|
|
4
|
+
* what it kept per change and per day between two measurements. Pure.
|
|
5
|
+
*
|
|
6
|
+
* History is the journal's tables (`journal_*`) and each component's
|
|
7
|
+
* versions, which the store keeps beside the component's table as
|
|
8
|
+
* `<comp>$versions`.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/ /** The table the journal keeps a row in per change. */ export let CHANGES = 'journal_change';
|
|
12
|
+
let BESIDE = '$versions';
|
|
13
|
+
/** A bundle's `kept`, if it has one. */ export let keptOf = (b)=>b.kept;
|
|
14
|
+
/**
|
|
15
|
+
* Whether a table is history's, and the component whose versions it holds.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { historic, versioned } from './kept.ts'
|
|
19
|
+
* import { equal } from '@yaks/testing'
|
|
20
|
+
*
|
|
21
|
+
* equal([historic('journal_field'), historic('doc$versions'), historic('doc')], [true, true, false])
|
|
22
|
+
* equal([versioned('doc$versions'), versioned('doc')], ['doc', undefined])
|
|
23
|
+
* ```
|
|
24
|
+
*/ export let historic = (table)=>table.startsWith('journal_') || !!versioned(table);
|
|
25
|
+
/** The component a `<comp>$versions` table keeps the versions of. */ export let versioned = (table)=>table.endsWith(BESIDE) ? table.slice(0, -BESIDE.length) : undefined;
|
|
26
|
+
/**
|
|
27
|
+
* The bytes history kept, and the changes the journal held.
|
|
28
|
+
*
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { changes, history, type Kept } from './kept.ts'
|
|
31
|
+
* import { equal } from '@yaks/testing'
|
|
32
|
+
*
|
|
33
|
+
* let k: Kept = { day: '2026-10-21', at: '', bytes: 100, free: 0, tables: { doc: [10, 2], journal_change: [30, 3], doc$versions: [20, 4] } }
|
|
34
|
+
* equal([history(k), changes(k)], [50, 3])
|
|
35
|
+
* ```
|
|
36
|
+
*/ export let history = (k)=>Object.entries(k.tables).reduce((n, [t, [bytes]])=>historic(t) ? n + bytes : n, 0);
|
|
37
|
+
/** The changes the journal held. */ export let changes = (k)=>k.tables[CHANGES]?.[1] ?? 0;
|
|
38
|
+
/**
|
|
39
|
+
* Between an older measurement and a newer one: the bytes history kept per
|
|
40
|
+
* change written between them, and `of`'s growth a day. Undefined where
|
|
41
|
+
* nothing was written between them.
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { history, type Kept, perChange, perDay } from './kept.ts'
|
|
45
|
+
* import { equal } from '@yaks/testing'
|
|
46
|
+
*
|
|
47
|
+
* let a: Kept = { day: '2026-10-20', at: '2026-10-20T04:00:00Z', bytes: 1000, free: 0, tables: { journal_change: [100, 10] } }
|
|
48
|
+
* let b: Kept = { day: '2026-10-22', at: '2026-10-22T04:00:00Z', bytes: 3000, free: 0, tables: { journal_change: [1570, 20] } }
|
|
49
|
+
* equal(perChange(a, b), 147)
|
|
50
|
+
* equal(perDay(a, b, history), 735)
|
|
51
|
+
* equal(perChange(a, a), undefined)
|
|
52
|
+
* ```
|
|
53
|
+
*/ export let perChange = (older, newer)=>{
|
|
54
|
+
let n = changes(newer) - changes(older);
|
|
55
|
+
return n > 0 ? (history(newer) - history(older)) / n : undefined;
|
|
56
|
+
};
|
|
57
|
+
/** `of`'s growth a day, between an older measurement and a newer one. */ export let perDay = (older, newer, of)=>{
|
|
58
|
+
let days = (Date.parse(newer.at) - Date.parse(older.at)) / 864e5;
|
|
59
|
+
return days > 0 ? (of(newer) - of(older)) / days : undefined;
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=kept.js.map
|
package/kept.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kept.js","sources":["./kept.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GAcD,qDAAqD,GACrD,OAAO,IAAI,UAAU,iBAAgB;AAErC,IAAI,SAAS;AAEb,sCAAsC,GACtC,OAAO,IAAI,SAAS,CAAC,IAAgC,EAAE,IAAI,CAAoB;AAE/E;;;;;;;;;;CAUC,GACD,OAAO,IAAI,WAAW,CAAC,QACrB,MAAM,UAAU,CAAC,eAAe,CAAC,CAAC,UAAU,OAAM;AAEpD,mEAAmE,GACnE,OAAO,IAAI,YAAY,CAAC,QACtB,MAAM,QAAQ,CAAC,UAAU,MAAM,KAAK,CAAC,GAAG,CAAC,OAAO,MAAM,IAAI,UAAS;AAErE;;;;;;;;;;CAUC,GACD,OAAO,IAAI,UAAU,CAAC,IACpB,OAAO,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,CAC7B,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,GAAK,SAAS,KAAK,IAAI,QAAQ,GAC/C,GACD;AAEH,kCAAkC,GAClC,OAAO,IAAI,UAAU,CAAC,IAAoB,EAAE,MAAM,CAAC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAC;AAErE;;;;;;;;;;;;;;;CAeC,GACD,OAAO,IAAI,YAAY,CAAC,OAAa;EACnC,IAAI,IAAI,QAAQ,SAAS,QAAQ;EACjC,OAAO,IAAI,IAAI,CAAC,QAAQ,SAAS,QAAQ,MAAM,IAAI,IAAI;AACzD,EAAC;AAED,uEAAuE,GACvE,OAAO,IAAI,SAAS,CAClB,OACA,OACA;EAEA,IAAI,OAAO,CAAC,KAAK,KAAK,CAAC,MAAM,EAAE,IAAI,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI;EAC3D,OAAO,OAAO,IAAI,CAAC,GAAG,SAAS,GAAG,MAAM,IAAI,OAAO;AACrD,EAAC"}
|
package/kept.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a day's `kept` row says (./service.ts), as the History page reads it:
|
|
3
|
+
* which tables are history, what history kept, how many changes it held, and
|
|
4
|
+
* what it kept per change and per day between two measurements. Pure.
|
|
5
|
+
*
|
|
6
|
+
* History is the journal's tables (`journal_*`) and each component's
|
|
7
|
+
* versions, which the store keeps beside the component's table as
|
|
8
|
+
* `<comp>$versions`.
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
|
|
13
|
+
|
|
14
|
+
/** One day's measurement. */
|
|
15
|
+
export type Kept = {
|
|
16
|
+
day: string
|
|
17
|
+
at: string
|
|
18
|
+
bytes: number
|
|
19
|
+
free: number
|
|
20
|
+
tables: Record<string, [bytes: number, rows: number]>
|
|
21
|
+
/** the changes the journal held, by component name */
|
|
22
|
+
changes?: Record<string, number>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The table the journal keeps a row in per change. */
|
|
26
|
+
export let CHANGES = 'journal_change'
|
|
27
|
+
|
|
28
|
+
let BESIDE = '$versions'
|
|
29
|
+
|
|
30
|
+
/** A bundle's `kept`, if it has one. */
|
|
31
|
+
export let keptOf = (b: Bundle): Kept | undefined => b.kept as Kept | undefined
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Whether a table is history's, and the component whose versions it holds.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { historic, versioned } from './kept.ts'
|
|
38
|
+
* import { equal } from '@yaks/testing'
|
|
39
|
+
*
|
|
40
|
+
* equal([historic('journal_field'), historic('doc$versions'), historic('doc')], [true, true, false])
|
|
41
|
+
* equal([versioned('doc$versions'), versioned('doc')], ['doc', undefined])
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export let historic = (table: string): boolean =>
|
|
45
|
+
table.startsWith('journal_') || !!versioned(table)
|
|
46
|
+
|
|
47
|
+
/** The component a `<comp>$versions` table keeps the versions of. */
|
|
48
|
+
export let versioned = (table: string): string | undefined =>
|
|
49
|
+
table.endsWith(BESIDE) ? table.slice(0, -BESIDE.length) : undefined
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The bytes history kept, and the changes the journal held.
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { changes, history, type Kept } from './kept.ts'
|
|
56
|
+
* import { equal } from '@yaks/testing'
|
|
57
|
+
*
|
|
58
|
+
* let k: Kept = { day: '2026-10-21', at: '', bytes: 100, free: 0, tables: { doc: [10, 2], journal_change: [30, 3], doc$versions: [20, 4] } }
|
|
59
|
+
* equal([history(k), changes(k)], [50, 3])
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export let history = (k: Kept): number =>
|
|
63
|
+
Object.entries(k.tables).reduce(
|
|
64
|
+
(n, [t, [bytes]]) => historic(t) ? n + bytes : n,
|
|
65
|
+
0,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
/** The changes the journal held. */
|
|
69
|
+
export let changes = (k: Kept): number => k.tables[CHANGES]?.[1] ?? 0
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Between an older measurement and a newer one: the bytes history kept per
|
|
73
|
+
* change written between them, and `of`'s growth a day. Undefined where
|
|
74
|
+
* nothing was written between them.
|
|
75
|
+
*
|
|
76
|
+
* ```ts
|
|
77
|
+
* import { history, type Kept, perChange, perDay } from './kept.ts'
|
|
78
|
+
* import { equal } from '@yaks/testing'
|
|
79
|
+
*
|
|
80
|
+
* let a: Kept = { day: '2026-10-20', at: '2026-10-20T04:00:00Z', bytes: 1000, free: 0, tables: { journal_change: [100, 10] } }
|
|
81
|
+
* let b: Kept = { day: '2026-10-22', at: '2026-10-22T04:00:00Z', bytes: 3000, free: 0, tables: { journal_change: [1570, 20] } }
|
|
82
|
+
* equal(perChange(a, b), 147)
|
|
83
|
+
* equal(perDay(a, b, history), 735)
|
|
84
|
+
* equal(perChange(a, a), undefined)
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export let perChange = (older: Kept, newer: Kept): number | undefined => {
|
|
88
|
+
let n = changes(newer) - changes(older)
|
|
89
|
+
return n > 0 ? (history(newer) - history(older)) / n : undefined
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `of`'s growth a day, between an older measurement and a newer one. */
|
|
93
|
+
export let perDay = (
|
|
94
|
+
older: Kept,
|
|
95
|
+
newer: Kept,
|
|
96
|
+
of: (k: Kept) => number,
|
|
97
|
+
): number | undefined => {
|
|
98
|
+
let days = (Date.parse(newer.at) - Date.parse(older.at)) / 864e5
|
|
99
|
+
return days > 0 ? (of(newer) - of(older)) / days : undefined
|
|
100
|
+
}
|