2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-28 11:00:38 +00:00
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2021-01-05 16:45:07 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/db/backend"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
lastSeenKey = "lastSeen"
|
|
|
|
connDurationKey = "lastConnDuration"
|
2014-09-28 11:00:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceStatistics struct {
|
2021-01-05 16:45:07 +00:00
|
|
|
LastSeen time.Time `json:"lastSeen"`
|
|
|
|
LastConnectionDurationS float64 `json:"lastConnectionDurationS"`
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeviceStatisticsReference struct {
|
2015-01-17 19:53:33 +00:00
|
|
|
ns *db.NamespacedKV
|
2021-01-05 16:45:07 +00:00
|
|
|
device protocol.DeviceID
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:07 +00:00
|
|
|
func NewDeviceStatisticsReference(dba backend.Backend, device protocol.DeviceID) *DeviceStatisticsReference {
|
2014-09-28 11:00:38 +00:00
|
|
|
return &DeviceStatisticsReference{
|
2021-01-05 16:45:07 +00:00
|
|
|
ns: db.NewDeviceStatisticsNamespace(dba, device.String()),
|
2014-09-28 11:00:38 +00:00
|
|
|
device: device,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
|
2021-01-05 16:45:07 +00:00
|
|
|
t, ok, err := s.ns.Time(lastSeenKey)
|
2019-11-30 12:03:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
} else if !ok {
|
2015-01-17 19:53:33 +00:00
|
|
|
// The default here is 1970-01-01 as opposed to the default
|
|
|
|
// time.Time{} from s.ns
|
2019-11-30 12:03:24 +00:00
|
|
|
return time.Unix(0, 0), nil
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
|
2019-11-30 12:03:24 +00:00
|
|
|
return t, nil
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:45:07 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
func (s *DeviceStatisticsReference) WasSeen() error {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
|
2021-01-05 16:45:07 +00:00
|
|
|
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())
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 12:03:24 +00:00
|
|
|
func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
|
|
|
|
lastSeen, err := s.GetLastSeen()
|
|
|
|
if err != nil {
|
|
|
|
return DeviceStatistics{}, err
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|
2021-01-05 16:45:07 +00:00
|
|
|
lastConnDuration, err := s.GetLastConnectionDuration()
|
|
|
|
if err != nil {
|
|
|
|
return DeviceStatistics{}, err
|
|
|
|
}
|
2019-11-30 12:03:24 +00:00
|
|
|
return DeviceStatistics{
|
2021-01-05 16:45:07 +00:00
|
|
|
LastSeen: lastSeen,
|
|
|
|
LastConnectionDurationS: lastConnDuration.Seconds(),
|
2019-11-30 12:03:24 +00:00
|
|
|
}, nil
|
2014-09-28 11:00:38 +00:00
|
|
|
}
|