From 422bd2f8358bf4b57b3303d4a092092750131e68 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Fri, 19 Nov 2021 15:42:52 +0100 Subject: [PATCH 1/9] Adds isAtlas property to instance detail --- .../src/instance-detail-helper.spec.ts | 4 ++++ .../src/instance-detail-helper.ts | 20 +++++++++++++++++++ packages/data-service/src/run-command.ts | 10 ++++++++++ packages/instance-model/lib/model.js | 7 +------ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/data-service/src/instance-detail-helper.spec.ts b/packages/data-service/src/instance-detail-helper.spec.ts index b9acba9097f..15c8e2b0cca 100644 --- a/packages/data-service/src/instance-detail-helper.spec.ts +++ b/packages/data-service/src/instance-detail-helper.spec.ts @@ -89,6 +89,10 @@ describe('instance-detail-helper', function () { null ); }); + + it('should not be identified as atlas', function () { + expect(instanceDetails).to.have.property('isAtlas', false); + }); }); }); diff --git a/packages/data-service/src/instance-detail-helper.ts b/packages/data-service/src/instance-detail-helper.ts index 3228aa30d66..d18c6f1d3f3 100644 --- a/packages/data-service/src/instance-detail-helper.ts +++ b/packages/data-service/src/instance-detail-helper.ts @@ -8,6 +8,7 @@ import toNS from 'mongodb-ns'; import createLogger from '@mongodb-js/compass-logging'; import { + AtlasVersionInfo, BuildInfo, CmdLineOpts, CollectionInfo, @@ -88,6 +89,7 @@ export type InstanceDetails = { genuineMongoDB: GenuineMongoDBDetails; dataLake: DataLakeDetails; featureCompatibilityVersion: string | null; + isAtlas: boolean; }; export async function getInstance( @@ -100,6 +102,7 @@ export async function getInstance( hostInfoResult, buildInfoResult, getParameterResult, + atlasVersionResult, ] = await Promise.all([ runCommand(adminDb, { getCmdLineOpts: 1 }).catch((err) => { /** @@ -122,6 +125,10 @@ export async function getInstance( getParameter: 1, featureCompatibilityVersion: 1, }).catch(() => null), + + runCommand(adminDb, { atlasVersion: 1 }).catch((err) => { + return { errmsg: err.message }; + }), ]); return { @@ -134,9 +141,22 @@ export async function getInstance( dataLake: buildDataLakeInfo(buildInfoResult), featureCompatibilityVersion: getParameterResult?.featureCompatibilityVersion.version ?? null, + isAtlas: checkIsAtlas(client, atlasVersionResult), }; } +function checkIsAtlas( + client: MongoClient, + atlasVersionInfo: AtlasVersionInfo +): boolean { + const firstHost = client.options.hosts[0]?.host || ''; + + if (undefined === atlasVersionInfo.version) { + return /mongodb(-dev)?.net$/i.test(firstHost); + } + return true; +} + function buildGenuineMongoDBInfo( buildInfo: Partial, cmdLineOpts: Partial diff --git a/packages/data-service/src/run-command.ts b/packages/data-service/src/run-command.ts index 51031c3a4cd..b169c779db1 100644 --- a/packages/data-service/src/run-command.ts +++ b/packages/data-service/src/run-command.ts @@ -134,6 +134,11 @@ interface RunDiagnosticsCommand { spec: { dbStats: 1; scale?: number }, options?: RunCommandOptions ): Promise; + ( + db: Db, + spec: { atlasVersion: 1 }, + options?: RunCommandOptions + ): Promise; } export type ListDatabasesOptions = { @@ -196,6 +201,11 @@ export type ListCollectionsOptionsNamesOnly = Omit< nameOnly: true; }; +export type AtlasVersionInfo = { + version: string; + gitVersion: string; +}; + export type ListCollectionsResult = { cursor: { firstBatch: CollectionType }; }; diff --git a/packages/instance-model/lib/model.js b/packages/instance-model/lib/model.js index 84fbffbd3c9..2bb45c464e6 100644 --- a/packages/instance-model/lib/model.js +++ b/packages/instance-model/lib/model.js @@ -105,14 +105,9 @@ const InstanceModel = AmpersandModel.extend( databasesStatusError: { type: 'string', default: null }, refreshingStatus: { type: 'string', default: 'initial' }, refreshingStatusError: { type: 'string', default: null }, + isAtla: { tyep: 'boolean', default: false } }, derived: { - isAtlas: { - deps: ['hostname'], - fn() { - return /mongodb.net$/i.test(this.hostname); - }, - }, isRefreshing: { deps: ['refreshingStatus'], fn() { From 4a5cb7af0a75c37bc32eb0c6f8d5a8fbf2953050 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Tue, 23 Nov 2021 11:40:49 +0100 Subject: [PATCH 2/9] fix typos --- packages/instance-model/lib/model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/instance-model/lib/model.js b/packages/instance-model/lib/model.js index 2bb45c464e6..5675a8ce436 100644 --- a/packages/instance-model/lib/model.js +++ b/packages/instance-model/lib/model.js @@ -105,7 +105,7 @@ const InstanceModel = AmpersandModel.extend( databasesStatusError: { type: 'string', default: null }, refreshingStatus: { type: 'string', default: 'initial' }, refreshingStatusError: { type: 'string', default: null }, - isAtla: { tyep: 'boolean', default: false } + isAtlas: { type: 'boolean', default: false } }, derived: { isRefreshing: { From f7f8c89205b3d62d0e4b20dd53c4d6a2e74b07dd Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Tue, 23 Nov 2021 11:41:16 +0100 Subject: [PATCH 3/9] moves undefined on the right --- packages/data-service/src/instance-detail-helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/data-service/src/instance-detail-helper.ts b/packages/data-service/src/instance-detail-helper.ts index d18c6f1d3f3..e7f4ccf294b 100644 --- a/packages/data-service/src/instance-detail-helper.ts +++ b/packages/data-service/src/instance-detail-helper.ts @@ -151,7 +151,7 @@ function checkIsAtlas( ): boolean { const firstHost = client.options.hosts[0]?.host || ''; - if (undefined === atlasVersionInfo.version) { + if (atlasVersionInfo.version === undefined) { return /mongodb(-dev)?.net$/i.test(firstHost); } return true; From 8ab97147d5f562b0bf614087bd20e72e6ae3e6c5 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Wed, 24 Nov 2021 16:57:17 +0100 Subject: [PATCH 4/9] use new isAtalas dataservice instance property in telemetry --- packages/compass-connect/src/stores/index.js | 5 +++-- packages/data-service/src/instance-detail-helper.ts | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/compass-connect/src/stores/index.js b/packages/compass-connect/src/stores/index.js index 434831420f5..51dda264521 100644 --- a/packages/compass-connect/src/stores/index.js +++ b/packages/compass-connect/src/stores/index.js @@ -7,7 +7,7 @@ const StateMixin = require('reflux-state-mixin'); const { promisify } = require('util'); const { getConnectionTitle, convertConnectionModelToInfo } = require('mongodb-data-service'); const debug = require('debug')('compass-connect:store'); -const { isAtlas, isLocalhost, isDigitalOcean } = require('mongodb-build-info'); +const { isLocalhost, isDigitalOcean } = require('mongodb-build-info'); const { getCloudInfo } = require('mongodb-cloud-info'); const Actions = require('../actions'); @@ -977,6 +977,7 @@ const Store = Reflux.createStore({ genuineMongoDB, host, build, + isAtlas } = await dataService.instance(); const { hostname, @@ -993,7 +994,7 @@ const Store = Reflux.createStore({ const trackEvent = { is_localhost: isLocalhost(hostname), - is_atlas: isAtlas(hostname), + is_atlas: isAtlas, is_dataLake: dataLake.isDataLake, is_enterprise: build.isEnterprise, is_public_cloud: isPublicCloud, diff --git a/packages/data-service/src/instance-detail-helper.ts b/packages/data-service/src/instance-detail-helper.ts index e7f4ccf294b..fde8a93e81b 100644 --- a/packages/data-service/src/instance-detail-helper.ts +++ b/packages/data-service/src/instance-detail-helper.ts @@ -127,7 +127,7 @@ export async function getInstance( }).catch(() => null), runCommand(adminDb, { atlasVersion: 1 }).catch((err) => { - return { errmsg: err.message }; + return { version: '', gitVersion: '' }; }), ]); @@ -151,7 +151,7 @@ function checkIsAtlas( ): boolean { const firstHost = client.options.hosts[0]?.host || ''; - if (atlasVersionInfo.version === undefined) { + if (atlasVersionInfo.version === '') { return /mongodb(-dev)?.net$/i.test(firstHost); } return true; From e9e15524ad60fa6130621689e32d16ba35a41dde Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Thu, 25 Nov 2021 12:24:07 +0100 Subject: [PATCH 5/9] updates tests with more cases --- .../src/instance-detail-helper.spec.ts | 45 +++++++++++++++++++ .../src/instance-detail-helper.ts | 1 + packages/data-service/test/helpers.ts | 7 +++ 3 files changed, 53 insertions(+) diff --git a/packages/data-service/src/instance-detail-helper.spec.ts b/packages/data-service/src/instance-detail-helper.spec.ts index 15c8e2b0cca..e7823dd5130 100644 --- a/packages/data-service/src/instance-detail-helper.spec.ts +++ b/packages/data-service/src/instance-detail-helper.spec.ts @@ -245,6 +245,51 @@ describe('instance-detail-helper', function () { 'documentdb' ); }); + + ['myserver.mongodb.net', 'myserver.mongodb-dev.net'].map((hostname) => { + it(`should be identified as atlas with hostname ${hostname}`, async function () { + const client = createMongoClientMock({ + hosts: [{ host: hostname, port: 9999 }], + commands: { + buildInfo: {}, + getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + }, + }); + + const instanceDetails = await getInstance(client); + + expect(instanceDetails).to.have.property('isAtlas', true); + }); + }); + it(`should be identified as atlas when atlasVersion command is present`, async function () { + const client = createMongoClientMock({ + hosts: [{ host: 'fakehost.my.server.com', port: 9999 }], + commands: { + atlasVersion: { version: '1.1.1', gitVersion: '1.2.3' }, + buildInfo: {}, + getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + }, + }); + + const instanceDetails = await getInstance(client); + + expect(instanceDetails).to.have.property('isAtlas', true); + }); + + it(`should not be identified as atlas when atlasVersion command is missing`, async function () { + const client = createMongoClientMock({ + hosts: [{ host: 'fakehost.my.server.com', port: 9999 }], + commands: { + atlasVersion: new Error('command not found'), + buildInfo: {}, + getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + }, + }); + + const instanceDetails = await getInstance(client); + + expect(instanceDetails).to.have.property('isAtlas', false); + }); }); }); diff --git a/packages/data-service/src/instance-detail-helper.ts b/packages/data-service/src/instance-detail-helper.ts index fde8a93e81b..f2651c90e92 100644 --- a/packages/data-service/src/instance-detail-helper.ts +++ b/packages/data-service/src/instance-detail-helper.ts @@ -149,6 +149,7 @@ function checkIsAtlas( client: MongoClient, atlasVersionInfo: AtlasVersionInfo ): boolean { + console.log(client.options.hosts); const firstHost = client.options.hosts[0]?.host || ''; if (atlasVersionInfo.version === '') { diff --git a/packages/data-service/test/helpers.ts b/packages/data-service/test/helpers.ts index 56b8cd57d3b..9ce873826c9 100644 --- a/packages/data-service/test/helpers.ts +++ b/packages/data-service/test/helpers.ts @@ -1,17 +1,20 @@ import { Db, MongoClient } from 'mongodb'; type ClientMockOptions = { + hosts: [{ host: string; port: number }]; commands: Partial<{ connectionStatus: unknown; getCmdLineOpts: unknown; hostInfo: unknown; buildInfo: unknown; getParameter: unknown; + atlasVersion: unknown; }>; collections: Record; }; export function createMongoClientMock({ + hosts = [{ host: 'localhost', port: 9999 }], commands = {}, collections = {}, }: Partial = {}): MongoClient { @@ -25,6 +28,7 @@ export function createMongoClientMock({ 'hostInfo', 'buildInfo', 'getParameter', + 'atlasVersion', ].includes(key) ); @@ -67,6 +71,9 @@ export function createMongoClientMock({ }, }; }, + options: { + hosts, + }, }; return client as MongoClient; From 97f00db2a8074615ed3cebee1d6dc7ff63b82a90 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Thu, 25 Nov 2021 14:46:37 +0100 Subject: [PATCH 6/9] updates linting in tests --- package-lock.json | 2 +- .../src/instance-detail-helper.spec.ts | 32 ++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 933acd1dc61..c3104ee6f5e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -178726,7 +178726,7 @@ "version": "file:packages/data-service", "requires": { "@mongodb-js/compass-logging": "^0.4.0", - "@mongodb-js/devtools-docker-test-envs": "1.2.2", + "@mongodb-js/devtools-docker-test-envs": "^1.2.2", "@mongodb-js/eslint-config-compass": "^0.4.0", "@mongodb-js/mocha-config-compass": "^0.5.0", "@mongodb-js/prettier-config-compass": "^0.3.0", diff --git a/packages/data-service/src/instance-detail-helper.spec.ts b/packages/data-service/src/instance-detail-helper.spec.ts index e7823dd5130..d01c131f4a3 100644 --- a/packages/data-service/src/instance-detail-helper.spec.ts +++ b/packages/data-service/src/instance-detail-helper.spec.ts @@ -245,22 +245,24 @@ describe('instance-detail-helper', function () { 'documentdb' ); }); - - ['myserver.mongodb.net', 'myserver.mongodb-dev.net'].map((hostname) => { - it(`should be identified as atlas with hostname ${hostname}`, async function () { - const client = createMongoClientMock({ - hosts: [{ host: hostname, port: 9999 }], - commands: { - buildInfo: {}, - getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, - }, - }); - - const instanceDetails = await getInstance(client); - - expect(instanceDetails).to.have.property('isAtlas', true); - }); + it(`should be identified as atlas with hostname correct hostnames`, function () { + ['myserver.mongodb.net', 'myserver.mongodb-dev.net'].map( + async (hostname) => { + const client = createMongoClientMock({ + hosts: [{ host: hostname, port: 9999 }], + commands: { + buildInfo: {}, + getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + }, + }); + + const instanceDetails = await getInstance(client); + + expect(instanceDetails).to.have.property('isAtlas', true); + } + ); }); + it(`should be identified as atlas when atlasVersion command is present`, async function () { const client = createMongoClientMock({ hosts: [{ host: 'fakehost.my.server.com', port: 9999 }], From a6ee2f6a0d0483137638efb1dc856d7d8c882185 Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Thu, 25 Nov 2021 14:54:22 +0100 Subject: [PATCH 7/9] removes console.log and updates regexp --- packages/data-service/src/instance-detail-helper.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/data-service/src/instance-detail-helper.ts b/packages/data-service/src/instance-detail-helper.ts index f2651c90e92..17778302cfd 100644 --- a/packages/data-service/src/instance-detail-helper.ts +++ b/packages/data-service/src/instance-detail-helper.ts @@ -149,11 +149,10 @@ function checkIsAtlas( client: MongoClient, atlasVersionInfo: AtlasVersionInfo ): boolean { - console.log(client.options.hosts); const firstHost = client.options.hosts[0]?.host || ''; if (atlasVersionInfo.version === '') { - return /mongodb(-dev)?.net$/i.test(firstHost); + return /mongodb(-dev)?\.net$/i.test(firstHost); } return true; } From 17cc50f3d0e7a9de390ce1408521dba778b633da Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Thu, 25 Nov 2021 17:46:11 +0100 Subject: [PATCH 8/9] checks isAtlas from instance object --- packages/compass-metrics/src/modules/rules.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/compass-metrics/src/modules/rules.js b/packages/compass-metrics/src/modules/rules.js index 7dacaf2cfb4..972b015334c 100644 --- a/packages/compass-metrics/src/modules/rules.js +++ b/packages/compass-metrics/src/modules/rules.js @@ -2,7 +2,6 @@ import schemaStats from 'mongodb-schema/lib/stats'; import { getCloudInfo } from 'mongodb-cloud-info'; import ConnectionString from 'mongodb-connection-string-url'; -const ATLAS = /mongodb.net[:/]/i; const LOCALHOST = /(^localhost)|(^127\.0\.0\.1)/gi; async function getCloudInfoFromDataService(dataService) { @@ -83,7 +82,7 @@ const RULES = [ 'server name': state.instance.genuineMongoDB === undefined ? 'mongodb' : state.instance.genuineMongoDB.dbType, 'is data lake': state.instance.dataLake === undefined ? false : state.instance.dataLake.isDataLake, 'data lake version': state.instance.dataLake === undefined ? null : state.instance.dataLake.version, - is_atlas: !!state.instance._id.match(ATLAS), + is_atlas: state.instance.isAtlas, is_localhost: !!state.instance._id.match(LOCALHOST), compass_version: version, ...cloudInfo From b9795cd94c39c628c1083b5f633e49b6a8eb861f Mon Sep 17 00:00:00 2001 From: Leonardo Rossi Date: Mon, 29 Nov 2021 09:38:48 +0100 Subject: [PATCH 9/9] uses standard command line options in tests --- packages/data-service/src/instance-detail-helper.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/data-service/src/instance-detail-helper.spec.ts b/packages/data-service/src/instance-detail-helper.spec.ts index d01c131f4a3..76c78be3869 100644 --- a/packages/data-service/src/instance-detail-helper.spec.ts +++ b/packages/data-service/src/instance-detail-helper.spec.ts @@ -252,7 +252,7 @@ describe('instance-detail-helper', function () { hosts: [{ host: hostname, port: 9999 }], commands: { buildInfo: {}, - getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + getCmdLineOpts: fixtures.CMD_LINE_OPTS, }, }); @@ -269,7 +269,7 @@ describe('instance-detail-helper', function () { commands: { atlasVersion: { version: '1.1.1', gitVersion: '1.2.3' }, buildInfo: {}, - getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + getCmdLineOpts: fixtures.CMD_LINE_OPTS, }, }); @@ -284,7 +284,7 @@ describe('instance-detail-helper', function () { commands: { atlasVersion: new Error('command not found'), buildInfo: {}, - getCmdLineOpts: fixtures.DOCUMENTDB_CMD_LINE_OPTS, + getCmdLineOpts: fixtures.CMD_LINE_OPTS, }, });