node.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package stats
  5. import (
  6. "time"
  7. "github.com/syncthing/syncthing/protocol"
  8. "github.com/syndtr/goleveldb/leveldb"
  9. )
  10. const (
  11. nodeStatisticTypeLastSeen = iota
  12. )
  13. var nodeStatisticsTypes = []byte{
  14. nodeStatisticTypeLastSeen,
  15. }
  16. type NodeStatistics struct {
  17. LastSeen time.Time
  18. }
  19. type NodeStatisticsReference struct {
  20. db *leveldb.DB
  21. node protocol.NodeID
  22. }
  23. func NewNodeStatisticsReference(db *leveldb.DB, node protocol.NodeID) *NodeStatisticsReference {
  24. return &NodeStatisticsReference{
  25. db: db,
  26. node: node,
  27. }
  28. }
  29. func (s *NodeStatisticsReference) key(stat byte) []byte {
  30. k := make([]byte, 1+1+32)
  31. k[0] = keyTypeNodeStatistic
  32. k[1] = stat
  33. copy(k[1+1:], s.node[:])
  34. return k
  35. }
  36. func (s *NodeStatisticsReference) GetLastSeen() time.Time {
  37. value, err := s.db.Get(s.key(nodeStatisticTypeLastSeen), nil)
  38. if err != nil {
  39. if err != leveldb.ErrNotFound {
  40. l.Warnln("NodeStatisticsReference: Failed loading last seen value for", s.node, ":", err)
  41. }
  42. return time.Unix(0, 0)
  43. }
  44. rtime := time.Time{}
  45. err = rtime.UnmarshalBinary(value)
  46. if err != nil {
  47. l.Warnln("NodeStatisticsReference: Failed parsing last seen value for", s.node, ":", err)
  48. return time.Unix(0, 0)
  49. }
  50. if debug {
  51. l.Debugln("stats.NodeStatisticsReference.GetLastSeen:", s.node, rtime)
  52. }
  53. return rtime
  54. }
  55. func (s *NodeStatisticsReference) WasSeen() {
  56. if debug {
  57. l.Debugln("stats.NodeStatisticsReference.WasSeen:", s.node)
  58. }
  59. value, err := time.Now().MarshalBinary()
  60. if err != nil {
  61. l.Warnln("NodeStatisticsReference: Failed serializing last seen value for", s.node, ":", err)
  62. return
  63. }
  64. err = s.db.Put(s.key(nodeStatisticTypeLastSeen), value, nil)
  65. if err != nil {
  66. l.Warnln("Failed serializing last seen value for", s.node, ":", err)
  67. }
  68. }
  69. // Never called, maybe because it's worth while to keep the data
  70. // or maybe because we have no easy way of knowing that a node has been removed.
  71. func (s *NodeStatisticsReference) Delete() error {
  72. for _, stype := range nodeStatisticsTypes {
  73. err := s.db.Delete(s.key(stype), nil)
  74. if debug && err == nil {
  75. l.Debugln("stats.NodeStatisticsReference.Delete:", s.node, stype)
  76. }
  77. if err != nil && err != leveldb.ErrNotFound {
  78. return err
  79. }
  80. }
  81. return nil
  82. }
  83. func (s *NodeStatisticsReference) GetStatistics() NodeStatistics {
  84. return NodeStatistics{
  85. LastSeen: s.GetLastSeen(),
  86. }
  87. }