@yaks/tracker 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.
Files changed (198) hide show
  1. package/Histogram.js +71 -0
  2. package/Histogram.js.map +1 -0
  3. package/Histogram.ts +100 -0
  4. package/LICENSE +202 -0
  5. package/README.md +639 -2
  6. package/_dist/brief.d.ts +25 -0
  7. package/_dist/brief.d.ts.map +1 -0
  8. package/_dist/changes.d.ts +9 -0
  9. package/_dist/changes.d.ts.map +1 -0
  10. package/_dist/chart.d.ts +24 -0
  11. package/_dist/chart.d.ts.map +1 -0
  12. package/_dist/digest.d.ts +64 -0
  13. package/_dist/digest.d.ts.map +1 -0
  14. package/_dist/effects.d.ts +73 -0
  15. package/_dist/effects.d.ts.map +1 -0
  16. package/_dist/errors.d.ts +14 -0
  17. package/_dist/errors.d.ts.map +1 -0
  18. package/_dist/everywhere.d.ts +13 -0
  19. package/_dist/everywhere.d.ts.map +1 -0
  20. package/_dist/fault.d.ts +29 -0
  21. package/_dist/fault.d.ts.map +1 -0
  22. package/_dist/file.d.ts +6 -0
  23. package/_dist/file.d.ts.map +1 -0
  24. package/_dist/fixtime.d.ts +20 -0
  25. package/_dist/fixtime.d.ts.map +1 -0
  26. package/_dist/frames.d.ts +6 -0
  27. package/_dist/frames.d.ts.map +1 -0
  28. package/_dist/graph.d.ts +3 -0
  29. package/_dist/graph.d.ts.map +1 -0
  30. package/_dist/group.d.ts +21 -0
  31. package/_dist/group.d.ts.map +1 -0
  32. package/_dist/instrument.d.ts +37 -0
  33. package/_dist/instrument.d.ts.map +1 -0
  34. package/_dist/intake.d.ts +21 -0
  35. package/_dist/intake.d.ts.map +1 -0
  36. package/_dist/merge.d.ts +11 -0
  37. package/_dist/merge.d.ts.map +1 -0
  38. package/_dist/mod.d.ts +7 -0
  39. package/_dist/mod.d.ts.map +1 -0
  40. package/_dist/model.d.ts +20 -0
  41. package/_dist/model.d.ts.map +1 -0
  42. package/_dist/outage.d.ts +7 -0
  43. package/_dist/outage.d.ts.map +1 -0
  44. package/_dist/people.d.ts +3 -0
  45. package/_dist/people.d.ts.map +1 -0
  46. package/_dist/poll.d.ts +101 -0
  47. package/_dist/poll.d.ts.map +1 -0
  48. package/_dist/poll_thread.d.ts +8 -0
  49. package/_dist/poll_thread.d.ts.map +1 -0
  50. package/_dist/reading.d.ts +34 -0
  51. package/_dist/reading.d.ts.map +1 -0
  52. package/_dist/remote.d.ts +61 -0
  53. package/_dist/remote.d.ts.map +1 -0
  54. package/_dist/report.d.ts +324 -0
  55. package/_dist/report.d.ts.map +1 -0
  56. package/_dist/resolution.d.ts +22 -0
  57. package/_dist/resolution.d.ts.map +1 -0
  58. package/_dist/routes.d.ts +33 -0
  59. package/_dist/routes.d.ts.map +1 -0
  60. package/_dist/script.d.ts +8 -0
  61. package/_dist/script.d.ts.map +1 -0
  62. package/_dist/service.d.ts +30 -0
  63. package/_dist/service.d.ts.map +1 -0
  64. package/_dist/sync_health.d.ts +4 -0
  65. package/_dist/sync_health.d.ts.map +1 -0
  66. package/_dist/ticket.d.ts +14 -0
  67. package/_dist/ticket.d.ts.map +1 -0
  68. package/_dist/tools.d.ts +39 -0
  69. package/_dist/tools.d.ts.map +1 -0
  70. package/_dist/trace-intake.d.ts +25 -0
  71. package/_dist/trace-intake.d.ts.map +1 -0
  72. package/_dist/views.d.ts +11 -0
  73. package/_dist/views.d.ts.map +1 -0
  74. package/_dist/vocab.d.ts +9 -0
  75. package/_dist/vocab.d.ts.map +1 -0
  76. package/box.json +51 -0
  77. package/brief.js +86 -0
  78. package/brief.js.map +1 -0
  79. package/brief.ts +120 -0
  80. package/changes.js +57 -0
  81. package/changes.js.map +1 -0
  82. package/changes.ts +70 -0
  83. package/chart.js +186 -0
  84. package/chart.js.map +1 -0
  85. package/chart.ts +193 -0
  86. package/deno.json +1 -0
  87. package/digest.js +329 -0
  88. package/digest.js.map +1 -0
  89. package/digest.ts +406 -0
  90. package/effects.js +136 -0
  91. package/effects.js.map +1 -0
  92. package/effects.ts +178 -0
  93. package/errors.js +130 -0
  94. package/errors.js.map +1 -0
  95. package/errors.ts +185 -0
  96. package/everywhere.js +46 -0
  97. package/everywhere.js.map +1 -0
  98. package/everywhere.ts +66 -0
  99. package/fault.js +65 -0
  100. package/fault.js.map +1 -0
  101. package/fault.ts +107 -0
  102. package/file.js +87 -0
  103. package/file.js.map +1 -0
  104. package/file.ts +93 -0
  105. package/fixtime.js +67 -0
  106. package/fixtime.js.map +1 -0
  107. package/fixtime.ts +86 -0
  108. package/frames.js +77 -0
  109. package/frames.js.map +1 -0
  110. package/frames.ts +69 -0
  111. package/graph.js +7 -0
  112. package/graph.js.map +1 -0
  113. package/graph.ts +8 -0
  114. package/group.js +444 -0
  115. package/group.js.map +1 -0
  116. package/group.ts +533 -0
  117. package/inspect.js +471 -0
  118. package/inspect.js.map +1 -0
  119. package/inspect.ts +693 -0
  120. package/instrument.js +69 -0
  121. package/instrument.js.map +1 -0
  122. package/instrument.ts +93 -0
  123. package/intake.js +43 -0
  124. package/intake.js.map +1 -0
  125. package/intake.ts +84 -0
  126. package/merge.js +126 -0
  127. package/merge.js.map +1 -0
  128. package/merge.ts +115 -0
  129. package/migrate.ts +36 -0
  130. package/mod.js +8 -0
  131. package/mod.js.map +1 -0
  132. package/mod.ts +19 -0
  133. package/model.js +8 -0
  134. package/model.js.map +1 -0
  135. package/model.ts +27 -0
  136. package/outage.js +129 -0
  137. package/outage.js.map +1 -0
  138. package/outage.ts +158 -0
  139. package/package.json +133 -4
  140. package/page.d.ts +2 -0
  141. package/page.js +905 -0
  142. package/people.js +51 -0
  143. package/people.js.map +1 -0
  144. package/people.ts +82 -0
  145. package/poll.js +277 -0
  146. package/poll.js.map +1 -0
  147. package/poll.ts +411 -0
  148. package/poll_thread.js +104 -0
  149. package/poll_thread.js.map +1 -0
  150. package/poll_thread.ts +126 -0
  151. package/poll_worker.ts +45 -0
  152. package/reading.js +122 -0
  153. package/reading.js.map +1 -0
  154. package/reading.ts +162 -0
  155. package/reconcile.js +113 -0
  156. package/reconcile.js.map +1 -0
  157. package/reconcile.ts +130 -0
  158. package/remote.js +312 -0
  159. package/remote.js.map +1 -0
  160. package/remote.ts +397 -0
  161. package/report.js +825 -0
  162. package/report.js.map +1 -0
  163. package/report.ts +944 -0
  164. package/resolution.js +114 -0
  165. package/resolution.js.map +1 -0
  166. package/resolution.ts +123 -0
  167. package/routes.js +130 -0
  168. package/routes.js.map +1 -0
  169. package/routes.ts +163 -0
  170. package/script.js +13 -0
  171. package/script.js.map +1 -0
  172. package/script.ts +16 -0
  173. package/service.js +174 -0
  174. package/service.js.map +1 -0
  175. package/service.ts +215 -0
  176. package/spread.js +94 -0
  177. package/spread.js.map +1 -0
  178. package/spread.ts +101 -0
  179. package/sync_health.js +36 -0
  180. package/sync_health.js.map +1 -0
  181. package/sync_health.ts +65 -0
  182. package/ticket.js +6 -0
  183. package/ticket.js.map +1 -0
  184. package/ticket.ts +24 -0
  185. package/tools.js +305 -0
  186. package/tools.js.map +1 -0
  187. package/tools.ts +411 -0
  188. package/trace-intake.js +164 -0
  189. package/trace-intake.js.map +1 -0
  190. package/trace-intake.ts +148 -0
  191. package/views.js +247 -0
  192. package/views.js.map +1 -0
  193. package/views.ts +254 -0
  194. package/vocab.js +20 -0
  195. package/vocab.js.map +1 -0
  196. package/vocab.json +834 -0
  197. package/vocab.ts +14 -0
  198. package/yak-tracker@.service +22 -0
package/effects.ts ADDED
@@ -0,0 +1,178 @@
1
+ // The downstream jobs. Intake never groups in its transaction; it only
2
+ // counts a flood's occurrences past what their bug keeps. No job writes a
3
+ // letter about one bug: agents hear of each bug from the open-bug watch
4
+ // (@yaks/heal), and a person from the daily digest (./digest.ts).
5
+
6
+ import type { Graph } from 'jsr:@yaks/graph@0.2.36'
7
+ import type { Handlers } from 'jsr:@yaks/effects@0.2.36'
8
+ import { comp, type Desk, str } from './model.js'
9
+ import { type Enrich, group, reframe, trim } from './group.js'
10
+ import { enrichFrames } from './frames.js'
11
+ import { occurring } from './reading.js'
12
+ import { type Explanation, explanation, unexplained } from './resolution.js'
13
+ import { tags } from './spread.js'
14
+ import { send } from './digest.js'
15
+
16
+ export type Options = {
17
+ enrich?: Enrich
18
+ /** The digest's recipient, by entity: the host resolves its address. */
19
+ to?: string
20
+ from?: string
21
+ /** Global eid of this tracker store, which the digest's links scope. */
22
+ store?: string
23
+ /** Tracker web address for links in the digest. */
24
+ url?: string
25
+ /** Where the digest's chart is drawn: a `/chart.png` (./chart.ts). */
26
+ chart?: string
27
+ /** Sections of the host's own the digest carries (./digest.ts `Sending`). */
28
+ more?: (now: number) => Promise<string[]>
29
+ /** Where a failure the digest survives is reported. */
30
+ report?: (error: unknown) => void | Promise<void>
31
+ retain?: number
32
+ /** Archive these occurrences in the tracker of `scope` (a space's) for
33
+ * `reason`, as its owner's `fixed` would, and resolve with `said` a bug of
34
+ * theirs left with none open: the platform's, which resolves bugs reported
35
+ * from people's apps ({@link settle}). */
36
+ settle?: (
37
+ scope: string,
38
+ eids: string[],
39
+ reason: string,
40
+ said?: Explanation,
41
+ ) => Promise<unknown>
42
+ /** Run again the work a resolved bug `bug` stopped for `owner` (a space,
43
+ * whose Operator keeps it), named by the faults its occurrences tagged
44
+ * `rerun` with the owner were reported under ({@link settle}). */
45
+ rerun?: (
46
+ owner: string,
47
+ faults: string[],
48
+ bug: string,
49
+ said?: Explanation,
50
+ ) => Promise<unknown>
51
+ }
52
+
53
+ /** A resolved bug closes the reports its occurrences stand for elsewhere: an
54
+ * occurrence tagged with another tracker's `scope` and its `occurrence` there
55
+ * (a break in a person's app, which yaks.app also records) is archived in
56
+ * that tracker, so the app's owner and their Operator stop hearing of a break
57
+ * yaks.app fixed; the reason is the resolution's cause. The resolution's
58
+ * explanation goes with them, and resolves the space's bug they leave with no
59
+ * open occurrence, so one fix never leaves a second copy open. An occurrence
60
+ * tagged `rerun` stopped work its owner (a space) keeps under the
61
+ * occurrence's fault, and each owner is handed the faults, so it runs that
62
+ * work again now it can finish (the tracker keeps one such occurrence of each
63
+ * owner, `retained`). Only occurrences from before the resolution are closed
64
+ * or run again; a later one is the bug back, open in both. Archiving again
65
+ * changes nothing, and an owner runs its work once for each bug, so a retry
66
+ * is safe.
67
+ *
68
+ * ```ts
69
+ * import { settle } from './effects.ts'
70
+ * import { equal } from '@yaks/testing'
71
+ * let at = (eid: string, t: string, tags = {}) =>
72
+ * ({ entity: { eid }, error: { at: t, bug: 'b', fault: 'f', tags } })
73
+ * let rows = [
74
+ * { entity: { eid: 'b' }, bug: {}, resolved: { at: '2026-10-18T16:00Z', cause: 'a leak', recur: 'no' } },
75
+ * at('e1', '2026-10-18T15:49Z', { scope: 's', occurrence: 'o1' }),
76
+ * at('e2', '2026-10-18T15:50Z', { scope: 's', occurrence: 'o2' }),
77
+ * at('e3', '2026-10-18T15:51Z'),
78
+ * at('e4', '2026-10-19T09:00Z', { scope: 's', occurrence: 'o4' }),
79
+ * at('e5', '2026-10-18T15:52Z', { rerun: 'ada' }),
80
+ * at('e6', '2026-10-19T09:01Z', { rerun: 'bo' }),
81
+ * ]
82
+ * let g = {
83
+ * get: (eids: string[]) => Promise.resolve(rows.filter((r) => eids.includes(r.entity.eid))),
84
+ * read: () => Promise.resolve(rows.filter((r) => 'error' in r)),
85
+ * }
86
+ * let sent: unknown[][] = [], ran: unknown[][] = []
87
+ * await settle(g as never, 'b', {
88
+ * settle: (...a) => Promise.resolve(sent.push(a)),
89
+ * rerun: (...a) => Promise.resolve(ran.push(a)),
90
+ * })
91
+ * let said = { cause: 'a leak', recur: 'no' }
92
+ * equal(sent, [['s', ['o1', 'o2'], 'Fixed by yaks.app: a leak', said]])
93
+ * equal(ran, [['ada', ['f'], 'b', said]])
94
+ * ```
95
+ */
96
+ export let settle = async (
97
+ g: Desk,
98
+ eid: string,
99
+ options: Options,
100
+ ): Promise<void> => {
101
+ if (!options.settle && !options.rerun) return
102
+ let [row] = await g.get([eid])
103
+ if (!row?.bug || !row.resolved) return
104
+ let resolved = comp(row, 'resolved')
105
+ let until = str(resolved.at)
106
+ let why = str(resolved.cause) || str(resolved.broke)
107
+ let reason = why ? `Fixed by yaks.app: ${why}` : 'Fixed by yaks.app'
108
+ // A resolution from before explanations were kept resolves nothing more.
109
+ let said = unexplained(resolved).length
110
+ ? undefined
111
+ : explanation(resolved) as Explanation
112
+ let by = new Map<string, string[]>(), runs = new Map<string, Set<string>>()
113
+ for (let b of await g.read(occurring(eid))) {
114
+ let error = comp(b, 'error')
115
+ if (str(error.at) > until) continue
116
+ let { scope, occurrence, rerun } = Object.fromEntries(tags(b))
117
+ if (scope && occurrence) by.set(scope, [...by.get(scope) ?? [], occurrence])
118
+ if (rerun && error.fault) {
119
+ runs.set(rerun, (runs.get(rerun) ?? new Set()).add(str(error.fault)))
120
+ }
121
+ }
122
+ if (options.settle) {
123
+ for (let [scope, eids] of by) {
124
+ await options.settle(scope, eids, reason, said)
125
+ }
126
+ }
127
+ if (options.rerun) {
128
+ for (let [owner, faults] of runs) {
129
+ await options.rerun(owner, [...faults], eid, said)
130
+ }
131
+ }
132
+ }
133
+
134
+ export let effects = (
135
+ host: { graph: Graph; config?: { port?: number } },
136
+ options: Options = {},
137
+ ): Handlers => {
138
+ let enrich = options.enrich ?? enrichFrames()
139
+ return {
140
+ // Grouping trims the bug it grew, which reads one row until the bug
141
+ // outgrows what it keeps: a run of its own per report cost more rows
142
+ // than the report.
143
+ error_group: async (e) => {
144
+ let bug = await group(host.graph, e.entity.eid)
145
+ if (bug) await trim(host.graph, bug, options.retain)
146
+ },
147
+ error_frames: async (e, _tx, _write, attempt) => {
148
+ try {
149
+ await reframe(host.graph, e.entity.eid, enrich)
150
+ } catch (error) {
151
+ if (
152
+ attempt?.defer && error && typeof error == 'object' &&
153
+ 'retry' in error
154
+ ) {
155
+ await attempt.defer(5_000)
156
+ return
157
+ }
158
+ throw error
159
+ }
160
+ },
161
+ // The day's digest is the one due when its wake fired, so a retry
162
+ // writes the same letter.
163
+ bug_digest: async (e) => {
164
+ let [row] = await host.graph.get([e.entity.eid], ['fired'])
165
+ let fired = str(comp(row, 'fired').at)
166
+ let at = Date.parse(fired)
167
+ if (Number.isFinite(at)) {
168
+ await send(
169
+ host.graph,
170
+ at,
171
+ { ...options, fired: { entity: row.entity, at: fired } },
172
+ (e) => void options.report?.(e),
173
+ )
174
+ }
175
+ },
176
+ bug_settle: (e) => settle(host.graph, e.entity.eid, options),
177
+ }
178
+ }
package/errors.js ADDED
@@ -0,0 +1,130 @@
1
+ // yaks.app's errors as its trackers hold them, the platform's and every
2
+ // space's: their unresolved bugs, newest first, and how long the bugs
3
+ // resolved in a window took to fix. One report for every door that reads it:
4
+ // admin_errors on the box, and the platform admin's Operator on yaks.app.
5
+ import { CallError } from "@yaks/tools/errors";
6
+ import { whom } from "./brief.js";
7
+ import { fixes, fixtime, opened, span } from "./fixtime.js";
8
+ import { explained } from "./resolution.js";
9
+ import { read } from "./routes.js";
10
+ import { everywhere } from "./everywhere.js";
11
+ let object = (v)=>v && typeof v == 'object' && !Array.isArray(v) ? v : {};
12
+ let text = (v)=>typeof v == 'string' ? v : v == null ? '' : JSON.stringify(v);
13
+ let line = (v)=>text(v).replace(/\s+/g, ' ').trim();
14
+ let time = (v)=>{
15
+ let n = typeof v == 'number' ? v : Date.parse(text(v));
16
+ return Number.isFinite(n) ? n : 0;
17
+ };
18
+ let iso = (n)=>n ? new Date(n).toISOString() : '?';
19
+ let UNITS = {
20
+ s: 1,
21
+ m: 60,
22
+ h: 3600,
23
+ d: 86_400
24
+ };
25
+ /** Seconds in a window said as 10m, 6h or 2d. */ export let duration = (since)=>{
26
+ let match = /^(\d+)(s|m|h|d)?$/.exec(since);
27
+ let seconds = match ? Number(match[1]) * UNITS[match[2] || 's'] : 0;
28
+ if (!Number.isSafeInteger(seconds) || seconds <= 0) {
29
+ throw new CallError('since', '--since needs a positive duration (for example 10m)');
30
+ }
31
+ return seconds;
32
+ };
33
+ /** The window a report covers, ending at `end`: `since` back, or a week. */ export let windowOf = (since, end)=>{
34
+ let seconds = since == undefined ? 7 * 86_400 : duration(since);
35
+ let from = new Date(end - seconds * 1000).toISOString();
36
+ return {
37
+ seconds,
38
+ from,
39
+ last: since ?? '7d'
40
+ };
41
+ };
42
+ // Every bug a query matches in the tracker `scope` names, a page of 100 at a
43
+ // time.
44
+ let pages = async (options, go, query, scope)=>{
45
+ let rows = [], after = '';
46
+ for(;;){
47
+ let params = new URLSearchParams({
48
+ scope,
49
+ query: query + (after ? ` .after=${after}` : ''),
50
+ limit: '100'
51
+ });
52
+ let got = await read(options, 'bugs', params, undefined, go);
53
+ if (!got.ok) throw new CallError('tracker', got.error);
54
+ if (!Array.isArray(got.value)) throw Error('tracker answered no bug rows');
55
+ let body = got.value;
56
+ rows.push(...body);
57
+ if (body.length < 100) return rows;
58
+ let cursor = body.at(-1)?.entity?.eid;
59
+ if (!cursor || cursor == after) throw Error('tracker repeated a bug page');
60
+ after = cursor;
61
+ }
62
+ };
63
+ // The tracker a bug is in: the platform's, or the space's it names.
64
+ let space = (row)=>text(row.$scope) || 'platform';
65
+ let named = (row)=>`${row.entity.eid}${row.$scope ? ` in space ${row.$scope}` : ''} ${line(object(row.doc).title).slice(0, 80)}`;
66
+ /** The report's lines. The optional window matches unresolved bugs by their
67
+ * latest occurrence and times the fixes resolved within it; without one,
68
+ * fixes over the last week. Hits and first seen cover the whole group,
69
+ * including older samples. `resolved` adds each fix beside the bug it closed,
70
+ * as its resolver explained it. */ export let errors = async (since, options, { fetch: go = fetch, resolved = false, end = Date.now() } = {})=>{
71
+ let window = '', seen = '';
72
+ let { seconds, from, last } = windowOf(since, end);
73
+ if (since != undefined) {
74
+ window = ` .bug.last>=${from} .bug.last<=${new Date(end).toISOString()}`;
75
+ seen = ` last seen in preceding ${seconds}s`;
76
+ }
77
+ let [open, closed] = await Promise.all([
78
+ `!resolved !archived${window} .order=-bug.last`,
79
+ `.resolved .resolved.at>=${from}`
80
+ ].map((query)=>everywhere(options, (scope)=>pages(options, go, query, scope), go)));
81
+ let rows = open.rows.toSorted((a, b)=>time(object(b.bug).last) - time(object(a.bug).last));
82
+ let fixed = closed.rows;
83
+ let { count, median, worst } = fixes(fixed);
84
+ let report = [
85
+ `Tracker: ${rows.length} unresolved bugs from yaks.app, its spaces and the box (not resolved or archived)${seen}, newest first; counts and first seen cover each bug's lifetime. SPACE is the tracker that holds it, the scope bug_show and bug_resolve take`,
86
+ 'COUNT FIRST SEEN LAST SEEN BUG SPACE REACHED PLACE MESSAGE',
87
+ ...rows.map((row)=>{
88
+ let bug = object(row.bug);
89
+ return [
90
+ bug.hits,
91
+ iso(time(bug.first)),
92
+ iso(time(bug.last)),
93
+ row.entity.eid,
94
+ space(row),
95
+ whom(Number(bug.people ?? 0), typeof bug.account == 'string' ? bug.account : null),
96
+ bug.spot || '-',
97
+ text(object(row.doc).title)
98
+ ].map(line).join(' ');
99
+ }),
100
+ `${rows.reduce((n, row)=>n + Number(object(row.bug).hits || 0), 0)} occurrences, ${rows.length} bugs`,
101
+ worst ? `Time to fix, resolved in the last ${last}: ${count} bug${count == 1 ? '' : 's'}, median ${span(median)}, worst ${span(worst.ms)} (${named(worst.of)})` : `Time to fix: no bug resolved in the last ${last}`
102
+ ];
103
+ // Each fix beside the bug it closed, newest first, as its resolver
104
+ // explained it; those resolved before explanations were kept are counted.
105
+ let resolvedAt = (b)=>text(object(b.resolved).at);
106
+ let bare = 0;
107
+ for (let row of resolved ? fixed.toSorted((a, b)=>resolvedAt(b).localeCompare(resolvedAt(a))) : []){
108
+ let ms = fixtime(row), lines = explained(row);
109
+ if (!lines.length) {
110
+ bare++;
111
+ continue;
112
+ }
113
+ report.push(`Resolved${ms == undefined ? '' : ` in ${span(ms)}`}: ${named(row)}`, ...lines.map(([label, said])=>` ${label}: ${line(said)}`));
114
+ }
115
+ if (bare) {
116
+ report.push(`${bare} bug${bare == 1 ? '' : 's'} resolved with no explanation recorded`);
117
+ }
118
+ for (let why of new Set([
119
+ ...open.unread,
120
+ ...closed.unread
121
+ ])){
122
+ report.push(`Not read: ${why}`);
123
+ }
124
+ let oldest = rows.reduce((a, b)=>!a || opened(b) < opened(a) ? b : a, undefined);
125
+ if (oldest) {
126
+ report.push(`Open longest: ${span(end - opened(oldest))} (${named(oldest)})`);
127
+ }
128
+ return report;
129
+ };
130
+ //# sourceMappingURL=errors.js.map
package/errors.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sources":["./errors.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,sEAAsE;AACtE,6EAA6E;AAC7E,0EAA0E;AAE1E,SAAS,SAAS,kCAAuC;AACzD,SAAS,IAAI,qBAAoB;AACjC,SAAS,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,uBAAsB;AAC3D,SAAS,SAAS,0BAAyB;AAC3C,SAAuB,IAAI,sBAAqB;AAChD,SAAS,UAAU,0BAAyB;AAG5C,IAAI,SAAS,CAAC,IACZ,KAAK,OAAO,KAAK,YAAY,CAAC,MAAM,OAAO,CAAC,KAAK,IAAW,CAAC;AAC/D,IAAI,OAAO,CAAC,IACV,OAAO,KAAK,WAAW,IAAI,KAAK,OAAO,KAAK,KAAK,SAAS,CAAC;AAC7D,IAAI,OAAO,CAAC,IAAe,KAAK,GAAG,OAAO,CAAC,QAAQ,KAAK,IAAI;AAC5D,IAAI,OAAO,CAAC;EACV,IAAI,IAAI,OAAO,KAAK,WAAW,IAAI,KAAK,KAAK,CAAC,KAAK;EACnD,OAAO,OAAO,QAAQ,CAAC,KAAK,IAAI;AAClC;AACA,IAAI,MAAM,CAAC,IAAc,IAAI,IAAI,KAAK,GAAG,WAAW,KAAK;AAEzD,IAAI,QAAgC;EAAE,GAAG;EAAG,GAAG;EAAI,GAAG;EAAM,GAAG;AAAO;AAEtE,+CAA+C,GAC/C,OAAO,IAAI,WAAW,CAAC;EACrB,IAAI,QAAQ,oBAAoB,IAAI,CAAC;EACrC,IAAI,UAAU,QAAQ,OAAO,KAAK,CAAC,EAAE,IA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,KAAK,IAAI,EACV,QAAQ,IAAI,KAAK,IAClB,sCAAsC,CAAC;EAE5C;EACA,KAAK,IAAI,OAAO,IAAI,IAAI;OAAI,KAAK,MAAM;OAAK,OAAO,MAAM;GAAC,EAAG;IAC3D,OAAO,IAAI,CAAC,CAAC,UAAU,EAAE,KAAK;EAChC;EACA,IAAI,SAAS,KAAK,MAAM,CACtB,CAAC,GAAG,IAAM,CAAC,KAAK,OAAO,KAAK,OAAO,KAAK,IAAI,GAC5C;EAEF,IAAI,QAAQ;IACV,OAAO,IAAI,CACT,CAAC,cAAc,EAAE,KAAK,MAAM,OAAO,SAAS,EAAE,EAAE,MAAM,QAAQ,CAAC,CAAC;EAEpE;EACA,OAAO;AACT,EAAC"}
package/errors.ts ADDED
@@ -0,0 +1,185 @@
1
+ // yaks.app's errors as its trackers hold them, the platform's and every
2
+ // space's: their unresolved bugs, newest first, and how long the bugs
3
+ // resolved in a window took to fix. One report for every door that reads it:
4
+ // admin_errors on the box, and the platform admin's Operator on yaks.app.
5
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
6
+ import { CallError } from '@yaks/tools/errors'
7
+ import { whom } from './brief.js'
8
+ import { fixes, fixtime, opened, span } from './fixtime.js'
9
+ import { explained } from './resolution.js'
10
+ import { type Options, read } from './routes.js'
11
+ import { everywhere } from './everywhere.js'
12
+
13
+ type Row = Record<string, unknown>
14
+ let object = (v: unknown): Row =>
15
+ v && typeof v == 'object' && !Array.isArray(v) ? v as Row : {}
16
+ let text = (v: unknown): string =>
17
+ typeof v == 'string' ? v : v == null ? '' : JSON.stringify(v)
18
+ let line = (v: unknown) => text(v).replace(/\s+/g, ' ').trim()
19
+ let time = (v: unknown): number => {
20
+ let n = typeof v == 'number' ? v : Date.parse(text(v))
21
+ return Number.isFinite(n) ? n : 0
22
+ }
23
+ let iso = (n: number) => n ? new Date(n).toISOString() : '?'
24
+
25
+ let UNITS: Record<string, number> = { s: 1, m: 60, h: 3600, d: 86_400 }
26
+
27
+ /** Seconds in a window said as 10m, 6h or 2d. */
28
+ export let duration = (since: string): number => {
29
+ let match = /^(\d+)(s|m|h|d)?$/.exec(since)
30
+ let seconds = match ? Number(match[1]) * UNITS[match[2] || 's'] : 0
31
+ if (!Number.isSafeInteger(seconds) || seconds <= 0) {
32
+ throw new CallError(
33
+ 'since',
34
+ '--since needs a positive duration (for example 10m)',
35
+ )
36
+ }
37
+ return seconds
38
+ }
39
+
40
+ /** A report's window: its length in seconds, its start, and how it was asked. */
41
+ export type Window = { seconds: number; from: string; last: string }
42
+
43
+ /** The window a report covers, ending at `end`: `since` back, or a week. */
44
+ export let windowOf = (since: string | undefined, end: number): Window => {
45
+ let seconds = since == undefined ? 7 * 86_400 : duration(since)
46
+ let from = new Date(end - seconds * 1000).toISOString()
47
+ return { seconds, from, last: since ?? '7d' }
48
+ }
49
+
50
+ // Every bug a query matches in the tracker `scope` names, a page of 100 at a
51
+ // time.
52
+ let pages = async (
53
+ options: Options,
54
+ go: typeof fetch,
55
+ query: string,
56
+ scope: string,
57
+ ): Promise<Bundle[]> => {
58
+ let rows: Bundle[] = [], after = ''
59
+ for (;;) {
60
+ let params = new URLSearchParams({
61
+ scope,
62
+ query: query + (after ? ` .after=${after}` : ''),
63
+ limit: '100',
64
+ })
65
+ let got = await read(options, 'bugs', params, undefined, go)
66
+ if (!got.ok) throw new CallError('tracker', got.error)
67
+ if (!Array.isArray(got.value)) throw Error('tracker answered no bug rows')
68
+ let body = got.value as Bundle[]
69
+ rows.push(...body)
70
+ if (body.length < 100) return rows
71
+ let cursor = body.at(-1)?.entity?.eid
72
+ if (!cursor || cursor == after) throw Error('tracker repeated a bug page')
73
+ after = cursor
74
+ }
75
+ }
76
+
77
+ // The tracker a bug is in: the platform's, or the space's it names.
78
+ let space = (row: Bundle) => text(row.$scope) || 'platform'
79
+ let named = (row: Bundle) =>
80
+ `${row.entity.eid}${row.$scope ? ` in space ${row.$scope}` : ''} ${
81
+ line(object(row.doc).title).slice(0, 80)
82
+ }`
83
+
84
+ /** The report's lines. The optional window matches unresolved bugs by their
85
+ * latest occurrence and times the fixes resolved within it; without one,
86
+ * fixes over the last week. Hits and first seen cover the whole group,
87
+ * including older samples. `resolved` adds each fix beside the bug it closed,
88
+ * as its resolver explained it. */
89
+ export let errors = async (
90
+ since: string | undefined,
91
+ options: Options,
92
+ { fetch: go = fetch, resolved = false, end = Date.now() }: {
93
+ fetch?: typeof fetch
94
+ resolved?: boolean
95
+ end?: number
96
+ } = {},
97
+ ): Promise<string[]> => {
98
+ let window = '', seen = ''
99
+ let { seconds, from, last } = windowOf(since, end)
100
+ if (since != undefined) {
101
+ window = ` .bug.last>=${from} .bug.last<=${new Date(end).toISOString()}`
102
+ seen = ` last seen in preceding ${seconds}s`
103
+ }
104
+ let [open, closed] = await Promise.all(
105
+ [
106
+ `!resolved !archived${window} .order=-bug.last`,
107
+ `.resolved .resolved.at>=${from}`,
108
+ ]
109
+ .map((query) =>
110
+ everywhere(options, (scope) => pages(options, go, query, scope), go)
111
+ ),
112
+ )
113
+ let rows = open.rows.toSorted((a, b) =>
114
+ time(object(b.bug).last) - time(object(a.bug).last)
115
+ )
116
+ let fixed = closed.rows
117
+ let { count, median, worst } = fixes(fixed)
118
+ let report = [
119
+ `Tracker: ${rows.length} unresolved bugs from yaks.app, its spaces and the box (not resolved or archived)${seen}, newest first; counts and first seen cover each bug's lifetime. SPACE is the tracker that holds it, the scope bug_show and bug_resolve take`,
120
+ 'COUNT FIRST SEEN LAST SEEN BUG SPACE REACHED PLACE MESSAGE',
121
+ ...rows.map((row) => {
122
+ let bug = object(row.bug)
123
+ return [
124
+ bug.hits,
125
+ iso(time(bug.first)),
126
+ iso(time(bug.last)),
127
+ row.entity.eid,
128
+ space(row),
129
+ whom(
130
+ Number(bug.people ?? 0),
131
+ typeof bug.account == 'string' ? bug.account : null,
132
+ ),
133
+ bug.spot || '-',
134
+ text(object(row.doc).title),
135
+ ].map(line).join(' ')
136
+ }),
137
+ `${
138
+ rows.reduce((n, row) => n + Number(object(row.bug).hits || 0), 0)
139
+ } occurrences, ${rows.length} bugs`,
140
+ worst
141
+ ? `Time to fix, resolved in the last ${last}: ${count} bug${
142
+ count == 1 ? '' : 's'
143
+ }, median ${span(median!)}, worst ${span(worst.ms)} (${named(worst.of)})`
144
+ : `Time to fix: no bug resolved in the last ${last}`,
145
+ ]
146
+ // Each fix beside the bug it closed, newest first, as its resolver
147
+ // explained it; those resolved before explanations were kept are counted.
148
+ let resolvedAt = (b: Bundle) => text(object(b.resolved).at)
149
+ let bare = 0
150
+ for (
151
+ let row of resolved
152
+ ? fixed.toSorted((a, b) => resolvedAt(b).localeCompare(resolvedAt(a)))
153
+ : []
154
+ ) {
155
+ let ms = fixtime(row), lines = explained(row)
156
+ if (!lines.length) {
157
+ bare++
158
+ continue
159
+ }
160
+ report.push(
161
+ `Resolved${ms == undefined ? '' : ` in ${span(ms)}`}: ${named(row)}`,
162
+ ...lines.map(([label, said]) => ` ${label}: ${line(said)}`),
163
+ )
164
+ }
165
+ if (bare) {
166
+ report.push(
167
+ `${bare} bug${
168
+ bare == 1 ? '' : 's'
169
+ } resolved with no explanation recorded`,
170
+ )
171
+ }
172
+ for (let why of new Set([...open.unread, ...closed.unread])) {
173
+ report.push(`Not read: ${why}`)
174
+ }
175
+ let oldest = rows.reduce<Bundle | undefined>(
176
+ (a, b) => !a || opened(b) < opened(a) ? b : a,
177
+ undefined,
178
+ )
179
+ if (oldest) {
180
+ report.push(
181
+ `Open longest: ${span(end - opened(oldest))} (${named(oldest)})`,
182
+ )
183
+ }
184
+ return report
185
+ }
package/everywhere.js ADDED
@@ -0,0 +1,46 @@
1
+ // Every bug door reads every tracker on yaks.app: the platform's, and each
2
+ // space's, which keeps the bugs of that space's apps. The platform tracker
3
+ // lists the space trackers it has routed to; each is read in parallel through
4
+ // the same scoped read, and its rows name the scope they came from.
5
+ import { spaces } from "./remote.js";
6
+ import { read } from "./routes.js";
7
+ /** What `each` reads from the platform tracker (`platform`) and from every
8
+ * space tracker, all at once. The platform's failure is the read's; a space
9
+ * tracker's, or its list's, is said in `unread` beside the rest. */ export let everywhere = async (options, each, go = fetch)=>{
10
+ let unread = [];
11
+ // The platform's read and the list of spaces start together, and each is
12
+ // awaited from the start: the platform's refusal rejects this read while
13
+ // the list is still on its way, never escaping as a rejection nobody holds.
14
+ let [rows, theirs] = await Promise.all([
15
+ each('platform'),
16
+ spaces(options.remote ?? {}, go, true).then((scopes)=>Promise.all(scopes.map((scope)=>each(scope).then((got)=>got.map((row)=>({
17
+ ...row,
18
+ $scope: scope
19
+ })), (error)=>{
20
+ unread.push(`space ${scope}: ${error.message}`);
21
+ return [];
22
+ }))), (error)=>{
23
+ unread.push(`the list of space trackers: ${error.message}`);
24
+ return [];
25
+ })
26
+ ]);
27
+ return {
28
+ rows: [
29
+ ...rows,
30
+ ...theirs.flat()
31
+ ],
32
+ unread
33
+ };
34
+ };
35
+ /** A page of the bugs `query` answers in the tracker `scope` names, at most
36
+ * `limit`; a refusal throws. */ export let bugsIn = async (options, scope, query, limit = 50, go = fetch)=>{
37
+ let got = await read(options, 'bugs', new URLSearchParams({
38
+ scope,
39
+ query,
40
+ limit: String(limit)
41
+ }), undefined, go);
42
+ if (!got.ok) throw Error(got.error);
43
+ if (!Array.isArray(got.value)) throw Error('tracker answered no bug rows');
44
+ return got.value;
45
+ };
46
+ //# sourceMappingURL=everywhere.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"everywhere.js","sources":["./everywhere.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,2EAA2E;AAC3E,8EAA8E;AAC9E,oEAAoE;AAEpE,SAAS,MAAM,sBAAqB;AACpC,SAAuB,IAAI,sBAAqB;AAMhD;;kEAEkE,GAClE,OAAO,IAAI,aAAa,OACtB,SACA,MACA,KAAmB,KAAK;EAExB,IAAI,SAAmB,EAAE;EACzB,yEAAyE;EACzE,yEAAyE;EACzE,4EAA4E;EAC5E,IAAI,CAAC,MAAM,OAAO,GAAG,MAAM,QAAQ,GAAG,CAAC;IACrC,KAAK;IACL,OAAO,QAAQ,MAAM,IAAI,CAAC,GAAG,IAAI,MAAM,IAAI,CACzC,CAAC,SACC,QAAQ,GAAG,CAAC,OAAO,GAAG,CAAC,CAAC,QACtB,KAAK,OAAO,IAAI,CACd,CAAC,MAAQ,IAAI,GAAG,CAAC,CAAC,MAAQ,CAAC;cAAE,GAAG,GAAG;cAAE,QAAQ;YAAM,CAAC,IACpD,CAAC;UACC,OAAO,IAAI,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,AAAC,MAAgB,OAAO,EAAE;UACzD,OAAO,EAAE;QACX,MAGN,CAAC;MACC,OAAO,IAAI,CAAC,CAAC,4BAA4B,EAAE,AAAC,MAAgB,OAAO,EAAE;MACrE,OAAO,EAAE;IACX;GAEH;EACD,OAAO;IAAE,MAAM;SAAI;SAAS,OAAO,IAAI;KAAG;IAAE;EAAO;AACrD,EAAC;AAED;8BAC8B,GAC9B,OAAO,IAAI,SAAS,OAClB,SACA,OACA,OACA,QAAQ,EAAE,EACV,KAAmB,KAAK;EAExB,IAAI,MAAM,MAAM,KACd,SACA,QACA,IAAI,gBAAgB;IAAE;IAAO;IAAO,OAAO,OAAO;EAAO,IACzD,WACA;EAEF,IAAI,CAAC,IAAI,EAAE,EAAE,MAAM,MAAM,IAAI,KAAK;EAClC,IAAI,CAAC,MAAM,OAAO,CAAC,IAAI,KAAK,GAAG,MAAM,MAAM;EAC3C,OAAO,IAAI,KAAK;AAClB,EAAC"}
package/everywhere.ts ADDED
@@ -0,0 +1,66 @@
1
+ // Every bug door reads every tracker on yaks.app: the platform's, and each
2
+ // space's, which keeps the bugs of that space's apps. The platform tracker
3
+ // lists the space trackers it has routed to; each is read in parallel through
4
+ // the same scoped read, and its rows name the scope they came from.
5
+ import type { Bundle } from 'jsr:@yaks/graph@0.2.36'
6
+ import { spaces } from './remote.js'
7
+ import { type Options, read } from './routes.js'
8
+
9
+ /** Every tracker's rows, the platform's first, a space tracker's naming its
10
+ * space in `$scope`; and each tracker that could not be read, and why. */
11
+ export type Everywhere = { rows: Bundle[]; unread: string[] }
12
+
13
+ /** What `each` reads from the platform tracker (`platform`) and from every
14
+ * space tracker, all at once. The platform's failure is the read's; a space
15
+ * tracker's, or its list's, is said in `unread` beside the rest. */
16
+ export let everywhere = async (
17
+ options: Options,
18
+ each: (scope: string) => Promise<Bundle[]>,
19
+ go: typeof fetch = fetch,
20
+ ): Promise<Everywhere> => {
21
+ let unread: string[] = []
22
+ // The platform's read and the list of spaces start together, and each is
23
+ // awaited from the start: the platform's refusal rejects this read while
24
+ // the list is still on its way, never escaping as a rejection nobody holds.
25
+ let [rows, theirs] = await Promise.all([
26
+ each('platform'),
27
+ spaces(options.remote ?? {}, go, true).then(
28
+ (scopes) =>
29
+ Promise.all(scopes.map((scope) =>
30
+ each(scope).then(
31
+ (got) => got.map((row) => ({ ...row, $scope: scope })),
32
+ (error) => {
33
+ unread.push(`space ${scope}: ${(error as Error).message}`)
34
+ return []
35
+ },
36
+ )
37
+ )),
38
+ (error) => {
39
+ unread.push(`the list of space trackers: ${(error as Error).message}`)
40
+ return []
41
+ },
42
+ ),
43
+ ])
44
+ return { rows: [...rows, ...theirs.flat()], unread }
45
+ }
46
+
47
+ /** A page of the bugs `query` answers in the tracker `scope` names, at most
48
+ * `limit`; a refusal throws. */
49
+ export let bugsIn = async (
50
+ options: Options,
51
+ scope: string,
52
+ query: string,
53
+ limit = 50,
54
+ go: typeof fetch = fetch,
55
+ ): Promise<Bundle[]> => {
56
+ let got = await read(
57
+ options,
58
+ 'bugs',
59
+ new URLSearchParams({ scope, query, limit: String(limit) }),
60
+ undefined,
61
+ go,
62
+ )
63
+ if (!got.ok) throw Error(got.error)
64
+ if (!Array.isArray(got.value)) throw Error('tracker answered no bug rows')
65
+ return got.value as Bundle[]
66
+ }