device.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package controllers
  2. import (
  3. "errors"
  4. "fmt"
  5. "sparrow/pkg/rpcs"
  6. "sparrow/pkg/server"
  7. "sparrow/services/knowoapi/services"
  8. "strconv"
  9. "time"
  10. "github.com/kataras/iris"
  11. )
  12. // DeviceController api
  13. type DeviceController struct {
  14. Ctx iris.Context
  15. Service services.DeviceService
  16. Token
  17. }
  18. // Get 获取激活设备列表
  19. // GET /device?pi=&ps=&proid=&device_id=
  20. func (a *DeviceController) Get() {
  21. pi, err := a.Ctx.URLParamInt("pi")
  22. if err != nil {
  23. badRequest(a.Ctx, err)
  24. return
  25. }
  26. ps, err := a.Ctx.URLParamInt("ps")
  27. if err != nil {
  28. badRequest(a.Ctx, err)
  29. return
  30. }
  31. proid, err := a.Ctx.URLParamInt("proid")
  32. if err != nil {
  33. proid = 0
  34. }
  35. deviceid := a.Ctx.URLParam("device_id")
  36. vid := a.getVendorID(a.Ctx)
  37. datas, total, err := a.Service.GetDevices(vid, proid, pi, ps, deviceid)
  38. if err != nil {
  39. responseError(a.Ctx, ErrDatabase, err.Error())
  40. return
  41. }
  42. done(a.Ctx, map[string]interface{}{
  43. "list": datas,
  44. "total": total,
  45. })
  46. }
  47. //获取设备在线状态
  48. // GET /devicestatus?device_id=
  49. func (a *DeviceController) GetDevicestatus() {
  50. deviceid, err := a.Ctx.URLParamInt("device_id")
  51. if err != nil {
  52. deviceid = 0
  53. }
  54. onlineargs := rpcs.ArgsGetDeviceOnlineStatus{
  55. Id: uint64(deviceid),
  56. }
  57. onlinereply := rpcs.ReplyGetDeviceOnlineStatus{}
  58. err = server.RPCCallByName(nil, "devicemanager", "DeviceManager.GetDeviceOnlineStatus", onlineargs, &onlinereply)
  59. if err != nil {
  60. server.Log.Errorf("get devie online status error: %v", err)
  61. }
  62. var status int
  63. if int(onlinereply.HeartbeatInterval) > 0 && len(onlinereply.ClientIP) > 0 {
  64. status = 1
  65. }
  66. done(a.Ctx, map[string]interface{}{
  67. "status": status,
  68. })
  69. }
  70. // GetBannerdata 获取设备激活和活跃数据
  71. // GET /bannerdata?proid=
  72. func (a *DeviceController) GetBannerdata() {
  73. proid, err := a.Ctx.URLParamInt("proid")
  74. if err != nil {
  75. proid = 0
  76. }
  77. var (
  78. //今日激活
  79. tact = 0
  80. // 今日活跃
  81. tlive = 0
  82. // 昨日激活
  83. yact = 0
  84. // 昨日活跃
  85. ylive = 0
  86. // 累计激活
  87. totolact = 0
  88. // 环比昨日激活
  89. comparedWithYesterdayActive = 0.00
  90. //活跃占比
  91. rateOfTodayLive = 0.00
  92. //环比昨日活跃
  93. comparedWithYesterdayLive = 0.00
  94. )
  95. vendorid := a.getVendorID(a.Ctx)
  96. totolact, err = a.Service.GetDeviceCount(vendorid)
  97. if err != nil {
  98. responseError(a.Ctx, ErrDatabase, err.Error())
  99. return
  100. }
  101. today := time.Now().Format("2006-01-02")
  102. yestoday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  103. tact, err = a.Service.GetActiveNumberOfDate(vendorid, today)
  104. if err != nil {
  105. responseError(a.Ctx, ErrDatabase, err.Error())
  106. return
  107. }
  108. tlive, err = a.Service.GetLivelyCountOfDate(vendorid, today)
  109. if err != nil {
  110. responseError(a.Ctx, ErrDatabase, err.Error())
  111. return
  112. }
  113. yact, err = a.Service.GetActiveNumberOfDate(vendorid, yestoday)
  114. if err != nil {
  115. responseError(a.Ctx, ErrDatabase, err.Error())
  116. return
  117. }
  118. ylive, err = a.Service.GetLivelyCountOfDate(vendorid, yestoday)
  119. if err != nil {
  120. responseError(a.Ctx, ErrDatabase, err.Error())
  121. return
  122. }
  123. if tact != 0 && yact != 0 {
  124. comparedWithYesterdayActive = (float64(tact) / float64(yact)) * 100
  125. }
  126. if tlive != 0 && ylive != 0 {
  127. comparedWithYesterdayLive = (float64(tlive) / float64(ylive)) * 100
  128. rateOfTodayLive = float64(tlive) / float64(totolact) * 100
  129. }
  130. //TODO 这里的productid要去掉
  131. result := map[string]interface{}{
  132. "todayAct": tact,
  133. "todayLive": tlive,
  134. "yestodayAct": yact,
  135. "yestodayLive": ylive,
  136. "totalAct": totolact,
  137. "comparedWithYesterdayActive": fmt.Sprintf("%s%%", strconv.FormatFloat(comparedWithYesterdayActive, 'f', 2, 64)),
  138. "comparedWithYesterdayLive": fmt.Sprintf("%s%%", strconv.FormatFloat(comparedWithYesterdayLive, 'f', 2, 64)),
  139. "rateOfTodayLive": fmt.Sprintf("%s%%", strconv.FormatFloat(rateOfTodayLive, 'f', 2, 64)),
  140. "productid": proid,
  141. }
  142. done(a.Ctx, result)
  143. }
  144. // GetActivechart 获取N日的激活数据趋势
  145. func (a *DeviceController) GetActivechart() {
  146. // proid, err := a.Ctx.URLParamInt("proid")
  147. // if err != nil {
  148. // proid = 0
  149. // }
  150. days, err := a.Ctx.URLParamInt("days")
  151. if err != nil {
  152. days = 7
  153. }
  154. if days > 30 {
  155. badRequest(a.Ctx, errors.New("非法参数"))
  156. return
  157. }
  158. vendorid := a.getVendorID(a.Ctx)
  159. datas, err := a.Service.GetActiveOfNumDays(vendorid, days)
  160. if err != nil {
  161. responseError(a.Ctx, ErrDatabase, err.Error())
  162. return
  163. }
  164. done(a.Ctx, map[string]interface{}{
  165. "chart": datas,
  166. })
  167. }
  168. // GetLivechart 获取N日内的活跃数据
  169. func (a *DeviceController) GetLivechart() {
  170. // proid, err := a.Ctx.URLParamInt("proid")
  171. // if err != nil {
  172. // proid = 0
  173. // }
  174. days, err := a.Ctx.URLParamInt("days")
  175. if err != nil {
  176. days = 7
  177. }
  178. if days > 30 {
  179. badRequest(a.Ctx, errors.New("非法参数"))
  180. return
  181. }
  182. vendorid := a.getVendorID(a.Ctx)
  183. datas, err := a.Service.GetLivelyOfNumDays(vendorid, days)
  184. if err != nil {
  185. responseError(a.Ctx, ErrDatabase, err.Error())
  186. return
  187. }
  188. done(a.Ctx, map[string]interface{}{
  189. "chart": datas,
  190. })
  191. }