script.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package http
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "go-common/app/admin/ep/melloi/conf"
  6. "go-common/app/admin/ep/melloi/model"
  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/binding"
  11. )
  12. func queryScripts(c *bm.Context) {
  13. qsr := model.QueryScriptRequest{}
  14. if err := c.BindWith(&qsr, binding.Form); err != nil {
  15. c.JSON(nil, err)
  16. return
  17. }
  18. qsr.Active = 1
  19. if err := qsr.Verify(); err != nil {
  20. c.JSON(nil, err)
  21. return
  22. }
  23. sessionID, err := c.Request.Cookie("_AJSESSIONID")
  24. if err != nil {
  25. c.JSON(nil, err)
  26. return
  27. }
  28. res, err := srv.QueryScriptsByPage(c, sessionID.Value, &qsr)
  29. if err != nil {
  30. log.Error("queryScripts Error", err)
  31. return
  32. }
  33. c.JSON(res, err)
  34. }
  35. func queryScripysFree(c *bm.Context) {
  36. script := model.Script{}
  37. if err := c.BindWith(&script, binding.Form); err != nil {
  38. c.JSON(nil, err)
  39. return
  40. }
  41. script.Active = 1
  42. res, err := srv.QueryScripts(&script, 1, 200)
  43. if err != nil {
  44. log.Error("queryScripts Error", err)
  45. return
  46. }
  47. var resMap = make(map[string]interface{})
  48. resMap["scripts"] = res
  49. c.JSON(resMap, err)
  50. }
  51. func queryScriptSnap(c *bm.Context) {
  52. scriptSnap := model.ScriptSnap{}
  53. if err := c.BindWith(&scriptSnap, binding.Form); err != nil {
  54. c.JSON(nil, err)
  55. return
  56. }
  57. scriptSnaps, err := srv.QueryScriptSnap(&scriptSnap)
  58. if err != nil {
  59. log.Error("QueryScriptSnap Error", err)
  60. return
  61. }
  62. var resMap = make(map[string]interface{})
  63. resMap["scriptSnaps"] = scriptSnaps
  64. c.JSON(resMap, nil)
  65. }
  66. func addJmeterSample(c *bm.Context) {
  67. script := model.Script{}
  68. if err := c.BindWith(&script, binding.JSON); err != nil {
  69. c.JSON(nil, err)
  70. return
  71. }
  72. c.JSON(srv.AddJmeterSample(&script))
  73. }
  74. func addThreadGroup(c *bm.Context) {
  75. script := model.Script{}
  76. sceneTyped := c.Request.Form.Get("scene_type")
  77. sceneType, err := strconv.Atoi(sceneTyped)
  78. if err != nil {
  79. log.Error("your string cannot strconv to int ")
  80. return
  81. }
  82. if err := c.BindWith(&script, binding.JSON); err != nil {
  83. c.JSON(nil, err)
  84. return
  85. }
  86. c.JSON(srv.AddThreadGroup(&script, sceneType))
  87. }
  88. func getThreadGroup(c *bm.Context) {
  89. scrThreadGroup := model.ScrThreadGroup{}
  90. if err := c.BindWith(&scrThreadGroup, binding.JSON); err != nil {
  91. c.JSON(nil, err)
  92. return
  93. }
  94. c.JSON(srv.GetThreadGroup(scrThreadGroup))
  95. }
  96. func addAndExecuteScript(c *bm.Context) {
  97. script := model.Script{}
  98. if err := c.BindWith(&script, binding.JSON); err != nil {
  99. c.JSON(nil, err)
  100. return
  101. }
  102. scene := model.Scene{}
  103. if err := c.BindWith(&scene, binding.Form); err != nil {
  104. c.JSON(nil, err)
  105. return
  106. }
  107. // 压测时间段检查
  108. userName, _ := c.Request.Cookie("username")
  109. // 非debug逻辑 && 需要检查 && 非保存 && 非复制
  110. if !script.IsDebug && conf.Conf.Melloi.CheckTime && !script.IsSave && !script.IsCopy {
  111. //if !srv.CheckRunTime() {
  112. // if !srv.CheckRunPermission(userName.Value) {
  113. // c.JSON("Non-working time", ecode.MelloiRunNotInTime)
  114. // return
  115. // }
  116. //}
  117. if !srv.CheckRunPermission(userName.Value) {
  118. c.JSON("Non-working time", ecode.MelloiRunNotInTime)
  119. return
  120. }
  121. }
  122. JSON, _ := json.Marshal(script)
  123. log.Info("script:----------", string(JSON))
  124. cookie := c.Request.Header.Get("Cookie")
  125. resp, err := srv.AddAndExcuScript(c, &script, cookie, &scene, true, false)
  126. if err != nil {
  127. c.JSON(nil, err)
  128. return
  129. }
  130. c.JSON(resp, err)
  131. }
  132. func delScript(c *bm.Context) {
  133. id := c.Request.Form.Get("id")
  134. ID, err := strconv.Atoi(id)
  135. if err != nil {
  136. c.JSON(nil, err)
  137. return
  138. }
  139. c.JSON(nil, srv.DelScript(ID))
  140. }
  141. func updateScript(c *bm.Context) {
  142. script := model.Script{}
  143. if err := c.BindWith(&script, binding.JSON); err != nil {
  144. c.JSON(nil, err)
  145. return
  146. }
  147. updateResult, err := srv.UpdateScript(&script)
  148. if err != nil {
  149. log.Error("UpdateScript err (%v)", err)
  150. return
  151. }
  152. var resMap = make(map[string]string)
  153. resMap["update_result"] = updateResult
  154. c.JSON(resMap, err)
  155. }
  156. func updateScriptAll(c *bm.Context) {
  157. script := model.Script{}
  158. if err := c.BindWith(&script, binding.JSON); err != nil {
  159. c.JSON(nil, err)
  160. return
  161. }
  162. scene := model.Scene{}
  163. if err := c.BindWith(&scene, binding.Form); err != nil {
  164. c.JSON(nil, err)
  165. return
  166. }
  167. updateResult, err := srv.UpdateScriptAll(&script, &scene)
  168. if err != nil {
  169. log.Error("UpdateScriptAll err (%v)", err)
  170. return
  171. }
  172. var resMap = make(map[string]string)
  173. resMap["update_result"] = updateResult
  174. c.JSON(resMap, err)
  175. }
  176. func runTimeCheck(c *bm.Context) {
  177. // 压测时间段检查
  178. userName, _ := c.Request.Cookie("username")
  179. // 暂时去掉压测时间
  180. //if !srv.CheckRunTime() {
  181. // if !srv.CheckRunPermission(userName.Value) {
  182. // c.JSON("Non-working time", ecode.MelloiRunNotInTime)
  183. // return
  184. // }
  185. //}
  186. if !srv.CheckRunPermission(userName.Value) {
  187. c.JSON("Non-working time", ecode.MelloiRunNotInTime)
  188. return
  189. }
  190. c.JSON(nil, nil)
  191. }
  192. func urlCheck(c *bm.Context) {
  193. // 检查url是否包含json串
  194. script := model.Script{}
  195. if err := c.BindWith(&script, binding.JSON); err != nil {
  196. c.JSON(nil, err)
  197. return
  198. }
  199. c.JSON(srv.URLCheck(&script))
  200. }
  201. // 增加定时器配置
  202. func addTimer(c *bm.Context) {
  203. script := model.Script{}
  204. if err := c.BindWith(&script, binding.JSON); err != nil {
  205. c.JSON(nil, err)
  206. return
  207. }
  208. c.JSON(nil, srv.AddTimer(&script))
  209. }