mysql_dashboard.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "go-common/app/admin/ep/merlin/model"
  6. )
  7. // MachineLifeCycle Statistics Machine Life Cycle.
  8. func (d *Dao) MachineLifeCycle() (machineLifeCycles []*model.MachineLifeCycle, err error) {
  9. var rows *sql.Rows
  10. SQL := "select CASE " +
  11. "when TIMESTAMPDIFF(DAY,ctime,end_time)>0 and TIMESTAMPDIFF(DAY,ctime,end_time) <=100 THEN'0-100' " +
  12. "when TIMESTAMPDIFF(DAY,ctime,end_time)>100 and TIMESTAMPDIFF(DAY,ctime,end_time) <=200 THEN'100-200' " +
  13. "when TIMESTAMPDIFF(DAY,ctime,end_time)>200 THEN'200+' ELSE '0-100' " +
  14. "End as lifecycle,count(-1)from machines GROUP BY lifecycle"
  15. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  16. return
  17. }
  18. defer rows.Close()
  19. for rows.Next() {
  20. mlc := &model.MachineLifeCycle{}
  21. if err = rows.Scan(&mlc.Duration, &mlc.Count); err != nil {
  22. return
  23. }
  24. machineLifeCycles = append(machineLifeCycles, mlc)
  25. }
  26. return
  27. }
  28. // MachineCountGroupByBusiness Machine Count Group By Business
  29. func (d *Dao) MachineCountGroupByBusiness() (machinesCount []*model.MachineCountGroupByBusiness, err error) {
  30. var rows *sql.Rows
  31. SQL := "SELECT business_unit,count(-1) as count FROM machines GROUP BY business_unit ORDER BY count DESC"
  32. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  33. return
  34. }
  35. defer rows.Close()
  36. for rows.Next() {
  37. mc := &model.MachineCountGroupByBusiness{}
  38. if err = rows.Scan(&mc.BusinessUnit, &mc.Count); err != nil {
  39. return
  40. }
  41. machinesCount = append(machinesCount, mc)
  42. }
  43. return
  44. }
  45. // MachineCountGroupByBusinessInRunning Machine Count Group By Business In Running
  46. func (d *Dao) MachineCountGroupByBusinessInRunning() (machinesCount []*model.MachineCountGroupByBusiness, err error) {
  47. var rows *sql.Rows
  48. SQL := "SELECT business_unit,count(-1) as count FROM machines where status>=100 GROUP BY business_unit ORDER BY count DESC"
  49. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  50. return
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. mc := &model.MachineCountGroupByBusiness{}
  55. if err = rows.Scan(&mc.BusinessUnit, &mc.Count); err != nil {
  56. return
  57. }
  58. machinesCount = append(machinesCount, mc)
  59. }
  60. return
  61. }
  62. // MachineWillBeExpired Statistics Machine Will Be Expired
  63. func (d *Dao) MachineWillBeExpired() (machines []*model.MachineCreatedAndEndTime, err error) {
  64. var rows *sql.Rows
  65. SQL := "SELECT id,name,username,app,ctime,end_time from machines where `status`>=100 order by end_time limit 10"
  66. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  67. return
  68. }
  69. defer rows.Close()
  70. for rows.Next() {
  71. mc := &model.MachineCreatedAndEndTime{}
  72. if err = rows.Scan(&mc.ID, &mc.MachineName, &mc.Username, &mc.App, &mc.CreateTime, &mc.EndTime); err != nil {
  73. return
  74. }
  75. machines = append(machines, mc)
  76. }
  77. return
  78. }
  79. // MachineLatestCreated Machine Latest Created
  80. func (d *Dao) MachineLatestCreated() (machines []*model.MachineCreatedAndEndTime, err error) {
  81. var rows *sql.Rows
  82. SQL := "SELECT id,name,username,app,ctime,end_time from machines where `status`>=100 order by ctime desc limit 10"
  83. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  84. return
  85. }
  86. defer rows.Close()
  87. for rows.Next() {
  88. mc := &model.MachineCreatedAndEndTime{}
  89. if err = rows.Scan(&mc.ID, &mc.MachineName, &mc.Username, &mc.App, &mc.CreateTime, &mc.EndTime); err != nil {
  90. return
  91. }
  92. machines = append(machines, mc)
  93. }
  94. return
  95. }
  96. // MobileMachineUserUsageCount Mobile Machine User Usage Count
  97. func (d *Dao) MobileMachineUserUsageCount() (mobileMachinesUsageCount []*model.MobileMachineUserUsageCount, err error) {
  98. var rows *sql.Rows
  99. SQL := fmt.Sprintf("select username,count(username) as count from mobile_machine_logs "+
  100. "where operation_type='%s' and operation_result ='%s' "+
  101. "group by username order by count desc", model.MBBindLog, model.OperationSuccessForMachineLog)
  102. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  103. return
  104. }
  105. defer rows.Close()
  106. for rows.Next() {
  107. mc := &model.MobileMachineUserUsageCount{}
  108. if err = rows.Scan(&mc.Username, &mc.Count); err != nil {
  109. return
  110. }
  111. mobileMachinesUsageCount = append(mobileMachinesUsageCount, mc)
  112. }
  113. return
  114. }
  115. // MobileMachineUserLendCount Mobile Machine Lend Count
  116. func (d *Dao) MobileMachineUserLendCount() (mobileMachinesUsageCount []*model.MobileMachineUserUsageCount, err error) {
  117. var rows *sql.Rows
  118. SQL := fmt.Sprintf("select username,count(username) as count from mobile_machine_logs "+
  119. "where operation_type='%s' and operation_result ='%s' "+
  120. "group by username order by count desc", model.MBLendOutLog, model.OperationSuccessForMachineLog)
  121. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. mc := &model.MobileMachineUserUsageCount{}
  127. if err = rows.Scan(&mc.Username, &mc.Count); err != nil {
  128. return
  129. }
  130. mobileMachinesUsageCount = append(mobileMachinesUsageCount, mc)
  131. }
  132. return
  133. }
  134. // MobileMachineUsageCount Mobile Machine Usage Count
  135. func (d *Dao) MobileMachineUsageCount() (mobileMachinesUsageCount []*model.MobileMachineUsageCount, err error) {
  136. var rows *sql.Rows
  137. SQL := fmt.Sprintf("select b.id,b.name,count(b.name) as count from mobile_machine_logs as a "+
  138. "left join mobile_machines as b on a.machine_id = b.id "+
  139. "where a.operation_type='%s' and a.operation_result = '%s' "+
  140. "group by b.id,b.`name` order by count desc", model.MBBindLog, model.OperationSuccessForMachineLog)
  141. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  142. return
  143. }
  144. defer rows.Close()
  145. for rows.Next() {
  146. mc := &model.MobileMachineUsageCount{}
  147. if err = rows.Scan(&mc.MobileMachineID, &mc.MobileMachineName, &mc.Count); err != nil {
  148. return
  149. }
  150. mobileMachinesUsageCount = append(mobileMachinesUsageCount, mc)
  151. }
  152. return
  153. }
  154. // MobileMachineLendCount Mobile Machine Lend Count
  155. func (d *Dao) MobileMachineLendCount() (mobileMachinesUsageCount []*model.MobileMachineUsageCount, err error) {
  156. var rows *sql.Rows
  157. SQL := fmt.Sprintf("select b.id,b.name,count(b.name) as count from mobile_machine_logs as a "+
  158. "left join mobile_machines as b on a.machine_id = b.id "+
  159. "where a.operation_type='%s' and a.operation_result = '%s' "+
  160. "group by b.id,b.`name` order by count desc", model.MBLendOutLog, model.OperationSuccessForMachineLog)
  161. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  162. return
  163. }
  164. defer rows.Close()
  165. for rows.Next() {
  166. mc := &model.MobileMachineUsageCount{}
  167. if err = rows.Scan(&mc.MobileMachineID, &mc.MobileMachineName, &mc.Count); err != nil {
  168. return
  169. }
  170. mobileMachinesUsageCount = append(mobileMachinesUsageCount, mc)
  171. }
  172. return
  173. }
  174. // MobileMachineModeCount Mobile Machine Mode Count.
  175. func (d *Dao) MobileMachineModeCount() (mobileMachinesTypeCount []*model.MobileMachineTypeCount, err error) {
  176. var rows *sql.Rows
  177. SQL := "select mode,count(mode) as count from mobile_machines where action > 0 group by mode order by count desc"
  178. if rows, err = d.db.Raw(SQL).Rows(); err != nil {
  179. return
  180. }
  181. defer rows.Close()
  182. for rows.Next() {
  183. mc := &model.MobileMachineTypeCount{}
  184. if err = rows.Scan(&mc.ModeName, &mc.Count); err != nil {
  185. return
  186. }
  187. mobileMachinesTypeCount = append(mobileMachinesTypeCount, mc)
  188. }
  189. return
  190. }
  191. // MobileMachineUseRecord Mobile Machine Use Record
  192. func (d *Dao) MobileMachineUseRecord() (mobileMachineLogs []*model.MobileMachineLog, err error) {
  193. err = d.db.Where("operation_type= ? or operation_type = ?", model.MBBindLog, model.MBReleaseLog).Order("machine_id,id").Find(&mobileMachineLogs).Error
  194. return
  195. }