Files
syncthing-arm/lib/stats/device.go
T

53 lines
1.3 KiB
Go
Raw Normal View History

2014-11-16 21:13:20 +01:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 21:43:32 +02:00
//
2015-03-07 21:36:35 +01: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 https://mozilla.org/MPL/2.0/.
package stats
import (
"time"
2015-08-06 11:29:25 +02:00
"github.com/syncthing/syncthing/lib/db"
)
type DeviceStatistics struct {
LastSeen time.Time `json:"lastSeen"`
}
type DeviceStatisticsReference struct {
2015-01-17 20:53:33 +01:00
ns *db.NamespacedKV
2015-09-04 13:22:59 +02:00
device string
}
func NewDeviceStatisticsReference(ldb *db.Instance, device string) *DeviceStatisticsReference {
2015-09-04 13:22:59 +02:00
prefix := string(db.KeyTypeDeviceStatistic) + device
return &DeviceStatisticsReference{
2015-01-17 20:53:33 +01:00
ns: db.NewNamespacedKV(ldb, prefix),
device: device,
}
}
func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
2015-01-17 20:53:33 +01: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
return time.Unix(0, 0)
}
l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
2015-01-17 20:53:33 +01:00
return t
}
func (s *DeviceStatisticsReference) WasSeen() {
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
2015-01-17 20:53:33 +01:00
s.ns.PutTime("lastSeen", time.Now())
}
func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
return DeviceStatistics{
LastSeen: s.GetLastSeen(),
}
}