2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-09-28 04:00:38 -07:00
|
|
|
|
|
|
|
package stats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2015-08-06 02:29:25 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2014-09-28 04:00:38 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type DeviceStatistics struct {
|
2015-03-10 15:45:43 -07:00
|
|
|
LastSeen time.Time `json:"lastSeen"`
|
2014-09-28 04:00:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeviceStatisticsReference struct {
|
2015-01-17 12:53:33 -07:00
|
|
|
ns *db.NamespacedKV
|
2015-09-04 04:22:59 -07:00
|
|
|
device string
|
2014-09-28 04:00:38 -07:00
|
|
|
}
|
|
|
|
|
2015-10-31 04:31:25 -07:00
|
|
|
func NewDeviceStatisticsReference(ldb *db.Instance, device string) *DeviceStatisticsReference {
|
2015-09-04 04:22:59 -07:00
|
|
|
prefix := string(db.KeyTypeDeviceStatistic) + device
|
2014-09-28 04:00:38 -07:00
|
|
|
return &DeviceStatisticsReference{
|
2015-01-17 12:53:33 -07:00
|
|
|
ns: db.NewNamespacedKV(ldb, prefix),
|
2014-09-28 04:00:38 -07:00
|
|
|
device: device,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
|
2015-01-17 12:53:33 -07:00
|
|
|
t, ok := s.ns.Time("lastSeen")
|
|
|
|
if !ok {
|
|
|
|
// The default here is 1970-01-01 as opposed to the default
|
|
|
|
// time.Time{} from s.ns
|
2014-09-28 04:00:38 -07:00
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
|
2015-01-17 12:53:33 -07:00
|
|
|
return t
|
2014-09-28 04:00:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) WasSeen() {
|
2015-10-03 08:25:21 -07:00
|
|
|
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
|
2015-01-17 12:53:33 -07:00
|
|
|
s.ns.PutTime("lastSeen", time.Now())
|
2014-09-28 04:00:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
|
|
|
|
return DeviceStatistics{
|
|
|
|
LastSeen: s.GetLastSeen(),
|
|
|
|
}
|
|
|
|
}
|