device.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 http://mozilla.org/MPL/2.0/.
  6. package stats
  7. import (
  8. "time"
  9. "github.com/syncthing/syncthing/lib/db"
  10. "github.com/syndtr/goleveldb/leveldb"
  11. )
  12. type DeviceStatistics struct {
  13. LastSeen time.Time `json:"lastSeen"`
  14. }
  15. type DeviceStatisticsReference struct {
  16. ns *db.NamespacedKV
  17. device string
  18. }
  19. func NewDeviceStatisticsReference(ldb *leveldb.DB, device string) *DeviceStatisticsReference {
  20. prefix := string(db.KeyTypeDeviceStatistic) + device
  21. return &DeviceStatisticsReference{
  22. ns: db.NewNamespacedKV(ldb, prefix),
  23. device: device,
  24. }
  25. }
  26. func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
  27. t, ok := s.ns.Time("lastSeen")
  28. if !ok {
  29. // The default here is 1970-01-01 as opposed to the default
  30. // time.Time{} from s.ns
  31. return time.Unix(0, 0)
  32. }
  33. if debug {
  34. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  35. }
  36. return t
  37. }
  38. func (s *DeviceStatisticsReference) WasSeen() {
  39. if debug {
  40. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  41. }
  42. s.ns.PutTime("lastSeen", time.Now())
  43. }
  44. func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
  45. return DeviceStatistics{
  46. LastSeen: s.GetLastSeen(),
  47. }
  48. }