Line data Source code
1 : import {
2 : createQueryExecutionMetrics,
3 : type QueryExecutionAction,
4 : type QueryExecutionHistoryState,
5 : type QueryExecutionState,
6 : type QueryExecutionTracker,
7 : reduceQueryAction,
8 : } from '../query/query_execution_state.js';
9 :
10 : export const SHELL_QUERY_HISTORY_LIMIT = 100;
11 :
12 1 : export class ShellQueryExecutionTracker implements QueryExecutionTracker {
13 1 : private state: QueryExecutionHistoryState = {
14 1 : queriesActive: new Map(),
15 1 : queriesActiveOrdered: [],
16 1 : queriesFinished: new Map(),
17 1 : queriesFinishedOrdered: [],
18 1 : snapshotQueriesActiveFinished: 1,
19 1 : metrics: createQueryExecutionMetrics(),
20 1 : };
21 1 : private snapshot: readonly QueryExecutionState[] = [];
22 1 : private readonly listeners = new Set<() => void>();
23 :
24 1 : readonly getSnapshot = (): readonly QueryExecutionState[] => this.snapshot;
25 :
26 1 : subscribe = (listener: () => void): (() => void) => {
27 1 : this.listeners.add(listener);
28 1 : return () => this.listeners.delete(listener);
29 1 : };
30 :
31 1 : dispatch(action: QueryExecutionAction): void {
32 1 : this.state = reduceQueryAction(this.state, action);
33 1 : while (this.state.queriesFinishedOrdered.length + this.state.queriesActiveOrdered.length > SHELL_QUERY_HISTORY_LIMIT) {
34 1 : const finishedQueryId = this.state.queriesFinishedOrdered.shift();
35 1 : if (finishedQueryId != null) {
36 1 : this.state.queriesFinished.delete(finishedQueryId);
37 1 : continue;
38 1 : }
39 1 : const activeQueryId = this.state.queriesActiveOrdered.shift();
40 1 : if (activeQueryId != null) this.state.queriesActive.delete(activeQueryId);
41 1 : }
42 1 : this.snapshot = [
43 1 : ...this.state.queriesFinishedOrdered.flatMap(queryId => {
44 1 : const execution = this.state.queriesFinished.get(queryId);
45 1 : return execution == null ? [] : [execution];
46 1 : }),
47 1 : ...this.state.queriesActiveOrdered.flatMap(queryId => {
48 1 : const execution = this.state.queriesActive.get(queryId);
49 1 : return execution == null ? [] : [execution];
50 1 : }),
51 1 : ];
52 1 : for (const listener of this.listeners) listener();
53 1 : }
54 1 : }
|