mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-23 01:25:38 -05:00
Add datavirtualization extension (#21594)
* initial * cleanup * Add typings ref * fix compile * remove unused * add missing * another unused * Use newer vscodetestcover * newer dataprotocol * format * cleanup ignores * fix out path * fix entry point * more cleanup * Move into src folder * Handle service client log messages * remove unused
This commit is contained in:
332
extensions/datavirtualization/src/services/contracts.ts
Normal file
332
extensions/datavirtualization/src/services/contracts.ts
Normal file
@@ -0,0 +1,332 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { ClientCapabilities as VSClientCapabilities, RequestType, NotificationType } from 'vscode-languageclient';
|
||||
import * as types from 'dataprotocol-client/lib/types';
|
||||
import * as azdata from 'azdata';
|
||||
|
||||
/**
|
||||
* @interface IMessage
|
||||
*/
|
||||
export interface IMessage {
|
||||
jsonrpc: string;
|
||||
}
|
||||
|
||||
// ------------------------------- < Telemetry Sent Event > ------------------------------------
|
||||
|
||||
/**
|
||||
* Event sent when the language service send a telemetry event
|
||||
*/
|
||||
export namespace TelemetryNotification {
|
||||
export const type = new NotificationType<TelemetryParams, void>('telemetry/sqlevent');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update event parameters
|
||||
*/
|
||||
export class TelemetryParams {
|
||||
public params: {
|
||||
eventName: string;
|
||||
properties: ITelemetryEventProperties;
|
||||
measures: ITelemetryEventMeasures;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ITelemetryEventProperties {
|
||||
[key: string]: string;
|
||||
}
|
||||
|
||||
export interface ITelemetryEventMeasures {
|
||||
[key: string]: number;
|
||||
}
|
||||
|
||||
|
||||
// ------------------------------- </ Telemetry Sent Event > ----------------------------------
|
||||
|
||||
/*
|
||||
* DataSourceWizardCreateSessionRequest
|
||||
*/
|
||||
export namespace DataSourceWizardCreateSessionRequest {
|
||||
export const type = new RequestType<azdata.connection.ConnectionProfile, DataSourceWizardConfigInfoResponse, void, void>('datasourcewizard/createsession');
|
||||
}
|
||||
|
||||
export interface DataSourceWizardConfigInfoResponse {
|
||||
sessionId: string;
|
||||
supportedSourceTypes: DataSourceType[];
|
||||
databaseList: DatabaseOverview[];
|
||||
serverMajorVersion: number;
|
||||
productLevel: string;
|
||||
}
|
||||
|
||||
export interface DatabaseOverview {
|
||||
name: string;
|
||||
hasMasterKey: boolean;
|
||||
}
|
||||
|
||||
// Defines the important information about a type of data source - its name, configuration properties, etc.
|
||||
export interface DataSourceType {
|
||||
typeName: string;
|
||||
authenticationTypes: string[];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* DisposeWizardSessionRequest
|
||||
*/
|
||||
export namespace DisposeWizardSessionRequest {
|
||||
export const type = new RequestType<string, boolean, void, void>('datasourcewizard/disposewizardsession');
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ValidateVirtualizeDataInputRequest
|
||||
*/
|
||||
export namespace ValidateVirtualizeDataInputRequest {
|
||||
export const type = new RequestType<VirtualizeDataInput, ValidateVirtualizeDataInputResponse, void, void>('datasourcewizard/validatevirtualizedatainput');
|
||||
}
|
||||
|
||||
export interface ValidateVirtualizeDataInputResponse {
|
||||
isValid: boolean;
|
||||
errorMessages: string[];
|
||||
}
|
||||
|
||||
export interface VirtualizeDataInput {
|
||||
sessionId: string;
|
||||
destDatabaseName: string;
|
||||
sourceServerType: string;
|
||||
destDbMasterKeyPwd: string;
|
||||
existingDataSourceName: string;
|
||||
newDataSourceName: string;
|
||||
sourceServerName: string;
|
||||
sourceDatabaseName: string;
|
||||
sourceAuthenticationType: string;
|
||||
existingCredentialName: string;
|
||||
newCredentialName: string;
|
||||
sourceUsername: string;
|
||||
sourcePassword: string;
|
||||
externalTableInfoList: ExternalTableInfo[];
|
||||
newSchemas: string[];
|
||||
}
|
||||
|
||||
export interface FileFormat {
|
||||
formatName: string;
|
||||
formatType: string;
|
||||
fieldTerminator: string; // string token that separates columns on each line of the file
|
||||
stringDelimiter: string; // string token that marks beginning/end of strings in the file
|
||||
firstRow: number;
|
||||
}
|
||||
|
||||
export interface ExternalTableInfo {
|
||||
externalTableName: string[];
|
||||
columnDefinitionList: ColumnDefinition[];
|
||||
sourceTableLocation: string[];
|
||||
fileFormat?: FileFormat;
|
||||
}
|
||||
|
||||
export interface ColumnDefinition {
|
||||
columnName: string;
|
||||
dataType: string;
|
||||
collationName: string;
|
||||
isNullable: boolean;
|
||||
isSupported?: boolean;
|
||||
}
|
||||
|
||||
// TODO: All response objects for data-source-browsing request have this format, and can be formed with this generic class.
|
||||
// Replace response objects with this class.
|
||||
export interface ExecutionResult<T> {
|
||||
isSuccess: boolean;
|
||||
returnValue: T;
|
||||
errorMessages: string[];
|
||||
}
|
||||
|
||||
// TODO: All parameter objects for querying list of database, list of tables, and list of column definitions have this format,
|
||||
// and can be formed with this generic class. Replace parameter objects with this class for those query requests.
|
||||
export interface DataSourceBrowsingParams<T> {
|
||||
virtualizeDataInput: VirtualizeDataInput;
|
||||
querySubject: T;
|
||||
}
|
||||
|
||||
export namespace GetSourceViewListRequest {
|
||||
export const type = new RequestType<DataSourceBrowsingParams<string>, ExecutionResult<SchemaViews[]>, void, void>('datasourcewizard/getsourceviewlist');
|
||||
}
|
||||
|
||||
/*
|
||||
* GetDatabaseInfoRequest
|
||||
*/
|
||||
export namespace GetDatabaseInfoRequest {
|
||||
export const type = new RequestType<GetDatabaseInfoRequestParams, GetDatabaseInfoResponse, void, void>('datasourcewizard/getdatabaseinfo');
|
||||
}
|
||||
|
||||
export interface GetDatabaseInfoResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
databaseInfo: DatabaseInfo;
|
||||
}
|
||||
|
||||
export interface DatabaseInfo {
|
||||
hasMasterKey: boolean;
|
||||
defaultSchema: string;
|
||||
schemaList: string[];
|
||||
existingCredentials: CredentialInfo[];
|
||||
externalDataSources: DataSourceInstance[];
|
||||
externalTables: TableInfo[];
|
||||
externalFileFormats: string[];
|
||||
}
|
||||
|
||||
export interface CredentialInfo {
|
||||
credentialName: string;
|
||||
username: string;
|
||||
}
|
||||
|
||||
export interface TableInfo {
|
||||
schemaName: string;
|
||||
tableName: string;
|
||||
}
|
||||
|
||||
export interface GetDatabaseInfoRequestParams {
|
||||
sessionId: string;
|
||||
databaseName: string;
|
||||
}
|
||||
|
||||
|
||||
// Defines the important information about an external data source that has already been created.
|
||||
export interface DataSourceInstance {
|
||||
name: string;
|
||||
location: string;
|
||||
authenticationType: string;
|
||||
username?: string;
|
||||
credentialName?: string;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* ProcessVirtualizeDataInputRequest
|
||||
*/
|
||||
export namespace ProcessVirtualizeDataInputRequest {
|
||||
export const type = new RequestType<VirtualizeDataInput, ProcessVirtualizeDataInputResponse, void, void>('datasourcewizard/processvirtualizedatainput');
|
||||
}
|
||||
|
||||
export interface ProcessVirtualizeDataInputResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
}
|
||||
|
||||
export namespace GenerateScriptRequest {
|
||||
export const type = new RequestType<VirtualizeDataInput, GenerateScriptResponse, void, void>('datasourcewizard/generatescript');
|
||||
}
|
||||
|
||||
export interface GenerateScriptResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
script: string;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* GetSourceDatabasesRequest
|
||||
*/
|
||||
export namespace GetSourceDatabasesRequest {
|
||||
export const type = new RequestType<VirtualizeDataInput, GetSourceDatabasesResponse, void, void>('datasourcewizard/getsourcedatabaselist');
|
||||
}
|
||||
|
||||
export interface GetSourceDatabasesResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
databaseNames: string[];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* GetSourceTablesRequest
|
||||
*/
|
||||
export namespace GetSourceTablesRequest {
|
||||
export const type = new RequestType<GetSourceTablesRequestParams, GetSourceTablesResponse, void, void>('datasourcewizard/getsourcetablelist');
|
||||
}
|
||||
|
||||
export interface GetSourceTablesRequestParams {
|
||||
sessionId: string;
|
||||
virtualizeDataInput: VirtualizeDataInput;
|
||||
sourceDatabaseName: string;
|
||||
}
|
||||
|
||||
export interface GetSourceTablesResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
schemaTablesList: SchemaTables[];
|
||||
}
|
||||
|
||||
export interface SchemaTables {
|
||||
schemaName: string;
|
||||
tableNames: string[];
|
||||
}
|
||||
|
||||
export interface SchemaViews {
|
||||
schemaName: string;
|
||||
viewNames: string[];
|
||||
}
|
||||
|
||||
/*
|
||||
* GetSourceColumnDefinitionsRequest
|
||||
*/
|
||||
export namespace GetSourceColumnDefinitionsRequest {
|
||||
export const type = new RequestType<GetSourceColumnDefinitionsRequestParams, GetSourceColumnDefinitionsResponse, void, void>('datasourcewizard/getsourcecolumndefinitionlist');
|
||||
}
|
||||
|
||||
export interface GetSourceColumnDefinitionsRequestParams {
|
||||
sessionId: string;
|
||||
virtualizeDataInput: VirtualizeDataInput;
|
||||
location: string[];
|
||||
}
|
||||
|
||||
export interface GetSourceColumnDefinitionsResponse {
|
||||
isSuccess: boolean;
|
||||
errorMessages: string[];
|
||||
columnDefinitions: ColumnDefinition[];
|
||||
}
|
||||
|
||||
/*
|
||||
* Prose
|
||||
*/
|
||||
export interface ColumnInfo {
|
||||
name: string;
|
||||
sqlType: string;
|
||||
isNullable: boolean;
|
||||
}
|
||||
|
||||
export interface ProseDiscoveryParams {
|
||||
filePath: string;
|
||||
tableName: string;
|
||||
schemaName?: string;
|
||||
fileType?: string;
|
||||
fileContents?: string;
|
||||
}
|
||||
|
||||
export interface ProseDiscoveryResponse {
|
||||
dataPreview: string[][];
|
||||
columnInfo: ColumnInfo[];
|
||||
columnDelimiter: string;
|
||||
firstRow: number;
|
||||
quoteCharacter: string;
|
||||
}
|
||||
|
||||
export namespace ProseDiscoveryRequest {
|
||||
export const type = new RequestType<ProseDiscoveryParams, ProseDiscoveryResponse, void, void>('flatfile/proseDiscovery');
|
||||
}
|
||||
|
||||
// ------------------------------- < Data Source Wizard API definition > ------------------------------------
|
||||
export interface DataSourceWizardService {
|
||||
providerId?: string;
|
||||
createDataSourceWizardSession(requestParams: azdata.connection.ConnectionProfile): Thenable<DataSourceWizardConfigInfoResponse>;
|
||||
disposeWizardSession(sessionId: string): Thenable<boolean>;
|
||||
validateVirtualizeDataInput(requestParams: VirtualizeDataInput): Thenable<ValidateVirtualizeDataInputResponse>;
|
||||
getDatabaseInfo(requestParams: GetDatabaseInfoRequestParams): Thenable<GetDatabaseInfoResponse>;
|
||||
processVirtualizeDataInput(requestParams: VirtualizeDataInput): Thenable<ProcessVirtualizeDataInputResponse>;
|
||||
generateScript(requestParams: VirtualizeDataInput): Thenable<GenerateScriptResponse>;
|
||||
getSourceDatabases(requestParams: VirtualizeDataInput): Thenable<GetSourceDatabasesResponse>;
|
||||
getSourceTables(requestParams: GetSourceTablesRequestParams): Thenable<GetSourceTablesResponse>;
|
||||
getSourceViewList(requestParams: DataSourceBrowsingParams<string>): Thenable<ExecutionResult<SchemaViews[]>>;
|
||||
getSourceColumnDefinitions(requestParams: GetSourceColumnDefinitionsRequestParams): Thenable<GetSourceColumnDefinitionsResponse>;
|
||||
sendProseDiscoveryRequest(requestParams: ProseDiscoveryParams): Thenable<ProseDiscoveryResponse>;
|
||||
}
|
||||
189
extensions/datavirtualization/src/services/features.ts
Normal file
189
extensions/datavirtualization/src/services/features.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { SqlOpsDataClient, SqlOpsFeature } from 'dataprotocol-client';
|
||||
import { ClientCapabilities, StaticFeature, RPCMessageType, ServerCapabilities } from 'vscode-languageclient';
|
||||
import * as UUID from 'vscode-languageclient/lib/utils/uuid';
|
||||
import { Disposable } from 'vscode';
|
||||
import * as azdata from 'azdata';
|
||||
|
||||
import { Telemetry } from './telemetry';
|
||||
import * as serviceUtils from './serviceUtils';
|
||||
import {
|
||||
TelemetryNotification,
|
||||
DataSourceWizardCreateSessionRequest, DataSourceWizardConfigInfoResponse,
|
||||
DataSourceWizardService, DisposeWizardSessionRequest, VirtualizeDataInput,
|
||||
ValidateVirtualizeDataInputResponse, GetDatabaseInfoRequestParams, GetDatabaseInfoResponse,
|
||||
ProcessVirtualizeDataInputResponse, GenerateScriptResponse, ValidateVirtualizeDataInputRequest,
|
||||
GetDatabaseInfoRequest, ProcessVirtualizeDataInputRequest, GenerateScriptRequest, GetSourceDatabasesResponse,
|
||||
GetSourceDatabasesRequest, GetSourceTablesResponse, GetSourceTablesRequestParams, GetSourceTablesRequest,
|
||||
GetSourceColumnDefinitionsRequestParams, GetSourceColumnDefinitionsResponse, GetSourceColumnDefinitionsRequest,
|
||||
ProseDiscoveryParams, ProseDiscoveryResponse, ProseDiscoveryRequest, DataSourceBrowsingParams, ExecutionResult, GetSourceViewListRequest, SchemaViews
|
||||
} from './contracts';
|
||||
import { managerInstance, ApiType } from './serviceApiManager';
|
||||
export class TelemetryFeature implements StaticFeature {
|
||||
|
||||
constructor(private _client: SqlOpsDataClient) { }
|
||||
|
||||
fillClientCapabilities(capabilities: ClientCapabilities): void {
|
||||
serviceUtils.ensure(capabilities, 'telemetry')!.telemetry = true;
|
||||
}
|
||||
|
||||
initialize(): void {
|
||||
this._client.onNotification(TelemetryNotification.type, e => {
|
||||
Telemetry.sendTelemetryEvent(e.params.eventName, e.params.properties, e.params.measures);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class DataSourceWizardFeature extends SqlOpsFeature<undefined> {
|
||||
private static readonly messagesTypes: RPCMessageType[] = [
|
||||
DataSourceWizardCreateSessionRequest.type
|
||||
];
|
||||
|
||||
constructor(client: SqlOpsDataClient) {
|
||||
super(client, DataSourceWizardFeature.messagesTypes);
|
||||
}
|
||||
|
||||
public fillClientCapabilities(capabilities: ClientCapabilities): void {
|
||||
// ensure(ensure(capabilities, 'connection')!, 'objectExplorer')!.dynamicRegistration = true;
|
||||
}
|
||||
|
||||
public initialize(capabilities: ServerCapabilities): void {
|
||||
this.register(this.messages, {
|
||||
id: UUID.generateUuid(),
|
||||
registerOptions: undefined
|
||||
});
|
||||
}
|
||||
|
||||
protected registerProvider(options: undefined): Disposable {
|
||||
const client = this._client;
|
||||
let createDataSourceWizardSession = (requestParams: azdata.connection.ConnectionProfile): Thenable<DataSourceWizardConfigInfoResponse> => {
|
||||
return client.sendRequest(DataSourceWizardCreateSessionRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(DataSourceWizardCreateSessionRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let disposeWizardSession = (sessionId: string): Thenable<boolean> => {
|
||||
return client.sendRequest(DisposeWizardSessionRequest.type, sessionId).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(DisposeWizardSessionRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let validateVirtualizeDataInput = (requestParams: VirtualizeDataInput): Thenable<ValidateVirtualizeDataInputResponse> => {
|
||||
return client.sendRequest(ValidateVirtualizeDataInputRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(ValidateVirtualizeDataInputRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let getDatabaseInfo = (requestParams: GetDatabaseInfoRequestParams): Thenable<GetDatabaseInfoResponse> => {
|
||||
return client.sendRequest(GetDatabaseInfoRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GetDatabaseInfoRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let processVirtualizeDataInput = (requestParams: VirtualizeDataInput): Thenable<ProcessVirtualizeDataInputResponse> => {
|
||||
return client.sendRequest(ProcessVirtualizeDataInputRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(ProcessVirtualizeDataInputRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let generateScript = (requestParams: VirtualizeDataInput): Thenable<GenerateScriptResponse> => {
|
||||
return client.sendRequest(GenerateScriptRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GenerateScriptRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let getSourceDatabases = (requestParams: VirtualizeDataInput): Thenable<GetSourceDatabasesResponse> => {
|
||||
return client.sendRequest(GetSourceDatabasesRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GetSourceDatabasesRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let getSourceTables = (requestParams: GetSourceTablesRequestParams): Thenable<GetSourceTablesResponse> => {
|
||||
return client.sendRequest(GetSourceTablesRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GetSourceTablesRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let getSourceViewList = (requestParams: DataSourceBrowsingParams<string>): Thenable<ExecutionResult<SchemaViews[]>> => {
|
||||
return client.sendRequest(GetSourceViewListRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GetSourceViewListRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let getSourceColumnDefinitions = (requestParams: GetSourceColumnDefinitionsRequestParams): Thenable<GetSourceColumnDefinitionsResponse> => {
|
||||
return client.sendRequest(GetSourceColumnDefinitionsRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(GetSourceColumnDefinitionsRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
let sendProseDiscoveryRequest = (requestParams: ProseDiscoveryParams): Thenable<ProseDiscoveryResponse> => {
|
||||
return client.sendRequest(ProseDiscoveryRequest.type, requestParams).then(
|
||||
r => r,
|
||||
e => {
|
||||
client.logFailedRequest(ProseDiscoveryRequest.type, e);
|
||||
return Promise.reject(e);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
return managerInstance.registerApi<DataSourceWizardService>(ApiType.DataSourceWizard, {
|
||||
providerId: client.providerId,
|
||||
createDataSourceWizardSession,
|
||||
disposeWizardSession,
|
||||
validateVirtualizeDataInput,
|
||||
getDatabaseInfo,
|
||||
processVirtualizeDataInput,
|
||||
generateScript,
|
||||
getSourceDatabases,
|
||||
getSourceTables,
|
||||
getSourceViewList,
|
||||
getSourceColumnDefinitions,
|
||||
sendProseDiscoveryRequest
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdata from 'azdata';
|
||||
import * as vscode from 'vscode';
|
||||
import { ApiWrapper } from '../apiWrapper';
|
||||
|
||||
export enum ApiType {
|
||||
ExplorerProvider = 'ExplorerProvider',
|
||||
DataSourceWizard = 'DataSourceWizard'
|
||||
}
|
||||
|
||||
export interface IServiceApi {
|
||||
onRegisteredApi<T>(type: ApiType): vscode.Event<T>;
|
||||
registerApi<T>(type: ApiType, feature: T): vscode.Disposable;
|
||||
}
|
||||
|
||||
export interface IModelViewDefinition {
|
||||
id: string;
|
||||
modelView: azdata.ModelView;
|
||||
}
|
||||
|
||||
class ServiceApiManager implements IServiceApi {
|
||||
private modelViewRegistrations: { [id: string]: boolean } = {};
|
||||
private featureEventChannels: { [type: string]: vscode.EventEmitter<any> } = {};
|
||||
private _onRegisteredModelView = new vscode.EventEmitter<IModelViewDefinition>();
|
||||
|
||||
public onRegisteredApi<T>(type: ApiType): vscode.Event<T> {
|
||||
let featureEmitter = this.featureEventChannels[type];
|
||||
if (!featureEmitter) {
|
||||
featureEmitter = new vscode.EventEmitter<T>();
|
||||
this.featureEventChannels[type] = featureEmitter;
|
||||
}
|
||||
return featureEmitter.event;
|
||||
}
|
||||
|
||||
public registerApi<T>(type: ApiType, feature: T): vscode.Disposable {
|
||||
let featureEmitter = this.featureEventChannels[type];
|
||||
if (featureEmitter) {
|
||||
featureEmitter.fire(feature);
|
||||
}
|
||||
// TODO handle unregistering API on close
|
||||
return {
|
||||
dispose: () => undefined
|
||||
};
|
||||
}
|
||||
|
||||
public get onRegisteredModelView(): vscode.Event<IModelViewDefinition> {
|
||||
return this._onRegisteredModelView.event;
|
||||
}
|
||||
|
||||
public registerModelView(id: string, modelView: azdata.ModelView): void {
|
||||
this._onRegisteredModelView.fire({
|
||||
id: id,
|
||||
modelView: modelView
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a one-time registration of a model view provider, where this will be
|
||||
* hooked to an event handler instead of having a predefined method that uses the model view
|
||||
*/
|
||||
public ensureModelViewRegistered(id: string, apiWrapper: ApiWrapper): any {
|
||||
if (!this.modelViewRegistrations[id]) {
|
||||
apiWrapper.registerModelViewProvider(id, (modelView) => {
|
||||
this.registerModelView(id, modelView);
|
||||
});
|
||||
this.modelViewRegistrations[id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export let managerInstance = new ServiceApiManager();
|
||||
171
extensions/datavirtualization/src/services/serviceClient.ts
Normal file
171
extensions/datavirtualization/src/services/serviceClient.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { SqlOpsDataClient, ClientOptions } from 'dataprotocol-client';
|
||||
import { IConfig, ServerProvider, Events, LogLevel } from '@microsoft/ads-service-downloader';
|
||||
import { ServerOptions, TransportKind } from 'vscode-languageclient';
|
||||
import * as vscode from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
import * as path from 'path';
|
||||
import { EventAndListener } from 'eventemitter2';
|
||||
|
||||
import { Telemetry, LanguageClientErrorHandler } from './telemetry';
|
||||
import { ApiWrapper } from '../apiWrapper';
|
||||
import * as Constants from '../constants';
|
||||
import { TelemetryFeature, DataSourceWizardFeature } from './features';
|
||||
import { promises as fs } from 'fs';
|
||||
|
||||
export class ServiceClient {
|
||||
private statusView: vscode.StatusBarItem;
|
||||
|
||||
constructor(private apiWrapper: ApiWrapper, private outputChannel: vscode.OutputChannel) {
|
||||
this.statusView = this.apiWrapper.createStatusBarItem(vscode.StatusBarAlignment.Left);
|
||||
}
|
||||
|
||||
public async startService(context: vscode.ExtensionContext): Promise<void> {
|
||||
const rawConfig = await fs.readFile(path.join(context.extensionPath, 'config.json'));
|
||||
const config: IConfig = JSON.parse(rawConfig.toString());
|
||||
config.installDirectory = path.join(context.extensionPath, config.installDirectory);
|
||||
config.proxy = this.apiWrapper.getConfiguration('http').get('proxy');
|
||||
config.strictSSL = this.apiWrapper.getConfiguration('http').get('proxyStrictSSL') || true;
|
||||
|
||||
const serverdownloader = new ServerProvider(config);
|
||||
serverdownloader.eventEmitter.onAny(this.generateHandleServerProviderEvent());
|
||||
|
||||
let clientOptions: ClientOptions = this.createClientOptions();
|
||||
|
||||
const installationStart = Date.now();
|
||||
let client: SqlOpsDataClient;
|
||||
return new Promise((resolve, reject) => {
|
||||
serverdownloader.getOrDownloadServer().then(e => {
|
||||
const installationComplete = Date.now();
|
||||
let serverOptions = this.generateServerOptions(e, context);
|
||||
client = new SqlOpsDataClient(Constants.serviceName, serverOptions, clientOptions);
|
||||
const processStart = Date.now();
|
||||
client.onReady().then(() => {
|
||||
const processEnd = Date.now();
|
||||
this.statusView.text = localize('serviceStarted', 'Service Started');
|
||||
setTimeout(() => {
|
||||
this.statusView.hide();
|
||||
}, 1500);
|
||||
Telemetry.sendTelemetryEvent('startup/LanguageClientStarted', {
|
||||
installationTime: String(installationComplete - installationStart),
|
||||
processStartupTime: String(processEnd - processStart),
|
||||
totalTime: String(processEnd - installationStart),
|
||||
beginningTimestamp: String(installationStart)
|
||||
});
|
||||
});
|
||||
this.statusView.show();
|
||||
this.statusView.text = localize('serviceStarting', 'Starting service');
|
||||
let disposable = client.start();
|
||||
context.subscriptions.push(disposable);
|
||||
resolve();
|
||||
}, e => {
|
||||
Telemetry.sendTelemetryEvent('ServiceInitializingFailed');
|
||||
this.apiWrapper.showErrorMessage(localize('serviceStartFailed', 'Failed to start Scale Out Data service:{0}', e));
|
||||
// Just resolve to avoid unhandled promise. We show the error to the user.
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private createClientOptions(): ClientOptions {
|
||||
return {
|
||||
providerId: Constants.providerId,
|
||||
errorHandler: new LanguageClientErrorHandler(),
|
||||
synchronize: {
|
||||
configurationSection: [Constants.extensionConfigSectionName, Constants.sqlConfigSectionName]
|
||||
},
|
||||
features: [
|
||||
// we only want to add new features
|
||||
TelemetryFeature,
|
||||
DataSourceWizardFeature
|
||||
],
|
||||
outputChannel: new CustomOutputChannel()
|
||||
};
|
||||
}
|
||||
|
||||
private generateServerOptions(executablePath: string, context: vscode.ExtensionContext): ServerOptions {
|
||||
let launchArgs = [];
|
||||
launchArgs.push('--log-dir');
|
||||
let logFileLocation = context['logPath'];
|
||||
launchArgs.push(logFileLocation);
|
||||
let config = vscode.workspace.getConfiguration(Constants.extensionConfigSectionName);
|
||||
if (config) {
|
||||
let logDebugInfo = config[Constants.configLogDebugInfo];
|
||||
if (logDebugInfo) {
|
||||
launchArgs.push('--enable-logging');
|
||||
}
|
||||
}
|
||||
|
||||
return { command: executablePath, args: launchArgs, transport: TransportKind.stdio };
|
||||
}
|
||||
|
||||
private generateHandleServerProviderEvent(): EventAndListener {
|
||||
let dots = 0;
|
||||
return (e: string, ...args: any[]) => {
|
||||
switch (e) {
|
||||
case Events.INSTALL_START:
|
||||
this.outputChannel.show(true);
|
||||
this.statusView.show();
|
||||
this.outputChannel.appendLine(localize('installingServiceDetailed', "Installing {0} to {1}", Constants.serviceName, args[0]));
|
||||
this.statusView.text = localize('installingService', "Installing {0}", Constants.serviceName);
|
||||
break;
|
||||
case Events.INSTALL_END:
|
||||
this.outputChannel.appendLine(localize('serviceInstalled', "Installed {0}", Constants.serviceName));
|
||||
break;
|
||||
case Events.DOWNLOAD_START:
|
||||
this.outputChannel.appendLine(localize('downloadingService', "Downloading {0}", args[0]));
|
||||
this.outputChannel.append(localize('downloadingServiceSize', "({0} KB)", Math.ceil(args[1] / 1024).toLocaleString(vscode.env.language)));
|
||||
this.statusView.text = localize('downloadingServiceStatus', "Downloading {0}", Constants.serviceName);
|
||||
break;
|
||||
case Events.DOWNLOAD_PROGRESS:
|
||||
let newDots = Math.ceil(args[0] / 5);
|
||||
if (newDots > dots) {
|
||||
this.outputChannel.append('.'.repeat(newDots - dots));
|
||||
dots = newDots;
|
||||
}
|
||||
break;
|
||||
case Events.DOWNLOAD_END:
|
||||
this.outputChannel.appendLine(localize('downloadingServiceComplete', "Done downloading {0}", Constants.serviceName));
|
||||
break;
|
||||
case Events.LOG_EMITTED:
|
||||
if (args[0] >= LogLevel.Warning) {
|
||||
this.outputChannel.appendLine(args[1]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
console.error(`Unknown event from Server Provider ${e}`);
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class CustomOutputChannel implements vscode.OutputChannel {
|
||||
name: string;
|
||||
append(value: string): void {
|
||||
console.log(value);
|
||||
}
|
||||
appendLine(value: string): void {
|
||||
console.log(value);
|
||||
}
|
||||
clear(): void {
|
||||
}
|
||||
show(preserveFocus?: boolean): void;
|
||||
show(column?: vscode.ViewColumn, preserveFocus?: boolean): void;
|
||||
show(column?: any, preserveFocus?: any): void {
|
||||
}
|
||||
hide(): void {
|
||||
}
|
||||
dispose(): void {
|
||||
}
|
||||
replace(value: string): void {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
}
|
||||
|
||||
62
extensions/datavirtualization/src/services/serviceUtils.ts
Normal file
62
extensions/datavirtualization/src/services/serviceUtils.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as path from 'path';
|
||||
import * as crypto from 'crypto';
|
||||
import * as os from 'os';
|
||||
|
||||
export function ensure(target: object, key: string): any {
|
||||
if (target[key] === void 0) {
|
||||
target[key] = {} as any;
|
||||
}
|
||||
return target[key];
|
||||
}
|
||||
|
||||
export function generateUserId(): Promise<string> {
|
||||
return new Promise<string>(resolve => {
|
||||
try {
|
||||
let interfaces = os.networkInterfaces();
|
||||
let mac;
|
||||
for (let key of Object.keys(interfaces)) {
|
||||
let item = interfaces[key][0];
|
||||
if (!item.internal) {
|
||||
mac = item.mac;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mac) {
|
||||
resolve(crypto.createHash('sha256').update(mac + os.homedir(), 'utf8').digest('hex'));
|
||||
} else {
|
||||
resolve(generateGuid());
|
||||
}
|
||||
} catch (err) {
|
||||
resolve(generateGuid()); // fallback
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function generateGuid(): string {
|
||||
let hexValues: string[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
||||
// c.f. rfc4122 (UUID version 4 = xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx)
|
||||
let oct: string = '';
|
||||
let tmp: number;
|
||||
/* tslint:disable:no-bitwise */
|
||||
for (let a: number = 0; a < 4; a++) {
|
||||
tmp = (4294967296 * Math.random()) | 0;
|
||||
oct += hexValues[tmp & 0xF] +
|
||||
hexValues[tmp >> 4 & 0xF] +
|
||||
hexValues[tmp >> 8 & 0xF] +
|
||||
hexValues[tmp >> 12 & 0xF] +
|
||||
hexValues[tmp >> 16 & 0xF] +
|
||||
hexValues[tmp >> 20 & 0xF] +
|
||||
hexValues[tmp >> 24 & 0xF] +
|
||||
hexValues[tmp >> 28 & 0xF];
|
||||
}
|
||||
|
||||
// 'Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively'
|
||||
let clockSequenceHi: string = hexValues[8 + (Math.random() * 4) | 0];
|
||||
return oct.substr(0, 8) + '-' + oct.substr(9, 4) + '-4' + oct.substr(13, 3) + '-' + clockSequenceHi + oct.substr(16, 3) + '-' + oct.substr(19, 12);
|
||||
/* tslint:enable:no-bitwise */
|
||||
}
|
||||
216
extensions/datavirtualization/src/services/telemetry.ts
Normal file
216
extensions/datavirtualization/src/services/telemetry.ts
Normal file
@@ -0,0 +1,216 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { ErrorAction, CloseAction } from 'vscode-languageclient';
|
||||
import TelemetryReporter from '@microsoft/ads-extension-telemetry';
|
||||
import { PlatformInformation } from '@microsoft/ads-service-downloader/out/platform';
|
||||
import * as vscode from 'vscode';
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
import { ApiWrapper } from '../apiWrapper';
|
||||
import * as constants from '../constants';
|
||||
import * as serviceUtils from './serviceUtils';
|
||||
import { IMessage, ITelemetryEventProperties, ITelemetryEventMeasures } from './contracts';
|
||||
|
||||
|
||||
/**
|
||||
* Handle Language Service client errors
|
||||
* @class LanguageClientErrorHandler
|
||||
*/
|
||||
export class LanguageClientErrorHandler {
|
||||
|
||||
/**
|
||||
* Creates an instance of LanguageClientErrorHandler.
|
||||
* @memberOf LanguageClientErrorHandler
|
||||
*/
|
||||
constructor(private apiWrapper?: ApiWrapper) {
|
||||
if (!this.apiWrapper) {
|
||||
this.apiWrapper = new ApiWrapper();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an error message prompt with a link to known issues wiki page
|
||||
* @memberOf LanguageClientErrorHandler
|
||||
*/
|
||||
showOnErrorPrompt(): void {
|
||||
// TODO add telemetry
|
||||
// Telemetry.sendTelemetryEvent('SqlToolsServiceCrash');
|
||||
let crashButtonText = localize('serviceCrashButton', 'View Known Issues');
|
||||
this.apiWrapper.showErrorMessage(
|
||||
localize('serviceCrashMessage', 'service component could not start'),
|
||||
crashButtonText
|
||||
).then(action => {
|
||||
if (action && action === crashButtonText) {
|
||||
vscode.env.openExternal(vscode.Uri.parse(constants.serviceCrashLink));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for language service client error
|
||||
*
|
||||
* @param error
|
||||
* @param message
|
||||
* @param count
|
||||
* @returns
|
||||
*
|
||||
* @memberOf LanguageClientErrorHandler
|
||||
*/
|
||||
error(error: Error, message: IMessage, count: number): ErrorAction {
|
||||
this.showOnErrorPrompt();
|
||||
|
||||
// we don't retry running the service since crashes leave the extension
|
||||
// in a bad, unrecovered state
|
||||
return ErrorAction.Shutdown;
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback for language service client closed
|
||||
*
|
||||
* @returns
|
||||
*
|
||||
* @memberOf LanguageClientErrorHandler
|
||||
*/
|
||||
closed(): CloseAction {
|
||||
this.showOnErrorPrompt();
|
||||
|
||||
// we don't retry running the service since crashes leave the extension
|
||||
// in a bad, unrecovered state
|
||||
return CloseAction.DoNotRestart;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters error paths to only include source files. Exported to support testing
|
||||
*/
|
||||
export function FilterErrorPath(line: string): string {
|
||||
if (line) {
|
||||
let values: string[] = line.split('/out/');
|
||||
if (values.length <= 1) {
|
||||
// Didn't match expected format
|
||||
return line;
|
||||
} else {
|
||||
return values[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export class Telemetry {
|
||||
private static reporter: TelemetryReporter;
|
||||
private static userId: string;
|
||||
private static platformInformation: PlatformInformation;
|
||||
private static disabled: boolean;
|
||||
|
||||
// Get the unique ID for the current user of the extension
|
||||
public static getUserId(): Promise<string> {
|
||||
return new Promise<string>(resolve => {
|
||||
// Generate the user id if it has not been created already
|
||||
if (typeof this.userId === 'undefined') {
|
||||
let id = serviceUtils.generateUserId();
|
||||
id.then(newId => {
|
||||
this.userId = newId;
|
||||
resolve(this.userId);
|
||||
});
|
||||
} else {
|
||||
resolve(this.userId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static getPlatformInformation(): Promise<PlatformInformation> {
|
||||
if (this.platformInformation) {
|
||||
return Promise.resolve(this.platformInformation);
|
||||
} else {
|
||||
return new Promise<PlatformInformation>(resolve => {
|
||||
PlatformInformation.getCurrent().then(info => {
|
||||
this.platformInformation = info;
|
||||
resolve(this.platformInformation);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable telemetry reporting
|
||||
*/
|
||||
public static disable(): void {
|
||||
this.disabled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the telemetry reporter for use.
|
||||
*/
|
||||
public static initialize(): void {
|
||||
if (typeof this.reporter === 'undefined') {
|
||||
// Check if the user has opted out of telemetry
|
||||
if (!vscode.workspace.getConfiguration('telemetry').get<boolean>('enableTelemetry', true)) {
|
||||
this.disable();
|
||||
return;
|
||||
}
|
||||
let packageInfo = vscode.extensions.getExtension('Microsoft.datavirtualization').packageJSON;
|
||||
this.reporter = new TelemetryReporter(packageInfo.name, packageInfo.version, packageInfo.aiKey);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a telemetry event for an exception
|
||||
*/
|
||||
public static sendTelemetryEventForException(
|
||||
err: any, methodName: string, extensionConfigName: string): void {
|
||||
try {
|
||||
let stackArray: string[];
|
||||
let firstLine: string = '';
|
||||
if (err !== undefined && err.stack !== undefined) {
|
||||
stackArray = err.stack.split('\n');
|
||||
if (stackArray !== undefined && stackArray.length >= 2) {
|
||||
firstLine = stackArray[1]; // The fist line is the error message and we don't want to send that telemetry event
|
||||
firstLine = FilterErrorPath(firstLine);
|
||||
}
|
||||
}
|
||||
|
||||
// Only adding the method name and the fist line of the stack trace. We don't add the error message because it might have PII
|
||||
this.sendTelemetryEvent('Exception', { methodName: methodName, errorLine: firstLine });
|
||||
// Utils.logDebug('Unhandled Exception occurred. error: ' + err + ' method: ' + methodName, extensionConfigName);
|
||||
} catch (telemetryErr) {
|
||||
// If sending telemetry event fails ignore it so it won't break the extension
|
||||
// Utils.logDebug('Failed to send telemetry event. error: ' + telemetryErr, extensionConfigName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a telemetry event using application insights
|
||||
*/
|
||||
public static sendTelemetryEvent(
|
||||
eventName: string,
|
||||
properties?: ITelemetryEventProperties,
|
||||
measures?: ITelemetryEventMeasures): void {
|
||||
|
||||
if (typeof this.disabled === 'undefined') {
|
||||
this.disabled = false;
|
||||
}
|
||||
|
||||
if (this.disabled || typeof (this.reporter) === 'undefined') {
|
||||
// Don't do anything if telemetry is disabled
|
||||
return;
|
||||
}
|
||||
|
||||
if (!properties || typeof properties === 'undefined') {
|
||||
properties = {};
|
||||
}
|
||||
|
||||
// Augment the properties structure with additional common properties before sending
|
||||
Promise.all([this.getUserId(), this.getPlatformInformation()]).then(() => {
|
||||
properties['userId'] = this.userId;
|
||||
properties['distribution'] = (this.platformInformation && this.platformInformation.distribution) ?
|
||||
`${this.platformInformation.distribution.name}, ${this.platformInformation.distribution.version}` : '';
|
||||
|
||||
this.reporter.sendTelemetryEvent(eventName, properties, measures);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Telemetry.initialize();
|
||||
Reference in New Issue
Block a user