device.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  34. return t
  35. }
  36. func (s *DeviceStatisticsReference) WasSeen() {
  37. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  38. s.ns.PutTime("lastSeen", time.Now())
  39. }
  40. func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
  41. return DeviceStatistics{
  42. LastSeen: s.GetLastSeen(),
  43. }
  44. }