mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-17 02:51:36 -05:00
SQL Operations Studio Public Preview 1 (0.23) release source code
This commit is contained in:
381
src/sql/parts/insights/browser/insightsDialogView.ts
Normal file
381
src/sql/parts/insights/browser/insightsDialogView.ts
Normal file
@@ -0,0 +1,381 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import 'vs/css!sql/parts/insights/browser/media/insightsDialog';
|
||||
|
||||
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import { Modal } from 'sql/base/browser/ui/modal/modal';
|
||||
import { IInsightsConfigDetails } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { attachModalDialogStyler, attachTableStyler } from 'sql/common/theme/styler';
|
||||
import { ITaskRegistry, Extensions as TaskExtensions } from 'sql/platform/tasks/taskRegistry';
|
||||
import { ConnectionProfile } from 'sql/parts/connection/common/connectionProfile';
|
||||
import * as TelemetryKeys from 'sql/common/telemetryKeys';
|
||||
import { IInsightsDialogModel, ListResource, IInsightDialogActionContext } from 'sql/parts/insights/common/interfaces';
|
||||
import { TableCollapsibleView } from 'sql/base/browser/ui/table/tableView';
|
||||
import { TableDataView } from 'sql/base/browser/ui/table/tableDataView';
|
||||
import { RowSelectionModel } from 'sql/base/browser/ui/table/plugins/rowSelectionModel.plugin';
|
||||
import { error } from 'sql/base/common/log';
|
||||
import { Table } from 'sql/base/browser/ui/table/table';
|
||||
import { CopyInsightDialogSelectionAction } from 'sql/parts/insights/common/insightDialogActions';
|
||||
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { IPartService } from 'vs/workbench/services/part/common/partService';
|
||||
import * as DOM from 'vs/base/browser/dom';
|
||||
import { SplitView, ViewSizing } from 'vs/base/browser/ui/splitview/splitview';
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import { attachButtonStyler } from 'vs/platform/theme/common/styler';
|
||||
import { IThemeService } from 'vs/platform/theme/common/themeService';
|
||||
import { IListService } from 'vs/platform/list/browser/listService';
|
||||
import * as nls from 'vs/nls';
|
||||
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
|
||||
import { Registry } from 'vs/platform/registry/common/platform';
|
||||
import { IAction } from 'vs/base/common/actions';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import * as types from 'vs/base/common/types';
|
||||
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
|
||||
import { Button } from 'vs/base/browser/ui/button/button';
|
||||
|
||||
/* Regex that matches the form `${value}` */
|
||||
export const insertValueRegex: RegExp = /\${(.*?)\}/;
|
||||
|
||||
const labelDisplay = nls.localize("item", "Item");
|
||||
const valueDisplay = nls.localize("value", "Value");
|
||||
|
||||
function stateFormatter(row: number, cell: number, value: any, columnDef: Slick.Column<ListResource>, resource: ListResource): string {
|
||||
// template
|
||||
const icon = DOM.$('span.icon-span');
|
||||
const badge = DOM.$('div.badge');
|
||||
const badgeContent = DOM.$('div.badge-content');
|
||||
DOM.append(badge, badgeContent);
|
||||
DOM.append(icon, badge);
|
||||
|
||||
// render icon if passed
|
||||
if (resource.icon) {
|
||||
icon.classList.add('icon');
|
||||
icon.classList.add(resource.icon);
|
||||
} else {
|
||||
icon.classList.remove('icon');
|
||||
}
|
||||
|
||||
//render state badge if present
|
||||
if (resource.stateColor) {
|
||||
badgeContent.style.backgroundColor = resource.stateColor;
|
||||
badgeContent.classList.remove('icon');
|
||||
} else if (resource.stateIcon) {
|
||||
badgeContent.style.backgroundColor = '';
|
||||
badgeContent.classList.add('icon');
|
||||
badgeContent.classList.add(resource.stateIcon);
|
||||
} else {
|
||||
badgeContent.classList.remove('icon');
|
||||
badgeContent.style.backgroundColor = '';
|
||||
}
|
||||
|
||||
return icon.outerHTML;
|
||||
}
|
||||
|
||||
export class InsightsDialogView extends Modal {
|
||||
|
||||
private _connectionProfile: IConnectionProfile;
|
||||
private _insight: IInsightsConfigDetails;
|
||||
private _splitView: SplitView;
|
||||
private _container: HTMLElement;
|
||||
private _topTable: Table<ListResource>;
|
||||
private _topTableData: TableDataView<ListResource>;
|
||||
private _bottomTable: Table<ListResource>;
|
||||
private _bottomTableData: TableDataView<ListResource>;
|
||||
private _taskButtonDisposables: IDisposable[] = [];
|
||||
private _topColumns: Array<Slick.Column<ListResource>> = [
|
||||
{
|
||||
name: '',
|
||||
field: 'state',
|
||||
id: 'state',
|
||||
width: 20,
|
||||
resizable: false,
|
||||
formatter: stateFormatter
|
||||
},
|
||||
{
|
||||
name: labelDisplay,
|
||||
field: 'label',
|
||||
id: 'label'
|
||||
},
|
||||
{
|
||||
name: valueDisplay,
|
||||
field: 'value',
|
||||
id: 'value'
|
||||
}
|
||||
];
|
||||
|
||||
private _bottomColumns: Array<Slick.Column<ListResource>> = [
|
||||
{
|
||||
name: nls.localize("property", "Property"),
|
||||
field: 'label',
|
||||
id: 'label'
|
||||
},
|
||||
{
|
||||
name: nls.localize("value", "Value"),
|
||||
field: 'value',
|
||||
id: 'value'
|
||||
}
|
||||
];
|
||||
|
||||
constructor(
|
||||
private _model: IInsightsDialogModel,
|
||||
@IInstantiationService private _instantiationService: IInstantiationService,
|
||||
@IThemeService private _themeService: IThemeService,
|
||||
@IListService private _listService: IListService,
|
||||
@IPartService partService: IPartService,
|
||||
@IContextMenuService private _contextMenuService: IContextMenuService,
|
||||
@ITelemetryService telemetryService: ITelemetryService,
|
||||
@IContextKeyService contextKeyService: IContextKeyService
|
||||
) {
|
||||
super(nls.localize("InsightsDialogTitle", "Insights"), TelemetryKeys.Insights, partService, telemetryService, contextKeyService);
|
||||
this._model.onDataChange(e => this.build());
|
||||
}
|
||||
|
||||
private updateTopColumns(): void {
|
||||
let labelName = this.labelColumnName ? this.labelColumnName : labelDisplay;
|
||||
let valueName = this._insight.value ? this._insight.value : valueDisplay;
|
||||
this._topColumns = [
|
||||
{
|
||||
name: '',
|
||||
field: 'state',
|
||||
id: 'state',
|
||||
width: 20,
|
||||
resizable: false,
|
||||
formatter: stateFormatter
|
||||
},
|
||||
{
|
||||
name: labelName,
|
||||
field: 'label',
|
||||
id: 'label'
|
||||
},
|
||||
{
|
||||
name: valueName,
|
||||
field: 'value',
|
||||
id: 'value'
|
||||
}
|
||||
];
|
||||
this._topTable.columns = this._topColumns;
|
||||
}
|
||||
|
||||
protected renderBody(container: HTMLElement) {
|
||||
this._container = container;
|
||||
|
||||
this._splitView = new SplitView(container);
|
||||
|
||||
this._topTableData = new TableDataView();
|
||||
this._bottomTableData = new TableDataView();
|
||||
let topTableView = new TableCollapsibleView(nls.localize("insights.dialog.items", "Items"), { sizing: ViewSizing.Flexible, ariaHeaderLabel: 'title' }, this._topTableData, this._topColumns, { forceFitColumns: true });
|
||||
this._topTable = topTableView.table;
|
||||
topTableView.addContainerClass('insights');
|
||||
this._topTable.setSelectionModel(new RowSelectionModel<ListResource>());
|
||||
let bottomTableView = new TableCollapsibleView(nls.localize("insights.dialog.itemDetails", "Item Details"), { sizing: ViewSizing.Flexible, ariaHeaderLabel: 'title' }, this._bottomTableData, this._bottomColumns, { forceFitColumns: true });
|
||||
this._bottomTable = bottomTableView.table;
|
||||
this._bottomTable.setSelectionModel(new RowSelectionModel<ListResource>());
|
||||
|
||||
this._register(this._topTable.onSelectedRowsChanged((e: DOMEvent, data: Slick.OnSelectedRowsChangedEventArgs<ListResource>) => {
|
||||
if (data.rows.length === 1) {
|
||||
let element = this._topTableData.getItem(data.rows[0]);
|
||||
let resourceArray: ListResource[] = [];
|
||||
for (let i = 0; i < this._model.columns.length; i++) {
|
||||
resourceArray.push({ label: this._model.columns[i], value: element.data[i], data: element.data });
|
||||
}
|
||||
this._bottomTableData.clear();
|
||||
this._bottomTableData.push(resourceArray);
|
||||
this._enableTaskButtons(true);
|
||||
} else {
|
||||
this._enableTaskButtons(false);
|
||||
}
|
||||
}));
|
||||
|
||||
this._register(this._topTable.onContextMenu((e: DOMEvent, data: Slick.OnContextMenuEventArgs<any>) => {
|
||||
if (this.hasActions()) {
|
||||
this._contextMenuService.showContextMenu({
|
||||
getAnchor: () => e.target as HTMLElement,
|
||||
getActions: () => this.insightActions,
|
||||
getActionsContext: () => this.topInsightContext(this._topTableData.getItem(this._topTable.getCellFromEvent(e).row), this._topTable.getCellFromEvent(e))
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
this._register(this._bottomTable.onContextMenu((e: DOMEvent, data: Slick.OnContextMenuEventArgs<any>) => {
|
||||
this._contextMenuService.showContextMenu({
|
||||
getAnchor: () => e.target as HTMLElement,
|
||||
getActions: () => TPromise.as([this._instantiationService.createInstance(CopyInsightDialogSelectionAction, CopyInsightDialogSelectionAction.ID, CopyInsightDialogSelectionAction.LABEL)]),
|
||||
getActionsContext: () => this.bottomInsightContext(this._bottomTableData.getItem(this._bottomTable.getCellFromEvent(e).row), this._bottomTable.getCellFromEvent(e))
|
||||
});
|
||||
}));
|
||||
|
||||
this._splitView.addView(topTableView);
|
||||
this._splitView.addView(bottomTableView);
|
||||
|
||||
this._register(attachTableStyler(this._topTable, this._themeService));
|
||||
this._register(attachTableStyler(this._bottomTable, this._themeService));
|
||||
}
|
||||
|
||||
public render() {
|
||||
super.render();
|
||||
let button = this.addFooterButton('Close', () => this.close());
|
||||
this._register(attachButtonStyler(button, this._themeService));
|
||||
this._register(attachModalDialogStyler(this, this._themeService));
|
||||
}
|
||||
|
||||
protected layout(height?: number): void {
|
||||
this._splitView.layout(DOM.getContentHeight(this._container));
|
||||
}
|
||||
|
||||
// insight object
|
||||
public open(input: IInsightsConfigDetails, connectionProfile: IConnectionProfile): void {
|
||||
if (types.isUndefinedOrNull(input) || types.isUndefinedOrNull(connectionProfile)) {
|
||||
return;
|
||||
}
|
||||
this._insight = input;
|
||||
this._connectionProfile = connectionProfile;
|
||||
this.updateTopColumns();
|
||||
this.show();
|
||||
}
|
||||
|
||||
private build(): void {
|
||||
let labelIndex: number;
|
||||
let valueIndex: number;
|
||||
let columnName = this.labelColumnName;
|
||||
if (this._insight.label === undefined || (labelIndex = this._model.columns.indexOf(columnName)) === -1) {
|
||||
labelIndex = 0;
|
||||
}
|
||||
if (this._insight.value === undefined || (valueIndex = this._model.columns.indexOf(this._insight.value)) === -1) {
|
||||
valueIndex = 1;
|
||||
}
|
||||
// convert
|
||||
let inputArray = this._model.getListResources(labelIndex, valueIndex);
|
||||
this._topTableData.clear();
|
||||
this._topTableData.push(inputArray);
|
||||
if (this._insight.actions && this._insight.actions.types) {
|
||||
const taskRegistry = Registry.as<ITaskRegistry>(TaskExtensions.TaskContribution);
|
||||
let tasks = taskRegistry.idToCtorMap;
|
||||
for (let action of this._insight.actions.types) {
|
||||
let ctor = tasks[action];
|
||||
if (ctor) {
|
||||
let button = this.addFooterButton(ctor.LABEL, () => {
|
||||
let element = this._topTable.getSelectedRows();
|
||||
let resource: ListResource;
|
||||
if (element && element.length > 0) {
|
||||
resource = this._topTableData.getItem(element[0]);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
this._instantiationService.createInstance(ctor, ctor.ID, ctor.LABEL, ctor.ICON).run(this.topInsightContext(resource));
|
||||
}, 'left');
|
||||
button.enabled = false;
|
||||
this._taskButtonDisposables.push(button);
|
||||
this._taskButtonDisposables.push(attachButtonStyler(button, this._themeService));
|
||||
}
|
||||
}
|
||||
}
|
||||
this.layout();
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this._topTableData.clear();
|
||||
this._bottomTableData.clear();
|
||||
}
|
||||
|
||||
private get labelColumnName(): string {
|
||||
return typeof this._insight.label === 'object' ? this._insight.label.column : this._insight.label;
|
||||
}
|
||||
|
||||
|
||||
public close() {
|
||||
this.hide();
|
||||
dispose(this._taskButtonDisposables);
|
||||
this._taskButtonDisposables = [];
|
||||
}
|
||||
|
||||
private hasActions(): boolean {
|
||||
return !!(this._insight && this._insight.actions && this._insight.actions.types
|
||||
&& this._insight.actions.types.length > 0);
|
||||
}
|
||||
|
||||
private get insightActions(): TPromise<IAction[]> {
|
||||
const taskRegistry = Registry.as<ITaskRegistry>(TaskExtensions.TaskContribution);
|
||||
let tasks = taskRegistry.idToCtorMap;
|
||||
let actions = this._insight.actions.types;
|
||||
let returnActions: IAction[] = [];
|
||||
for (let action of actions) {
|
||||
let ctor = tasks[action];
|
||||
if (ctor) {
|
||||
returnActions.push(this._instantiationService.createInstance(ctor, ctor.ID, ctor.LABEL, ctor.ICON));
|
||||
}
|
||||
}
|
||||
return TPromise.as(returnActions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the context that should be passed to the action passed on the selected element for the top table
|
||||
* @param element
|
||||
*/
|
||||
private topInsightContext(element: ListResource, cell?: Slick.Cell): IInsightDialogActionContext {
|
||||
let database = this._insight.actions.database || this._connectionProfile.databaseName;
|
||||
let server = this._insight.actions.server || this._connectionProfile.serverName;
|
||||
let user = this._insight.actions.user || this._connectionProfile.userName;
|
||||
let match: Array<string>;
|
||||
match = database.match(insertValueRegex);
|
||||
if (match && match.length > 0) {
|
||||
let index = this._model.columns.indexOf(match[1]);
|
||||
if (index === -1) {
|
||||
error('Could not find column', match[1]);
|
||||
} else {
|
||||
database = database.replace(match[0], element.data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
match = server.match(insertValueRegex);
|
||||
if (match && match.length > 0) {
|
||||
let index = this._model.columns.indexOf(match[1]);
|
||||
if (index === -1) {
|
||||
error('Could not find column', match[1]);
|
||||
} else {
|
||||
server = server.replace(match[0], element.data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
match = user.match(insertValueRegex);
|
||||
if (match && match.length > 0) {
|
||||
let index = this._model.columns.indexOf(match[1]);
|
||||
if (index === -1) {
|
||||
error('Could not find column', match[1]);
|
||||
} else {
|
||||
user = user.replace(match[0], element.data[index]);
|
||||
}
|
||||
}
|
||||
|
||||
let currentProfile = this._connectionProfile as ConnectionProfile;
|
||||
let profile = new ConnectionProfile(currentProfile.serverCapabilities, currentProfile);
|
||||
profile.databaseName = database;
|
||||
profile.serverName = server;
|
||||
profile.userName = user;
|
||||
|
||||
return { profile, cellData: undefined };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the context that should be passed to the action passed on the selected element for the bottom table
|
||||
* @param element
|
||||
*/
|
||||
private bottomInsightContext(element: ListResource, cell: Slick.Cell): IInsightDialogActionContext {
|
||||
|
||||
let cellData = element[this._bottomColumns[cell.cell].id];
|
||||
|
||||
return { profile: undefined, cellData };
|
||||
}
|
||||
|
||||
private _enableTaskButtons(val: boolean): void {
|
||||
for (let index = 0; index < this._taskButtonDisposables.length; index++) {
|
||||
let element = this._taskButtonDisposables[index];
|
||||
if (element instanceof Button) {
|
||||
element.enabled = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
33
src/sql/parts/insights/browser/media/insightsDialog.css
Normal file
33
src/sql/parts/insights/browser/media/insightsDialog.css
Normal file
@@ -0,0 +1,33 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
.insights span {
|
||||
display: inline-block;
|
||||
text-overflow: ellipsis;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.insights .icon-span {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
.insights .badge .badge-content {
|
||||
position: absolute;
|
||||
top: 7px;
|
||||
right: 8px;
|
||||
min-width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 3px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.insights .badge {
|
||||
position: absolute;
|
||||
top: 7px;
|
||||
left: 5px;
|
||||
overflow: hidden;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
28
src/sql/parts/insights/common/insightDialogActions.ts
Normal file
28
src/sql/parts/insights/common/insightDialogActions.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IInsightDialogActionContext } from 'sql/parts/insights/common/interfaces';
|
||||
|
||||
import { Action } from 'vs/base/common/actions';
|
||||
import * as nls from 'vs/nls';
|
||||
import { TPromise } from 'vs/base/common/winjs.base';
|
||||
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
|
||||
|
||||
export class CopyInsightDialogSelectionAction extends Action {
|
||||
public static ID = 'workbench.action.insights.copySelection';
|
||||
public static LABEL = nls.localize('workbench.action.insights.copySelection', "Copy Cell");
|
||||
|
||||
constructor(
|
||||
id: string, label: string,
|
||||
@IClipboardService private _clipboardService: IClipboardService
|
||||
) {
|
||||
super(id, label);
|
||||
}
|
||||
|
||||
public run(event?: IInsightDialogActionContext): TPromise<any> {
|
||||
this._clipboardService.writeText(event.cellData);
|
||||
return TPromise.as(void 0);
|
||||
}
|
||||
}
|
||||
128
src/sql/parts/insights/common/insightsDialogModel.ts
Normal file
128
src/sql/parts/insights/common/insightsDialogModel.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IInsightsDialogModel, ListResource } from './interfaces';
|
||||
import { IInsightsConfigDetails, IInsightsLabel } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { Conditional } from 'sql/parts/dashboard/common/interfaces';
|
||||
|
||||
import Event, { Emitter, debounceEvent } from 'vs/base/common/event';
|
||||
|
||||
export class InsightsDialogModel implements IInsightsDialogModel {
|
||||
private _rows: string[][];
|
||||
private _columns: string[];
|
||||
private _insight: IInsightsConfigDetails;
|
||||
|
||||
private _onDataChangeEmitter: Emitter<void> = new Emitter<void>();
|
||||
private _onDataChangeEvent: Event<void> = this._onDataChangeEmitter.event;
|
||||
public onDataChange: Event<void> = debounceEvent(this._onDataChangeEvent, (last, event) => event, 75, false);
|
||||
|
||||
public set insight(insight: IInsightsConfigDetails) {
|
||||
this._insight = insight;
|
||||
}
|
||||
|
||||
public set rows(val: string[][]) {
|
||||
this._rows = val;
|
||||
this._onDataChangeEmitter.fire();
|
||||
}
|
||||
|
||||
public get rows(): string[][] {
|
||||
return this._rows;
|
||||
}
|
||||
|
||||
public set columns(val: string[]) {
|
||||
this._columns = val;
|
||||
this._onDataChangeEmitter.fire();
|
||||
}
|
||||
|
||||
public get columns(): string[] {
|
||||
return this._columns;
|
||||
}
|
||||
|
||||
public reset(): void {
|
||||
this._columns = [];
|
||||
this._rows = [];
|
||||
this._onDataChangeEmitter.fire();
|
||||
}
|
||||
|
||||
public getListResources(labelIndex: number, valueIndex: number): ListResource[] {
|
||||
return this.rows.map((item) => {
|
||||
let label = item[labelIndex];
|
||||
let value = item[valueIndex];
|
||||
let state = this.calcInsightState(value);
|
||||
let data = item;
|
||||
let icon = typeof this._insight.label === 'object' ? this._insight.label.icon : undefined;
|
||||
let rval = { title: false, label, value, icon, data };
|
||||
if (state) {
|
||||
rval[state.type] = state.val;
|
||||
}
|
||||
return rval;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the state of the item value passed based on the insight conditions
|
||||
* @param item item to determine state for
|
||||
* @returns json that specifies whether the state is an icon or color and the val of that state
|
||||
*/
|
||||
private calcInsightState(item: string): { type: 'stateColor' | 'stateIcon', val: string } {
|
||||
if (typeof this._insight.label === 'string') {
|
||||
return undefined;
|
||||
} else {
|
||||
let label = <IInsightsLabel>this._insight.label;
|
||||
for (let cond of label.state) {
|
||||
switch (Conditional[cond.condition.if]) {
|
||||
case Conditional.always:
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
case Conditional.equals:
|
||||
if (item === cond.condition.equals) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
case Conditional.notEquals:
|
||||
if (item !== cond.condition.equals) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
case Conditional.greaterThanOrEquals:
|
||||
if (parseInt(item) >= parseInt(cond.condition.equals)) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
case Conditional.greaterThan:
|
||||
if (parseInt(item) > parseInt(cond.condition.equals)) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
case Conditional.lessThanOrEquals:
|
||||
if (parseInt(item) <= parseInt(cond.condition.equals)) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
case Conditional.lessThan:
|
||||
if (parseInt(item) < parseInt(cond.condition.equals)) {
|
||||
return cond.color
|
||||
? { type: 'stateColor', val: cond.color }
|
||||
: { type: 'stateIcon', val: cond.icon };
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// if we got to this point, there was no matching conditionals therefore no valid state
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
40
src/sql/parts/insights/common/interfaces.ts
Normal file
40
src/sql/parts/insights/common/interfaces.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import Event from 'vs/base/common/event';
|
||||
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
||||
import { IInsightsConfigDetails, IInsightsConfig } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import { ITaskActionContext } from 'sql/workbench/common/actions';
|
||||
|
||||
export interface IInsightsDialogModel {
|
||||
rows: string[][];
|
||||
columns: string[];
|
||||
getListResources(labelIndex: number, valueIndex: number): ListResource[];
|
||||
reset(): void;
|
||||
onDataChange: Event<void>;
|
||||
insight: IInsightsConfigDetails;
|
||||
}
|
||||
|
||||
export interface ListResource {
|
||||
value: string;
|
||||
label: string;
|
||||
icon?: string;
|
||||
data?: string[];
|
||||
stateColor?: string;
|
||||
stateIcon?: string;
|
||||
}
|
||||
|
||||
export const IInsightsDialogService = createDecorator<IInsightsDialogService>('insightsDialogService');
|
||||
|
||||
export interface IInsightsDialogService {
|
||||
_serviceBrand: any;
|
||||
show(input: IInsightsConfig, connectionProfile: IConnectionProfile): void;
|
||||
close();
|
||||
}
|
||||
|
||||
export interface IInsightDialogActionContext extends ITaskActionContext {
|
||||
cellData: string;
|
||||
}
|
||||
42
src/sql/parts/insights/insightsDialogService.ts
Normal file
42
src/sql/parts/insights/insightsDialogService.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
|
||||
import { InsightsDialogController } from 'sql/parts/insights/node/insightsDialogController';
|
||||
import { InsightsDialogView } from 'sql/parts/insights/browser/insightsDialogView';
|
||||
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import { IInsightsConfig } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import { IInsightsDialogModel, IInsightsDialogService } from 'sql/parts/insights/common/interfaces';
|
||||
import { InsightsDialogModel } from 'sql/parts/insights/common/insightsDialogModel';
|
||||
|
||||
export class InsightsDialogService implements IInsightsDialogService {
|
||||
_serviceBrand: any;
|
||||
private _insightsDialogController: InsightsDialogController;
|
||||
private _insightsDialogView: InsightsDialogView;
|
||||
private _insightsDialogModel: IInsightsDialogModel;
|
||||
|
||||
constructor( @IInstantiationService private _instantiationService: IInstantiationService) { }
|
||||
|
||||
// query string
|
||||
public show(input: IInsightsConfig, connectionProfile: IConnectionProfile): void {
|
||||
if (!this._insightsDialogView) {
|
||||
this._insightsDialogModel = new InsightsDialogModel();
|
||||
this._insightsDialogController = this._instantiationService.createInstance(InsightsDialogController, this._insightsDialogModel);
|
||||
this._insightsDialogView = this._instantiationService.createInstance(InsightsDialogView, this._insightsDialogModel);
|
||||
this._insightsDialogView.render();
|
||||
} else {
|
||||
this._insightsDialogModel.reset();
|
||||
this._insightsDialogView.reset();
|
||||
}
|
||||
|
||||
this._insightsDialogModel.insight = input.details;
|
||||
this._insightsDialogController.update(input.details, connectionProfile);
|
||||
this._insightsDialogView.open(input.details, connectionProfile);
|
||||
}
|
||||
|
||||
public close(): void {
|
||||
this._insightsDialogView.close();
|
||||
}
|
||||
}
|
||||
160
src/sql/parts/insights/node/insightsDialogController.ts
Normal file
160
src/sql/parts/insights/node/insightsDialogController.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IConnectionManagementService, IErrorMessageService } from 'sql/parts/connection/common/connectionManagement';
|
||||
import { IConnectionProfile } from 'sql/parts/connection/common/interfaces';
|
||||
import { IInsightsConfigDetails } from 'sql/parts/dashboard/widgets/insights/interfaces';
|
||||
import QueryRunner from 'sql/parts/query/execution/queryRunner';
|
||||
import * as Utils from 'sql/parts/connection/common/utils';
|
||||
import { IInsightsDialogModel } from 'sql/parts/insights/common/interfaces';
|
||||
|
||||
import { DbCellValue, IDbColumn, IResultMessage, QueryExecuteSubsetResult } from 'data';
|
||||
|
||||
import Severity from 'vs/base/common/severity';
|
||||
import * as types from 'vs/base/common/types';
|
||||
import * as pfs from 'vs/base/node/pfs';
|
||||
import * as nls from 'vs/nls';
|
||||
import { IMessageService } from 'vs/platform/message/common/message';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import { error } from 'sql/base/common/log';
|
||||
|
||||
export class InsightsDialogController {
|
||||
private _queryRunner: QueryRunner;
|
||||
private _connectionProfile: IConnectionProfile;
|
||||
private _connectionUri: string;
|
||||
private _columns: IDbColumn[];
|
||||
private _rows: DbCellValue[][];
|
||||
|
||||
constructor(
|
||||
private _model: IInsightsDialogModel,
|
||||
@IMessageService private _messageService: IMessageService,
|
||||
@IErrorMessageService private _errorMessageService: IErrorMessageService,
|
||||
@IInstantiationService private _instantiationService: IInstantiationService,
|
||||
@IConnectionManagementService private _connectionManagementService: IConnectionManagementService,
|
||||
) { }
|
||||
|
||||
public update(input: IInsightsConfigDetails, connectionProfile: IConnectionProfile): Thenable<void> {
|
||||
// execute string
|
||||
if (typeof input === 'object') {
|
||||
if (connectionProfile === undefined) {
|
||||
this._messageService.show(Severity.Error, nls.localize("insightsInputError", "No Connection Profile was passed to insights flyout"));
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (types.isStringArray(input.query)) {
|
||||
return this.createQuery(input.query.join(' '), connectionProfile).catch(e => {
|
||||
this._errorMessageService.showDialog(Severity.Error, nls.localize("insightsError", "Insights Error"), e);
|
||||
}).then(() => undefined);
|
||||
} else if (types.isString(input.query)) {
|
||||
return this.createQuery(input.query, connectionProfile).catch(e => {
|
||||
this._errorMessageService.showDialog(Severity.Error, nls.localize("insightsError", "Insights Error"), e);
|
||||
}).then(() => undefined);
|
||||
} else if (types.isString(input.queryFile)) {
|
||||
return new Promise((resolve, reject) => {
|
||||
pfs.readFile(input.queryFile).then(
|
||||
buffer => {
|
||||
this.createQuery(buffer.toString(), connectionProfile).catch(e => {
|
||||
this._errorMessageService.showDialog(Severity.Error, nls.localize("insightsError", "Insights Error"), e);
|
||||
}).then(() => resolve());
|
||||
},
|
||||
error => {
|
||||
this._messageService.show(Severity.Error, nls.localize("insightsFileError", "There was an error reading the query file: ") + error);
|
||||
resolve();
|
||||
}
|
||||
);
|
||||
});
|
||||
} else {
|
||||
error('Error reading details Query: ', input);
|
||||
this._messageService.show(Severity.Error, nls.localize("insightsConfigError", "There was an error parsing the insight config; could not find query array/string or queryfile"));
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
private async createQuery(queryString: string, connectionProfile: IConnectionProfile): Promise<void> {
|
||||
if (this._queryRunner) {
|
||||
if (!this._queryRunner.hasCompleted) {
|
||||
await this._queryRunner.cancelQuery();
|
||||
}
|
||||
try {
|
||||
await this.createNewConnection(connectionProfile);
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
this._queryRunner.uri = this._connectionUri;
|
||||
} else {
|
||||
try {
|
||||
await this.createNewConnection(connectionProfile);
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
this._queryRunner = this._instantiationService.createInstance(QueryRunner, this._connectionUri, undefined);
|
||||
this.addQueryEventListeners(this._queryRunner);
|
||||
}
|
||||
|
||||
return this._queryRunner.runQuery(queryString);
|
||||
}
|
||||
|
||||
private async createNewConnection(connectionProfile: IConnectionProfile): Promise<void> {
|
||||
// determine if we need to create a new connection
|
||||
if (!this._connectionProfile || connectionProfile.getOptionsKey() !== this._connectionProfile.getOptionsKey()) {
|
||||
if (this._connectionProfile) {
|
||||
try {
|
||||
await this._connectionManagementService.disconnect(this._connectionUri);
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
}
|
||||
this._connectionProfile = connectionProfile;
|
||||
this._connectionUri = Utils.generateUri(this._connectionProfile, 'insights');
|
||||
return this._connectionManagementService.connect(this._connectionProfile, this._connectionUri).then(result => undefined);
|
||||
}
|
||||
}
|
||||
|
||||
private addQueryEventListeners(queryRunner: QueryRunner): void {
|
||||
queryRunner.eventEmitter.on('complete', () => {
|
||||
this.queryComplete().catch(error => {
|
||||
this._errorMessageService.showDialog(Severity.Error, nls.localize("insightsError", "Insights Error"), error);
|
||||
});
|
||||
});
|
||||
queryRunner.eventEmitter.on('message', (message: IResultMessage) => {
|
||||
if (message.isError) {
|
||||
this._errorMessageService.showDialog(Severity.Error, nls.localize("insightsError", "Insights Error"), message.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async queryComplete(): Promise<void> {
|
||||
let batches = this._queryRunner.batchSets;
|
||||
// currently only support 1 batch set 1 resultset
|
||||
if (batches.length > 0) {
|
||||
let batch = batches[0];
|
||||
if (batch.resultSetSummaries.length > 0
|
||||
&& batch.resultSetSummaries[0].rowCount > 0
|
||||
) {
|
||||
let resultset = batch.resultSetSummaries[0];
|
||||
this._columns = resultset.columnInfo;
|
||||
let rows: QueryExecuteSubsetResult;
|
||||
try {
|
||||
rows = await this._queryRunner.getQueryRows(0, resultset.rowCount, batch.id, resultset.id);
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
this._rows = rows.resultSubset.rows;
|
||||
this.updateModel();
|
||||
}
|
||||
}
|
||||
// TODO issue #2746 should ideally show a warning inside the dialog if have no data
|
||||
}
|
||||
|
||||
private updateModel(): void {
|
||||
let data = this._rows.map(r => r.map(c => c.displayValue));
|
||||
let columns = this._columns.map(c => c.columnName);
|
||||
|
||||
this._model.rows = data;
|
||||
this._model.columns = columns;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user