Server stats/events module
authorDarren <darren@darrenwhitlen.com>
Sun, 27 Jul 2014 12:49:19 +0000 (13:49 +0100)
committerDarren <darren@darrenwhitlen.com>
Sun, 27 Jul 2014 12:49:19 +0000 (13:49 +0100)
server/client.js
server/httphandler.js
server/irc/connection.js
server/irc/server.js
server/stats.js [new file with mode: 0644]
server/weblistener.js
server/websocketrpc.js
server_modules/stats.js [new file with mode: 0644]

index 5f4b66542ccb6b30c1c0d3164ec9581a0773a5a9..73a0cb24e9b208a0256a429bf68d37182412c4d5 100755 (executable)
@@ -5,15 +5,25 @@ var util             = require('util'),
     State            = require('./irc/state.js'),
     IrcConnection    = require('./irc/connection.js').IrcConnection,
     ClientCommands   = require('./clientcommands.js'),
-    WebsocketRpc     = require('./websocketrpc.js');
+    WebsocketRpc     = require('./websocketrpc.js'),
+    Stats            = require('./stats.js');
 
 
 var Client = function (websocket) {
     var that = this;
 
+    Stats.incr('client.created');
+
     events.EventEmitter.call(this);
     this.websocket = websocket;
+
     this.rpc = new WebsocketRpc(this.websocket);
+    this.rpc.on('all', function(func_name, return_fn) {
+        if (typeof func_name === 'string' && typeof return_fn === 'function') {
+            Stats.incr('client.command');
+            Stats.incr('client.command.' + func_name);
+        }
+    });
 
     // Clients address
     this.real_address = this.websocket.meta.real_address;
@@ -72,6 +82,8 @@ Client.prototype.sendKiwiCommand = function (command, data, callback) {
 };
 
 Client.prototype.dispose = function () {
+    Stats.incr('client.disposed');
+
     this.disposed = true;
     this.rpc.dispose();
     this.emit('dispose');
index 3049972b0e50f229de597cf7420e5d5d7e0a2d05..c3fc067fa104efb60740d72a250c89aff44b41a4 100644 (file)
@@ -5,7 +5,8 @@ var url         = require('url'),
     _           = require('lodash'),
     config      = require('./configuration.js'),
     winston     = require('winston'),
-    SettingsGenerator = require('./settingsgenerator.js');
+    SettingsGenerator = require('./settingsgenerator.js'),
+    Stats       = require('./stats.js');
 
 
 
@@ -39,6 +40,10 @@ HttpHandler.prototype.serve = function (request, response) {
         request.url = '/index.html';
     }
 
+    if (request.url === '/index.html') {
+        Stats.incr('http.homepage');
+    }
+
     // If the 'magic' translation is requested, figure out the best language to use from
     // the Accept-Language HTTP header. If nothing is suitible, fallback to our en-gb default translation
     if (request.url.substr(0, 16) === '/assets/locales/') {
index 8b8e197fe66e4a78aa5dee0a3f8c36572612a549..3242452ec0c7df9efd61a615e90b6ab12036fc74 100644 (file)
@@ -12,6 +12,7 @@ var net             = require('net'),
     EE              = require('../ee.js'),
     iconv           = require('iconv-lite'),
     Proxy           = require('../proxy.js'),
+    Stats           = require('../stats.js'),
     Socks;
 
 
@@ -32,6 +33,8 @@ var IrcConnection = function (hostname, port, ssl, nick, user, options, state, c
     });
     this.setMaxListeners(0);
 
+    Stats.incr('irc.connection.created');
+
     options = options || {};
 
     // Socket state
@@ -281,6 +284,7 @@ IrcConnection.prototype.connect = function () {
                 rawSocketConnect.call(that, this);
             }
 
+            Stats.incr('irc.connection.connected');
             that.connected = true;
 
             socketConnectHandler.call(that);
@@ -321,9 +325,11 @@ IrcConnection.prototype.connect = function () {
             }
 
             if (should_reconnect) {
+                Stats.incr('irc.connection.reconnect');
                 that.reconnect_attempts++;
                 that.emit('reconnecting');
             } else {
+                Stats.incr('irc.connection.closed');
                 that.emit('close', had_error);
                 that.reconnect_attempts = 0;
             }
index 831e9bf58fb179de4dc51dfae63aeba27ac50422..42e32dff0b1e71886c74bb0419c125e384342725 100755 (executable)
@@ -1,6 +1,7 @@
 var util    = require('util'),
     EventBinder  = require('./eventbinder.js'),
-    _ = require('lodash');
+    _ = require('lodash'),
+    Stats = require('../stats.js');
 
 var IrcServer = function (irc_connection) {
     this.irc_connection = irc_connection;
@@ -61,6 +62,7 @@ IrcServer.prototype.reset = function() {
 
 
 function onConnect(event) {
+    Stats.incr('irc.connection.registered');
     this.registered = new Date();
 
     this.irc_connection.clientEvent('connect', {
diff --git a/server/stats.js b/server/stats.js
new file mode 100644 (file)
index 0000000..2898482
--- /dev/null
@@ -0,0 +1,5 @@
+module.exports = {
+    incr: function incr(stat_name) {
+        global.modules.emit('stat counter', {name: stat_name});
+    }
+};
\ No newline at end of file
index fcaeb7cca4a112ca9a2f1417cd9f0d12b8e537b0..475f017257b1e1052a74dbc2e6f9ecb9b2e80937 100644 (file)
@@ -13,7 +13,8 @@ var engine       = require('engine.io'),
     winston      = require('winston'),
     Client       = require('./client.js').Client,
     HttpHandler  = require('./httphandler.js').HttpHandler,
-    rehash       = require('./rehash.js');
+    rehash       = require('./rehash.js'),
+    Stats        = require('./stats.js');
 
 
 
@@ -86,6 +87,8 @@ var WebListener = module.exports = function (web_config) {
     hs.on('request', function(req, res){
         var transport_url = (global.config.http_base_path || '') + '/transport';
 
+        Stats.incr('http.request');
+
         // engine.io can sometimes "loose" the clients remote address. Keep note of it
         req.meta = {
             remote_address: req.connection.remoteAddress
@@ -102,6 +105,8 @@ var WebListener = module.exports = function (web_config) {
     });
 
     this.ws.on('connection', function(socket) {
+        Stats.incr('http.websocket');
+
         initialiseSocket(socket, function(err, authorised) {
             var client;
 
index 1fa035a4140986ac5935f48d6e9b14543e8cbd05..5f7c56e7a7bacaa3d10cb93242baaa1670aca468 100644 (file)
@@ -152,6 +152,7 @@ WebsocketRpc.prototype._onMessage = function(message_raw) {
             returnFn = this._noop;
         }
 
+        this.emit.apply(this, ['all', packet.method, returnFn].concat(packet.params));
         this.emit.apply(this, [packet.method, returnFn].concat(packet.params));
     }
 };
diff --git a/server_modules/stats.js b/server_modules/stats.js
new file mode 100644 (file)
index 0000000..b91eb33
--- /dev/null
@@ -0,0 +1,30 @@
+/**
+ * Stats counter
+ *
+ * Retreive stats for internal kiwi events. Handy for graphing
+ */
+
+var kiwiModules = require('../server/modules'),
+    fs = require('fs');
+
+
+
+var module = new kiwiModules.Module('stats_file');
+
+module.on('stat counter', function (event, event_data) {
+    var stat_name = event_data.name,
+        stats_file, timestamp,
+        ignored_events = [];
+
+    // Some events may want to be ignored
+    ignored_events.push('http.request');
+
+    if (ignored_events.indexOf(stat_name) > -1) {
+        return;
+    }
+
+    timestamp = Math.floor((new Date()).getTime() / 1000);
+
+    stats_file = fs.createWriteStream('kiwi_stats.log', {'flags': 'a'});
+    stats_file.write(timestamp.toString() + ' ' + stat_name + '\n');
+});
\ No newline at end of file