From 5bca079e0e94910438b6696dc53286365d615ebc Mon Sep 17 00:00:00 2001 From: Anemy Date: Mon, 28 Jun 2021 15:11:48 -0400 Subject: [PATCH 1/3] Update how we show collection info on time-series collections --- .../collection-stats-item.jsx | 7 +- .../collection-stats/collection-stats.jsx | 43 ++++--- .../collection-stats/collection-stats.spec.js | 80 +++++++++--- .../document-stats-item.jsx | 17 ++- .../document-stats-item.spec.js | 114 ++++++++++++------ .../index-stats-item/index-stats-item.jsx | 3 +- .../compass-collection-stats/src/index.js | 8 +- .../src/stores/store.js | 49 ++++---- .../src/stores/store.spec.js | 51 +++----- 9 files changed, 226 insertions(+), 146 deletions(-) diff --git a/packages/compass-collection-stats/src/components/collection-stats-item/collection-stats-item.jsx b/packages/compass-collection-stats/src/components/collection-stats-item/collection-stats-item.jsx index 91000d4b4f0..9ff1aa47cda 100644 --- a/packages/compass-collection-stats/src/components/collection-stats-item/collection-stats-item.jsx +++ b/packages/compass-collection-stats/src/components/collection-stats-item/collection-stats-item.jsx @@ -1,6 +1,5 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import classnames from 'classnames'; import styles from './collection-stats-item.less'; @@ -48,11 +47,11 @@ class CollectionStatsItem extends Component { */ render() { return ( -
-
+
+
{this.props.label}
-
+
{this.props.value}
diff --git a/packages/compass-collection-stats/src/components/collection-stats/collection-stats.jsx b/packages/compass-collection-stats/src/components/collection-stats/collection-stats.jsx index 41568ba2983..9ed77f3d502 100644 --- a/packages/compass-collection-stats/src/components/collection-stats/collection-stats.jsx +++ b/packages/compass-collection-stats/src/components/collection-stats/collection-stats.jsx @@ -1,25 +1,25 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; +import DocumentStatsItem from '../document-stats-item'; +import IndexStatsItem from '../index-stats-item'; + import styles from './collection-stats.less'; class CollectionStats extends Component { static displayName = 'CollectionStatsComponent'; static propTypes = { - isReadonly: PropTypes.bool + documentCount: PropTypes.string.isRequired, + totalDocumentSize: PropTypes.string.isRequired, + avgDocumentSize: PropTypes.string.isRequired, + indexCount: PropTypes.string.isRequired, + totalIndexSize: PropTypes.string.isRequired, + avgIndexSize: PropTypes.string.isRequired, + isReadonly: PropTypes.bool.isRequired, + isTimeSeries: PropTypes.bool.isRequired }; - /** - * Instantiate the component. - * - * @param {Object} props - The properties. - */ - constructor(props) { - super(props); - this.roles = global.hadronApp.appRegistry.getRole('CollectionHUD.Item'); - } - /** * Render CollectionStats component. * @@ -30,10 +30,23 @@ class CollectionStats extends Component { return
; } - const children = (this.roles || []).map((role, i) => { - return ; - }); - return
{children}
; + return ( +
+ + {!this.props.isTimeSeries && ( + + )} +
+ ); } } diff --git a/packages/compass-collection-stats/src/components/collection-stats/collection-stats.spec.js b/packages/compass-collection-stats/src/components/collection-stats/collection-stats.spec.js index b881471bf13..de2c93ff5c4 100644 --- a/packages/compass-collection-stats/src/components/collection-stats/collection-stats.spec.js +++ b/packages/compass-collection-stats/src/components/collection-stats/collection-stats.spec.js @@ -3,40 +3,86 @@ import { mount } from 'enzyme'; import AppRegistry from 'hadron-app-registry'; import CollectionStats from '../collection-stats'; +import DocumentStatsItem from '../document-stats-item'; +import IndexStatsItem from '../index-stats-item'; import styles from './collection-stats.less'; describe('CollectionStats [Component]', () => { - let component; + describe('when rendered', () => { + let component; - beforeEach(() => { - global.hadronApp = { - appRegistry: new AppRegistry() - }; - component = mount(); - }); + beforeEach(() => { + global.hadronApp = { + appRegistry: new AppRegistry() + }; + component = mount(); + }); - afterEach(() => { - component = null; - }); + afterEach(() => { + component = null; + }); + + it('renders the correct root classname', () => { + expect(component.find(`.${styles['collection-stats']}`)).to.be.present(); + }); - it('renders the correct root classname', () => { - expect(component.find(`.${styles['collection-stats']}`)).to.be.present(); + it('renders the document and index stats', () => { + expect(component.find(DocumentStatsItem)).to.be.present(); + expect(component.find(IndexStatsItem)).to.be.present(); + }); }); describe('When the collection is a view', () => { - let _component; + let component; before(() => { - _component = mount(); + component = mount(); }); - it('should hide the stats', () => { + it('renders an empty state', () => { expect( - _component.find(`.${styles['collection-stats-empty']}`) + component.find(`.${styles['collection-stats-empty']}`) ).to.be.present(); }); + it('does not render the document and index stats', () => { + expect(component.find(DocumentStatsItem)).to.not.be.present(); + expect(component.find(IndexStatsItem)).to.not.be.present(); + }); + after(() => { - _component = null; + component = null; + }); + }); + + describe('when the collection is a time-series collection', () => { + let component; + + beforeEach(() => { + global.hadronApp = { + appRegistry: new AppRegistry() + }; + component = mount(); + }); + + afterEach(() => { + component = null; + }); + + it('renders the document stats', () => { + expect(component.find(DocumentStatsItem)).to.be.present(); + }); + + it('does not render the index stats', () => { + expect(component.find(IndexStatsItem)).to.not.be.present(); }); }); }); diff --git a/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.jsx b/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.jsx index 8b46d7046c2..9ce6602b5f9 100644 --- a/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.jsx +++ b/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.jsx @@ -1,6 +1,5 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import classnames from 'classnames'; import CollectionStatsItem from '../collection-stats-item'; import styles from './document-stats-item.less'; @@ -33,6 +32,7 @@ class DocumentStatsItem extends Component { static propTypes = { documentCount: PropTypes.string.isRequired, + isTimeSeries: PropTypes.bool.isRequired, totalDocumentSize: PropTypes.string.isRequired, avgDocumentSize: PropTypes.string.isRequired }; @@ -44,11 +44,20 @@ class DocumentStatsItem extends Component { * */ render() { + const { isTimeSeries } = this.props; + return ( -
- +
+ {!isTimeSeries && } - + {!isTimeSeries && }
); } diff --git a/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.spec.js b/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.spec.js index 882c2618922..86fff660ddd 100644 --- a/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.spec.js +++ b/packages/compass-collection-stats/src/components/document-stats-item/document-stats-item.spec.js @@ -6,50 +6,94 @@ import styles from './document-stats-item.less'; import statsStyles from '../collection-stats-item/collection-stats-item.less'; describe('DocumentStatsItem [Component]', () => { - let component; + describe('when rendered', () => { + let component; - beforeEach(() => { - component = mount( - - ); - }); + beforeEach(() => { + component = mount( + + ); + }); - afterEach(() => { - component = null; - }); + afterEach(() => { + component = null; + }); - it('renders the correct root classname', () => { - expect(component.find(`.${styles['document-stats-item']}`)). - to.be.present(); - }); + it('renders the correct root classname', () => { + expect(component.find(`.${styles['document-stats-item']}`)). + to.be.present(); + }); - it('renders the count as primary', () => { - expect(component.find(`.${statsStyles['collection-stats-item-primary-label']}`)). - to.have.text('Documents'); - }); + it('renders the count as primary', () => { + expect(component.find(`.${statsStyles['collection-stats-item-primary-label']}`)). + to.have.text('Documents'); + }); - it('renders the count as primary value', () => { - expect(component.find(`.${statsStyles['collection-stats-item-primary-value']}`)). - to.have.text('10'); - }); + it('renders the count as primary value', () => { + expect(component.find(`.${statsStyles['collection-stats-item-primary-value']}`)). + to.have.text('10'); + }); - it('renders total document size as non primary label', () => { - expect(component.find(`.${statsStyles['collection-stats-item-label']}`).at(0)). - to.have.text('total size'); - }); + it('renders total document size as non primary label', () => { + expect(component.find(`.${statsStyles['collection-stats-item-label']}`).at(0)). + to.have.text('total size'); + }); - it('renders avg document size as a non primary value', () => { - expect(component.find(`.${statsStyles['collection-stats-item-value']}`).at(0)). - to.have.text('5kb'); - }); + it('renders avg document size as a non primary value', () => { + expect(component.find(`.${statsStyles['collection-stats-item-value']}`).at(0)). + to.have.text('5kb'); + }); - it('renders avg document size as non primary label', () => { - expect(component.find(`.${statsStyles['collection-stats-item-label']}`).at(1)). - to.have.text('avg. size'); + it('renders avg document size as non primary label', () => { + expect(component.find(`.${statsStyles['collection-stats-item-label']}`).at(1)). + to.have.text('avg. size'); + }); + + it('renders avg document size as a non primary value', () => { + expect(component.find(`.${statsStyles['collection-stats-item-value']}`).at(1)). + to.have.text('1k'); + }); }); - it('renders avg document size as a non primary value', () => { - expect(component.find(`.${statsStyles['collection-stats-item-value']}`).at(1)). - to.have.text('1k'); + describe('when time-series is true', () => { + let component; + + beforeEach(() => { + component = mount( + + ); + }); + + afterEach(() => { + component = null; + }); + + it('does not render the count', () => { + expect( + component.find(`.${statsStyles['collection-stats-item-primary-value']}`) + ).to.not.be.present(); + }); + + it('renders total document size', () => { + expect( + component.find(`.${statsStyles['collection-stats-item-label']}`).at(0) + ).to.be.present(); + }); + + it('renders avg document size', () => { + expect( + component.find(`.${statsStyles['collection-stats-item-value']}`).length + ).to.equal(1); + }); }); }); diff --git a/packages/compass-collection-stats/src/components/index-stats-item/index-stats-item.jsx b/packages/compass-collection-stats/src/components/index-stats-item/index-stats-item.jsx index 5158e504f92..3f5300daa11 100644 --- a/packages/compass-collection-stats/src/components/index-stats-item/index-stats-item.jsx +++ b/packages/compass-collection-stats/src/components/index-stats-item/index-stats-item.jsx @@ -1,6 +1,5 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import classnames from 'classnames'; import CollectionStatsItem from '../collection-stats-item'; import styles from './index-stats-item.less'; @@ -45,7 +44,7 @@ class IndexStatsItem extends Component { */ render() { return ( -
+
diff --git a/packages/compass-collection-stats/src/index.js b/packages/compass-collection-stats/src/index.js index ea06984a6ba..77cae11bbf3 100644 --- a/packages/compass-collection-stats/src/index.js +++ b/packages/compass-collection-stats/src/index.js @@ -25,22 +25,18 @@ const INDEXES_STATS_ITEM_ROLE = { }; /** - * Activate all the components in the Collection Stats package. + * Activate the Collection Stats plugin. * @param {Object} appRegistry - The Hadron appRegisrty to activate this plugin with. **/ function activate(appRegistry) { - appRegistry.registerRole('CollectionHUD.Item', DOCUMENTS_STATS_ITEM_ROLE); - appRegistry.registerRole('CollectionHUD.Item', INDEXES_STATS_ITEM_ROLE); appRegistry.registerRole('Collection.HUD', COLLECTION_HUD_ROLE); } /** - * Deactivate all the components in the Collection Stats package. + * Deactivate the Collection Stats plugin. * @param {Object} appRegistry - The Hadron appRegisrty to deactivate this plugin with. **/ function deactivate(appRegistry) { - appRegistry.deregisterRole('CollectionHUD.Item', DOCUMENTS_STATS_ITEM_ROLE); - appRegistry.deregisterRole('CollectionHUD.Item', INDEXES_STATS_ITEM_ROLE); appRegistry.deregisterRole('Collection.HUD', COLLECTION_HUD_ROLE); } diff --git a/packages/compass-collection-stats/src/stores/store.js b/packages/compass-collection-stats/src/stores/store.js index 0ae16079724..8d1c0144d85 100644 --- a/packages/compass-collection-stats/src/stores/store.js +++ b/packages/compass-collection-stats/src/stores/store.js @@ -41,6 +41,16 @@ export const setIsReadonly = (store, isReadonly) => { store.setState({ isReadonly: isReadonly }); }; +/** + * Set the isTimeSeries flag in the store. + * + * @param {Store} store - The store. + * @param {Boolean} isTimeSeries - If the collection is a time-series collection. + */ +export const setIsTimeSeries = (store, isTimeSeries) => { + store.setState({ isTimeSeries }); +}; + /** * Set the namespace in the store. * @@ -124,40 +134,17 @@ const configureStore = (options = {}) => { this.setState(this.getInitialState()); }, - ensureDocumentCount(result, cb) { - if (result.document_count !== undefined) { - return cb(null, result); - } - - this.dataService.estimatedCount(this.ns, {}, (err, estimatedCount) => { - if (err) { - return cb(null, result); // ignore the error - } - - cb(null, { - ...result, - document_count: estimatedCount - }); - }); - }, - fetchCollectionDetails() { this.dataService.collection(this.ns, {}, (collectionError, result) => { if (collectionError) { return this.handleFetchError(collectionError); } - this.ensureDocumentCount(result, (ensureDocumentCountError, resultWithDocumentCount) => { - if (ensureDocumentCountError) { - return this.handleFetchError(ensureDocumentCountError); - } - - const details = this._parseCollectionDetails(resultWithDocumentCount); - this.setState(details); - if (this.globalAppRegistry) { - this.globalAppRegistry.emit('compass:collection-stats:loaded', details); - } - }); + const details = this._parseCollectionDetails(result); + this.setState(details); + if (this.globalAppRegistry) { + this.globalAppRegistry.emit('compass:collection-stats:loaded', details); + } }); }, @@ -170,6 +157,7 @@ const configureStore = (options = {}) => { getInitialState() { return { isReadonly: false, + isTimeSeries: false, documentCount: INVALID, totalDocumentSize: INVALID, avgDocumentSize: INVALID, @@ -188,6 +176,7 @@ const configureStore = (options = {}) => { _parseCollectionDetails(result) { return { isReadonly: this.state.isReadonly || false, + isTimeSeries: this.state.isTimeSeries || false, documentCount: result.document_count !== undefined ? this._format(result.document_count) : INVALID, totalDocumentSize: this._format(result.document_size, 'b'), avgDocumentSize: this._format(this._avg(result.document_size, result.document_count), 'b'), @@ -268,6 +257,10 @@ const configureStore = (options = {}) => { setIsReadonly(store, options.isReadonly); } + if (options.isTimeSeries) { + setIsTimeSeries(store, options.isTimeSeries); + } + // Set the namespace - must happen third. if (options.namespace) { setNamespace(store, options.namespace); diff --git a/packages/compass-collection-stats/src/stores/store.spec.js b/packages/compass-collection-stats/src/stores/store.spec.js index 8b8771579a9..9b410f45957 100644 --- a/packages/compass-collection-stats/src/stores/store.spec.js +++ b/packages/compass-collection-stats/src/stores/store.spec.js @@ -16,6 +16,10 @@ describe('CollectionStatsstore [store]', () => { expect(store.state.isReadonly).to.be.false; }); + it('defaults isTimeSeries to false', () => { + expect(store.state.isTimeSeries).to.be.false; + }); + it('defaults document count to invalid', () => { expect(store.state.documentCount).to.be.equal('N/A'); }); @@ -81,15 +85,12 @@ describe('CollectionStatsstore [store]', () => { context('when providing a data provider', () => { let store; - let estimatedCountStub; let collectionStub; let dataService; beforeEach(() => { - estimatedCountStub = sinon.stub(); collectionStub = sinon.stub(); dataService = { - estimatedCount: estimatedCountStub, collection: collectionStub, isConnected: () => true }; @@ -119,20 +120,6 @@ describe('CollectionStatsstore [store]', () => { expect(store.state.documentCount).to.equal('123'); }); - it('re-fetch the document count if was not in stats (support hack for time-series in v5.0)', () => { - collectionStub.callsFake((ns, options, cb) => { - cb(null, {}); - }); - - estimatedCountStub.callsFake((ns, options, cb) => { - cb(null, 123); - }); - - store.loadCollectionStats(); - - expect(store.state.documentCount).to.equal('123'); - }); - it('resets the state in case of error (collection stats)', () => { collectionStub.callsFake((ns, options, cb) => { cb(new Error('failed')); @@ -145,24 +132,6 @@ describe('CollectionStatsstore [store]', () => { expect(store.state.documentCount).to.equal('N/A'); expect(store.dataService).to.equal(dataService); }); - - it('resets only the documentCount in case of error on estimated count', () => { - collectionStub.callsFake((ns, options, cb) => { - cb(null, { - index_count: 123 - }); - }); - - estimatedCountStub.callsFake((ns, options, cb) => { - cb(new Error('failed')); - }); - - store.loadCollectionStats(); - - expect(store.state.indexCount).to.equal('123'); - expect(store.state.documentCount).to.equal('N/A'); - expect(store.dataService).to.equal(dataService); - }); }); context('when providing is readonly', () => { @@ -177,6 +146,18 @@ describe('CollectionStatsstore [store]', () => { }); }); + context('when providing is time-series', () => { + let store; + + beforeEach(() => { + store = configureStore({ isTimeSeries: true }); + }); + + it('sets the is time-series value on the store', () => { + expect(store.state.isTimeSeries).to.equal(true); + }); + }); + context('wnen providing a namespace', () => { let store; From 409190d3150404dcde0ba1ea1409804582a70c7e Mon Sep 17 00:00:00 2001 From: Anemy Date: Mon, 28 Jun 2021 15:12:38 -0400 Subject: [PATCH 2/3] Remove commented code --- packages/compass-collection-stats/src/index.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/packages/compass-collection-stats/src/index.js b/packages/compass-collection-stats/src/index.js index 77cae11bbf3..d3e62b0fbdc 100644 --- a/packages/compass-collection-stats/src/index.js +++ b/packages/compass-collection-stats/src/index.js @@ -1,6 +1,4 @@ import CollectionStatsPlugin from './plugin'; -import DocumentStatsItem from './components/document-stats-item'; -import IndexStatsItem from './components/index-stats-item'; import configureStore from './stores'; const COLLECTION_HUD_ROLE = { @@ -12,18 +10,6 @@ const COLLECTION_HUD_ROLE = { storeName: 'CollectionStats.Store' }; -const DOCUMENTS_STATS_ITEM_ROLE = { - component: DocumentStatsItem, - name: 'document-stats', - order: 1 -}; - -const INDEXES_STATS_ITEM_ROLE = { - component: IndexStatsItem, - name: 'index-stats', - order: 2 -}; - /** * Activate the Collection Stats plugin. * @param {Object} appRegistry - The Hadron appRegisrty to activate this plugin with. From 31964da9ef45d42dd2e352923a75b920b879ea4a Mon Sep 17 00:00:00 2001 From: Anemy Date: Mon, 28 Jun 2021 15:37:45 -0400 Subject: [PATCH 3/3] remove unused dep :D --- package-lock.json | 2 -- packages/compass-collection-stats/package.json | 1 - 2 files changed, 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 77515f7b5d2..1eafd5b0684 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53940,7 +53940,6 @@ "bootstrap": "https://github.com/twbs/bootstrap/archive/v3.3.5.tar.gz", "chai": "^4.1.2", "chai-enzyme": "1.0.0-beta.1", - "classnames": "^2.2.5", "core-js": "^3.12.1", "cross-env": "^7.0.0", "css-loader": "^4.3.0", @@ -107241,7 +107240,6 @@ "bootstrap": "https://github.com/twbs/bootstrap/archive/v3.3.5.tar.gz", "chai": "^4.1.2", "chai-enzyme": "1.0.0-beta.1", - "classnames": "^2.2.5", "core-js": "^3.12.1", "cross-env": "^7.0.0", "css-loader": "^4.3.0", diff --git a/packages/compass-collection-stats/package.json b/packages/compass-collection-stats/package.json index c3e3e545c95..05fe5882c31 100644 --- a/packages/compass-collection-stats/package.json +++ b/packages/compass-collection-stats/package.json @@ -53,7 +53,6 @@ "bootstrap": "https://github.com/twbs/bootstrap/archive/v3.3.5.tar.gz", "chai": "^4.1.2", "chai-enzyme": "1.0.0-beta.1", - "classnames": "^2.2.5", "core-js": "^3.12.1", "cross-env": "^7.0.0", "css-loader": "^4.3.0",