@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.
Files changed (90) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +46 -0
  3. package/src/bootstrap.spec.tsx +290 -0
  4. package/src/bootstrap.tsx +195 -0
  5. package/src/colors.ts +23 -0
  6. package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
  7. package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
  8. package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
  9. package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
  10. package/src/components/ChartPanel/ChartPanel.tsx +151 -0
  11. package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
  12. package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
  13. package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
  14. package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
  15. package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
  16. package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
  17. package/src/components/Explorer/Explorer.spec.tsx +615 -0
  18. package/src/components/Explorer/Explorer.tsx +49 -0
  19. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
  20. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
  21. package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
  22. package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
  23. package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
  24. package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
  25. package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
  26. package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
  27. package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
  28. package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
  29. package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
  30. package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
  31. package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
  32. package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
  33. package/src/components/ResultsPanel/TabButton.tsx +11 -0
  34. package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
  35. package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
  36. package/src/components/ResultsTable/FilterCell.tsx +24 -0
  37. package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
  38. package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
  39. package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
  40. package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
  41. package/src/components/ResultsTable/ResultsTable.tsx +501 -0
  42. package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
  43. package/src/components/ResultsTable/VirtualCell.tsx +32 -0
  44. package/src/components/ResultsTable/VirtualRow.tsx +55 -0
  45. package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
  46. package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
  47. package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
  48. package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
  49. package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
  50. package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
  51. package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
  52. package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
  53. package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
  54. package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
  55. package/src/components/TabBar/TabBar.logic.ts +35 -0
  56. package/src/components/TabBar/TabBar.spec.tsx +929 -0
  57. package/src/components/TabBar/TabBar.tsx +284 -0
  58. package/src/components/TabBar/TabItem.tsx +166 -0
  59. package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
  60. package/src/components/VegaChart/VegaChart.tsx +224 -0
  61. package/src/config.spec.ts +44 -0
  62. package/src/config.ts +47 -0
  63. package/src/context/ExplorerColorContext.ts +14 -0
  64. package/src/context/ExplorerStateContext.ts +49 -0
  65. package/src/context/KustoClientContext.ts +12 -0
  66. package/src/context/context.spec.tsx +183 -0
  67. package/src/index.ts +14 -0
  68. package/src/services/cache.spec.ts +31 -0
  69. package/src/services/cache.ts +16 -0
  70. package/src/services/concurrency.spec.ts +139 -0
  71. package/src/services/concurrency.ts +46 -0
  72. package/src/services/kusto.spec.ts +919 -0
  73. package/src/services/kusto.ts +472 -0
  74. package/src/services/queryHistory.spec.ts +287 -0
  75. package/src/services/queryHistory.ts +146 -0
  76. package/src/services/schema.spec.ts +618 -0
  77. package/src/services/schema.ts +294 -0
  78. package/src/services/state-service.spec.ts +670 -0
  79. package/src/services/state-service.ts +602 -0
  80. package/src/services/useKustoQuery.spec.ts +234 -0
  81. package/src/services/useKustoQuery.ts +124 -0
  82. package/src/state/actions.ts +50 -0
  83. package/src/state/index.ts +3 -0
  84. package/src/state/reducer.spec.ts +644 -0
  85. package/src/state/reducer.ts +207 -0
  86. package/src/state/types.ts +54 -0
  87. package/tsconfig.json +7 -0
  88. package/tsconfig.lint.json +4 -0
  89. package/tsconfig.spec.json +7 -0
  90. package/vitest.config.ts +34 -0
@@ -0,0 +1,264 @@
1
+ /**
2
+ * Pure functions for the ResultsTable component.
3
+ * No React dependencies — fully unit-testable.
4
+ */
5
+
6
+ // --- Types ---
7
+
8
+ export type SortDir = 'asc' | 'desc' | null;
9
+
10
+ export interface CellRange {
11
+ startRow: number;
12
+ startCol: number;
13
+ endRow: number;
14
+ endCol: number;
15
+ }
16
+
17
+ export interface HighlightExpr {
18
+ column: string;
19
+ op: string;
20
+ value: string;
21
+ }
22
+
23
+ // --- Cell formatting ---
24
+
25
+ export function serializeCellValue(value: unknown): string {
26
+ if (value === null || value === undefined) {
27
+ return '';
28
+ }
29
+ if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint') {
30
+ return String(value);
31
+ }
32
+ if (value instanceof Date) {
33
+ return value.toISOString();
34
+ }
35
+ try {
36
+ const serialized = JSON.stringify(value);
37
+ return serialized === undefined ? String(value) : serialized;
38
+ } catch {
39
+ return String(value);
40
+ }
41
+ }
42
+
43
+ export function formatCellDisplayValue(value: unknown): string {
44
+ if (typeof value === 'number' && Number.isFinite(value)) {
45
+ return value.toLocaleString();
46
+ }
47
+ return serializeCellValue(value);
48
+ }
49
+
50
+ // --- Cell selection ---
51
+
52
+ export function isCellInRange(row: number, col: number, range: CellRange): boolean {
53
+ return row >= Math.min(range.startRow, range.endRow) && row <= Math.max(range.startRow, range.endRow)
54
+ && col >= Math.min(range.startCol, range.endCol) && col <= Math.max(range.startCol, range.endCol);
55
+ }
56
+
57
+ export function buildSelectionTsv(rows: Record<string, unknown>[], columns: string[], selection: CellRange | null): string {
58
+ if (!selection || rows.length === 0 || columns.length === 0) {
59
+ return '';
60
+ }
61
+ const minRow = Math.max(0, Math.min(selection.startRow, selection.endRow));
62
+ const maxRow = Math.min(rows.length - 1, Math.max(selection.startRow, selection.endRow));
63
+ const minCol = Math.max(0, Math.min(selection.startCol, selection.endCol));
64
+ const maxCol = Math.min(columns.length - 1, Math.max(selection.startCol, selection.endCol));
65
+
66
+ const lines: string[] = [];
67
+ for (let rowIdx = minRow; rowIdx <= maxRow; rowIdx++) {
68
+ const row = rows[rowIdx];
69
+ const cells: string[] = [];
70
+ for (let colIdx = minCol; colIdx <= maxCol; colIdx++) {
71
+ cells.push(serializeCellValue(row?.[columns[colIdx]]));
72
+ }
73
+ lines.push(cells.join('\t'));
74
+ }
75
+ return lines.join('\n');
76
+ }
77
+
78
+ // --- Highlight expression parsing ---
79
+
80
+ export function parseHighlightExpr(expr: string): HighlightExpr | null {
81
+ const match = expr.match(/^\s*(\w+)\s*(==|!=|>=|<=|>|<|contains|!contains)\s*(.+?)\s*$/i);
82
+ if (!match) {
83
+ return null;
84
+ }
85
+ return { column: match[1], op: match[2].toLowerCase(), value: match[3].replace(/^["']|["']$/g, '') };
86
+ }
87
+
88
+ export function matchesHighlight(row: Record<string, unknown>, expr: HighlightExpr): boolean {
89
+ const raw = row[expr.column];
90
+ const strVal = serializeCellValue(raw);
91
+ const numVal = Number(raw);
92
+ const exprNum = Number(expr.value);
93
+ switch (expr.op) {
94
+ case '==': return strVal === expr.value || (!isNaN(numVal) && !isNaN(exprNum) && numVal === exprNum);
95
+ case '!=': return strVal !== expr.value;
96
+ case '>': return !isNaN(numVal) && !isNaN(exprNum) && numVal > exprNum;
97
+ case '<': return !isNaN(numVal) && !isNaN(exprNum) && numVal < exprNum;
98
+ case '>=': return !isNaN(numVal) && !isNaN(exprNum) && numVal >= exprNum;
99
+ case '<=': return !isNaN(numVal) && !isNaN(exprNum) && numVal <= exprNum;
100
+ case 'contains': return strVal.toLowerCase().includes(expr.value.toLowerCase());
101
+ case '!contains': return !strVal.toLowerCase().includes(expr.value.toLowerCase());
102
+ default: return false;
103
+ }
104
+ }
105
+
106
+ // --- Selection aggregates ---
107
+
108
+ function fmt(n: number) {
109
+ return Number.isInteger(n) ? n.toLocaleString() : n.toLocaleString(undefined, { maximumFractionDigits: 2 });
110
+ }
111
+
112
+ export function computeAggregates(values: number[]): Record<string, string> {
113
+ if (values.length === 0) {
114
+ return {};
115
+ }
116
+ const sorted = [...values].sort((a, b) => a - b);
117
+ const sum = values.reduce((s, v) => s + v, 0);
118
+ const pct = (p: number) => {
119
+ const idx = (p / 100) * (sorted.length - 1);
120
+ const lo = Math.floor(idx);
121
+ const hi = Math.ceil(idx);
122
+ return lo === hi ? sorted[lo] : sorted[lo] + (sorted[hi] - sorted[lo]) * (idx - lo);
123
+ };
124
+ return {
125
+ Count: values.length.toLocaleString(),
126
+ Sum: fmt(sum),
127
+ Avg: fmt(sum / values.length),
128
+ Min: fmt(sorted[0]),
129
+ Max: fmt(sorted[sorted.length - 1]),
130
+ P50: fmt(pct(50)),
131
+ P75: fmt(pct(75)),
132
+ P90: fmt(pct(90)),
133
+ };
134
+ }
135
+
136
+ // --- Datetime delta ---
137
+
138
+ export function computeDateDelta(values: string[]): string | null {
139
+ const dates = values.map((v) => new Date(v).getTime()).filter((t) => !isNaN(t));
140
+ if (dates.length < 2) {
141
+ return null;
142
+ }
143
+ const min = Math.min(...dates);
144
+ const max = Math.max(...dates);
145
+ const deltaMs = max - min;
146
+ if (deltaMs < 1000) {
147
+ return `${deltaMs}ms`;
148
+ }
149
+ if (deltaMs < 60000) {
150
+ return `${(deltaMs / 1000).toFixed(1)}s`;
151
+ }
152
+ if (deltaMs < 3600000) {
153
+ return `${(deltaMs / 60000).toFixed(1)}m`;
154
+ }
155
+ if (deltaMs < 86400000) {
156
+ return `${(deltaMs / 3600000).toFixed(1)}h`;
157
+ }
158
+ return `${(deltaMs / 86400000).toFixed(1)}d`;
159
+ }
160
+
161
+ // --- Kusto type shortening ---
162
+
163
+ const KUSTO_TYPE_MAP: Record<string, string> = {
164
+ 'System.String': 'string',
165
+ 'System.Int64': 'long',
166
+ 'System.Int32': 'int',
167
+ 'System.Double': 'real',
168
+ 'System.Single': 'real',
169
+ 'System.Boolean': 'bool',
170
+ 'System.DateTime': 'datetime',
171
+ 'System.TimeSpan': 'timespan',
172
+ 'System.Guid': 'guid',
173
+ 'System.Object': 'dynamic',
174
+ 'System.SByte': 'bool',
175
+ };
176
+
177
+ export function shortType(t: string): string {
178
+ if (t.startsWith('System.')) {
179
+ return KUSTO_TYPE_MAP[t] ?? t.split('.').pop()?.toLowerCase() ?? t;
180
+ }
181
+ return t;
182
+ }
183
+
184
+ // --- Column width computation ---
185
+
186
+ export function computeColumnWidths(columns: string[], rows: Record<string, unknown>[], columnTypes?: Record<string, string>): number[] {
187
+ return columns.map((col) => {
188
+ const headerLen = col.length + (columnTypes?.[col] ? shortType(columnTypes[col]).length + 1 : 0);
189
+ let maxLen = headerLen;
190
+ const sampleSize = Math.min(rows.length, 100);
191
+ for (let i = 0; i < sampleSize; i++) {
192
+ const v = rows[i][col];
193
+ const len = v === null || v === undefined ? 4 : formatCellDisplayValue(v).length;
194
+ if (len > maxLen) {
195
+ maxLen = len;
196
+ }
197
+ }
198
+ return Math.min(Math.max(maxLen * 8 + 24, 60), 500);
199
+ });
200
+ }
201
+
202
+ // --- Sorting ---
203
+
204
+ export function sortRows(rows: Record<string, unknown>[], sortColumn: string | null, sortDir: SortDir): Record<string, unknown>[] {
205
+ if (!sortColumn || !sortDir) {
206
+ return rows;
207
+ }
208
+ return [...rows].sort((a, b) => {
209
+ const aVal = a[sortColumn];
210
+ const bVal = b[sortColumn];
211
+ if (aVal === null || aVal === undefined) {
212
+ return sortDir === 'asc' ? -1 : 1;
213
+ }
214
+ if (bVal === null || bVal === undefined) {
215
+ return sortDir === 'asc' ? 1 : -1;
216
+ }
217
+ const aNum = Number(aVal);
218
+ const bNum = Number(bVal);
219
+ if (!isNaN(aNum) && !isNaN(bNum)) {
220
+ return sortDir === 'asc' ? aNum - bNum : bNum - aNum;
221
+ }
222
+ const aStr = serializeCellValue(aVal);
223
+ const bStr = serializeCellValue(bVal);
224
+ return sortDir === 'asc' ? aStr.localeCompare(bStr) : bStr.localeCompare(aStr);
225
+ });
226
+ }
227
+
228
+ // --- Filtering ---
229
+
230
+ export function filterRows(rows: Record<string, unknown>[], columns: string[], globalSearch: string, columnFilters: Record<string, string>): Record<string, unknown>[] {
231
+ let filtered = rows;
232
+ if (globalSearch) {
233
+ const lower = globalSearch.toLowerCase();
234
+ filtered = filtered.filter((row) =>
235
+ columns.some((col) => {
236
+ const v = row[col];
237
+ return v !== null && v !== undefined && serializeCellValue(v).toLowerCase().includes(lower);
238
+ }),
239
+ );
240
+ }
241
+ for (const [col, filter] of Object.entries(columnFilters)) {
242
+ if (!filter) {
243
+ continue;
244
+ }
245
+ const lower = filter.toLowerCase();
246
+ filtered = filtered.filter((row) => {
247
+ const v = row[col];
248
+ return v !== null && v !== undefined && serializeCellValue(v).toLowerCase().includes(lower);
249
+ });
250
+ }
251
+ return filtered;
252
+ }
253
+
254
+ // --- Empty column detection ---
255
+
256
+ export function findEmptyColumns(columns: string[], rows: Record<string, unknown>[]): Set<string> {
257
+ const empty = new Set<string>();
258
+ for (const col of columns) {
259
+ if (rows.every((r) => { const v = r[col]; return v === null || v === undefined || v === ''; })) {
260
+ empty.add(col);
261
+ }
262
+ }
263
+ return empty;
264
+ }