@mhjuma/traverse 0.1.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/LICENSE +21 -0
- package/package.json +46 -0
- package/src/bootstrap.spec.tsx +290 -0
- package/src/bootstrap.tsx +195 -0
- package/src/colors.ts +23 -0
- package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
- package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
- package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
- package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
- package/src/components/ChartPanel/ChartPanel.tsx +151 -0
- package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
- package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
- package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
- package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
- package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
- package/src/components/Explorer/Explorer.spec.tsx +615 -0
- package/src/components/Explorer/Explorer.tsx +49 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
- package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
- package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
- package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
- package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
- package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
- package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
- package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
- package/src/components/ResultsPanel/TabButton.tsx +11 -0
- package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
- package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
- package/src/components/ResultsTable/FilterCell.tsx +24 -0
- package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
- package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
- package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
- package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
- package/src/components/ResultsTable/ResultsTable.tsx +501 -0
- package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
- package/src/components/ResultsTable/VirtualCell.tsx +32 -0
- package/src/components/ResultsTable/VirtualRow.tsx +55 -0
- package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
- package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
- package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
- package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
- package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
- package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
- package/src/components/TabBar/TabBar.logic.ts +35 -0
- package/src/components/TabBar/TabBar.spec.tsx +929 -0
- package/src/components/TabBar/TabBar.tsx +284 -0
- package/src/components/TabBar/TabItem.tsx +166 -0
- package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
- package/src/components/VegaChart/VegaChart.tsx +224 -0
- package/src/config.spec.ts +44 -0
- package/src/config.ts +47 -0
- package/src/context/ExplorerColorContext.ts +14 -0
- package/src/context/ExplorerStateContext.ts +49 -0
- package/src/context/KustoClientContext.ts +12 -0
- package/src/context/context.spec.tsx +183 -0
- package/src/index.ts +14 -0
- package/src/services/cache.spec.ts +31 -0
- package/src/services/cache.ts +16 -0
- package/src/services/concurrency.spec.ts +139 -0
- package/src/services/concurrency.ts +46 -0
- package/src/services/kusto.spec.ts +919 -0
- package/src/services/kusto.ts +472 -0
- package/src/services/queryHistory.spec.ts +287 -0
- package/src/services/queryHistory.ts +146 -0
- package/src/services/schema.spec.ts +618 -0
- package/src/services/schema.ts +294 -0
- package/src/services/state-service.spec.ts +670 -0
- package/src/services/state-service.ts +602 -0
- package/src/services/useKustoQuery.spec.ts +234 -0
- package/src/services/useKustoQuery.ts +124 -0
- package/src/state/actions.ts +50 -0
- package/src/state/index.ts +3 -0
- package/src/state/reducer.spec.ts +644 -0
- package/src/state/reducer.ts +207 -0
- package/src/state/types.ts +54 -0
- package/tsconfig.json +7 -0
- package/tsconfig.lint.json +4 -0
- package/tsconfig.spec.json +7 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
/** Minimal state service interface — compatible with any StateService implementation. */
|
|
2
|
+
export interface KustoStateService {
|
|
3
|
+
get<T>(store: string, key: string): T | null;
|
|
4
|
+
set<T>(store: string, key: string, value: T, options?: { ttl?: number }): void;
|
|
5
|
+
clear(store: string): void;
|
|
6
|
+
entryCount(store: string): number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
import { computeCacheKey } from './cache';
|
|
10
|
+
import { ConcurrencyLimiter } from './concurrency';
|
|
11
|
+
import type { Priority } from './concurrency';
|
|
12
|
+
|
|
13
|
+
const MAX_CONCURRENT = 8;
|
|
14
|
+
const MAX_RETRIES = 3;
|
|
15
|
+
const RETRY_BASE_MS = 2000;
|
|
16
|
+
|
|
17
|
+
export interface KustoTarget {
|
|
18
|
+
clusterUrl: string;
|
|
19
|
+
database: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface KustoColumn {
|
|
23
|
+
ColumnName: string;
|
|
24
|
+
ColumnType: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface QueryStats {
|
|
28
|
+
executionTime?: string;
|
|
29
|
+
cpuTime?: string;
|
|
30
|
+
memoryPeak?: number;
|
|
31
|
+
cacheHitBytes?: number;
|
|
32
|
+
cacheMissBytes?: number;
|
|
33
|
+
extentsScanned?: number;
|
|
34
|
+
extentsTotal?: number;
|
|
35
|
+
rowsScanned?: number;
|
|
36
|
+
rowsTotal?: number;
|
|
37
|
+
resultRows?: number;
|
|
38
|
+
resultSize?: number;
|
|
39
|
+
fromCache?: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface KustoVisualization {
|
|
43
|
+
type: string;
|
|
44
|
+
xColumn?: string;
|
|
45
|
+
yColumns?: string[];
|
|
46
|
+
series?: string;
|
|
47
|
+
title?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface KustoResultSet {
|
|
51
|
+
columns: KustoColumn[];
|
|
52
|
+
rows: Record<string, unknown>[];
|
|
53
|
+
visualization?: KustoVisualization;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface KustoResult<T = Record<string, unknown>> {
|
|
57
|
+
columns: KustoColumn[];
|
|
58
|
+
rows: T[];
|
|
59
|
+
stats?: QueryStats;
|
|
60
|
+
resultSets: KustoResultSet[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface RawTable {
|
|
64
|
+
Columns: { ColumnName: string; ColumnType?: string }[];
|
|
65
|
+
Rows: unknown[][];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Parse visualization metadata from QueryProperties entries in the TOC.
|
|
70
|
+
* Returns a map from primary-result ordinal → KustoVisualization.
|
|
71
|
+
*/
|
|
72
|
+
export function parseVisualizationMetadata(allTables: RawTable[], tocTable: RawTable, primaryIndices: number[]): Map<number, KustoVisualization> {
|
|
73
|
+
const vizByOrdinal = new Map<number, KustoVisualization>();
|
|
74
|
+
const tocCols = tocTable.Columns.map((c) => c.ColumnName);
|
|
75
|
+
const kindIdx = tocCols.indexOf('Kind');
|
|
76
|
+
const ordinalIdx = tocCols.indexOf('Ordinal');
|
|
77
|
+
if (kindIdx === -1 || ordinalIdx === -1) {
|
|
78
|
+
return vizByOrdinal;
|
|
79
|
+
}
|
|
80
|
+
const nameIdx = tocCols.indexOf('Name');
|
|
81
|
+
|
|
82
|
+
for (const row of tocTable.Rows) {
|
|
83
|
+
if (row[kindIdx] === 'QueryProperties' && nameIdx !== -1) {
|
|
84
|
+
const propTable = allTables[row[ordinalIdx] as number];
|
|
85
|
+
if (propTable) {
|
|
86
|
+
const propCols = propTable.Columns.map((c) => c.ColumnName);
|
|
87
|
+
const valIdx = propCols.indexOf('Value');
|
|
88
|
+
if (valIdx !== -1 && propTable.Rows.length > 0) {
|
|
89
|
+
try {
|
|
90
|
+
const rawVal = propTable.Rows[0][valIdx];
|
|
91
|
+
const raw = typeof rawVal === 'string' ? JSON.parse(rawVal) : rawVal;
|
|
92
|
+
if (raw && typeof raw === 'object' && raw.Visualization && raw.Visualization !== 'table') {
|
|
93
|
+
const viz: KustoVisualization = { type: raw.Visualization };
|
|
94
|
+
if (raw.XColumn) { viz.xColumn = raw.XColumn; }
|
|
95
|
+
if (raw.YColumns) { viz.yColumns = typeof raw.YColumns === 'string' ? raw.YColumns.split(',').map((s: string) => s.trim()) : raw.YColumns; }
|
|
96
|
+
if (raw.Series) { viz.series = typeof raw.Series === 'string' ? raw.Series : undefined; }
|
|
97
|
+
if (raw.Title) { viz.title = raw.Title; }
|
|
98
|
+
const ordinal = row[ordinalIdx] as number;
|
|
99
|
+
let prevResult: number | undefined;
|
|
100
|
+
for (let i = primaryIndices.length - 1; i >= 0; i--) {
|
|
101
|
+
if (primaryIndices[i] < ordinal) {
|
|
102
|
+
prevResult = primaryIndices[i];
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (prevResult !== undefined) {
|
|
107
|
+
vizByOrdinal.set(prevResult, viz);
|
|
108
|
+
} else if (primaryIndices.length > 0) {
|
|
109
|
+
vizByOrdinal.set(primaryIndices[0], viz);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
} catch { /* ignore malformed visualization metadata */ }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return vizByOrdinal;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Build result sets from primary tables, applying validated visualization metadata.
|
|
122
|
+
*/
|
|
123
|
+
export function buildResultSets(allTables: RawTable[], primaryIndices: number[], vizByOrdinal: Map<number, KustoVisualization>): KustoResultSet[] {
|
|
124
|
+
return primaryIndices.map((idx) => {
|
|
125
|
+
const tbl = allTables[idx];
|
|
126
|
+
const cols: KustoColumn[] = tbl.Columns.map((c) => ({ ColumnName: c.ColumnName, ColumnType: c.ColumnType ?? 'string' }));
|
|
127
|
+
const colNames = cols.map((c) => c.ColumnName);
|
|
128
|
+
const parsedRows = tbl.Rows.map((row: unknown[]) => {
|
|
129
|
+
const obj: Record<string, unknown> = {};
|
|
130
|
+
colNames.forEach((name, i) => { obj[name] = row[i]; });
|
|
131
|
+
return obj;
|
|
132
|
+
});
|
|
133
|
+
const resultSet: KustoResultSet = { columns: cols, rows: parsedRows };
|
|
134
|
+
const viz = vizByOrdinal.get(idx);
|
|
135
|
+
if (viz) {
|
|
136
|
+
const validViz: KustoVisualization = { type: viz.type };
|
|
137
|
+
if (viz.title) { validViz.title = viz.title; }
|
|
138
|
+
if (viz.xColumn && colNames.includes(viz.xColumn)) { validViz.xColumn = viz.xColumn; }
|
|
139
|
+
if (viz.series && colNames.includes(viz.series)) { validViz.series = viz.series; }
|
|
140
|
+
if (viz.yColumns) {
|
|
141
|
+
const validY = viz.yColumns.filter((y) => colNames.includes(y));
|
|
142
|
+
if (validY.length > 0) { validViz.yColumns = validY; }
|
|
143
|
+
}
|
|
144
|
+
resultSet.visualization = validViz;
|
|
145
|
+
}
|
|
146
|
+
return resultSet;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function extractStats(tables: { Columns: { ColumnName: string }[]; Rows: unknown[][] }[]): QueryStats {
|
|
151
|
+
const stats: QueryStats = {};
|
|
152
|
+
|
|
153
|
+
for (let t = 1; t < tables.length; t++) {
|
|
154
|
+
const table = tables[t];
|
|
155
|
+
const colNames = table.Columns.map((c) => c.ColumnName);
|
|
156
|
+
const severityIdx = colNames.indexOf('SeverityName');
|
|
157
|
+
const descIdx = colNames.indexOf('StatusDescription');
|
|
158
|
+
if (severityIdx === -1 || descIdx === -1) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const row of table.Rows) {
|
|
163
|
+
if (row[severityIdx] !== 'Stats') {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const raw = row[descIdx];
|
|
167
|
+
if (typeof raw !== 'string') {
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
try {
|
|
171
|
+
const payload = JSON.parse(raw);
|
|
172
|
+
|
|
173
|
+
if (payload.ExecutionTime !== null) {
|
|
174
|
+
stats.executionTime = typeof payload.ExecutionTime === 'number'
|
|
175
|
+
? `${(payload.ExecutionTime * 1000).toFixed(1)} ms`
|
|
176
|
+
: String(payload.ExecutionTime);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const ru = payload.resource_usage;
|
|
180
|
+
if (ru) {
|
|
181
|
+
if (ru.cpu?.['total cpu']) {
|
|
182
|
+
stats.cpuTime = ru.cpu['total cpu'];
|
|
183
|
+
}
|
|
184
|
+
if (ru.memory?.peak_per_node) {
|
|
185
|
+
stats.memoryPeak = ru.memory.peak_per_node;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const shards = ru.cache?.shards;
|
|
189
|
+
if (shards) {
|
|
190
|
+
const hotHit = shards.hot?.hitbytes ?? 0;
|
|
191
|
+
const coldHit = shards.cold?.hitbytes ?? 0;
|
|
192
|
+
const hotMiss = shards.hot?.missbytes ?? 0;
|
|
193
|
+
const coldMiss = shards.cold?.missbytes ?? 0;
|
|
194
|
+
stats.cacheHitBytes = hotHit + coldHit;
|
|
195
|
+
stats.cacheMissBytes = hotMiss + coldMiss;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (ru.cache?.results_cache_origin) {
|
|
199
|
+
stats.fromCache = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const input = payload.input_dataset_statistics;
|
|
204
|
+
if (input) {
|
|
205
|
+
if (input.extents) {
|
|
206
|
+
stats.extentsScanned = input.extents.scanned;
|
|
207
|
+
stats.extentsTotal = input.extents.total;
|
|
208
|
+
}
|
|
209
|
+
if (input.rows) {
|
|
210
|
+
stats.rowsScanned = input.rows.scanned;
|
|
211
|
+
stats.rowsTotal = input.rows.total;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const ds = payload.dataset_statistics;
|
|
216
|
+
if (Array.isArray(ds) && ds.length > 0) {
|
|
217
|
+
stats.resultRows = ds[0].table_row_count;
|
|
218
|
+
stats.resultSize = ds[0].table_size;
|
|
219
|
+
}
|
|
220
|
+
} catch {
|
|
221
|
+
// Not valid JSON — skip
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return stats;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export type QueryPriority = Priority;
|
|
230
|
+
|
|
231
|
+
export interface QueryKustoOptions {
|
|
232
|
+
bypassCache?: boolean;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export interface KustoClientConfig {
|
|
236
|
+
defaultTarget: KustoTarget;
|
|
237
|
+
getToken: (clusterUrl: string) => Promise<string>;
|
|
238
|
+
stateService: KustoStateService;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export interface KustoClient {
|
|
242
|
+
queryKusto<T = Record<string, unknown>>(
|
|
243
|
+
kql: string,
|
|
244
|
+
signal?: AbortSignal,
|
|
245
|
+
priority?: QueryPriority,
|
|
246
|
+
target?: KustoTarget,
|
|
247
|
+
options?: QueryKustoOptions,
|
|
248
|
+
): Promise<KustoResult<T>>;
|
|
249
|
+
queryKustoMgmt<T = Record<string, unknown>>(kql: string, target?: KustoTarget): Promise<KustoResult<T>>;
|
|
250
|
+
clearQueryCache(): void;
|
|
251
|
+
getQueryCacheSize(): number;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function fetchWithRetry(url: string, init: RequestInit, retries = MAX_RETRIES): Promise<Response> {
|
|
255
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
256
|
+
const response = await fetch(url, init);
|
|
257
|
+
if (response.status === 429 && attempt < retries) {
|
|
258
|
+
const delay = RETRY_BASE_MS * Math.pow(2, attempt);
|
|
259
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
return response;
|
|
263
|
+
}
|
|
264
|
+
throw new Error('Max retries exceeded');
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function createKustoClient(config: KustoClientConfig): KustoClient {
|
|
268
|
+
const { defaultTarget, getToken, stateService } = config;
|
|
269
|
+
const limiter = new ConcurrencyLimiter(MAX_CONCURRENT);
|
|
270
|
+
const inflightQueries = new Map<string, Promise<KustoResult<unknown>>>();
|
|
271
|
+
|
|
272
|
+
function targetCacheKey(kql: string, target: KustoTarget, prefix: string): string {
|
|
273
|
+
const targetKey = target.clusterUrl === defaultTarget.clusterUrl && target.database === defaultTarget.database
|
|
274
|
+
? '' : `${target.clusterUrl}|${target.database}|`;
|
|
275
|
+
return computeCacheKey(`${targetKey}${kql}`, prefix);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function executeQuery<T = Record<string, unknown>>(
|
|
279
|
+
kql: string,
|
|
280
|
+
signal?: AbortSignal,
|
|
281
|
+
priority?: QueryPriority,
|
|
282
|
+
target?: KustoTarget,
|
|
283
|
+
options?: QueryKustoOptions,
|
|
284
|
+
): Promise<KustoResult<T>> {
|
|
285
|
+
const t = target ?? defaultTarget;
|
|
286
|
+
const token = await getToken(t.clusterUrl);
|
|
287
|
+
|
|
288
|
+
await limiter.acquire(priority);
|
|
289
|
+
|
|
290
|
+
const controller = new AbortController();
|
|
291
|
+
const timeout = setTimeout(() => controller.abort(), 60000);
|
|
292
|
+
|
|
293
|
+
if (signal) {
|
|
294
|
+
if (signal.aborted) { controller.abort(); }
|
|
295
|
+
else { signal.addEventListener('abort', () => controller.abort(), { once: true }); }
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
try {
|
|
299
|
+
const response = await fetchWithRetry(`${t.clusterUrl}/v1/rest/query`, {
|
|
300
|
+
method: 'POST',
|
|
301
|
+
headers: {
|
|
302
|
+
'Authorization': `Bearer ${token}`,
|
|
303
|
+
'Content-Type': 'application/json',
|
|
304
|
+
},
|
|
305
|
+
body: JSON.stringify({
|
|
306
|
+
db: t.database,
|
|
307
|
+
csl: kql,
|
|
308
|
+
properties: JSON.stringify({
|
|
309
|
+
Options: {
|
|
310
|
+
servertimeout: '00:01:00',
|
|
311
|
+
query_results_cache_max_age: options?.bypassCache ? '00:00:00' : '00:05:00',
|
|
312
|
+
},
|
|
313
|
+
}),
|
|
314
|
+
}),
|
|
315
|
+
signal: controller.signal,
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
if (!response.ok) {
|
|
319
|
+
const text = await response.text();
|
|
320
|
+
throw new Error(`Kusto query failed: ${response.status} — ${text.slice(0, 200)}`);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const data = await response.json();
|
|
324
|
+
const allTables = data.Tables as { Columns: { ColumnName: string; ColumnType?: string }[]; Rows: unknown[][] }[];
|
|
325
|
+
|
|
326
|
+
const tocTable = allTables[allTables.length - 1];
|
|
327
|
+
const tocCols = tocTable?.Columns?.map((c) => c.ColumnName) ?? [];
|
|
328
|
+
const kindIdx = tocCols.indexOf('Kind');
|
|
329
|
+
const ordinalIdx = tocCols.indexOf('Ordinal');
|
|
330
|
+
|
|
331
|
+
const primaryIndices: number[] = [];
|
|
332
|
+
if (kindIdx !== -1 && ordinalIdx !== -1) {
|
|
333
|
+
for (const row of tocTable.Rows) {
|
|
334
|
+
if (row[kindIdx] === 'QueryResult') {
|
|
335
|
+
primaryIndices.push(row[ordinalIdx] as number);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (primaryIndices.length === 0) {
|
|
340
|
+
primaryIndices.push(0);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const vizByOrdinal = parseVisualizationMetadata(allTables, tocTable, primaryIndices);
|
|
344
|
+
const resultSets = buildResultSets(allTables, primaryIndices, vizByOrdinal);
|
|
345
|
+
|
|
346
|
+
const firstSet = resultSets[0];
|
|
347
|
+
const result: KustoResult<T> = {
|
|
348
|
+
columns: firstSet.columns,
|
|
349
|
+
rows: firstSet.rows as T[],
|
|
350
|
+
resultSets,
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
if (allTables.length > 1) {
|
|
354
|
+
result.stats = extractStats(allTables);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
stateService.set('queryCache', targetCacheKey(kql, t, 'q:'), result as KustoResult);
|
|
358
|
+
return result;
|
|
359
|
+
} finally {
|
|
360
|
+
clearTimeout(timeout);
|
|
361
|
+
limiter.release();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
queryKusto<T = Record<string, unknown>>(
|
|
367
|
+
kql: string,
|
|
368
|
+
signal?: AbortSignal,
|
|
369
|
+
priority?: QueryPriority,
|
|
370
|
+
target?: KustoTarget,
|
|
371
|
+
options?: QueryKustoOptions,
|
|
372
|
+
): Promise<KustoResult<T>> {
|
|
373
|
+
const t = target ?? defaultTarget;
|
|
374
|
+
const cacheKey = targetCacheKey(kql, t, 'q:');
|
|
375
|
+
|
|
376
|
+
if (!options?.bypassCache) {
|
|
377
|
+
const cached = stateService.get<KustoResult<T>>('queryCache', cacheKey);
|
|
378
|
+
if (cached) {
|
|
379
|
+
return Promise.resolve(cached);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const inflight = inflightQueries.get(cacheKey);
|
|
384
|
+
if (inflight) {
|
|
385
|
+
return inflight as Promise<KustoResult<T>>;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const promise = executeQuery<T>(kql, signal, priority, t, options).finally(() => {
|
|
389
|
+
inflightQueries.delete(cacheKey);
|
|
390
|
+
});
|
|
391
|
+
inflightQueries.set(cacheKey, promise as Promise<KustoResult<unknown>>);
|
|
392
|
+
return promise;
|
|
393
|
+
},
|
|
394
|
+
|
|
395
|
+
async queryKustoMgmt<T = Record<string, unknown>>(kql: string, target?: KustoTarget): Promise<KustoResult<T>> {
|
|
396
|
+
const t = target ?? defaultTarget;
|
|
397
|
+
const cacheKey = targetCacheKey(kql, t, 'm:');
|
|
398
|
+
|
|
399
|
+
const cached = stateService.get<KustoResult<T>>('queryCache', cacheKey);
|
|
400
|
+
if (cached) {
|
|
401
|
+
return cached;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const token = await getToken(t.clusterUrl);
|
|
405
|
+
|
|
406
|
+
await limiter.acquire();
|
|
407
|
+
|
|
408
|
+
const controller = new AbortController();
|
|
409
|
+
const timeout = setTimeout(() => controller.abort(), 60000);
|
|
410
|
+
|
|
411
|
+
try {
|
|
412
|
+
const response = await fetchWithRetry(`${t.clusterUrl}/v1/rest/mgmt`, {
|
|
413
|
+
method: 'POST',
|
|
414
|
+
headers: {
|
|
415
|
+
'Authorization': `Bearer ${token}`,
|
|
416
|
+
'Content-Type': 'application/json',
|
|
417
|
+
},
|
|
418
|
+
body: JSON.stringify({
|
|
419
|
+
db: t.database,
|
|
420
|
+
csl: kql,
|
|
421
|
+
}),
|
|
422
|
+
signal: controller.signal,
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
if (!response.ok) {
|
|
426
|
+
const text = await response.text();
|
|
427
|
+
throw new Error(`Kusto mgmt command failed: ${response.status} — ${text.slice(0, 200)}`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
const data = await response.json();
|
|
431
|
+
const table = data.Tables[0];
|
|
432
|
+
const columns: KustoColumn[] = table.Columns;
|
|
433
|
+
const columnNames = columns.map((c: KustoColumn) => c.ColumnName);
|
|
434
|
+
|
|
435
|
+
const rows: T[] = table.Rows.map((row: unknown[]) => {
|
|
436
|
+
const obj: Record<string, unknown> = {};
|
|
437
|
+
columnNames.forEach((name: string, i: number) => {
|
|
438
|
+
obj[name] = row[i];
|
|
439
|
+
});
|
|
440
|
+
return obj as T;
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
const result: KustoResult<T> = { columns, rows, resultSets: [{ columns, rows: rows as Record<string, unknown>[] }] };
|
|
444
|
+
stateService.set('queryCache', targetCacheKey(kql, t, 'm:'), result as KustoResult);
|
|
445
|
+
return result;
|
|
446
|
+
} finally {
|
|
447
|
+
clearTimeout(timeout);
|
|
448
|
+
limiter.release();
|
|
449
|
+
}
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
clearQueryCache() {
|
|
453
|
+
stateService.clear('queryCache');
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
getQueryCacheSize() {
|
|
457
|
+
return stateService.entryCount('queryCache');
|
|
458
|
+
},
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Re-export for convenience
|
|
463
|
+
export { clearQueryCache, getQueryCacheSize };
|
|
464
|
+
|
|
465
|
+
function clearQueryCache(): void {
|
|
466
|
+
// No-op — callers should use the client instance method.
|
|
467
|
+
// This exists only for backward compatibility during migration.
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function getQueryCacheSize(): number {
|
|
471
|
+
return 0;
|
|
472
|
+
}
|