@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,294 @@
|
|
|
1
|
+
import type { KustoClient, KustoTarget } from './kusto';
|
|
2
|
+
import { stateService } from './state-service';
|
|
3
|
+
|
|
4
|
+
// --- Types ---
|
|
5
|
+
|
|
6
|
+
export interface SchemaColumn {
|
|
7
|
+
name: string;
|
|
8
|
+
type: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SchemaTable {
|
|
12
|
+
name: string;
|
|
13
|
+
folder: string;
|
|
14
|
+
description: string;
|
|
15
|
+
columns: SchemaColumn[];
|
|
16
|
+
kind: 'table' | 'materializedView' | 'function' | 'externalTable';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// --- Live schema loader ---
|
|
20
|
+
|
|
21
|
+
export interface KustoSchemaDb {
|
|
22
|
+
Tables: Record<string, { Folder?: string; DocString?: string; OrderedColumns?: { Name: string; CslType: string }[] }>;
|
|
23
|
+
MaterializedViews?: Record<string, { Folder?: string; DocString?: string; OrderedColumns?: { Name: string; CslType: string }[] }>;
|
|
24
|
+
Functions?: Record<string, { Folder?: string; DocString?: string; Parameters?: string; Body?: string }>;
|
|
25
|
+
ExternalTables?: Record<string, { Folder?: string; DocString?: string; OrderedColumns?: { Name: string; CslType: string }[] }>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Per-cluster schema cache
|
|
29
|
+
const schemaCache = new Map<string, SchemaTable[]>();
|
|
30
|
+
const foldersCache = new Map<string, string[]>();
|
|
31
|
+
const loadingPromises = new Map<string, Promise<void>>();
|
|
32
|
+
|
|
33
|
+
function cacheKey(target?: KustoTarget): string {
|
|
34
|
+
if (!target) {
|
|
35
|
+
return '__default__';
|
|
36
|
+
}
|
|
37
|
+
return `${target.clusterUrl}|${target.database}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function persistSchemaKey(target: KustoTarget): string {
|
|
41
|
+
return `schema:${target.clusterUrl}|${target.database}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function persistDbKey(clusterUrl: string): string {
|
|
45
|
+
return `dbs:${clusterUrl}`;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function restoreSchemaFromStore(target: KustoTarget): boolean {
|
|
49
|
+
const key = cacheKey(target);
|
|
50
|
+
if (schemaCache.has(key)) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
const stored = stateService.get<SchemaTable[]>('explorerCache', persistSchemaKey(target));
|
|
54
|
+
if (stored && stored.length > 0) {
|
|
55
|
+
schemaCache.set(key, stored);
|
|
56
|
+
foldersCache.set(key, [...new Set(stored.map((t) => t.folder))].filter(Boolean).sort());
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function persistSchema(target: KustoTarget, schema: SchemaTable[]): void {
|
|
63
|
+
stateService.set('explorerCache', persistSchemaKey(target), schema);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function getPersistedDatabases(clusterUrl: string): string[] | null {
|
|
67
|
+
return stateService.get<string[]>('explorerCache', persistDbKey(clusterUrl));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function persistDatabases(clusterUrl: string, dbs: string[]): void {
|
|
71
|
+
stateService.set('explorerCache', persistDbKey(clusterUrl), dbs);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const isLookup = (t: SchemaTable) => t.kind === 'table' && t.folder === 'Lookup';
|
|
75
|
+
|
|
76
|
+
export function parseSchema(db: KustoSchemaDb): SchemaTable[] {
|
|
77
|
+
const tables: SchemaTable[] = [];
|
|
78
|
+
|
|
79
|
+
// Tables
|
|
80
|
+
for (const [name, tbl] of Object.entries(db.Tables ?? {})) {
|
|
81
|
+
tables.push({
|
|
82
|
+
name,
|
|
83
|
+
folder: tbl.Folder || '',
|
|
84
|
+
description: tbl.DocString || '',
|
|
85
|
+
columns: (tbl.OrderedColumns ?? []).map((c) => ({ name: c.Name, type: c.CslType })),
|
|
86
|
+
kind: 'table',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Materialized Views
|
|
91
|
+
for (const [name, mv] of Object.entries(db.MaterializedViews ?? {})) {
|
|
92
|
+
tables.push({
|
|
93
|
+
name,
|
|
94
|
+
folder: mv.Folder || '',
|
|
95
|
+
description: mv.DocString || 'Materialized view',
|
|
96
|
+
columns: (mv.OrderedColumns ?? []).map((c) => ({ name: c.Name, type: c.CslType })),
|
|
97
|
+
kind: 'materializedView',
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Functions (shown as schema items but without columns)
|
|
102
|
+
for (const [name, fn] of Object.entries(db.Functions ?? {})) {
|
|
103
|
+
tables.push({
|
|
104
|
+
name,
|
|
105
|
+
folder: fn.Folder || '',
|
|
106
|
+
description: fn.DocString || '',
|
|
107
|
+
columns: [],
|
|
108
|
+
kind: 'function',
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// External Tables
|
|
113
|
+
for (const [name, et] of Object.entries(db.ExternalTables ?? {})) {
|
|
114
|
+
tables.push({
|
|
115
|
+
name,
|
|
116
|
+
folder: et.Folder || '',
|
|
117
|
+
description: et.DocString || '',
|
|
118
|
+
columns: (et.OrderedColumns ?? []).map((c) => ({ name: c.Name, type: c.CslType })),
|
|
119
|
+
kind: 'externalTable',
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const kindOrder: Record<string, number> = { 'function': 0, 'materializedView': 1, 'table': 2, 'externalTable': 3 };
|
|
124
|
+
|
|
125
|
+
return tables.sort((a, b) => {
|
|
126
|
+
// Sort by folder first
|
|
127
|
+
const folderCmp = a.folder.localeCompare(b.folder);
|
|
128
|
+
if (folderCmp !== 0) {
|
|
129
|
+
return folderCmp;
|
|
130
|
+
}
|
|
131
|
+
// Within same folder: fn → MV → lookup (by folder) → table
|
|
132
|
+
const aOrder = isLookup(a) ? 1.5 : (kindOrder[a.kind] ?? 2);
|
|
133
|
+
const bOrder = isLookup(b) ? 1.5 : (kindOrder[b.kind] ?? 2);
|
|
134
|
+
if (aOrder !== bOrder) {
|
|
135
|
+
return aOrder - bOrder;
|
|
136
|
+
}
|
|
137
|
+
// Then alphabetical
|
|
138
|
+
return a.name.localeCompare(b.name);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Load schema for a cluster+database target. If the database is empty or wrong,
|
|
144
|
+
* auto-discovers the first available database via `.show databases`.
|
|
145
|
+
*/
|
|
146
|
+
export async function loadSchema(client: KustoClient, target?: KustoTarget): Promise<void> {
|
|
147
|
+
// Auto-discover database if not specified
|
|
148
|
+
let effectiveTarget = target;
|
|
149
|
+
if (target && !target.database) {
|
|
150
|
+
const discovered = await discoverDefaultDatabase(client, target.clusterUrl);
|
|
151
|
+
if (!discovered) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
effectiveTarget = discovered;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const key = cacheKey(effectiveTarget);
|
|
158
|
+
if (schemaCache.has(key)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Try restoring from persistent cache (IndexedDB)
|
|
163
|
+
if (effectiveTarget && restoreSchemaFromStore(effectiveTarget)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (loadingPromises.has(key)) {
|
|
168
|
+
return loadingPromises.get(key);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const promise = (async () => {
|
|
172
|
+
try {
|
|
173
|
+
const result = await client.queryKustoMgmt<{ DatabaseSchema: string }>('.show database schema as json', effectiveTarget);
|
|
174
|
+
if (result.rows.length > 0) {
|
|
175
|
+
const schemaJson = JSON.parse(result.rows[0].DatabaseSchema);
|
|
176
|
+
const dbName = Object.keys(schemaJson.Databases)[0];
|
|
177
|
+
const db = schemaJson.Databases[dbName] as KustoSchemaDb;
|
|
178
|
+
const parsed = parseSchema(db);
|
|
179
|
+
schemaCache.set(key, parsed);
|
|
180
|
+
foldersCache.set(key, [...new Set(parsed.map((t) => t.folder))].filter(Boolean).sort());
|
|
181
|
+
// Persist to IndexedDB
|
|
182
|
+
if (effectiveTarget) {
|
|
183
|
+
persistSchema(effectiveTarget, parsed);
|
|
184
|
+
}
|
|
185
|
+
// Also cache under the original target key if different
|
|
186
|
+
if (target && cacheKey(target) !== key) {
|
|
187
|
+
schemaCache.set(cacheKey(target), parsed);
|
|
188
|
+
foldersCache.set(cacheKey(target), [...new Set(parsed.map((t) => t.folder))].filter(Boolean).sort());
|
|
189
|
+
persistSchema(target, parsed);
|
|
190
|
+
}
|
|
191
|
+
} else {
|
|
192
|
+
schemaCache.set(key, []);
|
|
193
|
+
}
|
|
194
|
+
} catch {
|
|
195
|
+
if (effectiveTarget && effectiveTarget === target) {
|
|
196
|
+
const discovered = await discoverDefaultDatabase(client, effectiveTarget.clusterUrl);
|
|
197
|
+
if (discovered && discovered.database !== effectiveTarget.database) {
|
|
198
|
+
return loadSchema(client, discovered);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
} finally {
|
|
202
|
+
loadingPromises.delete(key);
|
|
203
|
+
}
|
|
204
|
+
})();
|
|
205
|
+
|
|
206
|
+
loadingPromises.set(key, promise);
|
|
207
|
+
return promise;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Try to discover the first database on a cluster via `.show databases`. */
|
|
211
|
+
async function discoverDefaultDatabase(client: KustoClient, clusterUrl: string): Promise<KustoTarget | null> {
|
|
212
|
+
try {
|
|
213
|
+
const dbs = await listDatabases(client, clusterUrl);
|
|
214
|
+
if (dbs.length > 0) {
|
|
215
|
+
return { clusterUrl, database: dbs[0] };
|
|
216
|
+
}
|
|
217
|
+
} catch { /* ignore */ }
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Force reload the schema from Kusto (clears persistent cache) */
|
|
222
|
+
export async function reloadSchema(client: KustoClient, target?: KustoTarget): Promise<void> {
|
|
223
|
+
const key = cacheKey(target);
|
|
224
|
+
schemaCache.delete(key);
|
|
225
|
+
foldersCache.delete(key);
|
|
226
|
+
if (target) {
|
|
227
|
+
stateService.delete('explorerCache', persistSchemaKey(target));
|
|
228
|
+
}
|
|
229
|
+
return loadSchema(client, target);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Get the current schema (live if loaded, otherwise fallback for default cluster or empty for others) */
|
|
233
|
+
export function getSchema(target?: KustoTarget): SchemaTable[] {
|
|
234
|
+
const cached = schemaCache.get(cacheKey(target));
|
|
235
|
+
if (cached) {
|
|
236
|
+
return cached;
|
|
237
|
+
}
|
|
238
|
+
// Only show fallback for the default cluster (no target specified)
|
|
239
|
+
return target ? [] : [];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function getSchemaFolders(target?: KustoTarget): string[] {
|
|
243
|
+
const cached = foldersCache.get(cacheKey(target));
|
|
244
|
+
if (cached) {
|
|
245
|
+
return cached;
|
|
246
|
+
}
|
|
247
|
+
return target ? [] : [];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** List databases on a Kusto cluster. Uses persistent cache; pass forceRefresh to bypass. */
|
|
251
|
+
export async function listDatabases(client: KustoClient, clusterUrl: string, forceRefresh = false): Promise<string[]> {
|
|
252
|
+
if (!forceRefresh) {
|
|
253
|
+
const cached = getPersistedDatabases(clusterUrl);
|
|
254
|
+
if (cached) {
|
|
255
|
+
return cached;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
try {
|
|
259
|
+
const target: KustoTarget = { clusterUrl, database: 'NetDefaultDB' };
|
|
260
|
+
const result = await client.queryKustoMgmt<{ DatabaseName: string }>('.show databases | project DatabaseName', target);
|
|
261
|
+
const dbs = result.rows.map((r) => r.DatabaseName).filter(Boolean).sort();
|
|
262
|
+
if (dbs.length > 0) {
|
|
263
|
+
persistDatabases(clusterUrl, dbs);
|
|
264
|
+
}
|
|
265
|
+
return dbs;
|
|
266
|
+
} catch (err) {
|
|
267
|
+
console.error('[schema] listDatabases failed for', clusterUrl, err);
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// --- KQL language tokens (not from Kusto, always static) ---
|
|
273
|
+
|
|
274
|
+
export const KQL_FUNCTIONS = [
|
|
275
|
+
'where', 'project', 'extend', 'summarize', 'count', 'take', 'top', 'order', 'sort',
|
|
276
|
+
'join', 'union', 'lookup', 'mv-expand', 'parse', 'evaluate',
|
|
277
|
+
'render', 'as', 'let', 'set', 'alias', 'restrict', 'pattern',
|
|
278
|
+
];
|
|
279
|
+
|
|
280
|
+
export const KQL_AGGREGATIONS = [
|
|
281
|
+
'count', 'countif', 'dcount', 'sum', 'sumif', 'avg', 'min', 'max',
|
|
282
|
+
'percentile', 'percentiles', 'stdev', 'variance', 'make_list', 'make_set',
|
|
283
|
+
'arg_max', 'arg_min', 'any', 'take_any',
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
export const KQL_SCALAR_FUNCTIONS = [
|
|
287
|
+
'ago', 'now', 'datetime', 'timespan', 'bin', 'floor', 'ceiling',
|
|
288
|
+
'strlen', 'substring', 'tolower', 'toupper', 'trim', 'replace',
|
|
289
|
+
'strcat', 'split', 'parse_json', 'tostring', 'toint', 'tolong', 'todouble', 'tobool',
|
|
290
|
+
'iif', 'iff', 'case', 'coalesce', 'isempty', 'isnotempty', 'isnull', 'isnotnull',
|
|
291
|
+
'format_datetime', 'datetime_diff', 'startofday', 'startofweek', 'startofmonth',
|
|
292
|
+
'round', 'abs', 'log', 'pow', 'sqrt',
|
|
293
|
+
'array_length', 'pack', 'bag_keys',
|
|
294
|
+
];
|