Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

enable adding / removing targets, rework websockets event commands

  • Loading branch information...
commit e2cd97cfc14c1f18869026f639a1d6014ef19e69 1 parent 882b0ce
Scott Smith authored
Showing with 28 additions and 6 deletions.
  1. +1 −0  .gitignore
  2. +27 −6 grockets.js
View
1  .gitignore
@@ -0,0 +1 @@
+node_modules
View
33 grockets.js
@@ -20,7 +20,7 @@ function processHandlers() {
handlers.map(function(handler) {
if (handler.shouldUpdate()) {
handler.data(function(data) {
- handler.socket.emit('metric_data', data);
+ handler.socket.emit('metricData', data);
});
}
});
@@ -40,6 +40,15 @@ function MetricHandler(socket, targets, from, updateInterval) {
'from': from
};
+ this.addTarget = function(target) {
+ queryParameters.targets.push(target);
+ }
+
+ this.removeTarget = function(target) {
+ var index = queryParameters.targets.indexOf(target);
+ queryParameters.targets.splice(index, 1);
+ }
+
this.data = function(callback) {
var httpOptions = {
host: graphiteHost,
@@ -47,16 +56,16 @@ function MetricHandler(socket, targets, from, updateInterval) {
path: '/render?' + buildQueryString(queryParameters)
};
- var metric_data = new String;
+ var metricData = new String;
http.get(httpOptions, function(res) {
res.on('data', function(chunk) {
- metric_data += chunk.toString();
+ metricData += chunk.toString();
}).on('error', function(e) {
console.log('Got error: ' + e.message);
}).on('end', function() {
lastUpdateTime = new Date().getTime();
- callback(metric_data);
+ callback(metricData);
});
});
};
@@ -76,9 +85,21 @@ function MetricHandler(socket, targets, from, updateInterval) {
var handlers = new Array;
io.sockets.on('connection', function(socket) {
- socket.on('metrics', function(queryData) {
+ socket.on('fetchMetrics', function(queryData) {
handlers.push(new MetricHandler(socket, queryData.targets, queryData.from, queryData.updateInterval));
- });
+ }).on('addTarget', function(target) {
+ handlers.forEach(function(handler) {
+ if (handler.socket == socket) {
+ handler.addTarget(target);
+ }
+ });
+ }).on('removeTarget', function(target) {
+ handlers.forEach(function(handler) {
+ if (handler.socket == socket) {
+ handler.removeTarget(target);
+ }
+ });
+ });;
});
processHandlers();

1 comment on commit e2cd97c

@Ajaay

Hi, any chance you could provide some insight or examples on how I'd program the client side to get this working? I'm not good enough with Javascript just yet piece it together with one half... Any help would be greatly appreciated! Thanks.

Please sign in to comment.
Something went wrong with that request. Please try again.