All files / src/stores store.js

47.06% Statements 24/51
19.23% Branches 5/26
30.77% Functions 4/13
47.06% Lines 24/51
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133    1x     1x   1x   1x   1x   1x   1x   1x   1x   1x   4x         1x         1x                                                                                                                                                                                   7x                  
import Reflux from 'reflux';
import StateMixin from 'reflux-state-mixin';
import toNS from 'mongodb-ns';
import numeral from 'numeral';
 
/**
 * Invalid stats.
 */
const INVALID = 'N/A';
 
/**
 * Collection Stats store.
 */
const CollectionStatsStore = Reflux.createStore({
  /**
   * adds a state to the store, similar to React.Component's state
   * @see https://github.com/yonatanmn/Super-Simple-Flux#reflux-state-mixin
   *
   * If you call `this.setState({...})` this will cause the store to trigger
   * and push down its state as props to connected components.
   */
  mixins: [StateMixin.store],
 
  /**
   * This method is called when all plugins are activated. You can register
   * listeners to other plugins' stores here, e.g.
   *
   * appRegistry.getStore('OtherPlugin.Store').listen(this.otherStoreChanged.bind(this));
   *
   * If this plugin does not depend on other stores, you can delete the method.
   *
   * @param {Object} appRegistry - app registry containing all stores and components
   */
  // eslint-disable-next-line no-unused-vars
  onActivated(appRegistry) {
    this.NamespaceStore = appRegistry.getStore('App.NamespaceStore');
    this.CollectionStore = appRegistry.getStore('App.CollectionStore');
    appRegistry.getAction('CRUD.Actions').documentRemoved.listen(this.onDocumentDeleted.bind(this));
    appRegistry.on('data-service-connected', this.onConnected.bind(this));
    appRegistry.on('collection-changed', this.onCollectionChanged.bind(this));
  },
 
  /**
   * Handle the data-service-connected event.
   *
   * @param {Error} err - The error.
   * @param {DataService} dataService - The data service.
   */
  onConnected(err, dataService) {
    if (!err) {
      this.dataService = dataService;
    }
  },
 
  /**
   * Handle document deletion.
   */
  onDocumentDeleted() {
    this.loadCollectionStats(this.NamespaceStore.ns);
  },
 
  /**
   * Handle collection being changed.
   *
   * @param {String} ns - The namespace.
   */
  onCollectionChanged(ns) {
    this.loadCollectionStats(ns);
  },

  /**
   * Load the collection stats.
   *
   * @param {String} ns - The namespace.
   */
  loadCollectionStats(ns) {
    if (toNS(ns || '').collection) {
      if (this.CollectionStore.isReadonly()) {
        this.setState(this.getInitialState());
      } else {
        this.dataService.collection(ns, {}, (err, result) => {
          if (!err) {
            this.setState(this._parseCollectionDetails(result));
          }
        });
      }
    }
  },
 
  /**
   * Initialize the Collection Stats store state. The returned object must
   * contain all keys that you might want to modify with this.setState().
   *
   * @return {Object} initial store state.
   */
  getInitialState() {
    return {
      documentCount: INVALID,
      totalDocumentSize: INVALID,
      avgDocumentSize: INVALID,
      indexCount: INVALID,
      totalIndexSize: INVALID,
      avgIndexSize: INVALID
    };
  },
 
  _parseCollectionDetails(result) {
    return {
      documentCount: this._format(result.document_count),
      totalDocumentSize: this._format(result.document_size, 'b'),
      avgDocumentSize: this._format(this._avg(result.document_size, result.document_count), 'b'),
      indexCount: this._format(result.index_count),
      totalIndexSize: this._format(result.index_size, 'b'),
      avgIndexSize: this._format(this._avg(result.index_size, result.index_count), 'b')
    };
  },
 
  _avg(size, count) {
    if (count <= 0) {
      return 0;
    }
    return size / count;
  },
 
  _format(value, format = 'a') {
    const precision = value <= 1000 ? '0' : '0.0';
    return numeral(value).format(precision + format);
  }
});
 
export default CollectionStatsStore;
export { CollectionStatsStore };