From afb27f7f02958872154fa67b61dced46551fe01b Mon Sep 17 00:00:00 2001 From: Audrius Butkevicius Date: Sun, 8 Apr 2018 20:13:55 +0100 Subject: [PATCH] cmd/strelaypoolsrv: Move metric scraping to the server itself (#4866) --- cmd/strelaypoolsrv/gui/index.html | 189 ++++++++----------- cmd/strelaypoolsrv/main.go | 304 +++++++++++++++++++++--------- cmd/strelaypoolsrv/stats.go | 213 +++++++++++++++++++++ cmd/strelaysrv/status.go | 4 + 4 files changed, 517 insertions(+), 193 deletions(-) create mode 100644 cmd/strelaypoolsrv/stats.go diff --git a/cmd/strelaypoolsrv/gui/index.html b/cmd/strelaypoolsrv/gui/index.html index 4b739713f..e1989a1cc 100644 --- a/cmd/strelaypoolsrv/gui/index.html +++ b/cmd/strelaypoolsrv/gui/index.html @@ -56,83 +56,83 @@ Address - + Sessions - - + + - + Connections - - + + - + Data relayed - - + + Transfer rate in the last period - + Uptime hours - + - + Provided by - - + + - + 10s - - + + - + 1m - - + + - + 5m - - + + - + 15m - - + + - + 30m - - + + - + 60m - - + + @@ -140,21 +140,21 @@ {{ relay.address }} - Looking up... - {{ relay.status.numActiveSessions }} - {{ relay.status.numConnections }} - {{ relay.status.bytesProxied | bytes }} - {{ relay.status.kbps10s1m5m15m30m60m[0] * 128 | bytes }}/s - {{ relay.status.kbps10s1m5m15m30m60m[1] * 128 | bytes }}/s - {{ relay.status.kbps10s1m5m15m30m60m[2] * 128 | bytes }}/s - {{ relay.status.kbps10s1m5m15m30m60m[3] * 128 | bytes }}/s - {{ relay.status.kbps10s1m5m15m30m60m[4] * 128 | bytes }}/s - {{ relay.status.kbps10s1m5m15m30m60m[5] * 128 | bytes }}/s - {{ relay.status.uptimeSeconds/60/60 | number:0 }} - - - {{ relay.status.options['provided-by'] || '' | limitTo:50 }} - … + + {{ relay.stats.numActiveSessions }} + {{ relay.stats.numConnections }} + {{ relay.stats.bytesProxied | bytes }} + {{ relay.stats.kbps10s1m5m15m30m60m[0] * 128 | bytes }}/s + {{ relay.stats.kbps10s1m5m15m30m60m[1] * 128 | bytes }}/s + {{ relay.stats.kbps10s1m5m15m30m60m[2] * 128 | bytes }}/s + {{ relay.stats.kbps10s1m5m15m30m60m[3] * 128 | bytes }}/s + {{ relay.stats.kbps10s1m5m15m30m60m[4] * 128 | bytes }}/s + {{ relay.stats.kbps10s1m5m15m30m60m[5] * 128 | bytes }}/s + {{ relay.stats.uptimeSeconds/60/60 | number:0 }} + + + {{ relay.stats.options['provided-by'] || '' | limitTo:50 }} + … @@ -235,16 +235,16 @@ $scope.mapBounds = new google.maps.LatLngBounds(); $scope.tooltipTemplate = $('#infoTemplate').html(); $scope.usedLocations = {}; - $scope.sortType = 'status.numActiveSessions'; + $scope.sortType = 'stats.numActiveSessions'; $scope.sortReverse = true; $scope.sortCompare = function(a, b) { if (a.value == b.value) { return 0; } - if (a.type == "undefined") { + if (a.type == "undefined" || a.type == "null") { return -1; } - if (b.type == "undefined") { + if (b.type == "undefined" || b.type == "null") { return 1; } return a.value > b.value ? 1 : -1; @@ -252,25 +252,31 @@ $http.get("/endpoint").then(function(response) { $scope.relays = response.data.relays; - var promises = []; - angular.forEach($scope.relays, function(relay) { + angular.forEach($scope.relays, function(relay) { relay.uri = constructURI(relay.url); relay.address = relay.url.split('/')[2]; addMarkerToMap(relay); - promises.push(getRelayStatus(relay)); + if (relay.stats) { + angular.forEach($scope.totals, function(value, key) { + if (typeof $scope.totals[key] == 'number') { + $scope.totals[key] += relay.stats[key]; + } else if (typeof $scope.totals[key] == 'object' && $scope.totals[key] instanceof Array) { + angular.forEach($scope.totals[key], function(value, index) { + $scope.totals[key][index] += relay.stats[key][index]; + }); + } + }); + } }); - // Can only add circles once we know the totals for transfers, which means - // we need to resolve all statuses. - $q.all(promises).then(function() { - angular.forEach($scope.relays, function(relay) { - if (relay.status) { - addCircleToMap(relay); - } - }); + // After the totals were calculated, add circles. + angular.forEach($scope.relays, function(relay) { + if (relay.stats) { + addCircleToMap(relay); + } }); $scope.map.fitBounds($scope.mapBounds); @@ -330,41 +336,10 @@ fillOpacity: 0.35, map: $scope.map, center: relay.marker.position, - radius: ((relay.status.bytesProxied * 100) / $scope.totals.bytesProxied) * 10000 + radius: ((relay.stats.bytesProxied * 100) / $scope.totals.bytesProxied) * 10000 }); } - function getRelayStatus(relay) { - // Normal timeout doesn't deal with relays which accept the TCP connection - // but don't respond (some firewalls do that), so deal with it this way. - var timeoutRequest = $q.defer(); - var resolveStatus = $q.defer(); - - - $http.get("http://" + relay.uri.hostname + ':' + ((relay.uri.args.statusAddr && relay.uri.args.statusAddr.split(':')[1]) || "22070") + "/status", { timeout: timeoutRequest.promise }).then(function (response) { - relay.status = response.data; - resolveStatus.resolve(); - angular.forEach($scope.totals, function(value, key) { - if (typeof $scope.totals[key] == 'number') { - $scope.totals[key] += response.data[key]; - } else if (typeof $scope.totals[key] == 'object' && $scope.totals[key] instanceof Array) { - angular.forEach($scope.totals[key], function(value, index) { - $scope.totals[key][index] += response.data[key][index]; - }); - } - }); - }, function() { - relay.status = null; - resolveStatus.resolve(); - }); - - $timeout(function() { - timeoutRequest.resolve(); - }, 5000); - - return resolveStatus.promise; - } - function constructURI(url) { var uri = document.createElement('a'); @@ -385,25 +360,25 @@