@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,287 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
InMemoryQueryHistoryStore,
|
|
5
|
+
normalizeQuery,
|
|
6
|
+
getHistory,
|
|
7
|
+
saveHistoryEntry,
|
|
8
|
+
recallResult,
|
|
9
|
+
deleteHistoryEntry,
|
|
10
|
+
clearHistory,
|
|
11
|
+
} from './queryHistory';
|
|
12
|
+
import type { QueryHistoryEntry } from './queryHistory';
|
|
13
|
+
|
|
14
|
+
function makeEntry(overrides: Partial<QueryHistoryEntry> = {}): QueryHistoryEntry {
|
|
15
|
+
return {
|
|
16
|
+
key: normalizeQuery(overrides.query ?? 'SELECT 1'),
|
|
17
|
+
query: 'SELECT 1',
|
|
18
|
+
timestamp: Date.now(),
|
|
19
|
+
elapsed: 100,
|
|
20
|
+
rowCount: 1,
|
|
21
|
+
columnCount: 1,
|
|
22
|
+
status: 'success',
|
|
23
|
+
...overrides,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('normalizeQuery', () => {
|
|
28
|
+
it('trims leading and trailing whitespace', () => {
|
|
29
|
+
expect(normalizeQuery(' hello ')).toBe('hello');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('collapses internal whitespace', () => {
|
|
33
|
+
expect(normalizeQuery('select * from table')).toBe('select * from table');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('handles tabs and newlines', () => {
|
|
37
|
+
expect(normalizeQuery('select\n\t*\n\tfrom\ttable')).toBe('select * from table');
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('returns empty string for whitespace-only input', () => {
|
|
41
|
+
expect(normalizeQuery(' ')).toBe('');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('InMemoryQueryHistoryStore', () => {
|
|
46
|
+
let store: InMemoryQueryHistoryStore;
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
store = new InMemoryQueryHistoryStore();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('save and getAll', () => {
|
|
53
|
+
it('starts empty', async () => {
|
|
54
|
+
const entries = await store.getAll();
|
|
55
|
+
expect(entries).toEqual([]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('saves and retrieves an entry', async () => {
|
|
59
|
+
const entry = makeEntry({ query: 'test query', key: 'test query' });
|
|
60
|
+
await store.save(entry);
|
|
61
|
+
|
|
62
|
+
const entries = await store.getAll();
|
|
63
|
+
expect(entries).toHaveLength(1);
|
|
64
|
+
expect(entries[0].query).toBe('test query');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('returns entries sorted by timestamp descending', async () => {
|
|
68
|
+
await store.save(makeEntry({ key: 'q1', query: 'q1', timestamp: 100 }));
|
|
69
|
+
await store.save(makeEntry({ key: 'q2', query: 'q2', timestamp: 300 }));
|
|
70
|
+
await store.save(makeEntry({ key: 'q3', query: 'q3', timestamp: 200 }));
|
|
71
|
+
|
|
72
|
+
const entries = await store.getAll();
|
|
73
|
+
expect(entries.map((e) => e.key)).toEqual(['q2', 'q3', 'q1']);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('updates an existing entry by key', async () => {
|
|
77
|
+
await store.save(makeEntry({ key: 'same-key', query: 'original', timestamp: 100 }));
|
|
78
|
+
await store.save(makeEntry({ key: 'same-key', query: 'updated', timestamp: 200 }));
|
|
79
|
+
|
|
80
|
+
const entries = await store.getAll();
|
|
81
|
+
expect(entries).toHaveLength(1);
|
|
82
|
+
expect(entries[0].query).toBe('updated');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('maxEntries eviction', () => {
|
|
87
|
+
it('evicts oldest entries when exceeding max (50)', async () => {
|
|
88
|
+
// Save 52 entries
|
|
89
|
+
for (let i = 0; i < 52; i++) {
|
|
90
|
+
await store.save(makeEntry({
|
|
91
|
+
key: `query-${i}`,
|
|
92
|
+
query: `query-${i}`,
|
|
93
|
+
timestamp: i,
|
|
94
|
+
}));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const entries = await store.getAll();
|
|
98
|
+
expect(entries).toHaveLength(50);
|
|
99
|
+
|
|
100
|
+
// The two oldest (timestamp 0 and 1) should be evicted
|
|
101
|
+
const keys = entries.map((e) => e.key);
|
|
102
|
+
expect(keys).not.toContain('query-0');
|
|
103
|
+
expect(keys).not.toContain('query-1');
|
|
104
|
+
expect(keys).toContain('query-2');
|
|
105
|
+
expect(keys).toContain('query-51');
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('delete', () => {
|
|
110
|
+
it('deletes an entry by key', async () => {
|
|
111
|
+
await store.save(makeEntry({ key: 'to-delete', query: 'to-delete' }));
|
|
112
|
+
await store.save(makeEntry({ key: 'to-keep', query: 'to-keep' }));
|
|
113
|
+
|
|
114
|
+
await store.delete('to-delete');
|
|
115
|
+
|
|
116
|
+
const entries = await store.getAll();
|
|
117
|
+
expect(entries).toHaveLength(1);
|
|
118
|
+
expect(entries[0].key).toBe('to-keep');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('is a no-op for non-existent key', async () => {
|
|
122
|
+
await store.save(makeEntry({ key: 'exists', query: 'exists' }));
|
|
123
|
+
await store.delete('non-existent');
|
|
124
|
+
|
|
125
|
+
const entries = await store.getAll();
|
|
126
|
+
expect(entries).toHaveLength(1);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe('clear', () => {
|
|
131
|
+
it('removes all entries', async () => {
|
|
132
|
+
await store.save(makeEntry({ key: 'a', query: 'a' }));
|
|
133
|
+
await store.save(makeEntry({ key: 'b', query: 'b' }));
|
|
134
|
+
|
|
135
|
+
await store.clear();
|
|
136
|
+
|
|
137
|
+
const entries = await store.getAll();
|
|
138
|
+
expect(entries).toEqual([]);
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
describe('recall', () => {
|
|
143
|
+
it('returns a matching success entry with rows and columns', async () => {
|
|
144
|
+
const entry = makeEntry({
|
|
145
|
+
key: 'recall query',
|
|
146
|
+
query: ' recall query ',
|
|
147
|
+
status: 'success',
|
|
148
|
+
columns: ['Col1'],
|
|
149
|
+
rows: [{ Col1: 'val' }],
|
|
150
|
+
});
|
|
151
|
+
await store.save(entry);
|
|
152
|
+
|
|
153
|
+
const recalled = await store.recall(' recall query ');
|
|
154
|
+
expect(recalled).not.toBeNull();
|
|
155
|
+
expect(recalled?.key).toBe('recall query');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('returns null for non-existent query', async () => {
|
|
159
|
+
const recalled = await store.recall('missing');
|
|
160
|
+
expect(recalled).toBeNull();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('returns null for error entries', async () => {
|
|
164
|
+
await store.save(makeEntry({
|
|
165
|
+
key: 'error query',
|
|
166
|
+
query: 'error query',
|
|
167
|
+
status: 'error',
|
|
168
|
+
columns: ['A'],
|
|
169
|
+
rows: [{ A: 1 }],
|
|
170
|
+
}));
|
|
171
|
+
|
|
172
|
+
const recalled = await store.recall('error query');
|
|
173
|
+
expect(recalled).toBeNull();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('returns null for success entries without rows', async () => {
|
|
177
|
+
await store.save(makeEntry({
|
|
178
|
+
key: 'no rows',
|
|
179
|
+
query: 'no rows',
|
|
180
|
+
status: 'success',
|
|
181
|
+
columns: ['A'],
|
|
182
|
+
}));
|
|
183
|
+
|
|
184
|
+
const recalled = await store.recall('no rows');
|
|
185
|
+
expect(recalled).toBeNull();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('returns null for success entries without columns', async () => {
|
|
189
|
+
await store.save(makeEntry({
|
|
190
|
+
key: 'no cols',
|
|
191
|
+
query: 'no cols',
|
|
192
|
+
status: 'success',
|
|
193
|
+
rows: [{ A: 1 }],
|
|
194
|
+
}));
|
|
195
|
+
|
|
196
|
+
const recalled = await store.recall('no cols');
|
|
197
|
+
expect(recalled).toBeNull();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('normalizes query before lookup', async () => {
|
|
201
|
+
await store.save(makeEntry({
|
|
202
|
+
key: 'table | take 10',
|
|
203
|
+
query: 'table | take 10',
|
|
204
|
+
status: 'success',
|
|
205
|
+
columns: ['Col'],
|
|
206
|
+
rows: [{ Col: 'v' }],
|
|
207
|
+
}));
|
|
208
|
+
|
|
209
|
+
const recalled = await store.recall(' table | take 10 ');
|
|
210
|
+
expect(recalled).not.toBeNull();
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// --- Mock StateService for public API tests ---
|
|
216
|
+
|
|
217
|
+
const mockExplorerCache = new Map<string, unknown>();
|
|
218
|
+
vi.mock('./state-service', () => ({
|
|
219
|
+
stateService: {
|
|
220
|
+
get: (_s: string, key: string) => mockExplorerCache.get(key) ?? null,
|
|
221
|
+
set: (_s: string, key: string, value: unknown) => { mockExplorerCache.set(key, value); },
|
|
222
|
+
delete: (_s: string, key: string) => { mockExplorerCache.delete(key); },
|
|
223
|
+
clear: (_s: string) => { mockExplorerCache.clear(); },
|
|
224
|
+
keys: () => [...mockExplorerCache.keys()],
|
|
225
|
+
subscribe: () => () => {},
|
|
226
|
+
},
|
|
227
|
+
}));
|
|
228
|
+
|
|
229
|
+
describe('Public API (delegates to StateService store)', () => {
|
|
230
|
+
beforeEach(() => {
|
|
231
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
232
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
233
|
+
mockExplorerCache.clear();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('getHistory returns entries from store', async () => {
|
|
237
|
+
const entries = await getHistory();
|
|
238
|
+
expect(Array.isArray(entries)).toBe(true);
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it('saveHistoryEntry writes to store', async () => {
|
|
242
|
+
const entry = makeEntry({ key: 'pub-save', query: 'pub-save' });
|
|
243
|
+
await saveHistoryEntry(entry);
|
|
244
|
+
expect(mockExplorerCache.has('pub-save')).toBe(true);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('recallResult returns null for non-existent query', async () => {
|
|
248
|
+
const result = await recallResult('nonexistent');
|
|
249
|
+
expect(result).toBeNull();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('recallResult returns matching success entry', async () => {
|
|
253
|
+
const entry = makeEntry({
|
|
254
|
+
key: 'recall test',
|
|
255
|
+
query: 'recall test',
|
|
256
|
+
status: 'success',
|
|
257
|
+
columns: ['Col1'],
|
|
258
|
+
rows: [{ Col1: 'value' }],
|
|
259
|
+
});
|
|
260
|
+
await saveHistoryEntry(entry);
|
|
261
|
+
const result = await recallResult('recall test');
|
|
262
|
+
expect(result).not.toBeNull();
|
|
263
|
+
expect(result?.key).toBe('recall test');
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it('triggers eviction when exceeding MAX_HISTORY', async () => {
|
|
267
|
+
for (let i = 0; i < 51; i++) {
|
|
268
|
+
await saveHistoryEntry(makeEntry({ key: `key-${String(i)}`, query: `query ${String(i)}`, timestamp: i }));
|
|
269
|
+
}
|
|
270
|
+
// Should have evicted the oldest, keeping 50
|
|
271
|
+
expect(mockExplorerCache.size).toBeLessThanOrEqual(50);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('deleteHistoryEntry removes from store', async () => {
|
|
275
|
+
const entry = makeEntry({ key: 'del-key', query: 'del-key' });
|
|
276
|
+
await saveHistoryEntry(entry);
|
|
277
|
+
await deleteHistoryEntry('del-key');
|
|
278
|
+
expect(mockExplorerCache.has('del-key')).toBe(false);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('clearHistory empties store', async () => {
|
|
282
|
+
await saveHistoryEntry(makeEntry({ key: 'a', query: 'a' }));
|
|
283
|
+
await saveHistoryEntry(makeEntry({ key: 'b', query: 'b' }));
|
|
284
|
+
await clearHistory();
|
|
285
|
+
expect(mockExplorerCache.size).toBe(0);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
const MAX_HISTORY = 50;
|
|
2
|
+
const STORE_NAME = 'explorerCache' as const;
|
|
3
|
+
|
|
4
|
+
import { stateService } from './state-service';
|
|
5
|
+
|
|
6
|
+
export interface QueryHistoryEntry {
|
|
7
|
+
/** Normalized query text (trimmed, collapsed whitespace) used as the key */
|
|
8
|
+
key: string;
|
|
9
|
+
/** Original query text as written */
|
|
10
|
+
query: string;
|
|
11
|
+
timestamp: number;
|
|
12
|
+
elapsed: number | null;
|
|
13
|
+
rowCount: number | null;
|
|
14
|
+
columnCount: number;
|
|
15
|
+
status: 'success' | 'error';
|
|
16
|
+
error?: string;
|
|
17
|
+
/** Stored result columns for Recall */
|
|
18
|
+
columns?: string[];
|
|
19
|
+
/** Stored result rows for Recall */
|
|
20
|
+
rows?: Record<string, unknown>[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function normalizeQuery(query: string): string {
|
|
24
|
+
return query.trim().replace(/\s+/g, ' ');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// --- Store interface (injectable for testing) ---
|
|
28
|
+
|
|
29
|
+
export interface IQueryHistoryStore {
|
|
30
|
+
getAll(): Promise<QueryHistoryEntry[]>;
|
|
31
|
+
save(entry: QueryHistoryEntry): Promise<void>;
|
|
32
|
+
recall(query: string): Promise<QueryHistoryEntry | null>;
|
|
33
|
+
delete(key: string): Promise<void>;
|
|
34
|
+
clear(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// --- StateService-backed implementation ---
|
|
38
|
+
|
|
39
|
+
class StateServiceQueryHistoryStore implements IQueryHistoryStore {
|
|
40
|
+
async getAll(): Promise<QueryHistoryEntry[]> {
|
|
41
|
+
const keys = stateService.keys(STORE_NAME);
|
|
42
|
+
const entries: QueryHistoryEntry[] = [];
|
|
43
|
+
for (const key of keys) {
|
|
44
|
+
const entry = stateService.get<QueryHistoryEntry>(STORE_NAME, key);
|
|
45
|
+
if (entry) {
|
|
46
|
+
entries.push(entry);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return entries.sort((a, b) => b.timestamp - a.timestamp);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async save(entry: QueryHistoryEntry): Promise<void> {
|
|
53
|
+
stateService.set(STORE_NAME, entry.key, entry);
|
|
54
|
+
|
|
55
|
+
// Evict oldest beyond MAX_HISTORY
|
|
56
|
+
const all = await this.getAll();
|
|
57
|
+
if (all.length > MAX_HISTORY) {
|
|
58
|
+
const toRemove = all.slice(MAX_HISTORY);
|
|
59
|
+
for (const e of toRemove) {
|
|
60
|
+
stateService.delete(STORE_NAME, e.key);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async recall(query: string): Promise<QueryHistoryEntry | null> {
|
|
66
|
+
const key = normalizeQuery(query);
|
|
67
|
+
const entry = stateService.get<QueryHistoryEntry>(STORE_NAME, key);
|
|
68
|
+
if (entry?.status === 'success' && entry.rows && entry.columns) {
|
|
69
|
+
return entry;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async delete(key: string): Promise<void> {
|
|
75
|
+
stateService.delete(STORE_NAME, key);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async clear(): Promise<void> {
|
|
79
|
+
stateService.clear(STORE_NAME);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// --- In-memory implementation (for testing) ---
|
|
84
|
+
|
|
85
|
+
export class InMemoryQueryHistoryStore implements IQueryHistoryStore {
|
|
86
|
+
private entries = new Map<string, QueryHistoryEntry>();
|
|
87
|
+
|
|
88
|
+
async getAll(): Promise<QueryHistoryEntry[]> {
|
|
89
|
+
return [...this.entries.values()].sort((a, b) => b.timestamp - a.timestamp);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async save(entry: QueryHistoryEntry): Promise<void> {
|
|
93
|
+
this.entries.set(entry.key, entry);
|
|
94
|
+
// Evict oldest beyond MAX_HISTORY
|
|
95
|
+
if (this.entries.size > MAX_HISTORY) {
|
|
96
|
+
const sorted = [...this.entries.entries()].sort(([, a], [, b]) => a.timestamp - b.timestamp);
|
|
97
|
+
const excess = sorted.length - MAX_HISTORY;
|
|
98
|
+
for (let i = 0; i < excess; i++) {
|
|
99
|
+
this.entries.delete(sorted[i][0]);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async recall(query: string): Promise<QueryHistoryEntry | null> {
|
|
105
|
+
const key = normalizeQuery(query);
|
|
106
|
+
const entry = this.entries.get(key);
|
|
107
|
+
if (entry?.status === 'success' && entry.rows && entry.columns) {
|
|
108
|
+
return entry;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async delete(key: string): Promise<void> {
|
|
114
|
+
this.entries.delete(key);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async clear(): Promise<void> {
|
|
118
|
+
this.entries.clear();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// --- Singleton store instance ---
|
|
123
|
+
|
|
124
|
+
const store: IQueryHistoryStore = new StateServiceQueryHistoryStore();
|
|
125
|
+
|
|
126
|
+
// --- Public API (backward-compatible, delegates to store) ---
|
|
127
|
+
|
|
128
|
+
export function getHistory(): Promise<QueryHistoryEntry[]> {
|
|
129
|
+
return store.getAll();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function saveHistoryEntry(entry: QueryHistoryEntry): Promise<void> {
|
|
133
|
+
return store.save(entry);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function recallResult(query: string): Promise<QueryHistoryEntry | null> {
|
|
137
|
+
return store.recall(query);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function deleteHistoryEntry(key: string): Promise<void> {
|
|
141
|
+
return store.delete(key);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function clearHistory(): Promise<void> {
|
|
145
|
+
return store.clear();
|
|
146
|
+
}
|