@johnoscott/isometric-engine 0.0.0-stage → 0.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.
- package/EDITOR-GUIDE.md +317 -0
- package/LICENSE +21 -0
- package/LICENSES/ajv.txt +22 -0
- package/LICENSES/lucide-static.txt +43 -0
- package/LICENSES/yaml.txt +13 -0
- package/README.md +62 -2
- package/assets/app.css +265 -0
- package/assets/default-theme.yaml +99 -0
- package/assets/icons.svg +1 -0
- package/assets/marks.css +35 -0
- package/assets/skin.css +181 -0
- package/iso-editor.d.ts +4 -0
- package/iso-editor.js +550 -0
- package/iso-render.d.ts +3 -0
- package/iso-render.js +388 -0
- package/iso-systems.d.ts +4 -0
- package/iso-systems.js +389 -0
- package/js/iso-core/compile-schema.js +22 -0
- package/js/iso-core/depth.js +55 -0
- package/js/iso-core/errors.js +65 -0
- package/js/iso-core/glyphs.js +100 -0
- package/js/iso-core/index.js +55 -0
- package/js/iso-core/kernel.js +158 -0
- package/js/iso-core/metrics.json +1 -0
- package/js/iso-core/package.json +6 -0
- package/js/iso-core/parse.js +79 -0
- package/js/iso-core/schema-errors.js +170 -0
- package/js/iso-core/shims/crypto.js +125 -0
- package/js/iso-core/shims/path.js +49 -0
- package/js/iso-core/svg-dom.js +144 -0
- package/js/iso-core/theme.js +48 -0
- package/js/iso-core/vdom.js +48 -0
- package/js/iso-editor/adapter.js +0 -0
- package/js/iso-editor/clipboard.js +152 -0
- package/js/iso-editor/commands.js +173 -0
- package/js/iso-editor/complete.js +55 -0
- package/js/iso-editor/doc.js +182 -0
- package/js/iso-editor/index.js +105 -0
- package/js/iso-editor/model.js +210 -0
- package/js/iso-editor/outline.js +111 -0
- package/js/iso-editor/package.json +6 -0
- package/js/iso-editor/path.js +19 -0
- package/js/iso-editor/providers.js +96 -0
- package/js/iso-editor/schema-nav.js +117 -0
- package/js/iso-editor/splice.js +441 -0
- package/js/iso-editor/storage-keys.js +6 -0
- package/js/iso-editor/ui/actions.js +116 -0
- package/js/iso-editor/ui/canvas-layer.js +0 -0
- package/js/iso-editor/ui/canvas.js +1205 -0
- package/js/iso-editor/ui/clip.js +133 -0
- package/js/iso-editor/ui/dialog.js +26 -0
- package/js/iso-editor/ui/flat-canvas.js +54 -0
- package/js/iso-editor/ui/icons.js +15 -0
- package/js/iso-editor/ui/keys.js +26 -0
- package/js/iso-editor/ui/later.js +28 -0
- package/js/iso-editor/ui/layer.js +11 -0
- package/js/iso-editor/ui/menu.js +105 -0
- package/js/iso-editor/ui/monaco-entry.js +2 -0
- package/js/iso-editor/ui/mount-kit.js +0 -0
- package/js/iso-editor/ui/mount.js +767 -0
- package/js/iso-editor/ui/pane.js +0 -0
- package/js/iso-editor/ui/part.js +0 -0
- package/js/iso-editor/ui/plain-icons.js +34 -0
- package/js/iso-editor/ui/props.js +314 -0
- package/js/iso-editor/ui/scope-css.js +81 -0
- package/js/iso-editor/ui/setup.js +145 -0
- package/js/iso-editor/ui/text.js +323 -0
- package/js/iso-editor/ui/tree.js +728 -0
- package/js/iso-editor/validate.js +33 -0
- package/js/iso-editor/window/protocol.js +8 -0
- package/js/iso-objects/assemble.js +43 -0
- package/js/iso-objects/assets.js +88 -0
- package/js/iso-objects/board.js +162 -0
- package/js/iso-objects/compile.js +60 -0
- package/js/iso-objects/css.js +154 -0
- package/js/iso-objects/flat.js +696 -0
- package/js/iso-objects/focus.js +16 -0
- package/js/iso-objects/geometry.js +76 -0
- package/js/iso-objects/index.js +93 -0
- package/js/iso-objects/kinds.js +12 -0
- package/js/iso-objects/labels.js +325 -0
- package/js/iso-objects/package.json +6 -0
- package/js/iso-objects/place.js +347 -0
- package/js/iso-objects/route.js +461 -0
- package/js/iso-objects/scene.js +350 -0
- package/js/iso-objects/steps.js +246 -0
- package/js/iso-objects/types.js +0 -0
- package/js/iso-systems/catalog.js +42 -0
- package/js/iso-systems/compile.js +34 -0
- package/js/iso-systems/css.js +8 -0
- package/js/iso-systems/index.js +16 -0
- package/js/iso-systems/nodes.js +124 -0
- package/js/iso-systems/package.json +6 -0
- package/js/iso-systems/schema.json +311 -0
- package/js/iso-systems/validate.js +35 -0
- package/js/iso-viewer/bundle.js +20 -0
- package/js/iso-viewer/card-motion.js +228 -0
- package/js/iso-viewer/dom.js +14 -0
- package/js/iso-viewer/dual-view.js +212 -0
- package/js/iso-viewer/flat-view.js +124 -0
- package/js/iso-viewer/history.js +990 -0
- package/js/iso-viewer/index.js +98 -0
- package/js/iso-viewer/morph.js +192 -0
- package/js/iso-viewer/overlay.js +199 -0
- package/js/iso-viewer/package.json +6 -0
- package/js/iso-viewer/page.js +138 -0
- package/js/iso-viewer/player.js +409 -0
- package/js/iso-viewer/still.js +81 -0
- package/js/iso-viewer/timeline-contract.js +14 -0
- package/js/iso-viewer/timeline-engine.js +475 -0
- package/js/iso-viewer/timeline-export.js +36 -0
- package/js/iso-viewer/timeline-scale.js +116 -0
- package/js/iso-viewer/timeline-scope.js +58 -0
- package/js/iso-viewer/timeline-search.js +216 -0
- package/js/iso-viewer/timeline-time.js +55 -0
- package/js/iso-viewer/view-css.js +68 -0
- package/js/iso-viewer/viewport.js +177 -0
- package/package.json +502 -4
- package/src/iso-core/compile-schema.ts +23 -0
- package/src/iso-core/depth.ts +56 -0
- package/src/iso-core/errors.ts +73 -0
- package/src/iso-core/glyphs.ts +108 -0
- package/src/iso-core/index.ts +27 -0
- package/src/iso-core/kernel.ts +163 -0
- package/src/iso-core/metrics.json +1 -0
- package/src/iso-core/package.json +6 -0
- package/src/iso-core/parse.ts +91 -0
- package/src/iso-core/schema-errors.ts +163 -0
- package/src/iso-core/shims/crypto.ts +37 -0
- package/src/iso-core/shims/path.ts +24 -0
- package/src/iso-core/svg-dom.ts +103 -0
- package/src/iso-core/theme.ts +64 -0
- package/src/iso-core/vdom.ts +41 -0
- package/src/iso-editor/adapter.ts +128 -0
- package/src/iso-editor/clipboard.ts +163 -0
- package/src/iso-editor/commands.ts +191 -0
- package/src/iso-editor/complete.ts +73 -0
- package/src/iso-editor/doc.ts +207 -0
- package/src/iso-editor/index.ts +67 -0
- package/src/iso-editor/model.ts +234 -0
- package/src/iso-editor/outline.ts +126 -0
- package/src/iso-editor/package.json +6 -0
- package/src/iso-editor/path.ts +18 -0
- package/src/iso-editor/providers.ts +113 -0
- package/src/iso-editor/schema-nav.ts +129 -0
- package/src/iso-editor/splice.ts +472 -0
- package/src/iso-editor/storage-keys.ts +6 -0
- package/src/iso-editor/ui/actions.ts +119 -0
- package/src/iso-editor/ui/app.css +230 -0
- package/src/iso-editor/ui/canvas-layer.ts +56 -0
- package/src/iso-editor/ui/canvas.ts +1071 -0
- package/src/iso-editor/ui/clip.ts +97 -0
- package/src/iso-editor/ui/dialog.ts +25 -0
- package/src/iso-editor/ui/flat-canvas.ts +51 -0
- package/src/iso-editor/ui/icons.ts +21 -0
- package/src/iso-editor/ui/keys.ts +30 -0
- package/src/iso-editor/ui/later.ts +22 -0
- package/src/iso-editor/ui/layer.ts +7 -0
- package/src/iso-editor/ui/marks.css +35 -0
- package/src/iso-editor/ui/menu.ts +67 -0
- package/src/iso-editor/ui/monaco-entry.ts +6 -0
- package/src/iso-editor/ui/mount-kit.ts +35 -0
- package/src/iso-editor/ui/mount.ts +684 -0
- package/src/iso-editor/ui/pane.ts +45 -0
- package/src/iso-editor/ui/part.ts +45 -0
- package/src/iso-editor/ui/plain-icons.ts +83 -0
- package/src/iso-editor/ui/props.ts +347 -0
- package/src/iso-editor/ui/scope-css.ts +65 -0
- package/src/iso-editor/ui/setup.ts +144 -0
- package/src/iso-editor/ui/skin.css +181 -0
- package/src/iso-editor/ui/text.ts +262 -0
- package/src/iso-editor/ui/tree.ts +657 -0
- package/src/iso-editor/validate.ts +62 -0
- package/src/iso-editor/window/protocol.ts +33 -0
- package/src/iso-objects/assemble.ts +57 -0
- package/src/iso-objects/assets.ts +122 -0
- package/src/iso-objects/board.ts +169 -0
- package/src/iso-objects/compile.ts +76 -0
- package/src/iso-objects/css.ts +163 -0
- package/src/iso-objects/flat.ts +653 -0
- package/src/iso-objects/focus.ts +14 -0
- package/src/iso-objects/geometry.ts +86 -0
- package/src/iso-objects/index.ts +33 -0
- package/src/iso-objects/kinds.ts +56 -0
- package/src/iso-objects/labels.ts +310 -0
- package/src/iso-objects/package.json +6 -0
- package/src/iso-objects/place.ts +318 -0
- package/src/iso-objects/route.ts +444 -0
- package/src/iso-objects/scene.ts +376 -0
- package/src/iso-objects/steps.ts +234 -0
- package/src/iso-objects/types.ts +68 -0
- package/src/iso-systems/catalog.ts +31 -0
- package/src/iso-systems/compile.ts +39 -0
- package/src/iso-systems/css.ts +9 -0
- package/src/iso-systems/index.ts +12 -0
- package/src/iso-systems/nodes.ts +126 -0
- package/src/iso-systems/package.json +6 -0
- package/src/iso-systems/schema.json +311 -0
- package/src/iso-systems/validate.ts +34 -0
- package/src/iso-viewer/bundle.ts +15 -0
- package/src/iso-viewer/card-motion.ts +237 -0
- package/src/iso-viewer/dom.ts +13 -0
- package/src/iso-viewer/dual-view.ts +212 -0
- package/src/iso-viewer/flat-view.ts +127 -0
- package/src/iso-viewer/history.ts +781 -0
- package/src/iso-viewer/index.ts +40 -0
- package/src/iso-viewer/morph.ts +191 -0
- package/src/iso-viewer/overlay.ts +212 -0
- package/src/iso-viewer/package.json +6 -0
- package/src/iso-viewer/page.ts +150 -0
- package/src/iso-viewer/player.ts +321 -0
- package/src/iso-viewer/still.ts +74 -0
- package/src/iso-viewer/timeline-contract.ts +342 -0
- package/src/iso-viewer/timeline-engine.ts +420 -0
- package/src/iso-viewer/timeline-export.ts +31 -0
- package/src/iso-viewer/timeline-scale.ts +129 -0
- package/src/iso-viewer/timeline-scope.ts +56 -0
- package/src/iso-viewer/timeline-search.ts +198 -0
- package/src/iso-viewer/timeline-time.ts +54 -0
- package/src/iso-viewer/view-css.ts +59 -0
- package/src/iso-viewer/viewport.ts +142 -0
- package/types/iso-core/compile-schema.d.ts +11 -0
- package/types/iso-core/depth.d.ts +6 -0
- package/types/iso-core/errors.d.ts +24 -0
- package/types/iso-core/glyphs.d.ts +19 -0
- package/types/iso-core/index.d.ts +17 -0
- package/types/iso-core/kernel.d.ts +130 -0
- package/types/iso-core/metrics.json +1 -0
- package/types/iso-core/parse.d.ts +18 -0
- package/types/iso-core/schema-errors.d.ts +14 -0
- package/types/iso-core/shims/crypto.d.ts +9 -0
- package/types/iso-core/svg-dom.d.ts +15 -0
- package/types/iso-core/theme.d.ts +38 -0
- package/types/iso-core/vdom.d.ts +26 -0
- package/types/iso-editor/adapter.d.ts +145 -0
- package/types/iso-editor/clipboard.d.ts +23 -0
- package/types/iso-editor/commands.d.ts +32 -0
- package/types/iso-editor/complete.d.ts +25 -0
- package/types/iso-editor/doc.d.ts +50 -0
- package/types/iso-editor/index.d.ts +57 -0
- package/types/iso-editor/model.d.ts +96 -0
- package/types/iso-editor/outline.d.ts +28 -0
- package/types/iso-editor/path.d.ts +7 -0
- package/types/iso-editor/providers.d.ts +43 -0
- package/types/iso-editor/schema-nav.d.ts +34 -0
- package/types/iso-editor/splice.d.ts +53 -0
- package/types/iso-editor/storage-keys.d.ts +5 -0
- package/types/iso-editor/ui/actions.d.ts +23 -0
- package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
- package/types/iso-editor/ui/canvas.d.ts +279 -0
- package/types/iso-editor/ui/clip.d.ts +14 -0
- package/types/iso-editor/ui/dialog.d.ts +7 -0
- package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
- package/types/iso-editor/ui/icons.d.ts +5 -0
- package/types/iso-editor/ui/keys.d.ts +7 -0
- package/types/iso-editor/ui/later.d.ts +12 -0
- package/types/iso-editor/ui/layer.d.ts +2 -0
- package/types/iso-editor/ui/menu.d.ts +21 -0
- package/types/iso-editor/ui/mount-kit.d.ts +31 -0
- package/types/iso-editor/ui/mount.d.ts +146 -0
- package/types/iso-editor/ui/pane.d.ts +60 -0
- package/types/iso-editor/ui/part.d.ts +40 -0
- package/types/iso-editor/ui/plain-icons.d.ts +10 -0
- package/types/iso-editor/ui/props.d.ts +57 -0
- package/types/iso-editor/ui/scope-css.d.ts +5 -0
- package/types/iso-editor/ui/setup.d.ts +40 -0
- package/types/iso-editor/ui/text.d.ts +19 -0
- package/types/iso-editor/ui/tree.d.ts +100 -0
- package/types/iso-editor/validate.d.ts +36 -0
- package/types/iso-editor/window/protocol.d.ts +60 -0
- package/types/iso-objects/assemble.d.ts +35 -0
- package/types/iso-objects/assets.d.ts +49 -0
- package/types/iso-objects/board.d.ts +36 -0
- package/types/iso-objects/compile.d.ts +35 -0
- package/types/iso-objects/css.d.ts +16 -0
- package/types/iso-objects/flat.d.ts +130 -0
- package/types/iso-objects/focus.d.ts +1 -0
- package/types/iso-objects/geometry.d.ts +59 -0
- package/types/iso-objects/index.d.ts +26 -0
- package/types/iso-objects/kinds.d.ts +53 -0
- package/types/iso-objects/labels.d.ts +92 -0
- package/types/iso-objects/place.d.ts +25 -0
- package/types/iso-objects/route.d.ts +67 -0
- package/types/iso-objects/scene.d.ts +63 -0
- package/types/iso-objects/steps.d.ts +90 -0
- package/types/iso-objects/types.d.ts +140 -0
- package/types/iso-systems/catalog.d.ts +4 -0
- package/types/iso-systems/compile.d.ts +6 -0
- package/types/iso-systems/css.d.ts +3 -0
- package/types/iso-systems/index.d.ts +5 -0
- package/types/iso-systems/nodes.d.ts +3 -0
- package/types/iso-viewer/card-motion.d.ts +56 -0
- package/types/iso-viewer/dom.d.ts +1 -0
- package/types/iso-viewer/dual-view.d.ts +44 -0
- package/types/iso-viewer/flat-view.d.ts +25 -0
- package/types/iso-viewer/history.d.ts +91 -0
- package/types/iso-viewer/index.d.ts +31 -0
- package/types/iso-viewer/morph.d.ts +28 -0
- package/types/iso-viewer/overlay.d.ts +135 -0
- package/types/iso-viewer/page.d.ts +13 -0
- package/types/iso-viewer/still.d.ts +26 -0
- package/types/iso-viewer/timeline-contract.d.ts +345 -0
- package/types/iso-viewer/timeline-engine.d.ts +20 -0
- package/types/iso-viewer/timeline-export.d.ts +5 -0
- package/types/iso-viewer/timeline-scale.d.ts +44 -0
- package/types/iso-viewer/timeline-scope.d.ts +33 -0
- package/types/iso-viewer/timeline-search.d.ts +52 -0
- package/types/iso-viewer/timeline-time.d.ts +8 -0
- package/types/iso-viewer/view-css.d.ts +7 -0
- package/types/iso-viewer/viewport.d.ts +50 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
// A timeline source held in memory (docs/timeline/README.md §8): a Recording's events and metrics, answered as a host
|
|
2
|
+
// would answer query() and subscribe(): rows by scope and depth, the parsed search and filters, binning in the owner's
|
|
3
|
+
// time zone, rollups, pages, retention, renames, and live batches as records are appended (a record given again
|
|
4
|
+
// replaces the first). A fixture source and a simulation's source are this; a host with its own store does the same
|
|
5
|
+
// work there. Pure (no DOM, no clock: now is given). The scope rules, the search's links and the rows' flags are the
|
|
6
|
+
// domain's catalogue's.
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
BIN_MS, SEVERITIES, type Batch, type Bin, type Catalog, type Days, type EffectiveRetention, type EventBin, type MetricPoint,
|
|
10
|
+
type MetricRollup, type Query, type Recording, type Result, type Retention, type Row, type Scope,
|
|
11
|
+
type TimelineEvent, type TimelineSource,
|
|
12
|
+
type TimelineCatalog,
|
|
13
|
+
} from './timeline-contract.ts';
|
|
14
|
+
import { levelOf, prefixAt, within, kindOf, nameOf, scopeRules } from './timeline-scope.ts';
|
|
15
|
+
import { searchFor, rangeOf } from './timeline-search.ts';
|
|
16
|
+
import { binStart, offsetAt } from './timeline-time.ts';
|
|
17
|
+
|
|
18
|
+
const DAY = 86_400_000, HOUR = 3_600_000;
|
|
19
|
+
/** an hour's events on one scope from one source: what a binned query without a search reads instead of every event */
|
|
20
|
+
interface HourBin { t: number; count: number; severity: Record<string, number>; kinds: Record<string, number>; first: number; last: number }
|
|
21
|
+
/** a stored metric sample: a point is {v, v, v, 1} */
|
|
22
|
+
interface Fine { t: number; min: number; max: number; sum: number; count: number }
|
|
23
|
+
interface SeriesStore { series: string; scope: Scope; fine: Fine[]; cache: Map<string, Fine[]> }
|
|
24
|
+
|
|
25
|
+
const byTimeId = (a: TimelineEvent, b: TimelineEvent) => a.t - b.t || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
26
|
+
/** the first index with e.t >= t */
|
|
27
|
+
function lower(es: { t: number }[], t: number): number {
|
|
28
|
+
let lo = 0, hi = es.length;
|
|
29
|
+
while (lo < hi) { const m = (lo + hi) >> 1; if (es[m].t < t) lo = m + 1; else hi = m; }
|
|
30
|
+
return lo;
|
|
31
|
+
}
|
|
32
|
+
const combine = (into: Fine, f: Fine) => { into.min = Math.min(into.min, f.min); into.max = Math.max(into.max, f.max); into.sum += f.sum; into.count += f.count; };
|
|
33
|
+
/** fine samples into bins (each bin the min of mins, max of maxes, sums of sums and counts) */
|
|
34
|
+
function rollUp(fine: Fine[], bin: Bin, tz?: string): Fine[] {
|
|
35
|
+
const out: Fine[] = [];
|
|
36
|
+
for (const f of fine) {
|
|
37
|
+
const t = binStart(f.t, bin, tz), last = out[out.length - 1];
|
|
38
|
+
if (last && last.t === t) combine(last, f); else out.push({ t, min: f.min, max: f.max, sum: f.sum, count: f.count });
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/** a sorted array: replace the item same() finds at x.t, or insert x in order */
|
|
43
|
+
function place<T extends { t: number }>(xs: T[], x: T, same: (a: T) => boolean, order: (a: T, b: T) => number) {
|
|
44
|
+
for (let i = lower(xs, x.t); i < xs.length && xs[i].t === x.t; i++) if (same(xs[i])) { xs[i] = x; return; }
|
|
45
|
+
const last = xs[xs.length - 1];
|
|
46
|
+
if (!last || order(last, x) <= 0) { xs.push(x); return; }
|
|
47
|
+
let i = lower(xs, x.t);
|
|
48
|
+
while (i < xs.length && order(xs[i], x) < 0) i++;
|
|
49
|
+
xs.splice(i, 0, x);
|
|
50
|
+
}
|
|
51
|
+
function remove<T extends { t: number }>(xs: T[], t: number, same: (a: T) => boolean) {
|
|
52
|
+
for (let i = lower(xs, t); i < xs.length && xs[i].t === t; i++) if (same(xs[i])) { xs.splice(i, 1); return; }
|
|
53
|
+
}
|
|
54
|
+
export interface MemorySourceOptions {
|
|
55
|
+
/** the host's now (default: the recording's catalog.now) */
|
|
56
|
+
now?: number;
|
|
57
|
+
/** the state at t, where one was recorded (frames), as the timeline may show it */
|
|
58
|
+
stateAt?(scope: Scope, t: number): Promise<unknown>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface MemorySource extends TimelineSource {
|
|
62
|
+
/** new or changed records, and the host's now: subscribers get them as a batch (an id, or a series, scope and t,
|
|
63
|
+
* given again replaces the first) */
|
|
64
|
+
append(events: TimelineEvent[], metrics?: (MetricPoint | MetricRollup)[], now?: number): void;
|
|
65
|
+
/** the user's retention for a source (what the host's settings would do) */
|
|
66
|
+
setRetention(source: string, r: Retention): void;
|
|
67
|
+
readonly now: number;
|
|
68
|
+
readonly size: { events: number; samples: number };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function memorySource(rec: Recording, cat: TimelineCatalog, o: MemorySourceOptions = {}): MemorySource {
|
|
72
|
+
const catalog = rec.catalog;
|
|
73
|
+
const { isScope } = scopeRules(cat), { parseSearch, matchAst, toAst } = searchFor(cat);
|
|
74
|
+
/** a name's flags, on its row */
|
|
75
|
+
const flagsOf = (n: object | undefined): object => Object.fromEntries(cat.flags.filter(f => (n as Record<string, unknown> | undefined)?.[f]).map(f => [f, true]));
|
|
76
|
+
let now = o.now ?? catalog.now;
|
|
77
|
+
// renames: an old scope's history shows under the new one
|
|
78
|
+
const aliases = (catalog.names ?? []).flatMap(n => (n.aliases ?? []).map(a => [a, n.scope] as const)).sort((a, b) => b[0].length - a[0].length);
|
|
79
|
+
const canonical = new Map<Scope, Scope>();
|
|
80
|
+
const canon = (s: Scope): Scope => {
|
|
81
|
+
if (!aliases.length) return s;
|
|
82
|
+
let c = canonical.get(s);
|
|
83
|
+
if (c === undefined) { const a = aliases.find(([from]) => within(s, from)); c = a ? a[1] + s.slice(a[0].length) : s; canonical.set(s, c); }
|
|
84
|
+
return c;
|
|
85
|
+
};
|
|
86
|
+
const names = new Map((catalog.names ?? []).map(n => [n.scope, n]));
|
|
87
|
+
const fix = (e: TimelineEvent) => (aliases.length ? { ...e, scope: canon(e.scope) } : e);
|
|
88
|
+
const keyOf = (e: TimelineEvent) => `${e.source}\u0000${e.id}`;
|
|
89
|
+
|
|
90
|
+
const events: TimelineEvent[] = rec.events.map(fix).sort(byTimeId);
|
|
91
|
+
const byId = new Map<string, TimelineEvent>();
|
|
92
|
+
for (const e of events) byId.set(keyOf(e), e);
|
|
93
|
+
/** closed spans by start, and the longest, so a range finds those that began before it; open spans apart */
|
|
94
|
+
const spans: TimelineEvent[] = events.filter(e => e.dur !== undefined && !e.open);
|
|
95
|
+
let maxDur = spans.reduce((m, e) => Math.max(m, e.dur!), 0);
|
|
96
|
+
const open = new Map<string, TimelineEvent>();
|
|
97
|
+
for (const e of events) if (e.open) open.set(keyOf(e), e);
|
|
98
|
+
const stores = new Map<string, SeriesStore>();
|
|
99
|
+
const scopes = new Set<Scope>();
|
|
100
|
+
const noteScope = (s: Scope) => { for (let l = levelOf(s); l >= 1; l--) { const p = prefixAt(s, l); if (scopes.has(p)) break; scopes.add(p); } };
|
|
101
|
+
const seriesType = new Map<string, 'counter' | 'gauge'>();
|
|
102
|
+
for (const s of catalog.sources) for (const x of s.series) seriesType.set(x.name, x.type);
|
|
103
|
+
const sensitive = catalog.sources.flatMap(s => (s.sensitive ?? []).map(a => `${s.name}.${a}`));
|
|
104
|
+
const retention = new Map<string, Omit<EffectiveRetention, 'keptFrom' | 'source'>>();
|
|
105
|
+
for (const s of catalog.sources) retention.set(s.name, { events: s.defaultRetention.events, rollups: { ...s.defaultRetention.rollups }, setBy: 'default' });
|
|
106
|
+
for (const r of catalog.retention) retention.set(r.source, { events: r.events, rollups: { ...r.rollups }, setBy: r.setBy });
|
|
107
|
+
const seriesSource = new Map<string, string>();
|
|
108
|
+
for (const s of catalog.sources) for (const x of s.series) seriesSource.set(x.name, s.name);
|
|
109
|
+
|
|
110
|
+
function addMetric(m: MetricPoint | MetricRollup) {
|
|
111
|
+
const scope = canon(m.scope), key = `${m.series}\u0000${scope}`;
|
|
112
|
+
let st = stores.get(key);
|
|
113
|
+
if (!st) { st = { series: m.series, scope, fine: [], cache: new Map() }; stores.set(key, st); noteScope(scope); }
|
|
114
|
+
const f: Fine = 'value' in m ? { t: m.t, min: m.value, max: m.value, sum: m.value, count: 1 } : { t: m.t, min: m.min, max: m.max, sum: m.sum, count: m.count };
|
|
115
|
+
place(st.fine, f, a => a.t === f.t, (a, b) => a.t - b.t);
|
|
116
|
+
st.cache.clear();
|
|
117
|
+
}
|
|
118
|
+
for (const e of events) noteScope(e.scope);
|
|
119
|
+
for (const m of rec.metrics) addMetric(m);
|
|
120
|
+
// the hour bins, kept as events come and go (scope → source → hour start → bin)
|
|
121
|
+
const hours = new Map<Scope, Map<string, Map<number, HourBin>>>();
|
|
122
|
+
/** the sources with events */
|
|
123
|
+
const present = new Set<string>();
|
|
124
|
+
const tally = (e: TimelineEvent, by: 1 | -1) => {
|
|
125
|
+
present.add(e.source);
|
|
126
|
+
let bySrc = hours.get(e.scope); if (!bySrc) hours.set(e.scope, bySrc = new Map());
|
|
127
|
+
let byT = bySrc.get(e.source); if (!byT) bySrc.set(e.source, byT = new Map());
|
|
128
|
+
const t = Math.floor(e.t / HOUR) * HOUR;
|
|
129
|
+
let b = byT.get(t); if (!b) byT.set(t, b = { t, count: 0, severity: {}, kinds: {}, first: Infinity, last: -Infinity });
|
|
130
|
+
b.count += by;
|
|
131
|
+
if (by > 0) { b.first = Math.min(b.first, e.t); b.last = Math.max(b.last, e.t); }
|
|
132
|
+
else if (e.t === b.first || e.t === b.last) {
|
|
133
|
+
// (called after the event left the list) the hour's others, again
|
|
134
|
+
b.first = Infinity; b.last = -Infinity;
|
|
135
|
+
for (let i = lower(events, t); i < events.length && events[i].t < t + HOUR; i++) { const x = events[i]; if (x.scope === e.scope && x.source === e.source) { b.first = Math.min(b.first, x.t); b.last = Math.max(b.last, x.t); } }
|
|
136
|
+
} b.severity[e.severity] = (b.severity[e.severity] ?? 0) + by; b.kinds[e.kind] = (b.kinds[e.kind] ?? 0) + by;
|
|
137
|
+
if (b.severity[e.severity] === 0) delete b.severity[e.severity];
|
|
138
|
+
if (b.kinds[e.kind] === 0) delete b.kinds[e.kind];
|
|
139
|
+
if (!b.count) byT.delete(t);
|
|
140
|
+
};
|
|
141
|
+
for (const e of events) tally(e, 1);
|
|
142
|
+
const binned = (st: SeriesStore, bin: Bin, tz?: string): Fine[] => {
|
|
143
|
+
const key = `${bin}|${tz ?? ''}`;
|
|
144
|
+
let c = st.cache.get(key);
|
|
145
|
+
if (!c) { c = bin === '1d' ? rollUp(binned(st, '1h', tz), '1d', tz) : rollUp(st.fine, bin, tz); st.cache.set(key, c); }
|
|
146
|
+
return c;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
/** a cut-off: before it, nothing of that kind is kept */
|
|
150
|
+
const cutoff = (d: Days) => (d === null ? -Infinity : now - d * DAY);
|
|
151
|
+
const eventCut = (source: string) => cutoff(retention.get(source)?.events ?? null);
|
|
152
|
+
const binCut = (source: string, bin?: Bin) => { const r = retention.get(source); return cutoff(r ? (bin ? r.rollups[bin] : r.events) : null); };
|
|
153
|
+
|
|
154
|
+
function effective(srcNames: Iterable<string>): EffectiveRetention[] {
|
|
155
|
+
const out: EffectiveRetention[] = [];
|
|
156
|
+
for (const name of new Set(srcNames)) {
|
|
157
|
+
const r = retention.get(name);
|
|
158
|
+
if (!r) continue;
|
|
159
|
+
const first = events.find(e => e.source === name && e.t >= eventCut(name))?.t;
|
|
160
|
+
const firstSample = (bin?: Bin) => {
|
|
161
|
+
let m = Infinity;
|
|
162
|
+
for (const st of stores.values()) if (seriesSource.get(st.series) === name) { const f = (bin ? binned(st, bin) : st.fine).find(x => x.t >= binCut(name, bin)); if (f && f.t < m) m = f.t; }
|
|
163
|
+
return m === Infinity ? undefined : m;
|
|
164
|
+
};
|
|
165
|
+
const ev = [first, firstSample()].filter((x): x is number => x !== undefined);
|
|
166
|
+
const rollups: Partial<Record<Bin, number>> = {};
|
|
167
|
+
for (const b of ['1m', '1h', '1d'] as Bin[]) {
|
|
168
|
+
const f = firstSample(b) ?? (first !== undefined && first >= binCut(name, b) ? first : undefined);
|
|
169
|
+
if (f !== undefined) rollups[b] = Math.max(f, binCut(name, b));
|
|
170
|
+
}
|
|
171
|
+
out.push({ source: name, events: r.events, rollups: { ...r.rollups }, setBy: r.setBy, keptFrom: { ...(ev.length ? { events: Math.min(...ev) } : {}), rollups } });
|
|
172
|
+
}
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** what a query keeps of an event: scope, filters, the parsed search, retention. A query with q and no where (a
|
|
177
|
+
* convenience for tests and fixtures) is parsed here, as the component would. */
|
|
178
|
+
function predicate(q: Query) {
|
|
179
|
+
let where = q.where, range = { from: q.from, to: q.to }, warnings: string[] = [];
|
|
180
|
+
if (!where && q.q) { const p = parseSearch(q.q, now); where = toAst(p); range = rangeOf(p, q.from, q.to); warnings = p.warnings; }
|
|
181
|
+
const m = matchAst(where, sensitive);
|
|
182
|
+
const f = q.filters ?? {};
|
|
183
|
+
const src = f.source?.length ? new Set(f.source) : undefined, kind = f.kind?.length ? new Set(f.kind) : undefined;
|
|
184
|
+
const sev = f.severity?.length ? new Set<string>(f.severity) : undefined, sc = f.scope?.length ? f.scope : undefined;
|
|
185
|
+
const test = (e: TimelineEvent) => within(e.scope, q.scope) && (!src || src.has(e.source)) && (!kind || kind.has(e.kind)) && (!sev || sev.has(e.severity))
|
|
186
|
+
&& (!sc || sc.some(s => within(e.scope, s))) && e.t >= eventCut(e.source) && m(e);
|
|
187
|
+
return { test, range, warnings };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** the rows of a query, and each scope's row (or undefined: not under the query's scope) */
|
|
191
|
+
function rowsOf(q: Query) {
|
|
192
|
+
const base = levelOf(q.scope), level = base + q.depth;
|
|
193
|
+
const rowOf = new Map<Scope, Scope | undefined>();
|
|
194
|
+
const at = (s: Scope): Scope | undefined => {
|
|
195
|
+
let r = rowOf.get(s);
|
|
196
|
+
if (r === undefined && !rowOf.has(s)) { r = within(s, q.scope) ? (levelOf(s) >= level ? prefixAt(s, level) : s) : undefined; rowOf.set(s, r); }
|
|
197
|
+
return r;
|
|
198
|
+
};
|
|
199
|
+
const children = (s: Scope) => { let n = 0; const l = levelOf(s) + 1; for (const x of scopes) if (levelOf(x) === l && within(x, s)) n++; return n; };
|
|
200
|
+
const rows = new Map<Scope, Row>();
|
|
201
|
+
const add = (s: Scope) => {
|
|
202
|
+
let r = rows.get(s);
|
|
203
|
+
if (!r) {
|
|
204
|
+
const d = levelOf(s) - base, n = names.get(s);
|
|
205
|
+
r = { scope: s, label: n?.label ?? nameOf(s), kind: kindOf(s), ...flagsOf(n), depth: d, ...(d < q.depth ? { own: true } : {}), children: children(s), count: 0 };
|
|
206
|
+
rows.set(s, r);
|
|
207
|
+
}
|
|
208
|
+
return r;
|
|
209
|
+
};
|
|
210
|
+
// every known scope at the rows' level is a row, with events in range or not
|
|
211
|
+
for (const s of scopes) if (levelOf(s) === level && within(s, q.scope)) add(s);
|
|
212
|
+
if (level === 0) add('');
|
|
213
|
+
return { at, add, rows };
|
|
214
|
+
}
|
|
215
|
+
const sortRows = (rows: Map<Scope, Row>) => [...rows.values()].sort((a, b) => (a.scope === b.scope ? 0 : a.scope < b.scope ? -1 : 1));
|
|
216
|
+
|
|
217
|
+
function inRange(from: number, to: number): TimelineEvent[] {
|
|
218
|
+
// spans that began before from and last into it (open ones last to now), then everything that starts in [from, to)
|
|
219
|
+
const out: TimelineEvent[] = [];
|
|
220
|
+
for (let i = lower(spans, from - maxDur); i < spans.length && spans[i].t < from; i++) if (spans[i].t + spans[i].dur! > from) out.push(spans[i]);
|
|
221
|
+
for (const e of open.values()) if (e.t < from && now > from) out.push(e);
|
|
222
|
+
for (let i = lower(events, from); i < events.length && events[i].t < to; i++) out.push(events[i]);
|
|
223
|
+
return out;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function metricsFor(q: Query, at: (s: Scope) => Scope | undefined, from: number, to: number): (MetricPoint | MetricRollup)[] {
|
|
227
|
+
const want = q.series ? new Set(q.series) : undefined;
|
|
228
|
+
const acc = new Map<string, MetricRollup>();
|
|
229
|
+
for (const st of stores.values()) {
|
|
230
|
+
if (want && !want.has(st.series)) continue;
|
|
231
|
+
const row = at(st.scope);
|
|
232
|
+
if (row === undefined) continue;
|
|
233
|
+
const src = seriesSource.get(st.series) ?? '';
|
|
234
|
+
const cut = binCut(src, q.bin);
|
|
235
|
+
const list = q.bin ? binned(st, q.bin, q.tz) : st.fine;
|
|
236
|
+
for (let i = lower(list, q.bin ? binStart(from, q.bin, q.tz) : from); i < list.length && list[i].t < to; i++) {
|
|
237
|
+
const f = list[i];
|
|
238
|
+
if (f.t < cut) continue;
|
|
239
|
+
const key = `${st.series}\u0000${row}\u0000${f.t}`;
|
|
240
|
+
const a = acc.get(key);
|
|
241
|
+
if (a) combine(a as unknown as Fine, f);
|
|
242
|
+
else acc.set(key, { series: st.series, scope: row, t: f.t, bin: q.bin ?? '1m', min: f.min, max: f.max, sum: f.sum, count: f.count });
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const out = [...acc.values()].sort((a, b) => a.t - b.t || (a.series < b.series ? -1 : 1) || (a.scope < b.scope ? -1 : 1));
|
|
246
|
+
if (q.bin) return out;
|
|
247
|
+
// unbinned: points, a row's samples at one time combined (counters add; a gauge's mean)
|
|
248
|
+
return out.map(r => ({ series: r.series, scope: r.scope, t: r.t, value: r.count === 1 || seriesType.get(r.series) !== 'gauge' ? r.sum : r.sum / r.count }));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const encode = (e: TimelineEvent) => `${e.t}:${e.id}`;
|
|
252
|
+
const after = (e: TimelineEvent, c: string) => { const i = c.indexOf(':'), t = Number(c.slice(0, i)), id = c.slice(i + 1); return e.t > t || (e.t === t && e.id > id); };
|
|
253
|
+
|
|
254
|
+
/** A binned query (1h or 1d) without a search, kind, severity or scope filter, in whole-hour time, with nothing in
|
|
255
|
+
* its range cut by retention, is answered from the hour bins: the same answer, without reading every event. */
|
|
256
|
+
function fastBinned(q: Query): Result | undefined {
|
|
257
|
+
if (q.bin !== '1h' && q.bin !== '1d') return undefined;
|
|
258
|
+
if ((q.where?.all.length ?? 0) || q.q || q.filters?.kind?.length || q.filters?.severity?.length || q.filters?.scope?.length) return undefined;
|
|
259
|
+
if (offsetAt(q.tz, q.from) % HOUR || offsetAt(q.tz, q.to) % HOUR) return undefined;
|
|
260
|
+
const srcs = q.filters?.source?.length ? new Set(q.filters.source) : undefined;
|
|
261
|
+
// each source from its own start: the query's, or later where retention has cut it
|
|
262
|
+
const fromOf = (src: string) => Math.max(q.from, eventCut(src));
|
|
263
|
+
const { at, add, rows } = rowsOf(q);
|
|
264
|
+
const sources = new Set<string>();
|
|
265
|
+
const bins = new Map<string, EventBin>();
|
|
266
|
+
let total = 0;
|
|
267
|
+
const binOf = (row: Scope, t: number) => {
|
|
268
|
+
const bt = binStart(t, q.bin!, q.tz), key = `${row}\u0000${bt}`;
|
|
269
|
+
let b = bins.get(key);
|
|
270
|
+
if (!b) { b = { scope: row, t: bt, bin: q.bin!, count: 0, severity: {}, kinds: {} }; bins.set(key, b); }
|
|
271
|
+
return b;
|
|
272
|
+
};
|
|
273
|
+
const count = (row: Scope, t: number, n: number) => { const r = add(row); r.count += n; total += n; if (r.first === undefined || t < r.first) r.first = t; if (r.last === undefined || t > r.last) r.last = t; };
|
|
274
|
+
// whole hours from the bins; the part-hours at each source's edges, and spans that began before, by their events
|
|
275
|
+
const inner1 = Math.floor(q.to / HOUR) * HOUR;
|
|
276
|
+
for (const [scope, bySrc] of hours) {
|
|
277
|
+
const row = at(scope);
|
|
278
|
+
if (row === undefined) continue;
|
|
279
|
+
for (const [source, byT] of bySrc) {
|
|
280
|
+
if (srcs && !srcs.has(source)) continue;
|
|
281
|
+
const inner0 = Math.ceil(fromOf(source) / HOUR) * HOUR;
|
|
282
|
+
for (const [t, hb] of byT) {
|
|
283
|
+
if (t < inner0 || t >= inner1) continue;
|
|
284
|
+
count(row, hb.first, hb.count);
|
|
285
|
+
if (hb.last > (rows.get(row)!.last ?? -Infinity)) rows.get(row)!.last = hb.last;
|
|
286
|
+
sources.add(source);
|
|
287
|
+
const b = binOf(row, t);
|
|
288
|
+
b.count += hb.count;
|
|
289
|
+
for (const [k, n] of Object.entries(hb.severity)) b.severity[k as keyof typeof b.severity] = (b.severity[k as keyof typeof b.severity] ?? 0) + n;
|
|
290
|
+
for (const [k, n] of Object.entries(hb.kinds)) b.kinds![k] = (b.kinds![k] ?? 0) + n;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const take = (e: TimelineEvent) => within(e.scope, q.scope) && (!srcs || srcs.has(e.source)) && e.t >= eventCut(e.source) && at(e.scope) !== undefined;
|
|
295
|
+
const one = (e: TimelineEvent) => { const row = at(e.scope)!; count(row, e.t, 1); sources.add(e.source); const b = binOf(row, e.t); b.count++; b.severity[e.severity] = (b.severity[e.severity] ?? 0) + 1; b.kinds![e.kind] = (b.kinds![e.kind] ?? 0) + 1; };
|
|
296
|
+
for (const src of present) {
|
|
297
|
+
if (srcs && !srcs.has(src)) continue;
|
|
298
|
+
const a0 = fromOf(src), inner0 = Math.ceil(a0 / HOUR) * HOUR;
|
|
299
|
+
const edges: [number, number][] = inner0 >= inner1 ? [[a0, q.to]] : [[a0, inner0], [inner1, q.to]];
|
|
300
|
+
for (const [a, z] of edges) for (let i = lower(events, a); i < events.length && events[i].t < z; i++) { const e = events[i]; if (e.source === src && take(e)) one(e); }
|
|
301
|
+
}
|
|
302
|
+
const w = BIN_MS[q.bin], long: TimelineEvent[] = [];
|
|
303
|
+
// spans that began before the range and last into it: in the total and on their row (not in a bin), as a scan has them
|
|
304
|
+
for (let i = lower(spans, q.from - maxDur); i < spans.length && spans[i].t < q.from; i++) {
|
|
305
|
+
const e = spans[i];
|
|
306
|
+
if (e.t + e.dur! > q.from && take(e)) { count(at(e.scope)!, e.t, 1); sources.add(e.source); if (e.dur! >= w && long.length < 5000) long.push(e); }
|
|
307
|
+
}
|
|
308
|
+
for (const e of open.values()) if (e.t < q.from && now > q.from && take(e)) { count(at(e.scope)!, e.t, 1); sources.add(e.source); if (long.length < 5000) long.push(e); }
|
|
309
|
+
for (let i = lower(spans, q.from); i < spans.length && spans[i].t < q.to && long.length < 5000; i++) if (spans[i].dur! >= w && take(spans[i])) long.push(spans[i]);
|
|
310
|
+
for (const e of open.values()) if (e.t >= q.from && e.t < q.to && long.length < 5000 && take(e)) long.push(e);
|
|
311
|
+
const metrics = metricsFor(q, at, q.from, q.to);
|
|
312
|
+
for (const m of metrics) { const s2 = seriesSource.get(m.series); if (s2) sources.add(s2); }
|
|
313
|
+
const out = [...bins.values()].sort((a, b) => a.t - b.t || (a.scope < b.scope ? -1 : 1));
|
|
314
|
+
return { timeline: 1, type: 'result', rows: sortRows(rows), bins: out, spans: long, metrics, total, retention: effective(sources) };
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
async function query(q: Query): Promise<Result> {
|
|
318
|
+
if (!isScope(q.scope)) throw new Error(`not a scope path: ${JSON.stringify(q.scope)}`);
|
|
319
|
+
const fast = fastBinned(q);
|
|
320
|
+
if (fast) return fast;
|
|
321
|
+
const { test, range, warnings } = predicate(q);
|
|
322
|
+
const { at, add, rows } = rowsOf(q);
|
|
323
|
+
const sources = new Set<string>();
|
|
324
|
+
const hits: TimelineEvent[] = [];
|
|
325
|
+
for (const e of inRange(range.from, range.to)) {
|
|
326
|
+
if (!test(e)) continue;
|
|
327
|
+
const row = at(e.scope);
|
|
328
|
+
if (row === undefined) continue;
|
|
329
|
+
const r = add(row);
|
|
330
|
+
r.count++;
|
|
331
|
+
if (r.first === undefined || e.t < r.first) r.first = e.t;
|
|
332
|
+
if (r.last === undefined || e.t > r.last) r.last = e.t;
|
|
333
|
+
sources.add(e.source);
|
|
334
|
+
hits.push(e);
|
|
335
|
+
}
|
|
336
|
+
hits.sort(byTimeId);
|
|
337
|
+
const metrics = metricsFor(q, at, range.from, range.to);
|
|
338
|
+
for (const m of metrics) { const s = seriesSource.get(m.series); if (s) sources.add(s); }
|
|
339
|
+
const base = { timeline: 1 as const, type: 'result' as const, total: hits.length, ...(warnings.length ? { warnings } : {}) };
|
|
340
|
+
if (q.bin) {
|
|
341
|
+
const w = BIN_MS[q.bin], bins = new Map<string, EventBin>(), long: TimelineEvent[] = [];
|
|
342
|
+
for (const e of hits) {
|
|
343
|
+
if ((e.open || (e.dur !== undefined && e.dur >= w)) && long.length < 5000) long.push(e);
|
|
344
|
+
if (e.t < range.from) continue; // a span that began before the range: counted in its own bin, elsewhere
|
|
345
|
+
const row = at(e.scope)!, t = binStart(e.t, q.bin, q.tz), key = `${row}\u0000${t}`;
|
|
346
|
+
let b = bins.get(key);
|
|
347
|
+
if (!b) { b = { scope: row, t, bin: q.bin, count: 0, severity: {}, kinds: {} }; bins.set(key, b); }
|
|
348
|
+
b.count++;
|
|
349
|
+
b.severity[e.severity] = (b.severity[e.severity] ?? 0) + 1;
|
|
350
|
+
b.kinds![e.kind] = (b.kinds![e.kind] ?? 0) + 1;
|
|
351
|
+
}
|
|
352
|
+
const out = [...bins.values()].sort((a, b) => a.t - b.t || (a.scope < b.scope ? -1 : 1));
|
|
353
|
+
return { ...base, rows: sortRows(rows), bins: out, spans: long, metrics, retention: effective(sources) };
|
|
354
|
+
}
|
|
355
|
+
const limit = Math.min(10000, Math.max(1, q.limit ?? 2000));
|
|
356
|
+
const start = q.cursor ? hits.findIndex(e => after(e, q.cursor!)) : 0;
|
|
357
|
+
const page = start < 0 ? [] : hits.slice(start, start + limit);
|
|
358
|
+
const more = start >= 0 && start + limit < hits.length;
|
|
359
|
+
return { ...base, rows: sortRows(rows), events: page, metrics, retention: effective(sources), ...(more ? { next: encode(page[page.length - 1]) } : {}) };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const subs = new Set<{ q: Query; on: (b: Batch) => void }>();
|
|
363
|
+
const severityOk = (s: string) => (SEVERITIES as readonly string[]).includes(s);
|
|
364
|
+
let seq = 0;
|
|
365
|
+
|
|
366
|
+
/** an event, new or replacing the one with its id */
|
|
367
|
+
function put(e: TimelineEvent) {
|
|
368
|
+
const key = keyOf(e), old = byId.get(key);
|
|
369
|
+
if (old) {
|
|
370
|
+
remove(events, old.t, x => x === old);
|
|
371
|
+
tally(old, -1);
|
|
372
|
+
if (old.dur !== undefined && !old.open) remove(spans, old.t, x => x === old);
|
|
373
|
+
open.delete(key);
|
|
374
|
+
}
|
|
375
|
+
byId.set(key, e);
|
|
376
|
+
tally(e, 1);
|
|
377
|
+
place(events, e, () => false, byTimeId);
|
|
378
|
+
if (e.open) open.set(key, e);
|
|
379
|
+
else if (e.dur !== undefined) { place(spans, e, () => false, byTimeId); maxDur = Math.max(maxDur, e.dur); }
|
|
380
|
+
noteScope(e.scope);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
return {
|
|
384
|
+
async catalog(): Promise<Catalog> {
|
|
385
|
+
return { ...catalog, now, retention: effective(catalog.sources.map(s => s.name)) };
|
|
386
|
+
},
|
|
387
|
+
query,
|
|
388
|
+
subscribe(q, on) {
|
|
389
|
+
const sub = { q, on };
|
|
390
|
+
subs.add(sub);
|
|
391
|
+
return () => { subs.delete(sub); };
|
|
392
|
+
},
|
|
393
|
+
...(o.stateAt ? { stateAt: (scope: Scope, t: number) => o.stateAt!(scope, t) } : {}),
|
|
394
|
+
append(es, ms = [], t) {
|
|
395
|
+
if (t !== undefined) now = Math.max(now, t);
|
|
396
|
+
const known = new Set(scopes);
|
|
397
|
+
const fixed = es.map(fix);
|
|
398
|
+
for (const e of fixed) {
|
|
399
|
+
if (!severityOk(e.severity)) throw new Error(`not a severity: ${e.severity}`);
|
|
400
|
+
put(e);
|
|
401
|
+
}
|
|
402
|
+
for (const m of ms) addMetric(m);
|
|
403
|
+
seq++;
|
|
404
|
+
for (const s of subs) {
|
|
405
|
+
const { test } = predicate(s.q);
|
|
406
|
+
const { at } = rowsOf(s.q);
|
|
407
|
+
const evs = fixed.filter(e => test(e) && at(e.scope) !== undefined);
|
|
408
|
+
const want = s.q.series ? new Set(s.q.series) : undefined;
|
|
409
|
+
const mets = ms.filter(m => within(canon(m.scope), s.q.scope) && (!want || want.has(m.series))).map(m => ({ ...m, scope: at(canon(m.scope))! }));
|
|
410
|
+
const fresh = [...scopes].filter(x => !known.has(x));
|
|
411
|
+
const level = levelOf(s.q.scope) + s.q.depth;
|
|
412
|
+
const rows = fresh.filter(x => levelOf(x) === level && within(x, s.q.scope)).map(x => ({ scope: x, label: names.get(x)?.label ?? nameOf(x), kind: kindOf(x), depth: s.q.depth, children: 0, count: 0 }) as Row);
|
|
413
|
+
s.on({ timeline: 1, type: 'batch', events: evs, metrics: mets, ...(rows.length ? { rows } : {}), now, resume: String(seq) });
|
|
414
|
+
}
|
|
415
|
+
},
|
|
416
|
+
setRetention(source, r) { retention.set(source, { events: r.events, rollups: { ...r.rollups }, setBy: 'user' }); },
|
|
417
|
+
get now() { return now; },
|
|
418
|
+
get size() { let n = 0; for (const st of stores.values()) n += st.fine.length; return { events: events.length, samples: n }; },
|
|
419
|
+
};
|
|
420
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// A brushed range's events, exported (docs/timeline/README.md §4): JSON (a recording's events, as they are) or CSV (one
|
|
2
|
+
// row per event, attrs and links as columns). A sensitive attr is left out unless asked for.
|
|
3
|
+
|
|
4
|
+
import type { TimelineEvent, TimelineTypes, AnyTimeline } from './timeline-contract.ts';
|
|
5
|
+
|
|
6
|
+
const BASE = ['id', 'time', 't', 'dur', 'scope', 'source', 'kind', 'severity', 'summary'] as const;
|
|
7
|
+
const cell = (v: unknown) => {
|
|
8
|
+
if (v === undefined || v === null) return '';
|
|
9
|
+
// a string a spreadsheet would run as a formula starts with a quote mark
|
|
10
|
+
const s = typeof v === 'string' && /^[=+\-@\t\r]/.test(v) ? `'${v}` : String(v);
|
|
11
|
+
return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
12
|
+
};
|
|
13
|
+
const drop = <E extends { source: string; attrs: Record<string, unknown> }>(e: E, sensitive: Set<string>): E => (sensitive.size ? { ...e, attrs: Object.fromEntries(Object.entries(e.attrs).filter(([k]) => !sensitive.has(`${e.source}.${k}`))) } : e);
|
|
14
|
+
|
|
15
|
+
/** sensitive: "source.attr" names to leave out */
|
|
16
|
+
export function toJSON<T extends TimelineTypes = AnyTimeline>(events: TimelineEvent<T>[], sensitive: Iterable<string> = []): string {
|
|
17
|
+
const s = new Set(sensitive);
|
|
18
|
+
return JSON.stringify(events.map(e => drop(e, s)), null, 2) + '\n';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** CSV: the base columns, then attr.* and link.* columns for every key present (spreadsheet formulas defused) */
|
|
22
|
+
export function toCSV<T extends TimelineTypes = AnyTimeline>(events: TimelineEvent<T>[], sensitive: Iterable<string> = []): string {
|
|
23
|
+
const s = new Set(sensitive);
|
|
24
|
+
const es = events.map(e => drop(e, s));
|
|
25
|
+
const attrs = [...new Set(es.flatMap(e => Object.keys(e.attrs)))].sort();
|
|
26
|
+
const links = [...new Set(es.flatMap(e => Object.keys(e.links)))].sort();
|
|
27
|
+
const head = [...BASE, ...attrs.map(a => `attr.${a}`), ...links.map(l => `link.${l}`)];
|
|
28
|
+
const rows = es.map(e => [e.id, new Date(e.t).toISOString(), e.t, e.dur, e.scope, e.source, e.kind, e.severity, e.summary,
|
|
29
|
+
...attrs.map(a => e.attrs[a]), ...links.map(l => (e.links as Record<string, string | undefined>)[l])].map(cell).join(','));
|
|
30
|
+
return [head.join(','), ...rows].join('\r\n') + '\r\n';
|
|
31
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// The timeline's x axis: time to pixels, piecewise, with long empty stretches collapsed into narrow "time breaks"
|
|
2
|
+
// (docs/timeline/README.md §8). Pure.
|
|
3
|
+
// - findGaps(): the stretches of the visible range with no activity in any row shown (activity: an event's start, a
|
|
4
|
+
// span's end, a bin with events, a metric that changed), longer than a share of the range and than a floor.
|
|
5
|
+
// - timeScale(): the mapping. Each gap is drawn `cut` px wide (or wider, while it is being expanded: weight 0..1),
|
|
6
|
+
// the rest of the width shared out in proportion to time. x(t) is monotonic; t(x) is its inverse, except that an
|
|
7
|
+
// x inside a cut gives the nearer edge, so a playhead never stops in one and a drag across one jumps it.
|
|
8
|
+
// - ticks(): round times in the owner's zone, every segment on its own, none inside a gap.
|
|
9
|
+
|
|
10
|
+
import { offsetAt } from './timeline-time.ts';
|
|
11
|
+
|
|
12
|
+
export interface Gap { from: number; to: number }
|
|
13
|
+
export interface Cut extends Gap { x0: number; x1: number; weight: number }
|
|
14
|
+
|
|
15
|
+
const MIN = 60_000, HOUR = 3_600_000, DAY = 86_400_000;
|
|
16
|
+
|
|
17
|
+
/** The gaps of [t0, t1): activity intervals are [a, b) (a mark is [t, t]); a gap is a stretch between them (or between
|
|
18
|
+
* the range's edges and them) longer than minFrac of the range and than minMs. */
|
|
19
|
+
export function findGaps(activity: [number, number][], t0: number, t1: number, o: { minFrac?: number; minMs?: number } = {}): Gap[] {
|
|
20
|
+
const minLen = Math.max((o.minFrac ?? 0.15) * (t1 - t0), o.minMs ?? 10 * MIN);
|
|
21
|
+
const xs = activity.filter(([a, b]) => b >= t0 && a < t1).map(([a, b]) => [Math.max(t0, a), Math.min(t1, Math.max(a, b))] as [number, number]).sort((p, q) => p[0] - q[0]);
|
|
22
|
+
const out: Gap[] = [];
|
|
23
|
+
// an empty view has nothing to judge against: no gaps
|
|
24
|
+
if (!xs.length) return out;
|
|
25
|
+
let at = t0;
|
|
26
|
+
for (const [a, b] of xs) { if (a - at > minLen) out.push({ from: at, to: a }); at = Math.max(at, b); }
|
|
27
|
+
if (t1 - at > minLen) out.push({ from: at, to: t1 });
|
|
28
|
+
return out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** "14 h", "2 d 3 h", "45 min" */
|
|
32
|
+
export function gapLabel(ms: number): string {
|
|
33
|
+
if (ms >= DAY) { const d = Math.floor(ms / DAY), hh = Math.round((ms - d * DAY) / HOUR); return hh ? `${d} d ${hh} h` : `${d} d`; }
|
|
34
|
+
if (ms >= HOUR) { const hh = Math.floor(ms / HOUR), m = Math.round((ms - hh * HOUR) / MIN); return m && hh < 10 ? `${hh} h ${m} min` : `${hh} h`; }
|
|
35
|
+
return `${Math.max(1, Math.round(ms / MIN))} min`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface TimeScale {
|
|
39
|
+
t0: number; t1: number; width: number;
|
|
40
|
+
cuts: Cut[];
|
|
41
|
+
x(t: number): number;
|
|
42
|
+
/** the time at x; inside a cut, its nearer edge */
|
|
43
|
+
t(x: number): number;
|
|
44
|
+
/** the cut at x, if any */
|
|
45
|
+
cutAt(x: number): Cut | undefined;
|
|
46
|
+
/** visible ms per px outside the cuts */
|
|
47
|
+
msPerPx: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** weights: how far each gap is expanded (0: a cut; 1: its own width in time), by gapKey */
|
|
51
|
+
export const gapKey = (g: Gap) => `${g.from}-${g.to}`;
|
|
52
|
+
|
|
53
|
+
export function timeScale(t0: number, t1: number, width: number, gaps: Gap[] = [], o: { cut?: number; weights?: Map<string, number> } = {}): TimeScale {
|
|
54
|
+
const cutPx = o.cut ?? 24, W = Math.max(1, width), span = Math.max(1, t1 - t0);
|
|
55
|
+
const gs = gaps.filter(g => g.to > t0 && g.from < t1).map(g => ({ from: Math.max(t0, g.from), to: Math.min(t1, g.to), w: o.weights?.get(gapKey(g)) ?? 0 })).sort((a, b) => a.from - b.from);
|
|
56
|
+
// a fully expanded gap is just time; a collapsed one costs cutPx. Solve for k (px per ms outside the cuts):
|
|
57
|
+
// W = k * (span - Σ len) + Σ ((1 - w) * cutPx + w * k * len)
|
|
58
|
+
const lenC = gs.reduce((n, g) => n + (g.to - g.from) * (1 - g.w), 0);
|
|
59
|
+
const fixed = gs.reduce((n, g) => n + (1 - g.w) * cutPx, 0);
|
|
60
|
+
let k = (W - fixed) / Math.max(1, span - lenC);
|
|
61
|
+
if (!(k > 0) || fixed > W * 0.6) { // too many cuts for the width: draw them as plain time
|
|
62
|
+
return timeScale(t0, t1, width, [], o);
|
|
63
|
+
}
|
|
64
|
+
// the breakpoints: (t, x) at every gap's edges
|
|
65
|
+
const pts: [number, number][] = [[t0, 0]];
|
|
66
|
+
let x = 0, t = t0;
|
|
67
|
+
const cuts: Cut[] = [];
|
|
68
|
+
for (const g of gs) {
|
|
69
|
+
x += (g.from - t) * k; pts.push([g.from, x]);
|
|
70
|
+
const wpx = (1 - g.w) * cutPx + g.w * k * (g.to - g.from);
|
|
71
|
+
cuts.push({ from: g.from, to: g.to, x0: x, x1: x + wpx, weight: g.w });
|
|
72
|
+
x += wpx; pts.push([g.to, x]); t = g.to;
|
|
73
|
+
}
|
|
74
|
+
pts.push([t1, x + (t1 - t) * k]);
|
|
75
|
+
const xOf = (tt: number) => {
|
|
76
|
+
if (tt <= t0) return (tt - t0) * k;
|
|
77
|
+
if (tt >= t1) return pts[pts.length - 1][1] + (tt - t1) * k;
|
|
78
|
+
let lo = 0, hi = pts.length - 1;
|
|
79
|
+
while (hi - lo > 1) { const m = (lo + hi) >> 1; if (pts[m][0] <= tt) lo = m; else hi = m; }
|
|
80
|
+
const [ta, xa] = pts[lo], [tb, xb] = pts[hi];
|
|
81
|
+
return tb === ta ? xa : xa + ((tt - ta) / (tb - ta)) * (xb - xa);
|
|
82
|
+
};
|
|
83
|
+
const cutAt = (px: number) => cuts.find(c => c.weight < 1 && px > c.x0 && px < c.x1);
|
|
84
|
+
const tOf = (px: number) => {
|
|
85
|
+
const c = cutAt(px);
|
|
86
|
+
if (c) return px - c.x0 < c.x1 - px ? c.from : c.to;
|
|
87
|
+
if (px <= 0) return t0 + px / k;
|
|
88
|
+
let lo = 0, hi = pts.length - 1;
|
|
89
|
+
if (px >= pts[hi][1]) return t1 + (px - pts[hi][1]) / k;
|
|
90
|
+
while (hi - lo > 1) { const m = (lo + hi) >> 1; if (pts[m][1] <= px) lo = m; else hi = m; }
|
|
91
|
+
const [ta, xa] = pts[lo], [tb, xb] = pts[hi];
|
|
92
|
+
return xb === xa ? ta : ta + ((px - xa) / (xb - xa)) * (tb - ta);
|
|
93
|
+
};
|
|
94
|
+
return { t0, t1, width: W, cuts, x: xOf, t: tOf, cutAt, msPerPx: 1 / k };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const STEPS = [1000, 5000, 15000, 30000, MIN, 5 * MIN, 15 * MIN, 30 * MIN, HOUR, 3 * HOUR, 6 * HOUR, 12 * HOUR, DAY, 2 * DAY, 7 * DAY, 14 * DAY, 28 * DAY];
|
|
98
|
+
export interface Tick { t: number; x: number; label: string; major: boolean }
|
|
99
|
+
|
|
100
|
+
/** round times at least minPx apart, in the owner's zone (local hours and days), skipping the cuts */
|
|
101
|
+
export function ticks(s: TimeScale, tz?: string, minPx = 80): Tick[] {
|
|
102
|
+
const step = STEPS.find(st => st / s.msPerPx >= minPx) ?? STEPS[STEPS.length - 1];
|
|
103
|
+
const out: Tick[] = [];
|
|
104
|
+
const fmt = (t: number) => {
|
|
105
|
+
const local = new Date(t + offsetAt(tz, t));
|
|
106
|
+
const hh = String(local.getUTCHours()).padStart(2, '0'), mm = String(local.getUTCMinutes()).padStart(2, '0'), ss = String(local.getUTCSeconds()).padStart(2, '0');
|
|
107
|
+
const day = `${local.getUTCDate()} ${['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][local.getUTCMonth()]}`;
|
|
108
|
+
const midnight = hh === '00' && mm === '00' && ss === '00';
|
|
109
|
+
if (step >= DAY || midnight) return { label: day, major: true };
|
|
110
|
+
return { label: step < MIN ? `${hh}:${mm}:${ss}` : `${hh}:${mm}`, major: false };
|
|
111
|
+
};
|
|
112
|
+
// each stretch between cuts on its own, so a tick never sits in a gap and resumes just after one
|
|
113
|
+
const segs: [number, number][] = [];
|
|
114
|
+
let a = s.t0;
|
|
115
|
+
for (const c of s.cuts) if (c.weight < 1) { segs.push([a, c.from]); a = c.to; }
|
|
116
|
+
segs.push([a, s.t1]);
|
|
117
|
+
for (const [from, to] of segs) {
|
|
118
|
+
// round local times (lt), each turned back to UTC at the offset in force then
|
|
119
|
+
const utc = (lt: number) => lt - offsetAt(tz, lt - offsetAt(tz, lt));
|
|
120
|
+
let lt = Math.ceil((from + offsetAt(tz, from)) / step) * step;
|
|
121
|
+
if (step >= 7 * DAY) lt = Math.ceil((from + offsetAt(tz, from)) / DAY) * DAY;
|
|
122
|
+
for (let guard = 0, t = utc(lt); t < to && guard < 2000; guard++, lt += step, t = utc(lt)) {
|
|
123
|
+
if (t < from) continue;
|
|
124
|
+
const x = s.x(t), prev = out[out.length - 1];
|
|
125
|
+
if (!prev || x - prev.x >= minPx * 0.6) out.push({ t, x, ...fmt(t) });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// Scope paths (docs/timeline/README.md §1): kind:name segments from the fleet ("") down, joined by "/". Pure. What
|
|
2
|
+
// a path may say (its kinds and their nesting) is the domain's catalogue's: scopeRules(cat).
|
|
3
|
+
|
|
4
|
+
import { scopePattern, type Scope, type TimelineCatalog } from './timeline-contract.ts';
|
|
5
|
+
|
|
6
|
+
export interface Segment { kind: string; name: string }
|
|
7
|
+
|
|
8
|
+
/** what a path of this catalogue may say: isScope, parseScope (its segments, names unescaped; throws on a path that
|
|
9
|
+
* isn't one), and wellNested (each segment may sit in its parent's kind) */
|
|
10
|
+
export function scopeRules(cat: Pick<TimelineCatalog, 'kinds' | 'parents'>) {
|
|
11
|
+
const RE = new RegExp(scopePattern(cat));
|
|
12
|
+
const isScope = (s: string) => RE.test(s);
|
|
13
|
+
function parseScope(s: Scope): Segment[] {
|
|
14
|
+
if (!RE.test(s)) throw new Error(`not a scope path: ${JSON.stringify(s)}`);
|
|
15
|
+
if (s === '') return [];
|
|
16
|
+
return s.split('/').map(seg => {
|
|
17
|
+
const i = seg.indexOf(':');
|
|
18
|
+
return { kind: seg.slice(0, i), name: decodeURIComponent(seg.slice(i + 1)) };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function wellNested(s: Scope): boolean {
|
|
22
|
+
let parent = '';
|
|
23
|
+
for (const seg of parseScope(s)) { if (!cat.parents[seg.kind].includes(parent)) return false; parent = seg.kind; }
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return { isScope, parseScope, wellNested };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** a name, escaped for a segment */
|
|
30
|
+
export const escapeName = (n: string) => n.replace(/[%/:\s]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase().padStart(2, '0')}`);
|
|
31
|
+
export const formatScope = (segs: Segment[]): Scope => segs.map(s => `${s.kind}:${escapeName(s.name)}`).join('/');
|
|
32
|
+
export const join = (parent: Scope, kind: string, name: string): Scope => (parent ? `${parent}/` : '') + `${kind}:${escapeName(name)}`;
|
|
33
|
+
|
|
34
|
+
/** levels below the fleet */
|
|
35
|
+
export const levelOf = (s: Scope) => (s === '' ? 0 : s.split('/').length);
|
|
36
|
+
/** the scope's prefix at a level (itself when it is shallower) */
|
|
37
|
+
export function prefixAt(s: Scope, level: number): Scope {
|
|
38
|
+
if (level <= 0) return '';
|
|
39
|
+
const parts = s === '' ? [] : s.split('/');
|
|
40
|
+
return parts.slice(0, level).join('/');
|
|
41
|
+
}
|
|
42
|
+
export const parentOf = (s: Scope): Scope => prefixAt(s, levelOf(s) - 1);
|
|
43
|
+
/** segment by segment: a prefix matches itself and everything under it */
|
|
44
|
+
export const within = (s: Scope, prefix: Scope) => prefix === '' || s === prefix || s.startsWith(`${prefix}/`);
|
|
45
|
+
/** any segment equal to seg ("kind:name") */
|
|
46
|
+
export const hasSegment = (s: Scope, seg: string) => s !== '' && s.split('/').includes(seg);
|
|
47
|
+
export const kindOf = (s: Scope): string => (s === '' ? 'fleet' : s.slice(s.lastIndexOf('/') + 1).split(':')[0]);
|
|
48
|
+
export const nameOf = (s: Scope) => (s === '' ? 'all hosts' : decodeURIComponent(s.slice(s.lastIndexOf('/') + 1).split(':').slice(1).join(':')));
|
|
49
|
+
|
|
50
|
+
/** breadcrumbs: the fleet, then each prefix */
|
|
51
|
+
export function crumbs(s: Scope): { scope: Scope; label: string; kind: string }[] {
|
|
52
|
+
const out: { scope: Scope; label: string; kind: string }[] = [{ scope: '', label: 'all hosts', kind: 'fleet' }];
|
|
53
|
+
for (let l = 1; l <= levelOf(s); l++) { const p = prefixAt(s, l); out.push({ scope: p, label: nameOf(p), kind: kindOf(p) }); }
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
|