task.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package http
  2. import (
  3. "net/http"
  4. "strconv"
  5. "time"
  6. "go-common/app/admin/main/videoup/model/archive"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/render"
  11. )
  12. func taskTooks(c *bm.Context) {
  13. req := c.Request
  14. params := req.Form
  15. stimeStr := params.Get("stime")
  16. etimeStr := params.Get("etime")
  17. if stimeStr == "" {
  18. stimeStr = time.Now().Format("2006-01-02") + " 00:00:00"
  19. }
  20. if etimeStr == "" {
  21. etimeStr = time.Now().Format("2006-01-02 15:04:05")
  22. }
  23. local, _ := time.LoadLocation("Local")
  24. stime, err := time.ParseInLocation("2006-01-02 15:04:05", stimeStr, local)
  25. if stime.Unix() < 1 || err != nil {
  26. c.JSON(nil, ecode.RequestErr)
  27. return
  28. }
  29. etime, err := time.ParseInLocation("2006-01-02 15:04:05", etimeStr, local)
  30. if etime.Unix() < 1 || err != nil {
  31. c.JSON(nil, ecode.RequestErr)
  32. return
  33. }
  34. tooks, err := vdaSvc.TaskTooksByHalfHour(c, stime, etime)
  35. if err != nil {
  36. c.JSON(nil, err)
  37. return
  38. }
  39. c.JSON(tooks, nil)
  40. }
  41. func next(c *bm.Context) {
  42. uidS := c.Request.Form.Get("uid")
  43. uid, err := strconv.Atoi(uidS)
  44. if err != nil {
  45. c.JSON(nil, ecode.RequestErr)
  46. return
  47. }
  48. task, err := vdaSvc.Next(c, int64(uid))
  49. if err != nil {
  50. log.Error("vdaSvc.Next(uid=%d) error(%v)", uid, err)
  51. c.JSON(nil, err)
  52. return
  53. }
  54. c.JSON(task, nil)
  55. }
  56. func list(c *bm.Context) {
  57. v := new(struct {
  58. UID int64 `form:"uid" default:"0"`
  59. IsLeader int8 `form:"isleader" default:"0"`
  60. Lt int8 `form:"listtype" default:"0"`
  61. Pn int `form:"page" default:"1"`
  62. Ps int `form:"pagesize" default:"20"`
  63. })
  64. err := c.Bind(v)
  65. if err != nil {
  66. c.JSON(nil, ecode.RequestErr)
  67. return
  68. }
  69. tasks, err := vdaSvc.List(c, v.UID, v.Pn, v.Ps, v.Lt, v.IsLeader)
  70. if err != nil {
  71. log.Error("vdaSvc.List(uid=%d,page=%d, pagesize=%d, listtype=%d, isleader=%d) error(%v)",
  72. v.UID, v.Pn, v.Ps, v.Lt, v.IsLeader, err)
  73. c.JSON(nil, err)
  74. return
  75. }
  76. c.JSON(tasks, nil)
  77. }
  78. func info(c *bm.Context) {
  79. tidS := c.Request.Form.Get("taskid")
  80. tid, err := strconv.Atoi(tidS)
  81. if err != nil {
  82. c.JSON(nil, ecode.RequestErr)
  83. return
  84. }
  85. task, err := vdaSvc.Info(c, int64(tid))
  86. if err != nil {
  87. log.Error("vdaSvc.Info(taskid=%d) error(%v)", tid, err)
  88. c.JSON(nil, err)
  89. return
  90. }
  91. c.JSON(task, nil)
  92. }
  93. // 权重管理
  94. func addwtconf(c *bm.Context) {
  95. var err error
  96. cfg := &archive.WeightConf{}
  97. if err = c.Bind(cfg); err != nil {
  98. log.Error("addwtconf error(%v)", err)
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. uid, uname := getUIDName(c)
  103. err = vdaSvc.AddWeightConf(c, cfg, uid, uname)
  104. if err != nil {
  105. data := map[string]interface{}{
  106. "code": ecode.RequestErr,
  107. "message": err.Error(),
  108. }
  109. c.Render(http.StatusOK, render.MapJSON(data))
  110. return
  111. }
  112. c.JSON(nil, nil)
  113. }
  114. func delwtconf(c *bm.Context) {
  115. var err error
  116. ids := c.Request.Form.Get("id")
  117. id, err := strconv.Atoi(ids)
  118. if err != nil {
  119. c.JSON(nil, ecode.RequestErr)
  120. return
  121. }
  122. c.JSON(vdaSvc.DelWeightConf(c, int64(id)), nil)
  123. }
  124. func listwtconf(c *bm.Context) {
  125. v := new(archive.Confs)
  126. if err := c.Bind(v); err != nil {
  127. c.JSON(nil, ecode.RequestErr)
  128. return
  129. }
  130. c.JSON(vdaSvc.ListWeightConf(c, v))
  131. }
  132. func maxweight(c *bm.Context) {
  133. c.JSON(vdaSvc.MaxWeight(c))
  134. }
  135. func listwtlog(c *bm.Context) {
  136. v := new(struct {
  137. Taskid int64 `form:"taskid" validate:"required"`
  138. Pn int `form:"page" default:"1"`
  139. Ps int `form:"ps" default:"20"`
  140. })
  141. if err := c.Bind(v); err != nil {
  142. c.JSON(nil, ecode.RequestErr)
  143. return
  144. }
  145. cfg, count, err := vdaSvc.ListWeightLogs(c, v.Taskid, v.Pn)
  146. if err != nil {
  147. c.JSON(nil, err)
  148. return
  149. }
  150. data := make(map[string]interface{})
  151. data["data"] = cfg
  152. data["pager"] = map[string]int{
  153. "current_page": v.Pn,
  154. "total_items": int(count),
  155. "page_size": 20,
  156. }
  157. c.JSONMap(data, err)
  158. }
  159. func show(c *bm.Context) {
  160. c.JSON(vdaSvc.ShowWeightVC(c))
  161. }
  162. func set(c *bm.Context) {
  163. v := new(archive.WeightVC)
  164. if err := c.Bind(v); err != nil {
  165. c.JSON(nil, ecode.RequestErr)
  166. return
  167. }
  168. c.JSON(nil, vdaSvc.SetWeightVC(c, v))
  169. }
  170. // 登录管理
  171. func on(c *bm.Context) {
  172. uid, uname := getUIDName(c)
  173. err := vdaSvc.HandsUp(c, uid, uname)
  174. if err != nil {
  175. data := map[string]interface{}{
  176. "code": ecode.RequestErr,
  177. "message": err.Error(),
  178. }
  179. c.Render(http.StatusOK, render.MapJSON(data))
  180. return
  181. }
  182. c.JSON(nil, nil)
  183. }
  184. // 踢出
  185. func forceoff(c *bm.Context) {
  186. uidS := c.Request.Form.Get("uid")
  187. uid, _ := strconv.ParseInt(uidS, 10, 64)
  188. if uid == 0 {
  189. c.JSON(nil, ecode.RequestErr)
  190. return
  191. }
  192. adminuid, _ := getUIDName(c)
  193. err := vdaSvc.HandsOff(c, adminuid, uid)
  194. if err != nil {
  195. data := map[string]interface{}{
  196. "code": ecode.RequestErr,
  197. "message": err.Error(),
  198. }
  199. c.Render(http.StatusOK, render.MapJSON(data))
  200. return
  201. }
  202. c.JSON(nil, nil)
  203. }
  204. func off(c *bm.Context) {
  205. adminuid, _ := getUIDName(c)
  206. err := vdaSvc.HandsOff(c, adminuid, 0)
  207. if err != nil {
  208. data := map[string]interface{}{
  209. "code": ecode.RequestErr,
  210. "message": err.Error(),
  211. }
  212. c.Render(http.StatusOK, render.MapJSON(data))
  213. return
  214. }
  215. c.JSON(nil, nil)
  216. }
  217. func online(c *bm.Context) {
  218. c.JSON(vdaSvc.Online(c))
  219. }
  220. func inoutlist(c *bm.Context) {
  221. v := new(struct {
  222. Unames string `form:"unames" default:""`
  223. Bt string `form:"bt" default:""`
  224. Et string `form:"et" default:""`
  225. })
  226. if err := c.Bind(v); err != nil {
  227. c.JSON(nil, ecode.RequestErr)
  228. return
  229. }
  230. c.JSON(vdaSvc.InOutList(c, v.Unames, v.Bt, v.Et))
  231. }
  232. // 任务管理
  233. func delay(c *bm.Context) {
  234. v := new(struct {
  235. Taskid int64 `form:"task_id" validate:"required"`
  236. Reason string `form:"reason"`
  237. })
  238. if err := c.Bind(v); err != nil {
  239. c.JSON(nil, ecode.RequestErr)
  240. return
  241. }
  242. uid, _ := getUIDName(c)
  243. c.JSON(nil, vdaSvc.Delay(c, v.Taskid, uid, v.Reason))
  244. }
  245. func taskfree(c *bm.Context) {
  246. var uid, rows int64
  247. uid, _ = getUIDName(c)
  248. rows = vdaSvc.Free(c, uid)
  249. log.Info("释放任务(uid=%d,rows=%d)", uid, rows)
  250. c.JSON(nil, nil)
  251. }
  252. func checkgroup() bm.HandlerFunc {
  253. return func(ctx *bm.Context) {
  254. uid, _ := getUIDName(ctx)
  255. role, err := vdaSvc.CheckGroup(ctx, uid)
  256. if err != nil || role == 0 {
  257. data := map[string]interface{}{
  258. "code": ecode.RequestErr,
  259. "message": "权限错误",
  260. }
  261. ctx.Render(http.StatusOK, render.MapJSON(data))
  262. ctx.Abort()
  263. return
  264. }
  265. }
  266. }
  267. // 校验任务操作权限
  268. func checkowner() bm.HandlerFunc {
  269. return func(ctx *bm.Context) {
  270. tidS := ctx.Request.Form.Get("task_id")
  271. tid, err := strconv.Atoi(tidS)
  272. if err != nil {
  273. ctx.JSON(nil, ecode.RequestErr)
  274. ctx.Abort()
  275. return
  276. }
  277. uid, _ := getUIDName(ctx)
  278. if err := vdaSvc.CheckOwner(ctx, int64(tid), uid); err != nil {
  279. data := map[string]interface{}{
  280. "code": ecode.RequestErr,
  281. "message": err.Error(),
  282. }
  283. ctx.Render(http.StatusOK, render.MapJSON(data))
  284. ctx.Abort()
  285. return
  286. }
  287. }
  288. }