From 3ea8080cce166bfffa8d273a68a5758c7471bf3b Mon Sep 17 00:00:00 2001 From: Prashanth <30420973+PrashanthCorp@users.noreply.github.com> Date: Tue, 19 Jun 2018 12:03:50 -0700 Subject: [PATCH] Display stored procedures in graphs (#639) * Display stored procedures in graphs * Pass client instead of collectionTreeItem --- .../tree/DocDBStoredProcedureTreeItem.ts | 6 +- .../tree/DocDBStoredProceduresTreeItem.ts | 5 +- src/graph/registerGraphCommands.ts | 5 +- src/graph/tree/GraphCollectionTreeItem.ts | 38 +++++++------ src/graph/tree/GraphDatabaseTreeItem.ts | 2 +- src/graph/tree/GraphTreeItem.ts | 56 +++++++++++++++++++ 6 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 src/graph/tree/GraphTreeItem.ts diff --git a/src/docdb/tree/DocDBStoredProcedureTreeItem.ts b/src/docdb/tree/DocDBStoredProcedureTreeItem.ts index 253f5d1..2b5724b 100644 --- a/src/docdb/tree/DocDBStoredProcedureTreeItem.ts +++ b/src/docdb/tree/DocDBStoredProcedureTreeItem.ts @@ -8,7 +8,6 @@ import * as vscode from "vscode"; import { IAzureTreeItem, IAzureNode, UserCancelledError, DialogResponses } from 'vscode-azureextensionui'; import { ProcedureMeta, DocumentClient } from 'documentdb'; import { getDocumentClient } from '../getDocumentClient'; -import { DocDBCollectionTreeItem } from './DocDBCollectionTreeItem'; /** * Represents a Cosmos DB DocumentDB (SQL) stored procedure @@ -18,7 +17,7 @@ export class DocDBStoredProcedureTreeItem implements IAzureTreeItem { public readonly contextValue: string = DocDBStoredProcedureTreeItem.contextValue; public readonly commandId: string = 'cosmosDB.openStoredProcedure'; - constructor(private _endpoint: string, private _masterKey: string, private _isEmulator: boolean, private _collection: DocDBCollectionTreeItem, public procedure: ProcedureMeta) { + constructor(private _endpoint: string, private _masterKey: string, private _isEmulator: boolean, private _client: DocumentClient, public procedure: ProcedureMeta) { } public get id(): string { @@ -34,8 +33,7 @@ export class DocDBStoredProcedureTreeItem implements IAzureTreeItem { } public async update(newProcBody: string): Promise { - const client: DocumentClient = this._collection.getDocumentClient(); - this.procedure = await new Promise((resolve, reject) => client.replaceStoredProcedure( + this.procedure = await new Promise((resolve, reject) => this._client.replaceStoredProcedure( this.link, { body: newProcBody, id: this.procedure.id }, (err, updated: ProcedureMeta) => { diff --git a/src/docdb/tree/DocDBStoredProceduresTreeItem.ts b/src/docdb/tree/DocDBStoredProceduresTreeItem.ts index a4b0c50..f4c869d 100644 --- a/src/docdb/tree/DocDBStoredProceduresTreeItem.ts +++ b/src/docdb/tree/DocDBStoredProceduresTreeItem.ts @@ -11,6 +11,7 @@ import { IAzureTreeItem, UserCancelledError, IAzureNode } from 'vscode-azureexte import { DocDBStoredProcedureTreeItem } from './DocDBStoredProcedureTreeItem'; import { defaultStoredProcedure } from '../../constants'; import { DocDBCollectionTreeItem } from './DocDBCollectionTreeItem'; +import { GraphCollectionTreeItem } from '../../graph/tree/GraphCollectionTreeItem'; /** * This class represents the DocumentDB "Stored Procedures" node in the tree @@ -20,12 +21,12 @@ export class DocDBStoredProceduresTreeItem extends DocDBTreeItemBase) => { + actionHandler.registerCommand('cosmosDB.openGraphExplorer', async (node: IAzureNode) => { if (!node) { - node = >await tree.showNodePicker(GraphCollectionTreeItem.contextValue); + node = >await tree.showNodePicker(GraphCollectionTreeItem.contextValue); } await node.treeItem.showExplorer(graphViewsManager); }); diff --git a/src/graph/tree/GraphCollectionTreeItem.ts b/src/graph/tree/GraphCollectionTreeItem.ts index 631d836..7f3c585 100644 --- a/src/graph/tree/GraphCollectionTreeItem.ts +++ b/src/graph/tree/GraphCollectionTreeItem.ts @@ -5,23 +5,28 @@ import * as path from 'path'; import { IAzureTreeItem, IAzureNode, UserCancelledError, DialogResponses } from 'vscode-azureextensionui'; -import { GraphViewsManager } from '../GraphViewsManager'; -import { GraphConfiguration } from '../GraphConfiguration'; import * as vscode from 'vscode'; -import { CollectionMeta } from 'documentdb'; +import { CollectionMeta, DocumentClient } from 'documentdb'; import { GraphDatabaseTreeItem } from './GraphDatabaseTreeItem'; +import { GraphTreeItem } from './GraphTreeItem'; +import { DocDBStoredProceduresTreeItem } from '../../docdb/tree/DocDBStoredProceduresTreeItem'; +import { getDocumentClient } from '../../docdb/getDocumentClient'; export class GraphCollectionTreeItem implements IAzureTreeItem { public static contextValue: string = "cosmosDBGraph"; public readonly contextValue: string = GraphCollectionTreeItem.contextValue; - public readonly commandId: string = 'cosmosDB.openGraphExplorer'; + + private readonly _graphTreeItem: GraphTreeItem; + private readonly _storedProceduresTreeItem: DocDBStoredProceduresTreeItem; private readonly _database: GraphDatabaseTreeItem; private readonly _collection: CollectionMeta; - constructor(database: GraphDatabaseTreeItem, collection: CollectionMeta) { + constructor(database: GraphDatabaseTreeItem, collection: CollectionMeta, private _documentEndpoint: string, private _masterKey: string, private _isEmulator: boolean) { this._database = database; this._collection = collection; + this._graphTreeItem = new GraphTreeItem(this._database, this._collection); + this._storedProceduresTreeItem = new DocDBStoredProceduresTreeItem(this._documentEndpoint, this._masterKey, this, this._isEmulator); } public get id(): string { @@ -43,6 +48,18 @@ export class GraphCollectionTreeItem implements IAzureTreeItem { }; } + public getDocumentClient(): DocumentClient { + return getDocumentClient(this._documentEndpoint, this._masterKey, this._isEmulator); + } + + public async loadMoreChildren(_node: IAzureNode, _clearCache: boolean): Promise { + return [this._graphTreeItem, this._storedProceduresTreeItem]; + } + + public hasMoreChildren(): boolean { + return false; + } + public async deleteTreeItem(_node: IAzureNode): Promise { const message: string = `Are you sure you want to delete graph '${this.label}' and its contents?`; const result = await vscode.window.showWarningMessage(message, { modal: true }, DialogResponses.deleteResponse, DialogResponses.cancel); @@ -57,15 +74,4 @@ export class GraphCollectionTreeItem implements IAzureTreeItem { throw new UserCancelledError(); } } - - public async showExplorer(graphViewsManager: GraphViewsManager): Promise { - await graphViewsManager.showGraphViewer(this.label, { - documentEndpoint: this._database.documentEndpoint, - gremlinEndpoint: this._database.gremlinEndpoint, - possibleGremlinEndpoints: this._database.possibleGremlinEndpoints, - databaseName: this._database.label, - graphName: this.label, - key: this._database.masterKey - }); - } } diff --git a/src/graph/tree/GraphDatabaseTreeItem.ts b/src/graph/tree/GraphDatabaseTreeItem.ts index 85377a7..2bf7b01 100644 --- a/src/graph/tree/GraphDatabaseTreeItem.ts +++ b/src/graph/tree/GraphDatabaseTreeItem.ts @@ -19,7 +19,7 @@ export class GraphDatabaseTreeItem extends DocDBDatabaseTreeItemBase { } public initChild(collection: CollectionMeta): IAzureTreeItem { - return new GraphCollectionTreeItem(this, collection); + return new GraphCollectionTreeItem(this, collection, this.documentEndpoint, this.masterKey, this.isEmulator); } // Gremlin endpoint, if definitely known diff --git a/src/graph/tree/GraphTreeItem.ts b/src/graph/tree/GraphTreeItem.ts new file mode 100644 index 0000000..0daeaee --- /dev/null +++ b/src/graph/tree/GraphTreeItem.ts @@ -0,0 +1,56 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as path from 'path'; +import { IAzureTreeItem } from 'vscode-azureextensionui'; +import { GraphViewsManager } from '../GraphViewsManager'; +import { GraphConfiguration } from '../GraphConfiguration'; +import * as vscode from 'vscode'; +import { CollectionMeta } from 'documentdb'; +import { GraphDatabaseTreeItem } from './GraphDatabaseTreeItem'; + +export class GraphTreeItem implements IAzureTreeItem { + public static contextValue: string = "cosmosDBGraphGraph"; + public readonly contextValue: string = GraphTreeItem.contextValue; + public readonly commandId: string = 'cosmosDB.openGraphExplorer'; + + private readonly _database: GraphDatabaseTreeItem; + private readonly _collection: CollectionMeta; + + constructor(database: GraphDatabaseTreeItem, collection: CollectionMeta) { + this._database = database; + this._collection = collection; + } + + public get id(): string { + return this._collection.id; + } + + public get label(): string { + return "Graph"; + } + + public get link(): string { + return this._collection._self; + } + + public get iconPath(): string | vscode.Uri | { light: string | vscode.Uri; dark: string | vscode.Uri } { + return { + light: path.join(__filename, '..', '..', '..', '..', '..', 'resources', 'icons', 'theme-agnostic', 'Collection.svg'), + dark: path.join(__filename, '..', '..', '..', '..', '..', 'resources', 'icons', 'theme-agnostic', 'Collection.svg'), + }; + } + + public async showExplorer(graphViewsManager: GraphViewsManager): Promise { + await graphViewsManager.showGraphViewer(this.label, { + documentEndpoint: this._database.documentEndpoint, + gremlinEndpoint: this._database.gremlinEndpoint, + possibleGremlinEndpoints: this._database.possibleGremlinEndpoints, + databaseName: this._database.label, + graphName: this.label, + key: this._database.masterKey + }); + } +}