syncthing/lib/stats/device.go
Jakob Borg 2a4fc28318 We should pass around db.Instance instead of leveldb.DB
We're going to need the db.Instance to keep some state, and for that to
work we need the same one passed around everywhere. Hence this moves the
leveldb-specific file opening stuff into the db package and exports the
dbInstance type.
2015-10-31 12:35:30 +01:00

53 lines
1.3 KiB
Go

// Copyright (C) 2014 The Syncthing Authors.
//
// 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/.
package stats
import (
"time"
"github.com/syncthing/syncthing/lib/db"
)
type DeviceStatistics struct {
LastSeen time.Time `json:"lastSeen"`
}
type DeviceStatisticsReference struct {
ns *db.NamespacedKV
device string
}
func NewDeviceStatisticsReference(ldb *db.Instance, device string) *DeviceStatisticsReference {
prefix := string(db.KeyTypeDeviceStatistic) + device
return &DeviceStatisticsReference{
ns: db.NewNamespacedKV(ldb, prefix),
device: device,
}
}
func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
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)
return t
}
func (s *DeviceStatisticsReference) WasSeen() {
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
s.ns.PutTime("lastSeen", time.Now())
}
func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
return DeviceStatistics{
LastSeen: s.GetLastSeen(),
}
}