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

Code before:
                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) {
                    $scope.idToWebAddress[id] = "";
                    if (!data.hasOwnProperty(id)) {
                        continue;
                    }
                    var port = $scope.findDevice(id).remoteGUIPort;
                    var isNotRelayConnection = !data[id].type.includes("relay");
                    if ($scope.showRemoteGUI && data[id].address !== "" && isNotRelayConnection) {
                        var newAddress = `http://${replaceAddressPort(data[id].address, port)}`
                        var machineExists = $scope.probeAddress(newAddress)
                        if ($scope.idToWebAddress[id] !== newAddress && machineExists) {
                            $scope.idToWebAddress[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);
            }).error($scope.emitHTTPError);
        }

        function refreshErrors() {
            $http.get(urlbase + '/system/error').success(function (data) {
                $scope.errors = data.errors;
                console.log("refreshErrors", data);
            }).error($scope.emitHTTPError);
        }

        function refreshConfig() {
            $http.get(urlbase + '/system/config').success(function (data) {
                updateLocalConfig(data);
                console.log("refreshConfig", data);
            }).error($scope.emitHTTPError);

Review comment (bug):
It seems this will unconditionally probe every 10 seconds or so, because I think that might be how often we refresh completion. I don't think we want that. We should only probe if the address we used to check is gone, or we failed the probe before.

Code after:
                    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] = "";
                    continue;
                }
                var newAddress = "http://" + replaceAddressPort(connections[id].address, port);
                if (!(newAddress in $scope.remoteGUICache)) {
                    // No cached result, trigger a new port probing asynchronously
                    $scope.probeRemoteGUIAddress(id, newAddress);
                } else {