device.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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.Instance, device string) *DeviceStatisticsReference {
  19. prefix := string(db.KeyTypeDeviceStatistic) + device
  20. return &DeviceStatisticsReference{
  21. ns: db.NewNamespacedKV(ldb, prefix),
  22. device: device,
  23. }
  24. }
  25. func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
  26. t, ok := s.ns.Time("lastSeen")
  27. if !ok {
  28. // The default here is 1970-01-01 as opposed to the default
  29. // time.Time{} from s.ns
  30. return time.Unix(0, 0)
  31. }
  32. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  33. return t
  34. }
  35. func (s *DeviceStatisticsReference) WasSeen() {
  36. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  37. s.ns.PutTime("lastSeen", time.Now())
  38. }
  39. func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
  40. return DeviceStatistics{
  41. LastSeen: s.GetLastSeen(),
  42. }
  43. }