device.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package stats
  7. import (
  8. "time"
  9. "github.com/syncthing/syncthing/lib/db"
  10. )
  11. type DeviceStatistics struct {
  12. LastSeen time.Time `json:"lastSeen"`
  13. }
  14. type DeviceStatisticsReference struct {
  15. ns *db.NamespacedKV
  16. device string
  17. }
  18. func NewDeviceStatisticsReference(ldb *db.Lowlevel, device string) *DeviceStatisticsReference {
  19. return &DeviceStatisticsReference{
  20. ns: db.NewDeviceStatisticsNamespace(ldb, device),
  21. device: device,
  22. }
  23. }
  24. func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
  25. t, ok := s.ns.Time("lastSeen")
  26. if !ok {
  27. // The default here is 1970-01-01 as opposed to the default
  28. // time.Time{} from s.ns
  29. return time.Unix(0, 0)
  30. }
  31. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  32. return t
  33. }
  34. func (s *DeviceStatisticsReference) WasSeen() {
  35. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  36. s.ns.PutTime("lastSeen", time.Now())
  37. }
  38. func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
  39. return DeviceStatistics{
  40. LastSeen: s.GetLastSeen(),
  41. }
  42. }