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 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.Lowlevel, device string) *DeviceStatisticsReference {
  19. return &DeviceStatisticsReference{
  20. ns: db.NewDeviceStatisticsNamespace(ldb, device),
  21. device: device,
  22. }
  23. }
  24. func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
  25. t, ok, err := s.ns.Time("lastSeen")
  26. if err != nil {
  27. return time.Time{}, err
  28. } else 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), nil
  32. }
  33. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  34. return t, nil
  35. }
  36. func (s *DeviceStatisticsReference) WasSeen() error {
  37. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  38. return s.ns.PutTime("lastSeen", time.Now())
  39. }
  40. func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
  41. lastSeen, err := s.GetLastSeen()
  42. if err != nil {
  43. return DeviceStatistics{}, err
  44. }
  45. return DeviceStatistics{
  46. LastSeen: lastSeen,
  47. }, nil
  48. }