Add loading text to query history view (#20717)

* Add max entries to query history

* Update query history README

* Add loading text to query history view

* add comment
This commit is contained in:
Charles Gagnon
2022-10-03 18:21:40 -07:00
committed by GitHub
parent 266326252a
commit 3df9985aff
6 changed files with 75 additions and 29 deletions

View File

@@ -189,6 +189,12 @@
"viewsWelcome": [
{
"view": "queryHistory",
"when": "queryHistory.loading",
"contents": "%queryHistory.loading%"
},
{
"view": "queryHistory",
"when": "queryHistory.noEntries",
"contents": "%queryHistory.noEntries%"
}
],

View File

@@ -13,6 +13,7 @@
"queryHistory.disableCapture": "Pause Query History Capture",
"queryHistory.enableCapture": "Start Query History Capture",
"queryHistory.noEntries": "No queries to display",
"queryHistory.loading": "Loading saved entries...",
"queryHistory.maxEntries": "Maximum number of entries to store. 0 means unlimited entries are stored. Increasing this limit may impact performance, especially if persistence is enabled.",
"queryHistory.openStorageFolder": "Open storage folder"
}

View File

@@ -11,3 +11,6 @@ export const MAX_ENTRIES_CONFIG_SECTION = 'maxEntries';
export const ITEM_SELECTED_COMMAND_ID = 'queryHistory.itemSelected';
export const CONTEXT_LOADING = 'queryHistory.loading';
export const CONTEXT_NOENTRIES = 'queryHistory.noEntries';

View File

@@ -7,7 +7,7 @@ import * as azdata from 'azdata';
import * as vscode from 'vscode';
import { DOUBLE_CLICK_ACTION_CONFIG_SECTION, ITEM_SELECTED_COMMAND_ID, QUERY_HISTORY_CONFIG_SECTION } from './constants';
import { QueryHistoryItem } from './queryHistoryItem';
import { QueryHistoryProvider } from './queryHistoryProvider';
import { QueryHistoryProvider, setLoadingContext } from './queryHistoryProvider';
import { promises as fs } from 'fs';
let lastSelectedItem: { item: QueryHistoryItem | undefined, time: number | undefined } = {
@@ -29,6 +29,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
console.error(`Error creating query history global storage folder ${context.globalStorageUri.fsPath}. ${err}`);
}
}
await setLoadingContext(true);
const treeDataProvider = new QueryHistoryProvider(context, storageUri);
context.subscriptions.push(treeDataProvider);
const treeView = vscode.window.createTreeView('queryHistory', {

View File

@@ -7,7 +7,7 @@ import * as vscode from 'vscode';
import * as azdata from 'azdata';
import { QueryHistoryItem } from './queryHistoryItem';
import { debounce, removeNewLines } from './utils';
import { CAPTURE_ENABLED_CONFIG_SECTION, ITEM_SELECTED_COMMAND_ID, MAX_ENTRIES_CONFIG_SECTION, PERSIST_HISTORY_CONFIG_SECTION, QUERY_HISTORY_CONFIG_SECTION } from './constants';
import { CAPTURE_ENABLED_CONFIG_SECTION, CONTEXT_LOADING, CONTEXT_NOENTRIES, ITEM_SELECTED_COMMAND_ID, MAX_ENTRIES_CONFIG_SECTION, PERSIST_HISTORY_CONFIG_SECTION, QUERY_HISTORY_CONFIG_SECTION } from './constants';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
@@ -35,6 +35,8 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
private _maxEntries: number = DEFAULT_MAX_ENTRIES;
private _historyStorageFile: string;
private _initPromise: Promise<void> | undefined = undefined;
private _disposables: vscode.Disposable[] = [];
private writeHistoryFileWorker: (() => void) | undefined;
@@ -48,7 +50,7 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
constructor(private _context: vscode.ExtensionContext, storageUri: vscode.Uri) {
this._historyStorageFile = path.join(storageUri.fsPath, HISTORY_STORAGE_FILE_NAME);
// Kick off initialization but then continue on since that may take a while and we don't want to block extension activation
void this.initialize();
this._initPromise = this.initialize();
this._disposables.push(vscode.workspace.onDidChangeConfiguration(async e => {
if (e.affectsConfiguration(QUERY_HISTORY_CONFIG_SECTION) || e.affectsConfiguration(MAX_ENTRIES_CONFIG_SECTION)) {
await this.updateConfigurationValues();
@@ -174,8 +176,12 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
console.error(`Error moving corrupted history file: ${err}`);
}
}
}
await this.updateNoEntriesContext();
// Done loading so hide the loading welcome text
await setLoadingContext(false);
this._initPromise = undefined;
}
/**
@@ -191,12 +197,14 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
public async clearAll(): Promise<void> {
this._queryHistoryItems = [];
this.writeHistoryFile();
await this.updateNoEntriesContext();
this._onDidChangeTreeData.fire(undefined);
}
public async deleteItem(item: QueryHistoryItem): Promise<void> {
this._queryHistoryItems = this._queryHistoryItems.filter(n => n !== item);
this.writeHistoryFile();
await this.updateNoEntriesContext();
this._onDidChangeTreeData.fire(undefined);
}
@@ -209,7 +217,8 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
return treeItem;
}
public getChildren(element?: QueryHistoryItem): QueryHistoryItem[] {
public async getChildren(element?: QueryHistoryItem): Promise<QueryHistoryItem[]> {
await this._initPromise;
// We only have top level items
return this._queryHistoryItems;
}
@@ -227,6 +236,10 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
this._maxEntries = configSection.get(MAX_ENTRIES_CONFIG_SECTION, DEFAULT_MAX_ENTRIES);
this.trimExtraEntries();
if (!this._persistHistory) {
// We're not persisting history so we can immediately set loading to false to immediately
// hide the loading text.
await setLoadingContext(false);
this._initPromise = undefined;
// If we're no longer persisting the history then clean up our storage file
try {
await fs.promises.rmdir(this._historyStorageFile);
@@ -271,4 +284,27 @@ export class QueryHistoryProvider implements vscode.TreeDataProvider<QueryHistor
this._persistHistory = enabled;
return vscode.workspace.getConfiguration(QUERY_HISTORY_CONFIG_SECTION).update(PERSIST_HISTORY_CONFIG_SECTION, this._persistHistory, vscode.ConfigurationTarget.Global);
}
/**
* Sets the 'queryHistory.noEntries context, which is used to display the "No Entries" text in the
* tree view when there are no entries stored.
* @returns A promise that completes when the setContext command has been executed
*/
public async updateNoEntriesContext(): Promise<void> {
// Only show the "No Entries" text if there's no loaded entries - otherwise it will show the text until
// the tree view actually displays the items.
// Note that we only have to call this when deleting items, not adding, since that's the only time outside
// the initial load that we may end up with 0 items in the list.
return vscode.commands.executeCommand('setContext', CONTEXT_NOENTRIES, this._queryHistoryItems.length === 0);
}
}
/**
* Sets the 'queryHistory.loaded' context, which is used to display the loading message in the tree view
* while entries are being loaded from disk.
* @param loading The loaded state to set
* @returns A promise that completes when the setContext command has been executed
*/
export async function setLoadingContext(loading: boolean): Promise<void> {
return vscode.commands.executeCommand('setContext', CONTEXT_LOADING, loading);
}

View File

@@ -28,9 +28,8 @@ describe('QueryHistoryProvider', () => {
textDocumentSandbox.replaceGetter(vscode.workspace, 'textDocuments', () => [azdataTest.mocks.vscode.createTextDocumentMock(testUri).object]);
const getConnectionStub = sinon.stub(azdata.connection, 'getConnection');
getConnectionStub.resolves(<any>{});
// const getConfigurationStub = sinon.stub(vscode.workspace, 'getConfiguration')
const contextMock = azdataTest.mocks.vscode.createExtensionContextMock();
testProvider = new QueryHistoryProvider(contextMock.object);
testProvider = new QueryHistoryProvider(contextMock.object, contextMock.object.globalStorageUri);
// Disable persistence during tests
await testProvider.setPersistenceEnabled(false);
});
@@ -39,14 +38,14 @@ describe('QueryHistoryProvider', () => {
sinon.restore();
});
it('There should be no children initially', function () {
const children = testProvider.getChildren();
it('There should be no children initially', async function () {
const children = await testProvider.getChildren();
should(children).length(0);
});
it('Clearing empty list does not throw', async function () {
await testProvider.clearAll();
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(0);
});
@@ -54,7 +53,7 @@ describe('QueryHistoryProvider', () => {
const types: azdata.queryeditor.QueryEventType[] = ['executionPlan', 'queryStart', 'queryUpdate', 'visualize'];
for (const type of types) {
await fireQueryEventAndWaitForRefresh(type, <any>{ uri: testUri.toString() }, { messages: [], batchRanges: [] }, 2000);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(0, `Should have no children after ${type} event`);
}
});
@@ -62,7 +61,7 @@ describe('QueryHistoryProvider', () => {
it('queryStop events cause children to be added', async function () {
setupTextEditorMock('SELECT 1');
await fireQueryStartAndStopAndWaitForRefresh(testUri);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
await fireQueryStartAndStopAndWaitForRefresh(testUri);
@@ -73,7 +72,7 @@ describe('QueryHistoryProvider', () => {
const content = 'SELECT 1\nSELECT 2';
setupTextEditorMock(content);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
should(children[0].queryText).be.equal(content, 'item content should be full text content');
});
@@ -83,7 +82,7 @@ describe('QueryHistoryProvider', () => {
const rangeWithContent2: azdataTest.mocks.vscode.RangeWithContent = { range: new vscode.Range(new vscode.Position(3, 0), new vscode.Position(3, 5)), content: 'SELECT 2' };
setupTextEditorMock([rangeWithContent1, rangeWithContent2], [new vscode.Selection(rangeWithContent1.range.start, rangeWithContent1.range.end)]);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
should(children[0].queryText).be.equal(rangeWithContent1.content, 'item content should be only active selection');
});
@@ -94,7 +93,7 @@ describe('QueryHistoryProvider', () => {
const message2: azdata.queryeditor.QueryMessage = { message: 'Error message', isError: true };
const message3: azdata.queryeditor.QueryMessage = { message: 'Message 2', isError: false };
await fireQueryStartAndStopAndWaitForRefresh(testUri, { messages: [message1, message2, message3], batchRanges: [] });
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
should(children[0].isSuccess).be.false('Event with errors should have error icon');
});
@@ -105,7 +104,7 @@ describe('QueryHistoryProvider', () => {
const message2: azdata.queryeditor.QueryMessage = { message: 'Message 2', isError: false };
const message3: azdata.queryeditor.QueryMessage = { message: 'Message 3', isError: false };
await fireQueryStartAndStopAndWaitForRefresh(testUri, { messages: [message1, message2, message3], batchRanges: [] });
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
should(children[0].isSuccess).be.true('Event without errors should have check icon');
});
@@ -115,17 +114,17 @@ describe('QueryHistoryProvider', () => {
const queryDocumentMock = azdataTest.mocks.azdata.queryeditor.createQueryDocumentMock(unknownUri.toString());
// Since we didn't find the text document we'll never update the item list so add a timeout since that event will never fire
await fireQueryEventAndWaitForRefresh('queryStop', queryDocumentMock.object, { messages: [], batchRanges: [] }, 2000);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(0, 'Should not have any children');
});
it('can clear all with one child', async function () {
await fireQueryStartAndStopAndWaitForRefresh(testUri);
let children = testProvider.getChildren();
let children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding item');
await waitForItemRefresh(() => testProvider.clearAll());
children = testProvider.getChildren();
children = await testProvider.getChildren();
should(children).length(0, 'Should have no children after clearing');
});
@@ -133,29 +132,29 @@ describe('QueryHistoryProvider', () => {
await fireQueryStartAndStopAndWaitForRefresh(testUri);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
let children = testProvider.getChildren();
let children = await testProvider.getChildren();
should(children).length(3, 'Should have 3 children after adding item');
await waitForItemRefresh(() => testProvider.clearAll());
children = testProvider.getChildren();
children = await testProvider.getChildren();
should(children).length(0, 'Should have no children after clearing');
});
it('delete item when no items doesn\'t throw', async function () {
const testItem: QueryHistoryItem = { queryText: 'SELECT 1', connectionProfile: azdataTest.stubs.azdata.createConnectionProfile(), timestamp: new Date().toLocaleString(), isSuccess: true };
await waitForItemRefresh(() => testProvider.deleteItem(testItem));
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(0, 'Should have no children after deleting item');
});
it('delete item that doesn\'t exist doesn\'t throw', async function () {
await fireQueryStartAndStopAndWaitForRefresh(testUri);
let children = testProvider.getChildren();
let children = await testProvider.getChildren();
should(children).length(1, 'Should have 1 child initially');
const testItem: QueryHistoryItem = { queryText: 'SELECT 1', connectionProfile: azdataTest.stubs.azdata.createConnectionProfile(), timestamp: new Date().toLocaleString(), isSuccess: true };
await waitForItemRefresh(() => testProvider.deleteItem(testItem));
children = testProvider.getChildren();
children = await testProvider.getChildren();
should(children).length(1, 'Should still have 1 child after deleting item');
});
@@ -163,31 +162,31 @@ describe('QueryHistoryProvider', () => {
await fireQueryStartAndStopAndWaitForRefresh(testUri);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
await fireQueryStartAndStopAndWaitForRefresh(testUri);
const firstChildren = testProvider.getChildren();
const firstChildren = await testProvider.getChildren();
should(firstChildren).length(3, 'Should have 3 children initially');
let itemToDelete: QueryHistoryItem = firstChildren[1];
await waitForItemRefresh(() => testProvider.deleteItem(itemToDelete));
const secondChildren = testProvider.getChildren();
const secondChildren = await testProvider.getChildren();
should(secondChildren).length(2, 'Should still have 2 child after deleting item');
should(secondChildren[0]).be.equal(firstChildren[0], 'First item should still exist after deleting first item');
should(secondChildren[1]).be.equal(firstChildren[2], 'Second item should still exist after deleting first item');
itemToDelete = secondChildren[0];
await waitForItemRefresh(() => testProvider.deleteItem(itemToDelete));
const thirdChildren = testProvider.getChildren();
const thirdChildren = await testProvider.getChildren();
should(thirdChildren).length(1, 'Should still have 1 child after deleting item');
should(thirdChildren[0]).be.equal(secondChildren[1], 'Second item should still exist after deleting second item');
itemToDelete = thirdChildren[0];
await waitForItemRefresh(() => testProvider.deleteItem(itemToDelete));
const fourthChildren = testProvider.getChildren();
const fourthChildren = await testProvider.getChildren();
should(fourthChildren).length(0, 'Should have no children after deleting all items');
});
it('pausing capture causes children not to be added', async function () {
await fireQueryStartAndStopAndWaitForRefresh(testUri);
const children = testProvider.getChildren();
const children = await testProvider.getChildren();
should(children).length(1, 'Should have one child after adding initial item');
await testProvider.setCaptureEnabled(false);