device.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "github.com/syncthing/syncthing/lib/db/backend"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. const (
  14. lastSeenKey = "lastSeen"
  15. connDurationKey = "lastConnDuration"
  16. )
  17. type DeviceStatistics struct {
  18. LastSeen time.Time `json:"lastSeen"`
  19. LastConnectionDurationS float64 `json:"lastConnectionDurationS"`
  20. }
  21. type DeviceStatisticsReference struct {
  22. ns *db.NamespacedKV
  23. device protocol.DeviceID
  24. }
  25. func NewDeviceStatisticsReference(dba backend.Backend, device protocol.DeviceID) *DeviceStatisticsReference {
  26. return &DeviceStatisticsReference{
  27. ns: db.NewDeviceStatisticsNamespace(dba, device.String()),
  28. device: device,
  29. }
  30. }
  31. func (s *DeviceStatisticsReference) GetLastSeen() (time.Time, error) {
  32. t, ok, err := s.ns.Time(lastSeenKey)
  33. if err != nil {
  34. return time.Time{}, err
  35. } else if !ok {
  36. // The default here is 1970-01-01 as opposed to the default
  37. // time.Time{} from s.ns
  38. return time.Unix(0, 0), nil
  39. }
  40. l.Debugln("stats.DeviceStatisticsReference.GetLastSeen:", s.device, t)
  41. return t, nil
  42. }
  43. func (s *DeviceStatisticsReference) GetLastConnectionDuration() (time.Duration, error) {
  44. d, ok, err := s.ns.Int64(connDurationKey)
  45. if err != nil {
  46. return 0, err
  47. } else if !ok {
  48. return 0, nil
  49. }
  50. l.Debugln("stats.DeviceStatisticsReference.GetLastConnectionDuration:", s.device, d)
  51. return time.Duration(d), nil
  52. }
  53. func (s *DeviceStatisticsReference) WasSeen() error {
  54. l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
  55. return s.ns.PutTime(lastSeenKey, time.Now().Truncate(time.Second))
  56. }
  57. func (s *DeviceStatisticsReference) LastConnectionDuration(d time.Duration) error {
  58. l.Debugln("stats.DeviceStatisticsReference.LastConnectionDuration:", s.device, d)
  59. return s.ns.PutInt64(connDurationKey, d.Nanoseconds())
  60. }
  61. func (s *DeviceStatisticsReference) GetStatistics() (DeviceStatistics, error) {
  62. lastSeen, err := s.GetLastSeen()
  63. if err != nil {
  64. return DeviceStatistics{}, err
  65. }
  66. lastConnDuration, err := s.GetLastConnectionDuration()
  67. if err != nil {
  68. return DeviceStatistics{}, err
  69. }
  70. return DeviceStatistics{
  71. LastSeen: lastSeen,
  72. LastConnectionDurationS: lastConnDuration.Seconds(),
  73. }, nil
  74. }