~xenrox/syncthing-status

f1f120e1cb425943617f302c79cbfd32a85338ae — Thorben Günther 2 years ago 7341152
Add average over remote devices
2 files changed, 53 insertions(+), 3 deletions(-)

M main.go
M status.go
M main.go => main.go +3 -1
@@ 16,7 16,9 @@ func main() {
	var status Status

	localStatus := client.localStatus()
	status.Local = localStatus.Completion
	remoteStatus := client.remoteStatus()
	status.Local = int(localStatus.Completion)
	status.Remote = int(remoteStatus.Completion)

	data, err := json.MarshalIndent(status, "", "  ")
	if err != nil {

M status.go => status.go +50 -2
@@ 1,9 1,24 @@
package main

import "log"
import (
	"fmt"
	"log"
)

type LocalStatus struct {
	Completion int `json:"completion"`
	Completion float64 `json:"completion"`
}

type RemoteStatus struct {
	Completion float64 `json:"completion"`
}

type Connection struct {
	Connected bool `json:"connected"`
}

type Connections struct {
	List map[string]Connection `json:"connections"`
}

func (client *Client) localStatus() *LocalStatus {


@@ 16,3 31,36 @@ func (client *Client) localStatus() *LocalStatus {

	return status
}

func (client *Client) remoteStatus() *RemoteStatus {
	var totalStatus RemoteStatus
	var connections Connections

	err := client.get("/rest/system/connections", &connections)
	if err != nil {
		log.Fatal(err)
	}

	var connectedDevices []string
	for key, con := range connections.List {
		if con.Connected {
			connectedDevices = append(connectedDevices, key)
		}
	}

	var sum float64
	for _, device := range connectedDevices {
		path := fmt.Sprintf("/rest/db/completion?device=%s", device)
		var status RemoteStatus
		err = client.get(path, &status)
		if err != nil {
			log.Fatal(err)
		}

		sum += status.Completion
	}

	totalStatus.Completion = sum / float64(len(connectedDevices))

	return &totalStatus
}