device.go 1.4 KB

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