From 0cba3154f0f52ed7277e1065a93422a947e812a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Colomb?= Date: Mon, 10 Jan 2022 10:26:45 +0100 Subject: [PATCH] lib/model: Remove bogus fields from connections API endpoint (fixes #8103) (#8104) * lib/model: Remove bogus fields from connections API endpoint. Switch the returned data type for the /rest/system/connections element "total" to use only the Statistics struct. The other fields of the ConnectionInfo struct are not populated and misleading. * Lowercase JSON field names. * lib/model: Get rid of ConnectionInfo.MarshalJSON(). It was missing the StartedAt field from the embedded Statistics struct. Just lowercasing the JSON attribute names can be done just as easily with annotations. * lib/model: Remove bogus startedAt field from totals. Instead of using the Statistics type with one field empty, just switch to a free-form map with the three needed fields. --- lib/model/model.go | 36 ++++++++++-------------------------- lib/protocol/protocol.go | 8 ++++---- 2 files changed, 14 insertions(+), 30 deletions(-) diff --git a/lib/model/model.go b/lib/model/model.go index e722a90cc..4bcfb1c1a 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -706,26 +706,12 @@ func (m *model) UsageReportingStats(report *contract.Report, version int, previe type ConnectionInfo struct { protocol.Statistics - Connected bool - Paused bool - Address string - ClientVersion string - Type string - Crypto string -} - -func (info ConnectionInfo) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]interface{}{ - "at": info.At, - "inBytesTotal": info.InBytesTotal, - "outBytesTotal": info.OutBytesTotal, - "connected": info.Connected, - "paused": info.Paused, - "address": info.Address, - "clientVersion": info.ClientVersion, - "type": info.Type, - "crypto": info.Crypto, - }) + Connected bool `json:"connected"` + Paused bool `json:"paused"` + Address string `json:"address"` + ClientVersion string `json:"clientVersion"` + Type string `json:"type"` + Crypto string `json:"crypto"` } // NumConnections returns the current number of active connected devices. @@ -769,12 +755,10 @@ func (m *model) ConnectionStats() map[string]interface{} { res["connections"] = conns in, out := protocol.TotalInOut() - res["total"] = ConnectionInfo{ - Statistics: protocol.Statistics{ - At: time.Now().Truncate(time.Second), - InBytesTotal: in, - OutBytesTotal: out, - }, + res["total"] = map[string]interface{}{ + "at": time.Now().Truncate(time.Second), + "inBytesTotal": in, + "outBytesTotal": out, } return res diff --git a/lib/protocol/protocol.go b/lib/protocol/protocol.go index a7f9408a0..7afca5a1e 100644 --- a/lib/protocol/protocol.go +++ b/lib/protocol/protocol.go @@ -994,10 +994,10 @@ func (c *rawConnection) pingReceiver() { } type Statistics struct { - At time.Time - InBytesTotal int64 - OutBytesTotal int64 - StartedAt time.Time + At time.Time `json:"at"` + InBytesTotal int64 `json:"inBytesTotal"` + OutBytesTotal int64 `json:"outBytesTotal"` + StartedAt time.Time `json:"startedAt"` } func (c *rawConnection) Statistics() Statistics {