dashboard.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package service
  2. import (
  3. "context"
  4. "math"
  5. "time"
  6. "go-common/app/admin/ep/merlin/model"
  7. "go-common/library/log"
  8. )
  9. // QueryMachineLifeCycle Query Machine Life Cycle.
  10. func (s *Service) QueryMachineLifeCycle(c context.Context) (machineResponse map[string]interface{}, err error) {
  11. var machineLifeCycles []*model.MachineLifeCycle
  12. machineResponse = make(map[string]interface{})
  13. if machineLifeCycles, err = s.dao.MachineLifeCycle(); err != nil {
  14. return
  15. }
  16. machineResponse["machine_life_cycle"] = machineLifeCycles
  17. return
  18. }
  19. // QueryMachineCount Query Machine Count.
  20. func (s *Service) QueryMachineCount(c context.Context) (machineResponse *model.MachineCountGroupResponse, err error) {
  21. var (
  22. machinesCount []*model.MachineCountGroupByBusiness
  23. machinesCountInRunning []*model.MachineCountGroupByBusiness
  24. businessUnits []string
  25. machineCntData []int
  26. machineCntInRunData []int
  27. )
  28. if machinesCount, err = s.dao.MachineCountGroupByBusiness(); err != nil {
  29. return
  30. }
  31. if machinesCountInRunning, err = s.dao.MachineCountGroupByBusinessInRunning(); err != nil {
  32. return
  33. }
  34. for _, machineCount := range machinesCount {
  35. var runningCount int
  36. businessUnits = append(businessUnits, machineCount.BusinessUnit)
  37. machineCntData = append(machineCntData, machineCount.Count)
  38. for _, machineCountInRunning := range machinesCountInRunning {
  39. if machineCountInRunning.BusinessUnit == machineCount.BusinessUnit {
  40. runningCount = machineCountInRunning.Count
  41. break
  42. }
  43. }
  44. machineCntInRunData = append(machineCntInRunData, runningCount)
  45. }
  46. machineCountItem := &model.MachineCountGroupItem{
  47. Type: "已创建机器数量",
  48. Data: machineCntData,
  49. }
  50. machineCountInRunningItem := &model.MachineCountGroupItem{
  51. Type: "正在运行机器数量",
  52. Data: machineCntInRunData,
  53. }
  54. machineResponse = &model.MachineCountGroupResponse{
  55. BusinessUnits: businessUnits,
  56. Items: []*model.MachineCountGroupItem{machineCountItem, machineCountInRunningItem},
  57. }
  58. return
  59. }
  60. // QueryMachineCreatedAndEndTime Query Machine Created And End Time.
  61. func (s *Service) QueryMachineCreatedAndEndTime(c context.Context) (machineResponse map[string]interface{}, err error) {
  62. var (
  63. machineCreatedTime []*model.MachineCreatedAndEndTime
  64. machineExpiredTime []*model.MachineCreatedAndEndTime
  65. )
  66. machineResponse = make(map[string]interface{})
  67. if machineCreatedTime, err = s.dao.MachineLatestCreated(); err != nil {
  68. return
  69. }
  70. if machineExpiredTime, err = s.dao.MachineWillBeExpired(); err != nil {
  71. return
  72. }
  73. machineResponse["machine_created_top"] = machineCreatedTime
  74. machineResponse["machine_expired_top"] = machineExpiredTime
  75. return
  76. }
  77. // QueryMachineUsage Query Machine Usage.
  78. func (s *Service) QueryMachineUsage(c context.Context) (machineResponse map[string]interface{}, err error) {
  79. var (
  80. machines []*model.Machine
  81. pmds []*model.PaasMachineDetail
  82. pqadmrs []*model.PaasQueryAndDelMachineRequest
  83. totalCPU float32
  84. totalMemory float32
  85. totalMachine int
  86. totalMachineInRunnint int
  87. )
  88. machineResponse = make(map[string]interface{})
  89. if totalMachine, err = s.dao.QueryMachineCount(); err != nil {
  90. return
  91. }
  92. if machines, err = s.dao.QueryMachineInRunning(); err != nil {
  93. log.Error("query MachineInRunning err(%v)", err)
  94. return
  95. }
  96. totalMachineInRunnint = len(machines)
  97. for _, machine := range machines {
  98. pqadmrs = append(pqadmrs, machine.ToPaasQueryAndDelMachineRequest())
  99. }
  100. if pmds, err = s.dao.QueryMachineUsageSummaryFromCache(c, pqadmrs); err != nil {
  101. return
  102. }
  103. for _, pmd := range pmds {
  104. pmd.ConvertUnits()
  105. totalCPU = totalCPU + pmd.CPULimit/model.CPURatio
  106. totalMemory = totalMemory + pmd.MemoryLimit/model.MemoryRatio
  107. }
  108. machineResponse["total_machine"] = totalMachine
  109. machineResponse["total_machine_in_running"] = totalMachineInRunnint
  110. machineResponse["total_cpu_usage"] = totalCPU
  111. machineResponse["total_memory_usage"] = totalMemory
  112. return
  113. }
  114. // QueryMobileMachineUsageCount Query Mobile Machines Count.
  115. func (s *Service) QueryMobileMachineUsageCount(c context.Context) (res map[string]interface{}, err error) {
  116. var (
  117. mobileMachinesUserUsageCount []*model.MobileMachineUserUsageCount
  118. mobileMachinesUserLendCount []*model.MobileMachineUserUsageCount
  119. mobileMachinesUsageCount []*model.MobileMachineUsageCount
  120. mobileMachinesLendCount []*model.MobileMachineUsageCount
  121. )
  122. if mobileMachinesUserUsageCount, err = s.dao.MobileMachineUserUsageCount(); err != nil {
  123. return
  124. }
  125. if mobileMachinesUserLendCount, err = s.dao.MobileMachineUserLendCount(); err != nil {
  126. return
  127. }
  128. if mobileMachinesUsageCount, err = s.dao.MobileMachineUsageCount(); err != nil {
  129. return
  130. }
  131. if mobileMachinesLendCount, err = s.dao.MobileMachineLendCount(); err != nil {
  132. return
  133. }
  134. res = make(map[string]interface{})
  135. res["user_usage_count"] = mobileMachinesUserUsageCount
  136. res["user_lend_count"] = mobileMachinesUserLendCount
  137. res["mobile_machine_usage_count"] = mobileMachinesUsageCount
  138. res["mobile_machine_lend_count"] = mobileMachinesLendCount
  139. return
  140. }
  141. // QueryMobileMachineModeCount Query Mobile Machine Mode Count.
  142. func (s *Service) QueryMobileMachineModeCount(c context.Context) (res map[string]interface{}, err error) {
  143. var mobileMachinesTypeCount []*model.MobileMachineTypeCount
  144. if mobileMachinesTypeCount, err = s.dao.MobileMachineModeCount(); err != nil {
  145. return
  146. }
  147. res = make(map[string]interface{})
  148. res["mobile_machine_mode_count"] = mobileMachinesTypeCount
  149. return
  150. }
  151. // QueryMobileMachineUsageTime Query Mobile Machine Usage Time.
  152. func (s *Service) QueryMobileMachineUsageTime(c context.Context) (ret []*model.MobileMachineUsageTimeResponse, err error) {
  153. var (
  154. mobileMachineLogs []*model.MobileMachineLog
  155. mobileMachineLogsMap = make(map[int64][]*model.MobileMachineLog)
  156. )
  157. if mobileMachineLogs, err = s.dao.MobileMachineUseRecord(); err != nil {
  158. return
  159. }
  160. //按机器id 分组
  161. for _, mobileMachineLog := range mobileMachineLogs {
  162. mobileMachineLogsMap[mobileMachineLog.MachineID] = append(mobileMachineLogsMap[mobileMachineLog.MachineID], mobileMachineLog)
  163. }
  164. //按机器计算 使用时长
  165. for machineID := range mobileMachineLogsMap {
  166. var (
  167. mobileMachine *model.MobileMachine
  168. isStartTimeFound bool
  169. isEndTimeFound bool
  170. startTime time.Time
  171. endTime time.Time
  172. username string
  173. preMobileMachinesUsageTime []*model.MobileMachineUsageTime
  174. totalDuration float64
  175. )
  176. if mobileMachine, err = s.dao.FindMobileMachineByID(machineID); err != nil {
  177. continue
  178. }
  179. for index, preMobileMachineLog := range mobileMachineLogsMap[machineID] {
  180. if preMobileMachineLog.OperateType == model.MBBindLog {
  181. startTime = preMobileMachineLog.OperateTime
  182. username = preMobileMachineLog.Username
  183. isStartTimeFound = true
  184. }
  185. if preMobileMachineLog.OperateType == model.MBReleaseLog && isStartTimeFound {
  186. endTime = preMobileMachineLog.OperateTime
  187. isEndTimeFound = true
  188. }
  189. //处理中间绑定和解绑 计算使用时间
  190. if isStartTimeFound && isEndTimeFound {
  191. duration := math.Trunc(endTime.Sub(startTime).Minutes()*1e2+0.5) * 1e-2
  192. MobileMachineUsageTime := &model.MobileMachineUsageTime{
  193. Username: username,
  194. StartTime: startTime,
  195. EndTime: endTime,
  196. Duration: duration,
  197. }
  198. preMobileMachinesUsageTime = append(preMobileMachinesUsageTime, MobileMachineUsageTime)
  199. isStartTimeFound = false
  200. isEndTimeFound = false
  201. totalDuration = totalDuration + duration
  202. }
  203. // 最后次记录和绑定记录且机器在相应人名下 计算实时机器使用时长
  204. if index == (len(mobileMachineLogsMap[machineID])-1) && isStartTimeFound && !isEndTimeFound && mobileMachine.Username != "" {
  205. timeNow := time.Now()
  206. duration := math.Trunc(timeNow.Sub(startTime).Minutes()*1e2+0.5) * 1e-2
  207. MobileMachineUsageTime := &model.MobileMachineUsageTime{
  208. Username: username,
  209. StartTime: timeNow,
  210. EndTime: endTime,
  211. Duration: duration,
  212. }
  213. preMobileMachinesUsageTime = append(preMobileMachinesUsageTime, MobileMachineUsageTime)
  214. isStartTimeFound = false
  215. isEndTimeFound = false
  216. totalDuration = totalDuration + duration
  217. }
  218. }
  219. mobileMachineUsageTimeResponse := &model.MobileMachineUsageTimeResponse{
  220. MobileMachineID: machineID,
  221. MobileMachineName: mobileMachine.Name,
  222. ModeName: mobileMachine.Mode,
  223. TotalDuration: totalDuration,
  224. MobileMachinesUsageTime: preMobileMachinesUsageTime,
  225. }
  226. ret = append(ret, mobileMachineUsageTimeResponse)
  227. }
  228. return
  229. }