@akohn/dashql 0.0.2-dev.17 → 0.0.19-dev.247

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.
@@ -140,7 +140,6 @@ export declare class Runtime {
140
140
  }>;
141
141
  exportLink(workbookId: string, linkBase: string, options?: Omit<ExportOptions, 'results'>): Promise<string>;
142
142
  exportTables(workbookId: string, options: ExportOptions, consume: (tables: WorkbookTables) => Promise<void>): Promise<core.ExportedResults>;
143
- importCachedResult(key: string, data: Blob): Promise<boolean>;
144
143
  readImport(source: ImportSource): Promise<core.ImportPreview>;
145
144
  commitImport(importId: number, answers: core.ImportAnswers, onStep?: (step: core.ImportStep, results: core.ImportedResults | null) => void): Promise<core.ImportOutcome>;
146
145
  get destroyed(): boolean;
@@ -371,15 +371,6 @@ var Runtime = class {
371
371
  await consume(tables);
372
372
  })).results;
373
373
  }
374
- async importCachedResult(key, data) {
375
- const module = this.live();
376
- const file = this.handles.register(data);
377
- try {
378
- return await module.importCachedResult(key, file);
379
- } finally {
380
- this.handles.release(file);
381
- }
382
- }
383
374
  async readImport(source) {
384
375
  const module = this.live();
385
376
  if (!("archive" in source)) return await module.readImport(source);
@@ -0,0 +1,22 @@
1
+ import type { ConnectionState } from '../platform/connections/connection_state.js';
2
+ import type { RunEvent } from '../platform/workbook/run_events.js';
3
+ import { type WorkbookScripts } from '../platform/workbook/workbook_scripts.js';
4
+ import type { Runtime } from './runtime.js';
5
+ export declare function describeRunError(error: unknown): string;
6
+ export declare function runScriptsOf(workbook: WorkbookScripts, names: readonly string[]): {
7
+ selected: string[];
8
+ requested: boolean;
9
+ unknown: string[];
10
+ };
11
+ export interface ReportedRunOptions {
12
+ label: string;
13
+ scripts: readonly string[];
14
+ refresh: boolean;
15
+ signal: AbortSignal;
16
+ progressIntervalMs: number | null;
17
+ prepareConnection?: (conn: ConnectionState) => Promise<string | null>;
18
+ resultFile?: (key: string) => Promise<string | null>;
19
+ onReportError?: (error: unknown) => void;
20
+ }
21
+ export type ReportedRunEnd = 'ok' | 'failed' | 'interrupted';
22
+ export declare function runWorkbookReported(runtime: Runtime, workbook: WorkbookScripts, options: ReportedRunOptions, emit: (event: RunEvent) => void): Promise<ReportedRunEnd>;
@@ -0,0 +1,248 @@
1
+ import { LoggableException } from "../platform/logger/logger.js";
2
+ import { scriptResultOwner } from "../platform/workbook/workbook_scripts.js";
3
+ // dist/.stage/src/runtime/workbook_run_report.ts
4
+ function describeRunError(error) {
5
+ if (error instanceof LoggableException) {
6
+ const text = (value) => typeof value === "string" && value.length > 0 ? value : null;
7
+ const [errorMessage, status, message] = [
8
+ text(error.keyValues.errorMessage),
9
+ text(error.keyValues.status),
10
+ text(error.keyValues.message)
11
+ ];
12
+ if (errorMessage) return errorMessage;
13
+ if (status) return `${error.message} (${status}${message ? ` ${message}` : ""})`;
14
+ return error.message;
15
+ }
16
+ if (!(error instanceof Error)) return String(error);
17
+ return error.cause instanceof Error ? `${error.message}: ${error.cause.message}` : error.message;
18
+ }
19
+ function runConnectionLabel(conn) {
20
+ const params = conn.params;
21
+ if ("trino" in params) return `trino ${params.trino.endpoint}`;
22
+ if ("hyper" in params) return params.hyper.protocol === "WASM" ? "the embedded hyper" : `hyper ${params.hyper.endpoint ?? ""}`.trim();
23
+ if ("salesforce" in params) return "salesforce";
24
+ return "dbcom";
25
+ }
26
+ function runScriptsOf(workbook, names) {
27
+ const files = [...workbook.scriptOrder];
28
+ const requested = new Set(names.map((name) => name.split(/[\\/]/).pop() ?? name));
29
+ const unknown = [...requested].filter((file) => !files.includes(file));
30
+ return {
31
+ selected: requested.size === 0 ? files : files.filter((file) => requested.has(file)),
32
+ requested: requested.size > 0,
33
+ unknown
34
+ };
35
+ }
36
+ async function runWorkbookReported(runtime, workbook, options, emit) {
37
+ const { workbookId } = workbook;
38
+ const files = [...workbook.scriptOrder];
39
+ const { selected, requested } = runScriptsOf(workbook, options.scripts);
40
+ const scriptOfOwner = new Map(files.map((file) => [scriptResultOwner(workbookId, workbook.scriptRefs[file].scriptId), file]));
41
+ const { signal } = options;
42
+ const runStarted = performance.now();
43
+ emit({
44
+ type: "run_started",
45
+ workbook: options.label,
46
+ scripts: selected
47
+ });
48
+ const prepare = options.prepareConnection;
49
+ const prepareConnection = prepare == null ? void 0 : async (connectionId) => {
50
+ const conn = runtime.connection(connectionId);
51
+ if (conn == null) throw new Error(`the workbook has no connection ${connectionId}`);
52
+ const label = runConnectionLabel(conn);
53
+ const started = performance.now();
54
+ emit({
55
+ type: "connection_setup",
56
+ connection: label
57
+ });
58
+ const failure = await prepare(conn);
59
+ if (failure != null) {
60
+ emit({
61
+ type: "connection_failed",
62
+ connection: label,
63
+ error: signal.aborted ? "interrupted" : failure
64
+ });
65
+ throw new Error(failure);
66
+ }
67
+ emit({
68
+ type: "connection_ready",
69
+ connection: label,
70
+ ms: performance.now() - started
71
+ });
72
+ };
73
+ const queryOf = (queryId) => {
74
+ const view = runtime.module.views("query", [queryId])[0] ?? null;
75
+ return view === "busy" ? null : view;
76
+ };
77
+ let stopProgress = null;
78
+ if (options.progressIntervalMs != null) {
79
+ const lastProgress = /* @__PURE__ */ new Map();
80
+ const sampleProgress = () => {
81
+ for (const conn of runtime.store.all("connection").values()) for (const queryId of conn.queriesActiveOrdered) {
82
+ const query = runtime.store.get("query", queryId);
83
+ const script = scriptOfOwner.get(query?.queryMetadata.resultOwner ?? "");
84
+ const update = query?.latestProgressUpdate;
85
+ if (script == null || update == null) continue;
86
+ const progress = {
87
+ phase: update.phase ?? null,
88
+ percent: update.progressPercent ?? null,
89
+ rows: update.rowsProcessed ?? null
90
+ };
91
+ const text = JSON.stringify(progress);
92
+ if (lastProgress.get(script) === text) continue;
93
+ lastProgress.set(script, text);
94
+ emit({
95
+ type: "script_progress",
96
+ script,
97
+ ...progress
98
+ });
99
+ }
100
+ };
101
+ let sample = null;
102
+ const unsubscribe = runtime.store.subscribeKind("query", () => {
103
+ sample ??= setTimeout(() => {
104
+ sample = null;
105
+ sampleProgress();
106
+ }, options.progressIntervalMs);
107
+ });
108
+ stopProgress = () => {
109
+ unsubscribe();
110
+ if (sample != null) clearTimeout(sample);
111
+ sample = null;
112
+ };
113
+ }
114
+ try {
115
+ const outcomes = /* @__PURE__ */ new Map();
116
+ const startedAt = /* @__PURE__ */ new Map();
117
+ const elapsed = (script) => performance.now() - (startedAt.get(script) ?? runStarted);
118
+ let reports = Promise.resolve();
119
+ const report = (task) => {
120
+ reports = reports.then(task).then((event) => {
121
+ if (event != null) emit(event);
122
+ }, (error) => options.onReportError?.(error));
123
+ };
124
+ const resultOf = async (script, queryId, key, cached) => {
125
+ const ms = elapsed(script);
126
+ const table = queryId == null ? null : runtime.table(queryOf(queryId)?.resultTable?.id) ?? null;
127
+ await runtime.module.flushCacheWrites();
128
+ const file = key == null || options.resultFile == null ? null : await options.resultFile(key);
129
+ return {
130
+ type: "script_done",
131
+ script,
132
+ ms,
133
+ rows: table?.numRows ?? null,
134
+ table,
135
+ cached,
136
+ key,
137
+ file
138
+ };
139
+ };
140
+ const onScript = (script, event) => {
141
+ switch (event.type) {
142
+ case "planned":
143
+ report(async () => ({
144
+ type: "script_planned",
145
+ script,
146
+ follows: event.follows
147
+ }));
148
+ break;
149
+ case "started":
150
+ startedAt.set(script, performance.now());
151
+ report(async () => ({
152
+ type: "script_started",
153
+ script
154
+ }));
155
+ break;
156
+ case "cached":
157
+ case "done": {
158
+ outcomes.set(script, "ok");
159
+ const cached = event.type === "cached";
160
+ report(() => resultOf(script, event.queryId, event.key, cached));
161
+ break;
162
+ }
163
+ case "failed": {
164
+ outcomes.set(script, "failed");
165
+ const ms = elapsed(script);
166
+ const statement = event.queryId == null ? null : queryOf(event.queryId)?.queryText ?? null;
167
+ report(async () => ({
168
+ type: "script_failed",
169
+ script,
170
+ ms,
171
+ error: describeRunError(event.error),
172
+ statement
173
+ }));
174
+ break;
175
+ }
176
+ case "cancelled": {
177
+ outcomes.set(script, "cancelled");
178
+ const ms = elapsed(script);
179
+ report(async () => ({
180
+ type: "script_cancelled",
181
+ script,
182
+ ms
183
+ }));
184
+ break;
185
+ }
186
+ case "skipped":
187
+ outcomes.set(script, "not run");
188
+ report(async () => ({
189
+ type: "script_not_run",
190
+ script,
191
+ reason: event.reason
192
+ }));
193
+ }
194
+ };
195
+ const stopRun = () => runtime.stopRun(workbookId);
196
+ signal.addEventListener("abort", stopRun, { once: true });
197
+ let run = null;
198
+ try {
199
+ if (!signal.aborted) run = await runtime.runWorkbook(workbookId, {
200
+ targets: requested ? selected.map((file) => files.indexOf(file)) : void 0,
201
+ refresh: options.refresh,
202
+ analyze: false
203
+ }, {
204
+ onScript,
205
+ prepareConnection
206
+ });
207
+ } finally {
208
+ signal.removeEventListener("abort", stopRun);
209
+ }
210
+ await reports;
211
+ stopProgress?.();
212
+ const interrupted = signal.aborted;
213
+ const scriptFailed = [...outcomes.values()].includes("failed");
214
+ let error = null;
215
+ if (!interrupted && !scriptFailed) {
216
+ if (run == null || run.outcome.refused != null) error = "the workbook is running elsewhere";
217
+ else if (run.outcome.failure != null && run.outcome.failure.kind !== "cancelled") error = run.outcome.failure.message;
218
+ }
219
+ if (error != null) emit({
220
+ type: "run_failed",
221
+ error
222
+ });
223
+ for (const script of selected) {
224
+ if (outcomes.has(script)) continue;
225
+ outcomes.set(script, "not run");
226
+ emit({
227
+ type: "script_not_run",
228
+ script,
229
+ reason: interrupted ? "interrupted" : scriptFailed ? "the run stopped" : "the run failed"
230
+ });
231
+ }
232
+ const tally = (outcome) => [...outcomes.entries()].filter(([script, value]) => value === outcome && (outcome !== "ok" || selected.includes(script))).length;
233
+ emit({
234
+ type: "run_finished",
235
+ ms: performance.now() - runStarted,
236
+ interrupted,
237
+ ok: tally("ok"),
238
+ failed: tally("failed"),
239
+ cancelled: tally("cancelled"),
240
+ notRun: tally("not run")
241
+ });
242
+ if (interrupted) return "interrupted";
243
+ return error == null && selected.every((script) => outcomes.get(script) === "ok") && tally("failed") + tally("cancelled") + tally("not run") === 0 ? "ok" : "failed";
244
+ } finally {
245
+ stopProgress?.();
246
+ }
247
+ }
248
+ export { describeRunError, runScriptsOf, runWorkbookReported };