You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hub_strorage_transfer.go 2.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package servicestats
  2. import (
  3. "math"
  4. "sync"
  5. "time"
  6. "gitlink.org.cn/cloudream/common/utils/math2"
  7. jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
  8. )
  9. type HubStorageTransferStats struct {
  10. data HubStorageTransferStatsData
  11. fromHubID jcstypes.HubID
  12. lock *sync.Mutex
  13. }
  14. type HubStorageTransferStatsData struct {
  15. Entries map[jcstypes.StorageID]*HubStorageTransferStatsEntry
  16. StartTime time.Time
  17. EndTime time.Time
  18. }
  19. type HubStorageTransferStatsEntry struct {
  20. DestStorageID jcstypes.StorageID
  21. OutputBytes int64
  22. MaxOutputBytes int64
  23. MinOutputBytes int64
  24. TotalOutputReqs int64
  25. SuccessOutputReqs int64
  26. InputBytes int64
  27. MaxInputBytes int64
  28. MinInputBytes int64
  29. TotalInputReqs int64
  30. SuccessInputReqs int64
  31. }
  32. func (s *HubStorageTransferStats) RecordUpload(dstStorageID jcstypes.StorageID, transferBytes int64, isSuccess bool) {
  33. s.lock.Lock()
  34. defer s.lock.Unlock()
  35. e := s.data.Entries[dstStorageID]
  36. if e == nil {
  37. e = &HubStorageTransferStatsEntry{
  38. DestStorageID: dstStorageID,
  39. MinInputBytes: math.MaxInt64,
  40. MinOutputBytes: math.MaxInt64,
  41. }
  42. s.data.Entries[dstStorageID] = e
  43. }
  44. e.OutputBytes += transferBytes
  45. e.MaxOutputBytes = math2.Max(e.MaxOutputBytes, transferBytes)
  46. e.MinOutputBytes = math2.Min(e.MinOutputBytes, transferBytes)
  47. if isSuccess {
  48. e.SuccessOutputReqs++
  49. }
  50. e.TotalOutputReqs++
  51. }
  52. func (s *HubStorageTransferStats) RecordDownload(dstStorageID jcstypes.StorageID, transferBytes int64, isSuccess bool) {
  53. s.lock.Lock()
  54. defer s.lock.Unlock()
  55. e := s.data.Entries[dstStorageID]
  56. if e == nil {
  57. e = &HubStorageTransferStatsEntry{
  58. DestStorageID: dstStorageID,
  59. MinInputBytes: math.MaxInt64,
  60. MinOutputBytes: math.MaxInt64,
  61. }
  62. s.data.Entries[dstStorageID] = e
  63. }
  64. e.InputBytes += transferBytes
  65. e.MaxInputBytes = math2.Max(e.MaxInputBytes, transferBytes)
  66. e.MinInputBytes = math2.Min(e.MinInputBytes, transferBytes)
  67. if isSuccess {
  68. e.SuccessInputReqs++
  69. }
  70. }
  71. func (s *HubStorageTransferStats) DumpData(reset bool) HubStorageTransferStatsData {
  72. s.lock.Lock()
  73. defer s.lock.Unlock()
  74. data := s.data
  75. data.Entries = make(map[jcstypes.StorageID]*HubStorageTransferStatsEntry)
  76. for k, v := range s.data.Entries {
  77. v2 := *v
  78. data.Entries[k] = &v2
  79. }
  80. data.EndTime = time.Now()
  81. if reset {
  82. s.data.Entries = make(map[jcstypes.StorageID]*HubStorageTransferStatsEntry)
  83. s.data.StartTime = time.Now()
  84. }
  85. return data
  86. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。