Make connection for new Notebook (#4770)

* Make connection for new Notebook

* Resolve merge issue

* User connectionUri to track the connects need to be disconnected

* Removed debugging log
This commit is contained in:
Yurong He
2019-04-02 15:52:01 -07:00
committed by GitHub
parent 22c62fb524
commit 219dfe66d0
2 changed files with 27 additions and 13 deletions

View File

@@ -72,9 +72,9 @@ export class NotebookModel extends Disposable implements INotebookModel {
private _onValidConnectionSelected = new Emitter<boolean>(); private _onValidConnectionSelected = new Emitter<boolean>();
private _oldKernel: nb.IKernel; private _oldKernel: nb.IKernel;
private _clientSessionListeners: IDisposable[] = []; private _clientSessionListeners: IDisposable[] = [];
private _connectionsToDispose: ConnectionProfile[] = []; private _connectionUrisToDispose: string[] = [];
constructor(private _notebookOptions: INotebookModelOptions, startSessionImmediately?: boolean, private connectionProfile?: IConnectionProfile) { constructor(private _notebookOptions: INotebookModelOptions, startSessionImmediately?: boolean, public connectionProfile?: IConnectionProfile) {
super(); super();
if (!_notebookOptions || !_notebookOptions.notebookUri || !_notebookOptions.notebookManagers) { if (!_notebookOptions || !_notebookOptions.notebookUri || !_notebookOptions.notebookManagers) {
throw new Error('path or notebook service not defined'); throw new Error('path or notebook service not defined');
@@ -490,7 +490,7 @@ export class NotebookModel extends Disposable implements INotebookModel {
} }
private isValidConnection(profile: IConnectionProfile | connection.Connection) { private isValidConnection(profile: IConnectionProfile | connection.Connection) {
let standardKernels = this._notebookOptions.standardKernels.find(kernel => this._savedKernelInfo && kernel.displayName === this._savedKernelInfo.display_name); let standardKernels = this._notebookOptions.standardKernels.find(kernel => this._defaultKernel && kernel.displayName === this._defaultKernel.display_name);
let connectionProviderIds = standardKernels ? standardKernels.connectionProviderIds : undefined; let connectionProviderIds = standardKernels ? standardKernels.connectionProviderIds : undefined;
return profile && connectionProviderIds && connectionProviderIds.find(provider => provider === profile.providerName) !== undefined; return profile && connectionProviderIds && connectionProviderIds.find(provider => provider === profile.providerName) !== undefined;
} }
@@ -652,7 +652,6 @@ export class NotebookModel extends Disposable implements INotebookModel {
if (newConnection) { if (newConnection) {
this._activeConnection = newConnection; this._activeConnection = newConnection;
this.refreshConnections(newConnection); this.refreshConnections(newConnection);
console.log(this._activeConnection);
this._activeClientSession.updateConnection(newConnection.toIConnectionProfile()).then( this._activeClientSession.updateConnection(newConnection.toIConnectionProfile()).then(
result => { result => {
//Remove 'Select connection' from 'Attach to' drop-down since its a valid connection //Remove 'Select connection' from 'Attach to' drop-down since its a valid connection
@@ -746,8 +745,8 @@ export class NotebookModel extends Disposable implements INotebookModel {
return newKernelDisplayName; return newKernelDisplayName;
} }
public addAttachToConnectionsToBeDisposed(conn: ConnectionProfile) { public addAttachToConnectionsToBeDisposed(connUri: string) {
this._connectionsToDispose.push(conn); this._connectionUrisToDispose.push(connUri);
} }
private setErrorState(errMsg: string): void { private setErrorState(errMsg: string): void {
@@ -896,16 +895,17 @@ export class NotebookModel extends Disposable implements INotebookModel {
// let connectionUri = Utils.generateUri(connection, 'notebook'); // let connectionUri = Utils.generateUri(connection, 'notebook');
private async disconnectNotebookConnection(conn: ConnectionProfile): Promise<void> { private async disconnectNotebookConnection(conn: ConnectionProfile): Promise<void> {
if (this.notebookOptions.connectionService.getConnectionUri(conn).includes(uriPrefixes.notebook)) { if (this.notebookOptions.connectionService.getConnectionUri(conn).includes(uriPrefixes.notebook)) {
await this.notebookOptions.connectionService.disconnect(conn).catch(e => console.log(e)); let uri = this._notebookOptions.connectionService.getConnectionUri(conn);
await this.notebookOptions.connectionService.disconnect(uri).catch(e => console.log(e));
} }
} }
// Disconnect any connections that were added through the "Add new connection" functionality in the Attach To dropdown // Disconnect any connections that were added through the "Add new connection" functionality in the Attach To dropdown
private async disconnectAttachToConnections(): Promise<void> { private async disconnectAttachToConnections(): Promise<void> {
this._connectionsToDispose.forEach(async conn => { notebookUtils.asyncForEach(this._connectionUrisToDispose, async conn => {
await this.notebookOptions.connectionService.disconnect(conn).catch(e => console.log(e)); await this.notebookOptions.connectionService.disconnect(conn).catch(e => console.log(e));
}); });
this._connectionsToDispose = []; this._connectionUrisToDispose = [];
} }
/** /**

View File

@@ -21,7 +21,7 @@ import { ConnectionProfile } from 'sql/platform/connection/common/connectionProf
import { noKernel } from 'sql/workbench/services/notebook/common/sessionManager'; import { noKernel } from 'sql/workbench/services/notebook/common/sessionManager';
import { IConnectionDialogService } from 'sql/workbench/services/connection/common/connectionDialogService'; import { IConnectionDialogService } from 'sql/workbench/services/connection/common/connectionDialogService';
import { NotebookModel } from 'sql/parts/notebook/models/notebookModel'; import { NotebookModel } from 'sql/parts/notebook/models/notebookModel';
import { CellModel } from 'sql/parts/notebook/models/cell'; import { generateUri } from 'sql/platform/connection/common/utils';
const msgLoading = localize('loading', "Loading kernels..."); const msgLoading = localize('loading', "Loading kernels...");
const msgChanging = localize('changing', "Changing kernel..."); const msgChanging = localize('changing', "Changing kernel...");
@@ -320,6 +320,19 @@ export class AttachToDropdown extends SelectBox {
} }
private updateAttachToDropdown(model: INotebookModel): void { private updateAttachToDropdown(model: INotebookModel): void {
if (this.model.connectionProfile && this.model.connectionProfile.serverName) {
let connectionUri = generateUri(this.model.connectionProfile, 'notebook');
this.model.notebookOptions.connectionService.connect(this.model.connectionProfile, connectionUri).then(result => {
if (result.connected) {
let connectionProfile = new ConnectionProfile(this._capabilitiesService, result.connectionProfile);
this.model.addAttachToConnectionsToBeDisposed(connectionUri);
this.doChangeContext(connectionProfile);
} else {
this.openConnectionDialog(true);
}
}).catch(err =>
console.log(err));
}
model.onValidConnectionSelected(validConnection => { model.onValidConnectionSelected(validConnection => {
this.handleContextsChanged(!validConnection); this.handleContextsChanged(!validConnection);
}); });
@@ -427,15 +440,16 @@ export class AttachToDropdown extends SelectBox {
* Bind the server value to 'Attach To' drop down * Bind the server value to 'Attach To' drop down
* Connected server is displayed at the top of drop down * Connected server is displayed at the top of drop down
**/ **/
public async openConnectionDialog(): Promise<void> { public async openConnectionDialog(useProfile: boolean = false): Promise<void> {
try { try {
await this._connectionDialogService.openDialogAndWait(this._connectionManagementService, { connectionType: 1, providers: this.model.getApplicableConnectionProviderIds(this.model.clientSession.kernel.name) }).then(connection => { await this._connectionDialogService.openDialogAndWait(this._connectionManagementService, { connectionType: 1, providers: this.model.getApplicableConnectionProviderIds(this.model.clientSession.kernel.name) }, useProfile ? this.model.connectionProfile : undefined).then(connection => {
let attachToConnections = this.values; let attachToConnections = this.values;
if (!connection) { if (!connection) {
this.loadAttachToDropdown(this.model, this.getKernelDisplayName()); this.loadAttachToDropdown(this.model, this.getKernelDisplayName());
this.doChangeContext(undefined, true); this.doChangeContext(undefined, true);
return; return;
} }
let connectionUri = this._connectionManagementService.getConnectionUri(connection);
let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection); let connectionProfile = new ConnectionProfile(this._capabilitiesService, connection);
let connectedServer = connectionProfile.title? connectionProfile.title : connectionProfile.serverName; let connectedServer = connectionProfile.title? connectionProfile.title : connectionProfile.serverName;
//Check to see if the same server is already there in dropdown. We only have server names in dropdown //Check to see if the same server is already there in dropdown. We only have server names in dropdown
@@ -458,7 +472,7 @@ export class AttachToDropdown extends SelectBox {
} }
this.select(index); this.select(index);
this.model.addAttachToConnectionsToBeDisposed(connectionProfile); this.model.addAttachToConnectionsToBeDisposed(connectionUri);
// Call doChangeContext to set the newly chosen connection in the model // Call doChangeContext to set the newly chosen connection in the model
this.doChangeContext(connectionProfile); this.doChangeContext(connectionProfile);
}); });