bfs.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/bfs/model"
  5. "go-common/library/ecode"
  6. )
  7. // Total .
  8. func (s *Service) Total(c context.Context, arg *model.ArgCluster) (resp *model.RespTotal, err error) {
  9. volumes, err := s.Volumes(c, arg)
  10. if err != nil {
  11. return
  12. }
  13. num := int64(len(volumes.Volumes))
  14. var fs int64
  15. for _, volume := range volumes.Volumes {
  16. fs += int64(volume.FreeSpace)
  17. }
  18. fs = (fs * 8) / 1024 / 1024 / 1024 // GB
  19. // groups
  20. groups, err := s.d.Groups(arg.Cluster)
  21. if err != nil {
  22. return
  23. }
  24. // stores
  25. racks, err := s.d.Racks(arg.Cluster)
  26. if err != nil {
  27. return
  28. }
  29. var stores int64
  30. for _, rack := range racks {
  31. stores += int64(len(rack.Stores))
  32. }
  33. resp = &model.RespTotal{
  34. Space: 32 * num,
  35. FreeSpace: fs,
  36. Groups: int64(len(groups)),
  37. Stores: stores,
  38. Volumes: num,
  39. }
  40. return
  41. }
  42. // Racks .
  43. func (s *Service) Racks(c context.Context, arg *model.ArgCluster) (resp *model.RespRack, err error) {
  44. racks, err := s.d.Racks(arg.Cluster)
  45. if err != nil {
  46. return
  47. }
  48. for _, rack := range racks {
  49. for _, store := range rack.Stores {
  50. store.ParseStates()
  51. }
  52. }
  53. resp = &model.RespRack{Racks: racks}
  54. return
  55. }
  56. // Groups .
  57. func (s *Service) Groups(c context.Context, arg *model.ArgCluster) (resp *model.RespGroup, err error) {
  58. groups, err := s.d.Groups(arg.Cluster)
  59. if err != nil {
  60. return
  61. }
  62. racks, err := s.Racks(c, arg)
  63. if err != nil {
  64. return
  65. }
  66. volumes, err := s.Volumes(c, arg)
  67. if err != nil {
  68. return
  69. }
  70. for _, group := range groups {
  71. group.StoreDatas = make(map[string]*model.Store)
  72. for _, rack := range racks.Racks {
  73. for sname, store := range rack.Stores {
  74. for _, name := range group.Stores {
  75. if sname == name {
  76. group.StoreDatas[name] = store
  77. }
  78. }
  79. }
  80. }
  81. for _, store := range group.StoreDatas {
  82. num := int64(len(store.Volumes))
  83. var fs int64
  84. for _, volume := range store.Volumes {
  85. fs += int64(volumes.Volumes[volume].FreeSpace)
  86. }
  87. fs = (fs * 8) / 1024 / 1024 / 1024 // GB
  88. group.Total.Space = 32 * num
  89. group.Total.FreeSpace = fs
  90. group.Total.Volumes = num
  91. break
  92. }
  93. }
  94. resp = &model.RespGroup{Groups: groups}
  95. return
  96. }
  97. // Volumes .
  98. func (s *Service) Volumes(c context.Context, arg *model.ArgCluster) (resp *model.RespVolume, err error) {
  99. volumes, err := s.d.Volumes(arg.Cluster)
  100. if err != nil {
  101. return
  102. }
  103. resp = &model.RespVolume{Volumes: volumes}
  104. return
  105. }
  106. // AddVolume add volume.
  107. func (s *Service) AddVolume(c context.Context, arg *model.ArgAddVolume) (err error) {
  108. return s.d.AddVolume(c, arg.Group, arg.Num)
  109. }
  110. // AddFreeVolume add free volume.
  111. func (s *Service) AddFreeVolume(c context.Context, arg *model.ArgAddFreeVolume) (err error) {
  112. return s.d.AddFreeVolume(c, arg.Group, arg.Dir, arg.Num)
  113. }
  114. // Compact compact store.
  115. func (s *Service) Compact(c context.Context, arg *model.ArgCompact) (err error) {
  116. return s.d.Compact(c, arg.Group, arg.Vid)
  117. }
  118. // SetGroupStatus set group status(read,write,sync,health).
  119. func (s *Service) SetGroupStatus(c context.Context, arg *model.ArgGroupStatus) (err error) {
  120. if arg.Status != "read" && arg.Status != "write" && arg.Status != "sync" && arg.Status != "health" {
  121. err = ecode.RequestErr
  122. return
  123. }
  124. return s.d.SetGroupStatus(c, arg.Group, arg.Status)
  125. }