diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 2a5174642..1d1f20ea9 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -111,6 +111,9 @@ func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) erro getRestMux.HandleFunc("/rest/upgrade", restGetUpgrade) getRestMux.HandleFunc("/rest/version", restGetVersion) + // Debug endpoints, not for general use + getRestMux.HandleFunc("/rest/debug/peerCompletion", withModel(m, restGetPeerCompletion)) + // The POST handlers postRestMux := http.NewServeMux() postRestMux.HandleFunc("/rest/config", withModel(m, restPostConfig)) @@ -525,6 +528,31 @@ func getQR(w http.ResponseWriter, r *http.Request) { w.Write(code.PNG()) } +func restGetPeerCompletion(m *model.Model, w http.ResponseWriter, r *http.Request) { + tot := map[string]float64{} + count := map[string]float64{} + + for _, repo := range cfg.Repositories { + for _, node := range repo.NodeIDs() { + nodeStr := node.String() + if m.ConnectedTo(node) { + tot[nodeStr] += m.Completion(node, repo.ID) + } else { + tot[nodeStr] = 0 + } + count[nodeStr]++ + } + } + + comp := map[string]int{} + for node := range tot { + comp[node] = int(tot[node] / count[node]) + } + + w.Header().Set("Content-Type", "application/json; charset=utf-8") + json.NewEncoder(w).Encode(comp) +} + func basicAuthMiddleware(username string, passhash string, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if validAPIKey(r.Header.Get("X-API-Key")) { diff --git a/integration/test-delupd.sh b/integration/test-delupd.sh index fb9e1a593..bcc45a402 100755 --- a/integration/test-delupd.sh +++ b/integration/test-delupd.sh @@ -31,9 +31,9 @@ stop() { testConvergence() { while true ; do sleep 5 - s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/connections" | ./json "$id1/Completion") - s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8083/rest/connections" | ./json "$id2/Completion") - s3comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/connections" | ./json "$id3/Completion") + s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/debug/peerCompletion" | ./json "$id1") + s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8083/rest/debug/peerCompletion" | ./json "$id2") + s3comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/debug/peerCompletion" | ./json "$id3") s1comp=${s1comp:-0} s2comp=${s2comp:-0} s3comp=${s3comp:-0} diff --git a/integration/test-folders.sh b/integration/test-folders.sh index 62ad1c7b7..57d0766b1 100755 --- a/integration/test-folders.sh +++ b/integration/test-folders.sh @@ -46,8 +46,8 @@ setup() { testConvergence() { while true ; do sleep 5 - s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/connections" | ./json "$id1/Completion") - s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/connections" | ./json "$id2/Completion") + s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/debug/peerCompletion" | ./json "$id1") + s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/debug/peerCompletion" | ./json "$id2") s1comp=${s1comp:-0} s2comp=${s2comp:-0} tot=$(($s1comp + $s2comp)) diff --git a/integration/test-merge.sh b/integration/test-merge.sh index 55760faf4..f5395ce27 100755 --- a/integration/test-merge.sh +++ b/integration/test-merge.sh @@ -40,9 +40,9 @@ clean() { testConvergence() { while true ; do sleep 5 - s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/connections" | ./json "$id1/Completion") - s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8083/rest/connections" | ./json "$id2/Completion") - s3comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/connections" | ./json "$id3/Completion") + s1comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8082/rest/debug/peerCompletion" | ./json "$id1") + s2comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8083/rest/debug/peerCompletion" | ./json "$id2") + s3comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/debug/peerCompletion" | ./json "$id3") s1comp=${s1comp:-0} s2comp=${s2comp:-0} s3comp=${s3comp:-0} diff --git a/integration/test-reconnect.sh b/integration/test-reconnect.sh index eb08f9dec..3d4ac0076 100755 --- a/integration/test-reconnect.sh +++ b/integration/test-reconnect.sh @@ -43,7 +43,7 @@ testConvergence() { while true ; do sleep 5 - comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/connections" | ./json "$id2/Completion") + comp=$(curl -HX-API-Key:abc123 -s "http://localhost:8081/rest/debug/peerCompletion" | ./json "$id2") comp=${comp:-0} echo $comp / 100