@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,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure functions and types for the Explorer page.
|
|
3
|
+
* No React dependencies — fully unit-testable.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Re-export config items that other files import from here
|
|
7
|
+
export type { KustoConnection, WellKnownCluster } from '../../config';
|
|
8
|
+
export { CONNECTION_COLORS, DEFAULT_CONNECTION, buildWellKnownClusters, DEFAULT_QUERY, KEYBOARD_SHORTCUTS } from '../../config';
|
|
9
|
+
|
|
10
|
+
import type { KustoConnection } from '../../config';
|
|
11
|
+
import { DEFAULT_CONNECTION } from '../../config';
|
|
12
|
+
import { stateService } from '../../services/state-service';
|
|
13
|
+
|
|
14
|
+
export interface QueryTab {
|
|
15
|
+
id: string;
|
|
16
|
+
title: string;
|
|
17
|
+
kql: string;
|
|
18
|
+
connectionId: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function loadConnections(defaultClusters: KustoConnection[]): KustoConnection[] {
|
|
22
|
+
const stored = stateService.get<KustoConnection[]>('explorerConnections', 'list');
|
|
23
|
+
if (stored && Array.isArray(stored) && stored.length > 0) {
|
|
24
|
+
return stored;
|
|
25
|
+
}
|
|
26
|
+
return defaultClusters;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function saveConnections(connections: KustoConnection[]): void {
|
|
30
|
+
stateService.set('explorerConnections', 'list', connections);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const EXPLORER_ACTIVE_CONNECTION_KEY = 'explorer-active-connection';
|
|
34
|
+
|
|
35
|
+
export function loadActiveConnectionId(connections: KustoConnection[]): string {
|
|
36
|
+
const stored = stateService.get<string>('config', EXPLORER_ACTIVE_CONNECTION_KEY);
|
|
37
|
+
if (stored && connections.some((connection) => connection.id === stored)) {
|
|
38
|
+
return stored;
|
|
39
|
+
}
|
|
40
|
+
return connections[0]?.id ?? DEFAULT_CONNECTION.id;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function saveActiveConnectionId(connectionId: string): void {
|
|
44
|
+
stateService.set('config', EXPLORER_ACTIVE_CONNECTION_KEY, connectionId);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// --- Pure functions ---
|
|
48
|
+
|
|
49
|
+
let tabCounter = 1;
|
|
50
|
+
export function createTab(kql = '', title?: string, connectionId = ''): QueryTab {
|
|
51
|
+
const n = tabCounter++;
|
|
52
|
+
const id = `tab-${n}`;
|
|
53
|
+
return { id, title: title ?? `Query ${n}`, kql, connectionId };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function nextQueryTitle(existingTabs: QueryTab[]): string {
|
|
57
|
+
const used = new Set(
|
|
58
|
+
existingTabs
|
|
59
|
+
.map((t) => t.title.match(/^Query (\d+)$/))
|
|
60
|
+
.filter(Boolean)
|
|
61
|
+
.map((m) => Number(m![1])),
|
|
62
|
+
);
|
|
63
|
+
let n = 1;
|
|
64
|
+
while (used.has(n)) {
|
|
65
|
+
n++;
|
|
66
|
+
}
|
|
67
|
+
return `Query ${n}`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function formatBytes(bytes: number): string {
|
|
71
|
+
if (bytes === 0) {
|
|
72
|
+
return '0 B';
|
|
73
|
+
}
|
|
74
|
+
if (bytes < 1024) {
|
|
75
|
+
return `${bytes} B`;
|
|
76
|
+
}
|
|
77
|
+
if (bytes < 1024 * 1024) {
|
|
78
|
+
return `${(bytes / 1024).toFixed(1)} KB`;
|
|
79
|
+
}
|
|
80
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function downloadFile(content: string, filename: string, mimeType: string): void {
|
|
84
|
+
const blob = new Blob([content], { type: mimeType });
|
|
85
|
+
const url = URL.createObjectURL(blob);
|
|
86
|
+
const a = document.createElement('a');
|
|
87
|
+
a.href = url;
|
|
88
|
+
a.download = filename;
|
|
89
|
+
a.click();
|
|
90
|
+
URL.revokeObjectURL(url);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function escapeCsvValue(v: string) {
|
|
94
|
+
return v.includes(',') || v.includes('"') || v.includes('\n') ? `"${v.replace(/"/g, '""')}"` : v;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function exportCsv(cols: string[], rows: Record<string, unknown>[]): void {
|
|
98
|
+
const csv = [cols.map(escapeCsvValue).join(','), ...rows.map((r) => cols.map((c) => escapeCsvValue(String((r[c] ?? '') as string | number))).join(','))].join('\n');
|
|
99
|
+
downloadFile(csv, `query-results-${Date.now()}.csv`, 'text/csv');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function exportJson(_cols: string[], rows: Record<string, unknown>[]): void {
|
|
103
|
+
downloadFile(JSON.stringify(rows, null, 2), `query-results-${Date.now()}.json`, 'application/json');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Format a KQL query: newline before each `|`, indent continuation lines,
|
|
108
|
+
* normalize whitespace, and preserve `let` statements and comments.
|
|
109
|
+
*/
|
|
110
|
+
export function formatKql(kql: string): string {
|
|
111
|
+
const statements = kql.split(/\n\s*\n/);
|
|
112
|
+
return statements.map((stmt) => {
|
|
113
|
+
const trimmed = stmt.trim();
|
|
114
|
+
if (!trimmed) {
|
|
115
|
+
return '';
|
|
116
|
+
}
|
|
117
|
+
const flat = trimmed.split('\n').map((l) => l.trim()).join(' ');
|
|
118
|
+
const parts: string[] = [];
|
|
119
|
+
let current = '';
|
|
120
|
+
let inSingle = false;
|
|
121
|
+
let inDouble = false;
|
|
122
|
+
for (const ch of flat) {
|
|
123
|
+
if (ch === "'" && !inDouble) {
|
|
124
|
+
inSingle = !inSingle;
|
|
125
|
+
}
|
|
126
|
+
else if (ch === '"' && !inSingle) {
|
|
127
|
+
inDouble = !inDouble;
|
|
128
|
+
}
|
|
129
|
+
else if (ch === '|' && !inSingle && !inDouble) {
|
|
130
|
+
parts.push(current.trim());
|
|
131
|
+
current = '';
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
current += ch;
|
|
135
|
+
}
|
|
136
|
+
if (current.trim()) {
|
|
137
|
+
parts.push(current.trim());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (parts.length <= 1) {
|
|
141
|
+
return trimmed;
|
|
142
|
+
}
|
|
143
|
+
return parts[0] + '\n' + parts.slice(1).map((p) => '| ' + p).join('\n');
|
|
144
|
+
}).join('\n\n');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// --- Query result helpers (pure, testable without React) ---
|
|
148
|
+
|
|
149
|
+
export interface KustoColumnDef {
|
|
150
|
+
ColumnName: string;
|
|
151
|
+
ColumnType: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Parse Kusto result columns into column names and type maps. */
|
|
155
|
+
export function parseResultColumns(kustoColumns: KustoColumnDef[]): { columns: string[]; columnTypes: Record<string, string> } {
|
|
156
|
+
const columns = kustoColumns.map((c) => c.ColumnName);
|
|
157
|
+
const columnTypes: Record<string, string> = {};
|
|
158
|
+
kustoColumns.forEach((c) => { columnTypes[c.ColumnName] = c.ColumnType; });
|
|
159
|
+
return { columns, columnTypes };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Build a success history entry from query results. */
|
|
163
|
+
export function buildSuccessEntry(query: string, normalizedKey: string, elapsed: number, columns: string[], rows: Record<string, unknown>[]) {
|
|
164
|
+
return {
|
|
165
|
+
key: normalizedKey,
|
|
166
|
+
query,
|
|
167
|
+
timestamp: Date.now(),
|
|
168
|
+
elapsed,
|
|
169
|
+
rowCount: rows.length,
|
|
170
|
+
columnCount: columns.length,
|
|
171
|
+
status: 'success' as const,
|
|
172
|
+
columns,
|
|
173
|
+
rows,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Build an error history entry. */
|
|
178
|
+
export function buildErrorEntry(query: string, normalizedKey: string, errorMsg: string) {
|
|
179
|
+
return {
|
|
180
|
+
key: normalizedKey,
|
|
181
|
+
query,
|
|
182
|
+
timestamp: Date.now(),
|
|
183
|
+
elapsed: null,
|
|
184
|
+
rowCount: null,
|
|
185
|
+
columnCount: 0,
|
|
186
|
+
status: 'error' as const,
|
|
187
|
+
error: errorMsg,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function shortenClusterUrl(url: string): string {
|
|
192
|
+
return url.replace(/^https?:\/\//, '').replace(/\.kusto\.windows\.net$/, '');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// --- Query detection ---
|
|
196
|
+
|
|
197
|
+
export interface QueryRange {
|
|
198
|
+
query: string;
|
|
199
|
+
range: { startLine: number; endLine: number };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// --- Drag-to-split zone calculation ---
|
|
203
|
+
|
|
204
|
+
export type DropZone = 'left' | 'right' | 'top' | 'bottom';
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Given the editor bounding rect and a drag cursor position, returns
|
|
208
|
+
* which edge zone the cursor is closest to.
|
|
209
|
+
*/
|
|
210
|
+
export function getEditorDropTarget(rect: DOMRect, clientX: number, clientY: number): DropZone {
|
|
211
|
+
const relX = (clientX - rect.left) / rect.width;
|
|
212
|
+
const relY = (clientY - rect.top) / rect.height;
|
|
213
|
+
const distLeft = relX;
|
|
214
|
+
const distRight = 1 - relX;
|
|
215
|
+
const distTop = relY;
|
|
216
|
+
const distBottom = 1 - relY;
|
|
217
|
+
const minDist = Math.min(distLeft, distRight, distTop, distBottom);
|
|
218
|
+
if (minDist === distLeft) {
|
|
219
|
+
return 'left';
|
|
220
|
+
} else if (minDist === distRight) {
|
|
221
|
+
return 'right';
|
|
222
|
+
} else if (minDist === distTop) {
|
|
223
|
+
return 'top';
|
|
224
|
+
} else {
|
|
225
|
+
return 'bottom';
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- Keyboard shortcut decision logic ---
|
|
230
|
+
|
|
231
|
+
export type ShortcutAction = 'add-tab' | 'close-tab' | 'close-tab-confirm' | null;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Pure decision function for tab keyboard shortcuts (Cmd/Ctrl+T and Cmd/Ctrl+W).
|
|
235
|
+
* Returns the action to take, or null if the event is not a known shortcut.
|
|
236
|
+
* The component is still responsible for preventDefault and dispatch.
|
|
237
|
+
*/
|
|
238
|
+
export function getShortcutAction(e: { metaKey: boolean; ctrlKey: boolean; key: string }, tabCount: number, activeTabKql: string): ShortcutAction {
|
|
239
|
+
const mod = e.metaKey || e.ctrlKey;
|
|
240
|
+
if (!mod) {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
if (e.key === 't') {
|
|
244
|
+
return 'add-tab';
|
|
245
|
+
}
|
|
246
|
+
if (e.key === 'w' && tabCount > 1) {
|
|
247
|
+
if (activeTabKql.trim()) {
|
|
248
|
+
return 'close-tab-confirm';
|
|
249
|
+
}
|
|
250
|
+
return 'close-tab';
|
|
251
|
+
}
|
|
252
|
+
return null;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Given a Monaco editor model and cursor position, returns the query text to
|
|
257
|
+
* execute: selected text if any, otherwise the "current statement" — the
|
|
258
|
+
* contiguous block of non-blank lines around the cursor, matching ADX behavior.
|
|
259
|
+
*/
|
|
260
|
+
export function getQueryToRun(
|
|
261
|
+
getSelection: () => { isEmpty: () => boolean; startLineNumber: number; endLineNumber: number } | null, getValueInRange: (sel: { startLineNumber: number; endLineNumber: number }) => string, getPosition: () => { lineNumber: number } | null, getLineCount: () => number, getLineContent: (line: number) => string): QueryRange | null {
|
|
262
|
+
const selection = getSelection();
|
|
263
|
+
|
|
264
|
+
// If there's a non-empty selection, use that
|
|
265
|
+
if (selection && !selection.isEmpty()) {
|
|
266
|
+
const text = getValueInRange(selection).trim();
|
|
267
|
+
if (text) {
|
|
268
|
+
return {
|
|
269
|
+
query: text,
|
|
270
|
+
range: { startLine: selection.startLineNumber, endLine: selection.endLineNumber },
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Otherwise find the current statement block around the cursor
|
|
276
|
+
const cursorLine = getPosition()?.lineNumber ?? 1;
|
|
277
|
+
const totalLines = getLineCount();
|
|
278
|
+
|
|
279
|
+
let startLine = cursorLine;
|
|
280
|
+
while (startLine > 1 && getLineContent(startLine - 1).trim() !== '') {
|
|
281
|
+
startLine--;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
let endLine = cursorLine;
|
|
285
|
+
while (endLine < totalLines && getLineContent(endLine + 1).trim() !== '') {
|
|
286
|
+
endLine++;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const lines: string[] = [];
|
|
290
|
+
for (let i = startLine; i <= endLine; i++) {
|
|
291
|
+
lines.push(getLineContent(i));
|
|
292
|
+
}
|
|
293
|
+
const text = lines.join('\n').trim();
|
|
294
|
+
if (!text) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
return { query: text, range: { startLine, endLine } };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Helper to extract QueryRange from a Monaco editor instance.
|
|
303
|
+
* Wraps getQueryToRun with Monaco-specific accessors.
|
|
304
|
+
*/
|
|
305
|
+
export function getQueryToRunFromEditor(ed: { getSelection: () => unknown; getModel: () => unknown; getPosition: () => unknown }): QueryRange | null {
|
|
306
|
+
const model = ed.getModel() as {
|
|
307
|
+
getValueInRange: (sel: unknown) => string;
|
|
308
|
+
getLineCount: () => number;
|
|
309
|
+
getLineContent: (line: number) => string;
|
|
310
|
+
} | null;
|
|
311
|
+
if (!model) {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const selection = ed.getSelection() as { isEmpty: () => boolean; startLineNumber: number; endLineNumber: number } | null;
|
|
316
|
+
|
|
317
|
+
return getQueryToRun(
|
|
318
|
+
() => selection,
|
|
319
|
+
(sel) => model.getValueInRange(sel),
|
|
320
|
+
() => ed.getPosition() as { lineNumber: number } | null,
|
|
321
|
+
() => model.getLineCount(),
|
|
322
|
+
(line) => model.getLineContent(line),
|
|
323
|
+
);
|
|
324
|
+
}
|