|
@@ -10,26 +10,34 @@ import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
|
|
+ "github.com/syncthing/syncthing/lib/db/backend"
|
|
|
+ "github.com/syncthing/syncthing/lib/protocol"
|
|
|
+)
|
|
|
+
|
|
|
+const (
|
|
|
+ lastSeenKey = "lastSeen"
|
|
|
+ connDurationKey = "lastConnDuration"
|
|
|
)
|
|
|
|
|
|
type DeviceStatistics struct {
|
|
|
- LastSeen time.Time `json:"lastSeen"`
|
|
|
+ LastSeen time.Time `json:"lastSeen"`
|
|
|
+ LastConnectionDurationS float64 `json:"lastConnectionDurationS"`
|
|
|
}
|
|
|
|
|
|
type DeviceStatisticsReference struct {
|
|
|
ns *db.NamespacedKV
|
|
|
- device string
|
|
|
+ device protocol.DeviceID
|
|
|
}
|
|
|
|
|
|
-func NewDeviceStatisticsReference(ldb *db.Lowlevel, device string) *DeviceStatisticsReference {
|
|
|
+func NewDeviceStatisticsReference(dba backend.Backend, device protocol.DeviceID) *DeviceStatisticsReference {
|
|
|
return &DeviceStatisticsReference{
|
|
|
- ns: db.NewDeviceStatisticsNamespace(ldb, device),
|
|
|
+ ns: db.NewDeviceStatisticsNamespace(dba, device.String()),
|
|
|
device: device,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
|
|
|
- t, ok, err := s.ns.Time("lastSeen")
|
|
|
+ t, ok, err := s.ns.Time(lastSeenKey)
|
|
|
if err != nil {
|
|
|
return time.Time{}, err
|
|
|
} else if !ok {
|
|
@@ -41,9 +49,25 @@ func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
|
|
|
return t, nil
|
|
|
}
|
|
|
|
|
|
+func (s *DeviceStatisticsReference) GetLastConnectionDuration() (time.Duration, error) {
|
|
|
+ d, ok, err := s.ns.Int64(connDurationKey)
|
|
|
+ if err != nil {
|
|
|
+ return 0, err
|
|
|
+ } else if !ok {
|
|
|
+ return 0, nil
|
|
|
+ }
|
|
|
+ l.Debugln("stats.DeviceStatisticsReference.GetLastConnectionDuration:", s.device, d)
|
|
|
+ return time.Duration(d), nil
|
|
|
+}
|
|
|
+
|
|
|
func (s *DeviceStatisticsReference) WasSeen() error {
|
|
|
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
|
|
|
- return s.ns.PutTime("lastSeen", time.Now())
|
|
|
+ return s.ns.PutTime(lastSeenKey, time.Now())
|
|
|
+}
|
|
|
+
|
|
|
+func (s *DeviceStatisticsReference) LastConnectionDuration(d time.Duration) error {
|
|
|
+ l.Debugln("stats.DeviceStatisticsReference.LastConnectionDuration:", s.device, d)
|
|
|
+ return s.ns.PutInt64(connDurationKey, d.Nanoseconds())
|
|
|
}
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
|
|
@@ -51,7 +75,12 @@ func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
|
|
|
if err != nil {
|
|
|
return DeviceStatistics{}, err
|
|
|
}
|
|
|
+ lastConnDuration, err := s.GetLastConnectionDuration()
|
|
|
+ if err != nil {
|
|
|
+ return DeviceStatistics{}, err
|
|
|
+ }
|
|
|
return DeviceStatistics{
|
|
|
- LastSeen: lastSeen,
|
|
|
+ LastSeen: lastSeen,
|
|
|
+ LastConnectionDurationS: lastConnDuration.Seconds(),
|
|
|
}, nil
|
|
|
}
|