启动页参数改为非必传、v2发起接口

master
luziqi 3 years ago
parent cdb85768b7
commit 6f0a13f99c

@ -5,6 +5,7 @@ import (
"autogo/dbsql"
"autogo/models"
"autogo/monkey"
"strings"
)
func CheckMonkeyTasks() {
@ -18,8 +19,12 @@ func CheckMonkeyTasks() {
db.Model(models.MonkeyTask{}).Where("status = ? AND is_del = 0", "WAITTING").Find(&list)
for _, task := range list {
product_name := task.Product
if strings.Contains(task.Product, "-") {
product_name = strings.Split(task.Product, "-")[0]
}
var device models.Device
db.Table("device").Model(models.Device{}).Where("project = ? AND status = ?", task.Project, "online").First(&device)
db.Table("device").Model(models.Device{}).Where("project = ? AND product_name = ? AND status = ?", task.Project, product_name, "online").First(&device)
if device.ID < 1 {
// 没有空闲设备
continue

@ -918,6 +918,35 @@ const docTemplate = `{
}
}
},
"/api/monkey/v2/task/command": {
"get": {
"description": "通过任务id获取任务的执行命令",
"consumes": [
"application/x-www-form-urlencoded"
],
"tags": [
"Monkey相关 /api/monkey/v2/"
],
"summary": "获取Monkey任务的执行命令",
"parameters": [
{
"type": "integer",
"description": "任务id",
"name": "task_id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "返回执行命令",
"schema": {
"$ref": "#/definitions/models.Response"
}
}
}
}
},
"/api/monkey/v2/task/handle/set": {
"post": {
"description": "更新Monkey任务跟进状态",

@ -906,6 +906,35 @@
}
}
},
"/api/monkey/v2/task/command": {
"get": {
"description": "通过任务id获取任务的执行命令",
"consumes": [
"application/x-www-form-urlencoded"
],
"tags": [
"Monkey相关 /api/monkey/v2/"
],
"summary": "获取Monkey任务的执行命令",
"parameters": [
{
"type": "integer",
"description": "任务id",
"name": "task_id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "返回执行命令",
"schema": {
"$ref": "#/definitions/models.Response"
}
}
}
}
},
"/api/monkey/v2/task/handle/set": {
"post": {
"description": "更新Monkey任务跟进状态",

@ -608,6 +608,25 @@ paths:
summary: 新建Monkey任务
tags:
- Monkey相关 /api/monkey/v2/
/api/monkey/v2/task/command:
get:
consumes:
- application/x-www-form-urlencoded
description: 通过任务id获取任务的执行命令
parameters:
- description: 任务id
in: query
name: task_id
required: true
type: integer
responses:
"200":
description: 返回执行命令
schema:
$ref: '#/definitions/models.Response'
summary: 获取Monkey任务的执行命令
tags:
- Monkey相关 /api/monkey/v2/
/api/monkey/v2/task/handle/set:
post:
consumes:

24
env/env.go vendored

@ -1,24 +0,0 @@
package env
import (
"autogo/dbsql"
"autogo/models"
"fmt"
)
func InitDB() {
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
return
}
err = db.AutoMigrate(&models.Base{})
if err != nil {
fmt.Println(err)
return
}
err = db.AutoMigrate(&models.MonkeyTask{})
if err != nil {
fmt.Println(err)
return
}
}

@ -5,7 +5,6 @@ import (
"autogo/crontask"
"autogo/dbsql"
"autogo/device"
"autogo/env"
"autogo/monkey"
"autogo/qatool"
"flag"
@ -38,7 +37,7 @@ func main() {
}
dbsql.DSN = dbsql.DSN_local
env.InitDB()
// env.InitDB()
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
@ -73,6 +72,7 @@ func main() {
// monkey v2
r.POST("/api/monkey/v2/create_task", monkey.CreateTaskV2)
r.POST("/api/monkey/v2/task/handle/set", monkey.UpdateHandleStatus)
r.GET("/api/monkey/v2/task/command", monkey.GetTaskCommand)
// device
r.GET("/api/device/v1/list", device.GetDevices)

@ -50,9 +50,9 @@ func (t *MonkeyTask) Check() error {
if t.Platform == "" {
return errors.New("task.Platform为空")
}
if t.LaunchActivity == "" && t.Platform == "adr" {
return errors.New("task.LaunchActivity为空platform=adr时需要传入LaunchActivity参数")
}
// if t.LaunchActivity == "" && t.Platform == "adr" {
// return errors.New("task.LaunchActivity为空platform=adr时需要传入LaunchActivity参数")
// }
if t.PackageURL == "" {
return errors.New("task.PackageURL为空")
}

@ -38,7 +38,9 @@ func RunAndroidMonkeyCmd(task models.MonkeyTask, udid string) {
log.Debug("下载完毕,安装包路径", pkg_path)
}
cmd_content += " -package " + task.PackageName
cmd_content += " -launch_activity " + task.LaunchActivity
if task.LaunchActivity != "" {
cmd_content += " -launch_activity " + task.LaunchActivity
}
cmd_content += " -run_time " + cast.ToString(task.RunTime)
if udid != "" {
cmd_content += " -device_udid " + udid

@ -8,8 +8,10 @@ import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
"gorm.io/gorm"
)
@ -152,6 +154,57 @@ func UpdateHandleStatus(c *gin.Context) {
c.JSON(http.StatusOK, rsp.Success())
}
// @Tags Monkey相关 /api/monkey/v2/
// @Summary 获取Monkey任务的执行命令
// @Description 通过任务id获取任务的执行命令
// @accept x-www-form-urlencoded
// @Param task_id query int true "任务id"
// @Success 200 {object} models.Response "返回执行命令"
// @Router /api/monkey/v2/task/command [get]
func GetTaskCommand(c *gin.Context) {
rsp := controllers.NewResponse()
task_id := cast.ToInt(c.Query("task_id"))
if task_id < 1 {
c.JSON(http.StatusOK, rsp.Error("参数task_id错误"+c.PostForm("task_id")))
return
}
var task models.MonkeyTask
db, err := dbsql.GetConn(dbsql.DSN)
if err != nil {
rsp.Error(err.Error())
c.JSON(http.StatusOK, rsp)
return
}
defer dbsql.Close(db)
db.Where("id = ?", task_id).Find(&task)
cmd_content := "cd /home/app/monkey && nohup python3 main_adr.py" +
" -task_id " + cast.ToString(task.Id)
if task.PackageURL != "" {
pkg_path := "/home/tmp/pkg/" + cast.ToString(time.Now().Unix()) + ".apk"
log.Debug("需要下载apk", task.PackageURL, "-O", pkg_path)
cmd_content += " -pkg_path " + pkg_path
log.Debug("下载完毕,安装包路径", pkg_path)
}
cmd_content += " -package " + task.PackageName
if task.LaunchActivity != "" {
cmd_content += " -launch_activity " + task.LaunchActivity
}
cmd_content += " -run_time " + cast.ToString(task.RunTime)
if task.Devices != "" {
cmd_content += " -device_udid " + task.Devices
} else {
cmd_content += " -device_udid [udid] "
}
cmd_content += " >> /home/app/logs/task_output/task_" + cast.ToString(task.Id) + ".log 2>&1"
rsp.Data = cmd_content
c.JSON(http.StatusOK, rsp.Success())
}
// 根据id查询monkey任务信息db传nil时会另外新建数据库链接
func getTaskById(id int, db *gorm.DB) (models.MonkeyTask, error) {
var task models.MonkeyTask

Loading…
Cancel
Save