device.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package stats
  16. import (
  17. "time"
  18. "github.com/syncthing/protocol"
  19. "github.com/syncthing/syncthing/internal/db"
  20. "github.com/syndtr/goleveldb/leveldb"
  21. )
  22. type DeviceStatistics struct {
  23. LastSeen time.Time
  24. }
  25. type DeviceStatisticsReference struct {
  26. ns *db.NamespacedKV
  27. device protocol.DeviceID
  28. }
  29. func NewDeviceStatisticsReference(ldb *leveldb.DB, device protocol.DeviceID) *DeviceStatisticsReference {
  30. prefix := string(db.KeyTypeDeviceStatistic) + device.String()
  31. return &DeviceStatisticsReference{
  32. ns: db.NewNamespacedKV(ldb, prefix),
  33. device: device,
  34. }
  35. }
  36. func (s *DeviceStatisticsReference) GetLastSeen() time.Time {
  37. t, ok := s.ns.Time("lastSeen")
  38. if !ok {
  39. // The default here is 1970-01-01 as opposed to the default
  40. // time.Time{} from s.ns
  41. return time.Unix(0, 0)
  42. }
  43. if debug {
  44. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  45. }
  46. return t
  47. }
  48. func (s *DeviceStatisticsReference) WasSeen() {
  49. if debug {
  50. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  51. }
  52. s.ns.PutTime("lastSeen", time.Now())
  53. }
  54. func (s *DeviceStatisticsReference) GetStatistics() DeviceStatistics {
  55. return DeviceStatistics{
  56. LastSeen: s.GetLastSeen(),
  57. }
  58. }