Skip to content

Commit

Permalink
fix(shell-api): coerce values in getShardDistribution to JS number MO…
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax authored Nov 30, 2022
1 parent 01194da commit 8df1a78
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
markAsExplainOutput,
assertArgsDefinedType,
isValidCollectionName,
shouldRunAggregationImmediately
shouldRunAggregationImmediately,
coerceToJSNumber
} from './helpers';
import {
AnyBulkWriteOperation,
Expand Down Expand Up @@ -1721,17 +1722,16 @@ export default class Collection extends ShellApiWithMongoClass {
(shardStats.numChunks === 0) ? 0 : Math.floor(shardStats.count / shardStats.numChunks);

result[key] = {
data: dataFormat(shardStats.size),
data: dataFormat(coerceToJSNumber(shardStats.size)),
docs: shardStats.count,
chunks: shardStats.numChunks,
'estimated data per chunk': dataFormat(estChunkData),
'estimated docs per chunk': estChunkCount
};


totals.size += shardStats.size;
totals.count += shardStats.count;
totals.numChunks += shardStats.numChunks;
totals.size += coerceToJSNumber(shardStats.size);
totals.count += coerceToJSNumber(shardStats.count);
totals.numChunks += coerceToJSNumber(shardStats.numChunks);

conciseShardsStats.push(shardStats);
})()
Expand Down
12 changes: 11 additions & 1 deletion packages/shell-api/src/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertArgsDefinedType, dataFormat, getPrintableShardStatus } from './helpers';
import { assertArgsDefinedType, coerceToJSNumber, dataFormat, getPrintableShardStatus } from './helpers';
import { Database, Mongo, ShellInstanceState } from './index';
import constructShellBson from './shell-bson';
import { ServiceProvider, bson } from '@mongosh/service-provider-core';
Expand Down Expand Up @@ -219,3 +219,13 @@ describe('getPrintableShardStatus', () => {
expect.fail('missed exception');
});
});

describe('coerceToJSNumber', () => {
it('converts various BSON types to JS numbers', () => {
expect(coerceToJSNumber(0)).to.equal(0);
expect(coerceToJSNumber(new bson.Int32(0))).to.equal(0);
expect(coerceToJSNumber(new bson.Long(0))).to.equal(0);
expect(coerceToJSNumber(new bson.Long('9223372036854775807'))).to.equal(9223372036854776000);
expect(coerceToJSNumber(new bson.Double(1e30))).to.equal(1e30);
});
});
8 changes: 7 additions & 1 deletion packages/shell-api/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type Database from './database';
import type Collection from './collection';
import { CursorIterationResult } from './result';
import { ShellApiErrors } from './error-codes';
import { BinaryType, ReplPlatform } from '@mongosh/service-provider-core';
import { BinaryType, ReplPlatform, bson } from '@mongosh/service-provider-core';
import { ClientSideFieldLevelEncryptionOptions } from './field-level-encryption';
import { AutoEncryptionOptions } from 'mongodb';
import { shellApiType } from './enums';
Expand Down Expand Up @@ -499,6 +499,12 @@ export async function getConfigDB(db: Database): Promise<Database> {
return db.getSiblingDB('config');
}

type AnyBsonNumber = number | typeof bson.Long.prototype | typeof bson.Int32.prototype | typeof bson.Double.prototype;
export function coerceToJSNumber(n: AnyBsonNumber): number {
if (typeof n === 'number') {return n;}
return 'toNumber' in n ? n.toNumber() : n.valueOf();
}

export function dataFormat(bytes?: number): string {
if (bytes === null || bytes === undefined) {
return '0B';
Expand Down

0 comments on commit 8df1a78

Please sign in to comment.