-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
210 lines (189 loc) · 5.48 KB
/
app.js
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
var net = require('net');
var controllers = angular.module('controllers', [])
var statsd = angular.module('statsd', [
'controllers',
])
controllers.controller('main', function($scope){
var updateInterval;
var hash = {
"": {
id: "",
title: "Root",
nodes: [],
expand: true
}
};
function splitAddress(str){
var arr = str.split(":", 2);
return {
address: arr[0],
port: arr[1] || 8126
}
}
function saveDefaultAddress(address){
localStorage.setItem('defaultAddress', address);
}
function loadDefaultAddress(){
return localStorage.getItem('defaultAddress') || "127.0.0.1:8126";
}
function statsdQuery(str, callback){
var client = new net.Socket();
var config = splitAddress($scope.address);
var buffer = new Buffer(0);
client.connect(config.port, config.address, function() {
client.write(str);
client.end();
});
client.on('connect', function() {
saveDefaultAddress($scope.address);
$scope.connectionError = null;
$scope.$apply();
});
client.on('data', function(data) {
buffer = Buffer.concat([buffer, data])
});
client.on('error', function(err) {
console.log('err', err);
setTimeout(function(){
clearInterval(updateInterval);
$scope.connectionError = err.message || "Unknown connection error";
$scope.$apply();
}, 100);
});
client.on('close', function(err) {
if(!err){
callback(buffer.toString());
$scope.$apply();
client.destroy();
}
});
}
function parent(key){
var arr = key.split(".");
arr.pop();
return arr.join(".");
}
function formatDuration(n){
var str = "";
var dur = moment.duration(n, "seconds");
["second", "minute", "hour", "day", "month", "year"].forEach(function(x, i){
var v = dur[x + "s"]();
if(v || !i){
str = v + " " + x + (v != 1 ? "s" : "") + ", " + str;
}
})
return str.replace(/, $/, '');
}
function updateStats(){
statsdQuery("stats", function(data){
$scope.loaded = $scope.loaded | 8;
var durations = ["uptime", "graphite.last_flush", "graphite.last_exception", "graphite.flush_time", "messages.bad_lines_seen", "messages.last_msg_seen"];
$scope.stats = _.object(data.replace(/END$/m, '').split(/\n/).map(function(x){
var arr = x.split(": ", 2);
var key = arr[0];
var value = Number(arr[1]);
if(durations.indexOf(key) >= 0){
value = formatDuration(value);
}
return [key, value];
}));
delete $scope.stats[""];
});
}
function updateCounters(){
["counters", "gauges", "timers"].forEach(function(name, i){
statsdQuery(name, function(data){
$scope.loaded = $scope.loaded | (1 << i);
data = data.replace(/END$/m, '')
eval("data = " + data); // TODO: write a parser
// turning {"x":{}} to {"counters.x":{}}
data = _.object(_.pairs(data).map(function(x){ return [(name + "." + x[0]).replace(/\.$/,''), x[1]] }));
_.keys(data).forEach(function(key){
var arr = key.split(".");
var title = _.last(arr);
var value = data[key];
data[key] = {
"id": key,
"title": title,
"nodes": [],
"level": arr.length,
"value": value,
"name": name
};
});
_.keys(data).forEach(function(key){
var arr = key.split(".");
for(var i = 0; i < arr.length; i++){
var newArr = arr.slice(0, i)
var newKey = newArr.join(".");
var title = _.last(newArr);
if(!data[newKey]){
data[newKey] = {
"id": newKey,
"title": title,
"nodes": [],
"level": newArr.length
};
}
}
});
_.extend(hash, data)
// assigning children nodes
_.keys(data).sort().forEach(function(key){
var parentKey = parent(key);
if(hash[parentKey]){
hash[parentKey].nodes.push(hash[key])
hash[key].parent = hash[parentKey];
}
});
$scope.tree = _.compact([hash["counters"], hash["gauges"], hash["timers"]]);
});
});
}
document.body.style.opacity = 1;
$scope.tab = "metrics";
$scope.stats = {};
$scope.address = loadDefaultAddress();
$scope.firstTry = true;
$scope.connectionError = null;
$scope.show = {
"": true,
};
$scope.isVisible = function(key){
return $scope.show[parent(key)];
}
$scope.expand = function(key){
$scope.show[key] = true;
}
$scope.collapse = function(key){
$scope.show[key] = false;
}
$scope.refresh = function(){
$scope.connectionError = null;
$scope.loaded = 0;
$scope.firstTry = false;
updateStats();
clearInterval(updateInterval);
updateInterval = setInterval(updateStats, 2000);
updateCounters();
}
$scope.delete = function(node){
node.deleted = true;
var array = node.id.split(".");
if(array.length < 1){
return;
}
if(!confirm("Are you sure you want to delete [" + node.id + "]?")){
return;
}
var type = array.shift();
var command = "del" + type + " " + array.join(".");
statsdQuery(command, function(result){
console.log(result);
});
var command = "del" + type + " " + array.join(".") + ".*";
statsdQuery(command, function(result){
console.log(result);
});
}
});