All files / src/controllers server.js

100% Statements 27/27
100% Branches 8/8
100% Functions 15/15
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1301x                       124x                   7x       6x 3x 3x 3x 3x   3x                       3x     2x                   3x     2x                   3x     2x                       6x         5x                   3x     2x                   6x       5x 3x 3x 3x 3x   2x         1x  
const BaseControler = require('./base');
 
/**
 * @class ServerController
 * @property {Kuzzle} kuzzle - The Kuzzle SDK Instance
 */
class ServerController extends BaseControler {
 
  /**
   * @param {Kuzzle} kuzzle - The Kuzzle SDK Instance
   */
  constructor (kuzzle) {
    super(kuzzle, 'server');
  }
 
  /**
   * Checks if an administrator user exists
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Boolean>}
   */
  adminExists (options) {
    return this.query({
      action: 'adminExists'
    }, options)
      .then(response => {
        if (typeof response.result !== 'object' || typeof response.result.exists !== 'boolean') {
          const error = new Error('adminExists: bad response format');
          error.status = 400;
          error.response = response;
          return Promise.reject(error);
        }
        return response.result.exists;
      });
  }
 
 
  /**
   * Returns all stored statistics frames
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Object>}
   */
  getAllStats (options) {
    return this.query({
      action: 'getAllStats'
    }, options)
      .then(response => response.result);
  }
 
  /**
   * Returns the Kuzzle configuration
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Object>}
   */
  getConfig (options) {
    return this.query({
      action: 'getConfig'
    }, options)
      .then(response => response.result);
  }
 
  /**
   * Returns the last statistics frame
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Object>}
   */
  getLastStats (options) {
    return this.query({
      action: 'getLastStats'
    }, options)
      .then(response => response.result);
  }
 
  /**
   * Returns the statistics frame from a date
   *
   * @param {Number|String} startTime - begining of statistics frame set (timestamp or datetime format)
   * @param {Number|String} stopTime - end of statistics frame set (timestamp or datetime format)
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Object>}
   */
  getStats(startTime, stopTime, options) {
    return this.query({
      action: 'getStats',
      startTime,
      stopTime
    }, options)
      .then(response => response.result);
  }
 
  /**
   * Returns the Kuzzle server information
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Object>}
   */
  info (options) {
    return this.query({
      action: 'info'
    }, options)
      .then(response => response.result);
  }
 
  /**
   * Get server's current timestamp
   *
   * @param {Object} options - {queuable: Boolean(true)}
   * @returns {Promise<Number>}
   */
  now (options) {
    return this.query({
      action: 'now'
    }, options)
      .then(response => {
        if (typeof response.result !== 'object' || typeof response.result.now !== 'number') {
          const error = new Error('now: bad response format');
          error.status = 400;
          error.response = response;
          return Promise.reject(error);
        }
        return response.result.now;
      });
  }
}
 
module.exports = ServerController;