Mosca lib/stats.js

    Description

    Copyright (c) 2013-2014 Matteo Collina, http://matteocollina.com

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

    Source

    "use strict";
    
    var moment = require("moment");
    var movingAverage = require("moving-average");
    var version = "mosca " + require("../package").version;

    Stats

    function
    Stats()

      Description

      A Stats object is used to keep track of the state of a mosca.Server
      and can be wired() there.

      It provides the following stats:
      - connectedClients: the number of connected clients at this point in time
      - publishedMessages: the number of publish messages received since the the start

      It also track the load at 1min, 5min, and 15min of the same events.

      Source

      function Stats() {
        if (!(this instanceof Stats)) {
          return new Stats();
        }
      
        this.connectedClients = 0;
        this.publishedMessages = 0;
        this.lastIntervalPublishedMessages = 0;
        this.started = new Date();
      
        this.load = {
          m15: new Load(15),
          m5: new Load(5),
          m1: new Load(1)
        };
      }

        Description

        shint validthis:true

        Source

        this.stats.publishedMessages++;
          this.stats.lastIntervalPublishedMessages++;
        }

        wire

        method
        Stats.prototype.wire()
        • @param: {Server}serverThe Mosca Server.

        Description

        wire() adds the stats to a mosca.Server.

        Source

        Stats.prototype.wire = function wire(server) {
          server.stats = this;
        
          var count = 0;
        
          function doPublish(topic, value) {
            server.publish({
              topic: "$SYS/" + server.id + "/" + topic,
              payload: "" + value
            });
          }
        
          var mom = moment(this.started);
        
          var timer = setInterval(function() {
            var stats = server.stats;
            var mem = process.memoryUsage();
        
            var date = new Date();
        
            stats.load.m1.maConnectedClients.push(date, stats.connectedClients);
            stats.load.m5.maConnectedClients.push(date, stats.connectedClients);
            stats.load.m15.maConnectedClients.push(date, stats.connectedClients);
        
            stats.load.m1.maPublishedMessages.push(date, stats.lastIntervalPublishedMessages);
            stats.load.m5.maPublishedMessages.push(date, stats.lastIntervalPublishedMessages);
            stats.load.m15.maPublishedMessages.push(date, stats.lastIntervalPublishedMessages);
            stats.lastIntervalPublishedMessages = 0;
        
            doPublish("version", version);
            doPublish("started_at", server.stats.started.toISOString());
            doPublish("uptime", mom.from(Date.now(), true));
            doPublish("clients/connected", stats.connectedClients);
            doPublish("publish/received", stats.publishedMessages);
            doPublish("load/connections/15min", stats.load.m15.connectedClients);
            doPublish("load/publish/received/15min", stats.load.m15.publishedMessages);
            doPublish("load/connections/5min", stats.load.m5.connectedClients);
            doPublish("load/publish/received/5min", stats.load.m5.publishedMessages);
            doPublish("load/connections/1min", stats.load.m1.connectedClients);
            doPublish("load/publish/received/1min", stats.load.m1.publishedMessages);
            doPublish("memory/rss", mem.rss);
            doPublish("memory/heap/current", mem.heapUsed);
            doPublish("memory/heap/maximum", mem.heapTotal);
          }, 10 * 1000);
        
          events.forEach(function(event) {
            server.on(event.name, event);
          });
        
          server.once("closed", function() {
            clearInterval(timer);
        
            events.forEach(function(event) {
              server.removeListener(event.name, event);
            });
          });
        };
        
        module.exports = Stats;