@open-artifact/data-core 0.12.5 → 0.13.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-artifact/data-core",
3
- "version": "0.12.5",
3
+ "version": "0.13.0",
4
4
  "description": "The Data type's model, free of DOM and React: dataset and dashboard documents, filters and their rules, SQL filling, tables and data files, connector answer readers, dash/look colors, drafts, the view context and the project check.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -16,8 +16,8 @@
16
16
  "NOTICE"
17
17
  ],
18
18
  "dependencies": {
19
- "@open-artifact/live-store": "0.12.5",
20
- "@open-artifact/wasm-data-rules": "0.12.5"
19
+ "@open-artifact/live-store": "0.13.0",
20
+ "@open-artifact/wasm-data-rules": "0.13.0"
21
21
  },
22
22
  "license": "Apache-2.0",
23
23
  "author": {
package/src/check.js CHANGED
@@ -324,6 +324,8 @@ function checkDataset(id, data, params, loaders, context, report) {
324
324
  warn(`source.connector "${source.connector}" isn’t set up and turned on in connectors.json (${[...context.connectors].join(', ') || 'none is'}), so the query doesn’t run (runs says absent) until the person adds that server or you name one of theirs.`);
325
325
  if (source.tool === '')
326
326
  issue('source.tool is missing: the tool the query calls.');
327
+ if (data.snapshot !== undefined && data.snapshot !== false)
328
+ issue(`snapshot is false or left out (false keeps this query's rows off a published dashboard's snapshot); the page ignores ${JSON.stringify(data.snapshot)}.`);
327
329
  if (!source.call)
328
330
  report(doc, 'info', 'is written with the older kind "query"; "call" takes filters with rules.');
329
331
  if (source.rulesRefused)
package/src/index.d.ts CHANGED
@@ -23,3 +23,4 @@ export * from './imports.ts';
23
23
  export * from './view-context.ts';
24
24
  export * from './check.ts';
25
25
  export * from './check-page.ts';
26
+ export * from './live-snapshot.ts';
package/src/index.js CHANGED
@@ -23,3 +23,4 @@ export * from "./imports.js";
23
23
  export * from "./view-context.js";
24
24
  export * from "./check.js";
25
25
  export * from "./check-page.js";
26
+ export * from "./live-snapshot.js";
@@ -0,0 +1,99 @@
1
+ /**
2
+ * The live-data snapshot of a published dashboard: the answers its live queries gave on the owner's computer, kept as
3
+ * saved rows so a page with no connectors (every page a cloud host serves) can still show real figures.
4
+ *
5
+ * Connectors run only on the owner's computer. While a dashboard is published (and the owner confirmed that its data
6
+ * is published with it), the owner's linked daemon runs each live query there, at the dashboard's stored filters, and
7
+ * writes what the connector answered into the store's `data/` space:
8
+ *
9
+ * - `data/live` ({@link LiveSnapshotStatus}): when the snapshot was taken, on whose computer, and why the last refresh
10
+ * failed, if it did;
11
+ * - `data/live/answers/<dataset id>` ({@link LiveSnapshotAnswer}): one document per query, holding the calls it made and
12
+ * their answers as the connector gave them.
13
+ *
14
+ * A page that cannot call a connector replays these answers through the same readers and loaders a live answer goes
15
+ * through ({@link findSnapshotCall}): the figures are the owner's, as of `at`. A call the snapshot does not hold (a
16
+ * viewer's own filter value, a loader's follow-up call the daemon could not make) stays "not available in this view".
17
+ * Each answer is one document, so the store's 256 KB document limit is per query: a query whose answer is larger fails
18
+ * by itself, by name, and is never cut short. A failed refresh keeps the answers it had.
19
+ *
20
+ * A dataset opts out with `"snapshot": false` in its document: its query never leaves the owner's computer.
21
+ */
22
+ import type { UnknownRecord } from './types.ts';
23
+ import type { McpToolResult } from './connector-types.ts';
24
+ /** The snapshot's status document. */
25
+ export declare const LIVE_SNAPSHOT_PATH = "data/live";
26
+ /** The collection of the snapshot's answers, one document per dataset id. */
27
+ export declare const LIVE_SNAPSHOT_ANSWERS = "data/live/answers";
28
+ /** The largest snapshot document, bytes of its JSON (the store's document limit). */
29
+ export declare const LIVE_SNAPSHOT_DOC_MAX_BYTES: number;
30
+ /** Why a refresh did not bring new answers, and when. */
31
+ export interface LiveSnapshotFailure {
32
+ at: string;
33
+ reason: string;
34
+ }
35
+ /** `data/live`: the snapshot as a whole. */
36
+ export interface LiveSnapshotStatus {
37
+ /** When the newest answers were taken (ISO). */
38
+ at: string | null;
39
+ /** Whose computer took them: the owner's name. */
40
+ owner: string;
41
+ /** The queries the last refresh asked for, and the ones of them that failed (the rest of `data/live/answers` stays). */
42
+ queries?: string[];
43
+ /** The last refresh failed (in whole, or these queries): the answers before it are kept. Only the owner is shown it. */
44
+ failed?: LiveSnapshotFailure & {
45
+ queries?: string[];
46
+ };
47
+ }
48
+ /** One call a query made, and its answer as the connector gave it. */
49
+ export interface LiveSnapshotCall {
50
+ tool: string;
51
+ args: UnknownRecord;
52
+ result: McpToolResult;
53
+ }
54
+ /** `data/live/answers/<dataset id>`: one query's answers. */
55
+ export interface LiveSnapshotAnswer {
56
+ /** When these answers were taken (ISO); `null` before any refresh succeeded. */
57
+ at: string | null;
58
+ connector: string;
59
+ calls: LiveSnapshotCall[];
60
+ /** This query's last refresh failed: `calls` are the ones before it. */
61
+ failed?: LiveSnapshotFailure;
62
+ }
63
+ /** A live query of a dashboard, as a refresh runs it: its dataset, connector, tool and filled arguments. */
64
+ export interface LiveQueryPlan {
65
+ id: string;
66
+ connector: string;
67
+ tool: string;
68
+ args: UnknownRecord;
69
+ /** Why it can't run at the stored filters (an unfilled filter, a refused rule), else absent. */
70
+ problem?: string;
71
+ }
72
+ /** The path of a dataset's answers. */
73
+ export declare function liveSnapshotAnswerPath(id: string): string;
74
+ /** Whether a store path is part of the snapshot (only the owner's daemon writes it). */
75
+ export declare function isLiveSnapshotPath(path: string): boolean;
76
+ /** A JSON text whose object keys are sorted: two equal arguments give one text. */
77
+ export declare function canonicalJson(value: unknown): string;
78
+ /** Whether the dataset document keeps its query off the snapshot (`"snapshot": false`). */
79
+ export declare function isSnapshotOptedOut(document: unknown): boolean;
80
+ /**
81
+ * The dashboard's live queries as a refresh runs them: every query dataset not opted out, filled with the stored
82
+ * filters (`dash/params`), in dataset id order. `only` keeps the named ones.
83
+ */
84
+ export declare function planLiveQueries(documents: ReadonlyMap<string, UnknownRecord>, only?: readonly string[]): LiveQueryPlan[];
85
+ /** Whether a dashboard has any live query the snapshot would hold. */
86
+ export declare function hasLiveQueries(documents: ReadonlyMap<string, UnknownRecord>): boolean;
87
+ /** Bytes of a document's JSON. */
88
+ export declare function jsonBytes(value: unknown): number;
89
+ /** Why a query's answers can't be kept (larger than a document may be), naming it; `null` when they fit. */
90
+ export declare function liveAnswerSizeProblem(id: string, answer: LiveSnapshotAnswer): string | null;
91
+ /** A snapshot status document as read, or `null` when it isn't one. */
92
+ export declare function parseLiveSnapshotStatus(value: unknown): LiveSnapshotStatus | null;
93
+ /** A snapshot answer document as read, or `null` when it isn't one. */
94
+ export declare function parseLiveSnapshotAnswer(value: unknown): LiveSnapshotAnswer | null;
95
+ /** The recorded answer of this exact call (connector, tool and arguments, key order aside), or `null`. */
96
+ export declare function findSnapshotCall(answers: Iterable<LiveSnapshotAnswer>, connector: string, tool: string, args: UnknownRecord): {
97
+ result: McpToolResult;
98
+ at: string | null;
99
+ } | null;
@@ -0,0 +1,137 @@
1
+ /**
2
+ * The live-data snapshot of a published dashboard: the answers its live queries gave on the owner's computer, kept as
3
+ * saved rows so a page with no connectors (every page a cloud host serves) can still show real figures.
4
+ *
5
+ * Connectors run only on the owner's computer. While a dashboard is published (and the owner confirmed that its data
6
+ * is published with it), the owner's linked daemon runs each live query there, at the dashboard's stored filters, and
7
+ * writes what the connector answered into the store's `data/` space:
8
+ *
9
+ * - `data/live` ({@link LiveSnapshotStatus}): when the snapshot was taken, on whose computer, and why the last refresh
10
+ * failed, if it did;
11
+ * - `data/live/answers/<dataset id>` ({@link LiveSnapshotAnswer}): one document per query, holding the calls it made and
12
+ * their answers as the connector gave them.
13
+ *
14
+ * A page that cannot call a connector replays these answers through the same readers and loaders a live answer goes
15
+ * through ({@link findSnapshotCall}): the figures are the owner's, as of `at`. A call the snapshot does not hold (a
16
+ * viewer's own filter value, a loader's follow-up call the daemon could not make) stays "not available in this view".
17
+ * Each answer is one document, so the store's 256 KB document limit is per query: a query whose answer is larger fails
18
+ * by itself, by name, and is never cut short. A failed refresh keeps the answers it had.
19
+ *
20
+ * A dataset opts out with `"snapshot": false` in its document: its query never leaves the owner's computer.
21
+ */
22
+ import { isPlainObject } from "./text.js";
23
+ import { DATASETS_COLLECTION } from "./paths.js";
24
+ import { prepareDashboardContent } from "./fill.js";
25
+ import { composeDashboardDocuments } from "./records.js";
26
+ import { parseStoredQueryArgs } from "./connectors.js";
27
+ /** The snapshot's status document. */
28
+ export const LIVE_SNAPSHOT_PATH = 'data/live';
29
+ /** The collection of the snapshot's answers, one document per dataset id. */
30
+ export const LIVE_SNAPSHOT_ANSWERS = 'data/live/answers';
31
+ /** The largest snapshot document, bytes of its JSON (the store's document limit). */
32
+ export const LIVE_SNAPSHOT_DOC_MAX_BYTES = 256 * 1024;
33
+ /** The path of a dataset's answers. */
34
+ export function liveSnapshotAnswerPath(id) {
35
+ return `${LIVE_SNAPSHOT_ANSWERS}/${id}`;
36
+ }
37
+ /** Whether a store path is part of the snapshot (only the owner's daemon writes it). */
38
+ export function isLiveSnapshotPath(path) {
39
+ return path === LIVE_SNAPSHOT_PATH || path.startsWith(`${LIVE_SNAPSHOT_ANSWERS}/`);
40
+ }
41
+ /** A JSON text whose object keys are sorted: two equal arguments give one text. */
42
+ export function canonicalJson(value) {
43
+ if (Array.isArray(value))
44
+ return `[${value.map((item) => canonicalJson(item)).join(',')}]`;
45
+ if (typeof value !== 'object' || value === null)
46
+ return JSON.stringify(value) ?? 'null';
47
+ const record = value;
48
+ const entries = [];
49
+ for (const key of Object.keys(record).sort())
50
+ if (record[key] !== undefined)
51
+ entries.push(`${JSON.stringify(key)}:${canonicalJson(record[key])}`);
52
+ return `{${entries.join(',')}}`;
53
+ }
54
+ /** Whether the dataset document keeps its query off the snapshot (`"snapshot": false`). */
55
+ export function isSnapshotOptedOut(document) {
56
+ return isPlainObject(document) && document.snapshot === false;
57
+ }
58
+ /**
59
+ * The dashboard's live queries as a refresh runs them: every query dataset not opted out, filled with the stored
60
+ * filters (`dash/params`), in dataset id order. `only` keeps the named ones.
61
+ */
62
+ export function planLiveQueries(documents, only) {
63
+ const content = prepareDashboardContent(composeDashboardDocuments(documents).content);
64
+ const plans = [];
65
+ for (const dataset of content.datasets) {
66
+ const source = dataset.source;
67
+ if (source.kind !== 'query')
68
+ continue;
69
+ if (isSnapshotOptedOut(documents.get(`${DATASETS_COLLECTION}/${dataset.id}`)))
70
+ continue;
71
+ if (only !== undefined && !only.includes(dataset.id))
72
+ continue;
73
+ const problem = source.problem ?? source.unfilled ?? (source.connector === '' ? 'it names no connector' : source.tool === '' ? 'it names no tool' : undefined);
74
+ plans.push({ id: dataset.id, connector: source.connector, tool: source.tool, args: parseStoredQueryArgs(source), ...(problem === undefined ? {} : { problem }) });
75
+ }
76
+ return plans.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
77
+ }
78
+ /** Whether a dashboard has any live query the snapshot would hold. */
79
+ export function hasLiveQueries(documents) {
80
+ return planLiveQueries(documents).length > 0;
81
+ }
82
+ /** Bytes of a document's JSON. */
83
+ export function jsonBytes(value) {
84
+ return new TextEncoder().encode(JSON.stringify(value)).length;
85
+ }
86
+ /** Why a query's answers can't be kept (larger than a document may be), naming it; `null` when they fit. */
87
+ export function liveAnswerSizeProblem(id, answer) {
88
+ const bytes = jsonBytes(answer);
89
+ if (bytes <= LIVE_SNAPSHOT_DOC_MAX_BYTES)
90
+ return null;
91
+ const kb = (n) => `${Math.ceil(n / 1024)} KB`;
92
+ return `The answer of query "${id}" is ${kb(bytes)}, over the ${kb(LIVE_SNAPSHOT_DOC_MAX_BYTES)} a saved document may hold, so it was not published (nothing was cut). Aggregate it in the query, or keep it on this computer with "snapshot": false.`;
93
+ }
94
+ /** A snapshot status document as read, or `null` when it isn't one. */
95
+ export function parseLiveSnapshotStatus(value) {
96
+ if (!isPlainObject(value))
97
+ return null;
98
+ const at = typeof value.at === 'string' && value.at !== '' ? value.at : null;
99
+ const owner = typeof value.owner === 'string' ? value.owner : '';
100
+ const status = { at, owner };
101
+ if (Array.isArray(value.queries))
102
+ status.queries = value.queries.filter((item) => typeof item === 'string');
103
+ const failed = value.failed;
104
+ if (isPlainObject(failed) && typeof failed.reason === 'string') {
105
+ status.failed = { at: typeof failed.at === 'string' ? failed.at : '', reason: failed.reason };
106
+ if (Array.isArray(failed.queries))
107
+ status.failed.queries = failed.queries.filter((item) => typeof item === 'string');
108
+ }
109
+ return status;
110
+ }
111
+ /** A snapshot answer document as read, or `null` when it isn't one. */
112
+ export function parseLiveSnapshotAnswer(value) {
113
+ if (!isPlainObject(value) || typeof value.connector !== 'string' || !Array.isArray(value.calls))
114
+ return null;
115
+ const calls = [];
116
+ for (const call of value.calls) {
117
+ if (!isPlainObject(call) || typeof call.tool !== 'string' || !isPlainObject(call.result))
118
+ continue;
119
+ calls.push({ tool: call.tool, args: isPlainObject(call.args) ? call.args : {}, result: call.result });
120
+ }
121
+ const answer = { at: typeof value.at === 'string' && value.at !== '' ? value.at : null, connector: value.connector, calls };
122
+ if (isPlainObject(value.failed) && typeof value.failed.reason === 'string')
123
+ answer.failed = { at: typeof value.failed.at === 'string' ? value.failed.at : '', reason: value.failed.reason };
124
+ return answer;
125
+ }
126
+ /** The recorded answer of this exact call (connector, tool and arguments, key order aside), or `null`. */
127
+ export function findSnapshotCall(answers, connector, tool, args) {
128
+ const wanted = canonicalJson(args);
129
+ for (const answer of answers) {
130
+ if (answer.connector !== connector)
131
+ continue;
132
+ for (const call of answer.calls)
133
+ if (call.tool === tool && canonicalJson(call.args) === wanted)
134
+ return { result: call.result, at: answer.at };
135
+ }
136
+ return null;
137
+ }