Repository: syncthing/syncthing
File: gui/default/syncthing/core/syncthingController.js (JavaScript)

Code before:
            $http.get(urlbase + '/system/connections').success(function (data) {
                var now = Date.now(),
                    td = (now - prevDate) / 1000,
                    id;

                prevDate = now;

                try {
                    data.total.inbps = Math.max(0, (data.total.inBytesTotal - $scope.connectionsTotal.inBytesTotal) / td);
                    data.total.outbps = Math.max(0, (data.total.outBytesTotal - $scope.connectionsTotal.outBytesTotal) / td);
                } catch (e) {
                    data.total.inbps = 0;
                    data.total.outbps = 0;
                }
                $scope.connectionsTotal = data.total;

                data = data.connections;
                $scope.recentRemoteAddressCache = {};
                for (id in data) {
                    if (!data.hasOwnProperty(id)) {
                        continue;
                    }
                    if (!(id in $scope.idToRemoteAddress)) {
                        $scope.idToRemoteAddress[id] = "";
                    }
                    var port = $scope.findDevice(id).remoteAddressPort.toString();
                    var isNotRelayConnection = !data[id].type.includes("relay");
                    if ($scope.showRemoteAddress && isNotRelayConnection && port !== "0") {
                        var newAddress = "http://" + replaceAddressPort(data[id].address, port);
                        if ($scope.probeAddress(newAddress)) {
                            $scope.idToRemoteAddress[id] = newAddress;
                        }
                    }
                    try {
                        data[id].inbps = Math.max(0, (data[id].inBytesTotal - $scope.connections[id].inBytesTotal) / td);
                        data[id].outbps = Math.max(0, (data[id].outBytesTotal - $scope.connections[id].outBytesTotal) / td);
                    } catch (e) {
                        data[id].inbps = 0;
                        data[id].outbps = 0;
                    }
                }
                $scope.connections = data;
                console.log("refreshConnections", data);

                // Clean cache of addresses unseen
                for (var address in $scope.remoteAddressCache) {
                    if (!(address in $scope.recentRemoteAddressCache)) {
                        delete $scope.remoteAddressCache[address]
                    }
                }


Review comment (bug):
This is a nit-pick, so feel free to ignore, but I don't think the toString is needed here.

I think it'd be better if toString happened when the port is used in replaceAddressPort, carryng the expected type as far as we can, because the port is a number everywhere, and in this function it's always starts of as a string, so if the reader misses the toString(), he might do a comparison with a number 0 and introduce a bug.

Code after:
                    td = (now - prevDate) / 1000,
                    id;

                prevDate = now;

                try {
                    data.total.inbps = Math.max(0, (data.total.inBytesTotal - $scope.connectionsTotal.inBytesTotal) / td);
                    data.total.outbps = Math.max(0, (data.total.outBytesTotal - $scope.connectionsTotal.outBytesTotal) / td);
                } catch (e) {
                    data.total.inbps = 0;
                    data.total.outbps = 0;
                }
                $scope.connectionsTotal = data.total;

                data = data.connections;
                for (id in data) {
                    if (!data.hasOwnProperty(id)) {
                        continue;
                    }
                    try {
                        data[id].inbps = Math.max(0, (data[id].inBytesTotal - $scope.connections[id].inBytesTotal) / td);
                        data[id].outbps = Math.max(0, (data[id].outBytesTotal - $scope.connections[id].outBytesTotal) / td);
                    } catch (e) {
                        data[id].inbps = 0;
                        data[id].outbps = 0;
                    }
                }
                $scope.connections = data;
                console.log("refreshConnections", data);

                refreshRemoteGUI(data);
            }).error($scope.emitHTTPError);
        }

        function refreshRemoteGUI(connections) {
            if (!$scope.showRemoteGUI) {
                $scope.idToRemoteGUI = {}
                return
            }
            var newCache = {};
            for (var id in connections) {
                if (!(id in $scope.devices)) {
                    // Avoid errors when called before first updateLocalConfig()
                    continue;
                }
                var port = $scope.devices[id].remoteGUIPort;
                if (port <= 0
                    || !connections[id].address
                    || connections[id].type.includes("relay")) {
                    // Relay connections never work as desired here, nor incomplete addresses
                    $scope.idToRemoteGUI[id] = "";