Skip to content

Commit fca14b1

Browse files
committed
expanding contextValue for mongo* workloads
1 parent 9c71293 commit fca14b1

18 files changed

+79
-48
lines changed

package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -1143,38 +1143,38 @@
11431143
},
11441144
{
11451145
"command": "command.mongoClusters.dropCollection",
1146-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem == mongoClusters.item.collection"
1146+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.collection/i && viewItem =~ /(mongocluster|mongodb)/i"
11471147
},
11481148
{
11491149
"command": "command.mongoClusters.dropDatabase",
1150-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem == mongoClusters.item.database"
1150+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.database/i && viewItem =~ /(mongocluster|mongodb)/i"
11511151
},
11521152
{
11531153
"command": "command.mongoClusters.removeWorkspaceConnection",
1154-
"when": "vscodeDatabases.mongoClustersSupportEnabled && view == azureWorkspace && viewItem == mongoClusters.item.mongoCluster"
1154+
"when": "vscodeDatabases.mongoClustersSupportEnabled && view == azureWorkspace && viewItem =~ /treeitem.mongoCluster/i && viewItem =~ /(mongocluster|mongodb)/i"
11551155
},
11561156
{
11571157
"command": "command.mongoClusters.createCollection",
1158-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem == mongoClusters.item.database"
1158+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.database/i && viewItem =~ /(mongocluster|mongodb)/i"
11591159
},
11601160
{
11611161
"command": "command.mongoClusters.createDatabase",
1162-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /mongoClusters.item.mongoCluster/i",
1162+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.mongoCluster/i && viewItem =~ /(mongocluster|mongodb)/i",
11631163
"group": "1@1"
11641164
},
11651165
{
11661166
"command": "command.mongoClusters.importDocuments",
1167-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem == mongoClusters.item.collection",
1167+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.collection/i && viewItem =~ /(mongocluster|mongodb)/i",
11681168
"group": "1@1"
11691169
},
11701170
{
11711171
"command": "command.mongoClusters.exportDocuments",
1172-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem == mongoClusters.item.collection",
1172+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.collection/i && viewItem =~ /(mongocluster|mongodb)/i",
11731173
"group": "1@2"
11741174
},
11751175
{
11761176
"command": "command.mongoClusters.launchShell",
1177-
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /mongoClusters.item.(mongoCluster|database|collection)/i",
1177+
"when": "vscodeDatabases.mongoClustersSupportEnabled && viewItem =~ /treeitem.(mongoCluster|database|collection)/i && viewItem =~ /(mongocluster|mongodb)/i",
11781178
"group": "2@1"
11791179
}
11801180
],

src/mongoClusters/MongoClustersClient.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ export class MongoClustersClient {
132132

133133
async listDatabases(): Promise<DatabaseItemModel[]> {
134134
const rawDatabases: ListDatabasesResult = await this._mongoClient.db().admin().listDatabases();
135-
const databases: DatabaseItemModel[] = rawDatabases.databases;
135+
const databases: DatabaseItemModel[] = rawDatabases.databases.filter(
136+
// Filter out the 'admin' database if it's empty
137+
(databaseInfo) => !(databaseInfo.name && databaseInfo.name.toLowerCase() === 'admin' && databaseInfo.empty),
138+
);
136139

137140
return databases;
138141
}

src/mongoClusters/commands/createCollection.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { type CreateCollectionWizardContext } from '../wizards/create/createWiza
1111
import { CollectionNameStep } from '../wizards/create/PromptCollectionNameStep';
1212

1313
export async function createCollection(context: IActionContext, databaseNode?: DatabaseItem): Promise<void> {
14+
context.telemetry.properties.experience = databaseNode?.mongoCluster.dbExperience?.api;
15+
1416
// node ??= ... pick a node if not provided
1517
if (!databaseNode) {
1618
throw new Error('No database selected.');

src/mongoClusters/commands/createDatabase.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
import { DatabaseNameStep } from '../wizards/create/PromptDatabaseNameStep';
1616

1717
export async function createDatabase(context: IActionContext, clusterNode?: MongoClusterResourceItem): Promise<void> {
18+
context.telemetry.properties.experience = clusterNode?.mongoCluster.dbExperience?.api;
19+
1820
// node ??= ... pick a node if not provided
1921
if (!clusterNode) {
2022
throw new Error('No cluster selected.');

src/mongoClusters/commands/dropCollection.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { localize } from '../../utils/localize';
1010
import { type CollectionItem } from '../tree/CollectionItem';
1111

1212
export async function dropCollection(context: IActionContext, node?: CollectionItem): Promise<void> {
13+
context.telemetry.properties.experience = node?.mongoCluster.dbExperience?.api;
14+
1315
// node ??= ... pick a node if not provided
1416
if (!node) {
1517
throw new Error('No collection selected.');

src/mongoClusters/commands/dropDatabase.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { localize } from '../../utils/localize';
1010
import { type DatabaseItem } from '../tree/DatabaseItem';
1111

1212
export async function dropDatabase(context: IActionContext, node?: DatabaseItem): Promise<void> {
13+
context.telemetry.properties.experience = node?.mongoCluster.dbExperience?.api;
14+
1315
// node ??= ... pick a node if not provided
1416
if (!node) {
1517
throw new Error('No database selected.');

src/mongoClusters/commands/exportDocuments.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { MongoClustersClient } from '../MongoClustersClient';
1313
import { type CollectionItem } from '../tree/CollectionItem';
1414

1515
export async function mongoClustersExportEntireCollection(context: IActionContext, node?: CollectionItem) {
16+
context.telemetry.properties.experience = node?.mongoCluster.dbExperience?.api;
17+
1618
return mongoClustersExportQueryResults(context, node);
1719
}
1820

@@ -21,6 +23,8 @@ export async function mongoClustersExportQueryResults(
2123
node?: CollectionItem,
2224
props?: { queryText?: string; source?: string },
2325
): Promise<void> {
26+
context.telemetry.properties.experience = node?.mongoCluster.dbExperience?.api;
27+
2428
// node ??= ... pick a node if not provided
2529
if (!node) {
2630
throw new Error('No collection selected.');

src/mongoClusters/commands/importDocuments.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export async function mongoClustersImportDocuments(
1313
_collectionNodes?: CollectionItem[], // required by the TreeNodeCommandCallback, but not used
1414
...args: unknown[]
1515
): Promise<void> {
16+
context.telemetry.properties.experience = collectionNode?.mongoCluster.dbExperience?.api;
17+
1618
const source = (args[0] as { source?: string })?.source || 'contextMenu';
1719
context.telemetry.properties.calledFrom = source;
1820

src/mongoClusters/commands/launchShell.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ import {
1616
} from '../utils/connectionStringHelpers';
1717

1818
export async function launchShell(
19-
_context: IActionContext,
19+
context: IActionContext,
2020
node?: DatabaseItem | CollectionItem | MongoClusterResourceItem,
2121
): Promise<void> {
22+
context.telemetry.properties.experience = node?.mongoCluster.dbExperience?.api;
23+
2224
if (!node) {
2325
throw new Error('No database or collection selected.');
2426
}

src/mongoClusters/tree/CollectionItem.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { createGenericElement, type IActionContext, type TreeElementBase } from '@microsoft/vscode-azext-utils';
6+
import {
7+
createContextValue,
8+
createGenericElement,
9+
type IActionContext,
10+
type TreeElementBase,
11+
} from '@microsoft/vscode-azext-utils';
712
import { type Document } from 'bson';
813
import { ThemeIcon, TreeItemCollapsibleState, type TreeItem } from 'vscode';
914
import { ext } from '../../extensionVariables';
@@ -30,7 +35,7 @@ export class CollectionItem {
3035
async getChildren(): Promise<TreeElementBase[]> {
3136
return [
3237
createGenericElement({
33-
contextValue: 'mongoClusters.item.documents',
38+
contextValue: createContextValue(['treeitem.documents', this.mongoCluster.dbExperience?.api ?? '']),
3439
id: `${this.id}/documents`,
3540
label: 'Documents',
3641
commandId: 'command.internal.mongoClusters.containerView.open',
@@ -80,7 +85,7 @@ export class CollectionItem {
8085
getTreeItem(): TreeItem {
8186
return {
8287
id: this.id,
83-
contextValue: 'mongoClusters.item.collection',
88+
contextValue: createContextValue(['treeitem.collection', this.mongoCluster.dbExperience?.api ?? '']),
8489
label: this.collectionInfo.name,
8590
iconPath: new ThemeIcon('folder-opened'),
8691
collapsibleState: TreeItemCollapsibleState.Collapsed,

src/mongoClusters/tree/DatabaseItem.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import {
7+
createContextValue,
78
createGenericElement,
89
type IActionContext,
910
type TreeElementBase,
@@ -35,7 +36,10 @@ export class DatabaseItem implements TreeElementWithId {
3536
// no databases in there:
3637
return [
3738
createGenericElement({
38-
contextValue: 'mongoClusters.item.no-collection',
39+
contextValue: createContextValue([
40+
'treeitem.no-collections',
41+
this.mongoCluster.dbExperience?.api ?? '',
42+
]),
3943
id: `${this.id}/no-databases`,
4044
label: 'Create collection...',
4145
iconPath: new vscode.ThemeIcon('plus'),
@@ -82,7 +86,7 @@ export class DatabaseItem implements TreeElementWithId {
8286
getTreeItem(): TreeItem {
8387
return {
8488
id: this.id,
85-
contextValue: 'mongoClusters.item.database',
89+
contextValue: createContextValue(['treeitem.database', this.mongoCluster.dbExperience?.api ?? '']),
8690
label: this.databaseInfo.name,
8791
iconPath: new ThemeIcon('database'), // TODO: create our onw icon here, this one's shape can change
8892
collapsibleState: TreeItemCollapsibleState.Collapsed,

src/mongoClusters/tree/IndexItem.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { createGenericElement, type TreeElementBase } from '@microsoft/vscode-azext-utils';
6+
import { createContextValue, createGenericElement, type TreeElementBase } from '@microsoft/vscode-azext-utils';
77
import { ThemeIcon, TreeItemCollapsibleState, type TreeItem } from 'vscode';
88
import { type CollectionItemModel, type DatabaseItemModel, type IndexItemModel } from '../MongoClustersClient';
99
import { type MongoClusterModel } from './MongoClusterModel';
@@ -38,7 +38,7 @@ export class IndexItem {
3838
getTreeItem(): TreeItem {
3939
return {
4040
id: this.id,
41-
contextValue: 'mongoClusters.item.index',
41+
contextValue: createContextValue(['treeitem.index', this.mongoCluster.dbExperience?.api ?? '']),
4242
label: this.indexInfo.name,
4343
iconPath: new ThemeIcon('combine'), // TODO: create our onw icon here, this one's shape can change
4444
collapsibleState: TreeItemCollapsibleState.Collapsed,

src/mongoClusters/tree/IndexesItem.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { type TreeElementBase } from '@microsoft/vscode-azext-utils';
6+
import { createContextValue, type TreeElementBase } from '@microsoft/vscode-azext-utils';
77
import { ThemeIcon, TreeItemCollapsibleState, type TreeItem } from 'vscode';
88
import { MongoClustersClient, type CollectionItemModel, type DatabaseItemModel } from '../MongoClustersClient';
99
import { IndexItem } from './IndexItem';
@@ -31,7 +31,7 @@ export class IndexesItem {
3131
getTreeItem(): TreeItem {
3232
return {
3333
id: this.id,
34-
contextValue: 'mongoClusters.item.indexes',
34+
contextValue: createContextValue(['treeitem.indexes', this.mongoCluster.dbExperience?.api ?? '']),
3535
label: 'Indexes',
3636
iconPath: new ThemeIcon('combine'), // TODO: create our onw icon here, this one's shape can change
3737
collapsibleState: TreeItemCollapsibleState.Collapsed,

src/mongoClusters/tree/MongoClusterItemBase.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { createGenericElement, type IActionContext, type TreeElementBase } from '@microsoft/vscode-azext-utils';
6+
import {
7+
createContextValue,
8+
createGenericElement,
9+
type IActionContext,
10+
type TreeElementBase,
11+
} from '@microsoft/vscode-azext-utils';
712
import { type TreeItem } from 'vscode';
813

914
import * as vscode from 'vscode';
@@ -79,7 +84,10 @@ export abstract class MongoClusterItemBase implements TreeElementBase {
7984
if (databases.length === 0) {
8085
return [
8186
createGenericElement({
82-
contextValue: 'mongoClusters.item.no-databases',
87+
contextValue: createContextValue([
88+
'treeitem.no-databases',
89+
this.mongoCluster.dbExperience?.api ?? '',
90+
]),
8391
id: `${this.id}/no-databases`,
8492
label: 'Create database...',
8593
iconPath: new vscode.ThemeIcon('plus'),
@@ -123,7 +131,7 @@ export abstract class MongoClusterItemBase implements TreeElementBase {
123131
getTreeItem(): TreeItem {
124132
return {
125133
id: this.id,
126-
contextValue: 'mongoClusters.item.mongoCluster',
134+
contextValue: createContextValue(['treeitem.mongocluster', this.mongoCluster.dbExperience?.api ?? '']),
127135
label: this.mongoCluster.name,
128136
description: this.mongoCluster.sku !== undefined ? `(${this.mongoCluster.sku})` : false,
129137
// iconPath: getThemeAgnosticIconPath('CosmosDBAccount.svg'), // Uncomment if icon is available

src/mongoClusters/tree/MongoClusterModel.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { type MongoCluster, type Resource } from '@azure/arm-cosmosdb';
7-
import { type API } from '../../AzureDBExperiences';
7+
import { type Experience } from '../../AzureDBExperiences';
88

99
// Selecting only the properties used in the extension, but keeping an easy option to extend the model later and offer full coverage of MongoCluster
1010
// '|' means that you can only access properties that are common to both types.
@@ -35,7 +35,7 @@ interface ResourceModelInUse extends Resource {
3535
resourceGroup?: string;
3636

3737
// adding support for MongoRU and vCore
38-
dbExperience?: API.MongoDB | API.MongoClusters;
38+
dbExperience?: Experience;
3939

4040
isServerless?: boolean;
4141
}

src/mongoClusters/tree/workspace/MongoClusterWorkspaceItem.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import {
77
AzureWizard,
88
callWithTelemetryAndErrorHandling,
9+
createContextValue,
910
nonNullProp,
1011
nonNullValue,
1112
UserCancelledError,
@@ -153,7 +154,7 @@ export class MongoClusterWorkspaceItem extends MongoClusterItemBase {
153154
getTreeItem(): vscode.TreeItem {
154155
return {
155156
id: this.id,
156-
contextValue: 'mongoClusters.item.mongoCluster',
157+
contextValue: createContextValue(['treeitem.mongocluster', this.mongoCluster.dbExperience?.api ?? '']),
157158
label: this.mongoCluster.name,
158159
description: this.mongoCluster.sku !== undefined ? `(${this.mongoCluster.sku})` : false,
159160
iconPath: new vscode.ThemeIcon('server-environment'), // Uncomment if icon is available

src/mongoClusters/tree/workspace/MongoDBAccountsWorkspaceItem.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { createGenericElement, type TreeElementBase } from '@microsoft/vscode-azext-utils';
77
import { ThemeIcon, TreeItemCollapsibleState, type TreeItem } from 'vscode';
8-
import { API } from '../../../AzureDBExperiences';
8+
import { MongoClustersExprience } from '../../../AzureDBExperiences';
99
import { WorkspaceResourceType } from '../../../tree/workspace/sharedWorkspaceResourceProvider';
1010
import { SharedWorkspaceStorage } from '../../../tree/workspace/sharedWorkspaceStorage';
1111
import { type MongoClusterModel } from '../MongoClusterModel';
@@ -26,13 +26,13 @@ export class MongoDBAccountsWorkspaceItem implements TreeElementBase {
2626
const model: MongoClusterModel = {
2727
id: item.id,
2828
name: item.name,
29-
dbExperience: API.MongoClusters,
29+
dbExperience: MongoClustersExprience,
3030
connectionString: item?.secrets?.[0] ?? undefined,
3131
};
3232
return new MongoClusterWorkspaceItem(model);
3333
}),
3434
createGenericElement({
35-
contextValue: this.id + '/newConnection',
35+
contextValue: 'treeitem.newConnection',
3636
id: this.id + '/newConnection',
3737
label: 'New Connection...',
3838
iconPath: new ThemeIcon('plus'),

src/tree/mongo/MongoAccountResourceItem.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { CosmosAccountResourceItemBase } from '../CosmosAccountResourceItemBase'
2323
import { type MongoAccountModel } from './MongoAccountModel';
2424

2525
/**
26-
* This implementation relies on information from the CosmosAccountModel, i.e.
26+
* This implementation relies on information from the MongoAccountModel, i.e.
2727
* will only behave as expected when used in the context of an Azure Subscription.
2828
*/
2929
export class MongoAccountResourceItem extends CosmosAccountResourceItemBase {
@@ -122,8 +122,7 @@ export class MongoAccountResourceItem extends CosmosAccountResourceItemBase {
122122
throw new Error('Failed to connect.');
123123
}
124124

125-
// TODO: add support for single databases via connection string.
126-
// move it to monogoclustersclient
125+
// TODO: add support for single databases via connection string. move it to monogoclustersclient
127126
//
128127
// const databaseInConnectionString = getDatabaseNameFromConnectionString(this.account.connectionString);
129128
// if (databaseInConnectionString && !this.isEmulator) {
@@ -137,25 +136,20 @@ export class MongoAccountResourceItem extends CosmosAccountResourceItemBase {
137136
// ];
138137
// }
139138

140-
// https://mongodb.github.io/node-mongodb-native/3.1/api/index.html
141-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
142139
const databases = await mongoClient.listDatabases();
143140

144-
return databases
145-
.filter(
146-
(databaseInfo) =>
147-
!(databaseInfo.name && databaseInfo.name.toLowerCase() === 'admin' && databaseInfo.empty),
148-
) // Filter out the 'admin' database if it's empty
149-
.map((database) => {
150-
const clusterInfo = this.account as MongoClusterModel;
151-
// eslint-disable-next-line no-unused-vars
152-
const databaseInfo: DatabaseItemModel = {
153-
name: database.name,
154-
empty: database.empty,
155-
};
156-
157-
return new DatabaseItem(clusterInfo, databaseInfo);
158-
});
141+
return databases.map((database) => {
142+
const clusterInfo = this.account as MongoClusterModel;
143+
clusterInfo.dbExperience = this.experience;
144+
145+
// eslint-disable-next-line no-unused-vars
146+
const databaseInfo: DatabaseItemModel = {
147+
name: database.name,
148+
empty: database.empty,
149+
};
150+
151+
return new DatabaseItem(clusterInfo, databaseInfo);
152+
});
159153

160154
// } catch (error) {
161155
// const message = parseError(error).message;

0 commit comments

Comments
 (0)