From fefb89c9cbe11a27cfe4ace018f91bd17cd262a1 Mon Sep 17 00:00:00 2001 From: Doflatango Date: Tue, 7 Feb 2017 18:05:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=98=BE=E7=A4=BA=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=92=8C=E6=97=A5=E5=BF=97=E8=AF=A6=E7=BB=86=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/job_log.go | 29 ++++++ web/job_log.go | 70 +++++++++++++ web/routers.go | 8 ++ web/ui/src/components/JobEdit.vue | 17 +++- web/ui/src/components/JobEditRule.vue | 4 +- web/ui/src/components/Log.vue | 139 ++++++++++++++++++++++---- web/ui/src/components/LogDetail.vue | 53 ++++++++++ web/ui/src/main.js | 2 + 8 files changed, 296 insertions(+), 26 deletions(-) create mode 100644 models/job_log.go create mode 100644 web/job_log.go create mode 100644 web/ui/src/components/LogDetail.vue diff --git a/models/job_log.go b/models/job_log.go new file mode 100644 index 0000000..d4520c6 --- /dev/null +++ b/models/job_log.go @@ -0,0 +1,29 @@ +package models + +import ( + "time" + + "gopkg.in/mgo.v2/bson" +) + +// 任务执行记录 +type JobLog struct { + Id bson.ObjectId `bson:"_id,omitempty" json:"id"` + JobId string `bson:"jobId" json:"jobId"` // 任务 Id,索引 + JobGroup string `bson:"jobGroup" json:"jobGroup"` // 任务分组,配合 Id 跳转用 + Name string `bson:"name" json:"name"` // 任务名称 + Node string `bson:"node" json:"node"` // 运行此次任务的节点 ip,索引 + Command string `bson:"command" json:"command,omitempty"` // 执行的命令,包括参数 + Output string `bson:"output" json:"output,omitempty"` // 任务输出的所有内容 + ExitCode uint8 `bson:"exitCode" json:"exitCode"` // 脚本退出状态码 + BeginTime time.Time `bson:"beginTime" json:"beginTime"` // 任务开始执行时间,精确到毫秒,索引 + EndTime time.Time `bson:"endTime" json:"endTime"` // 任务执行完毕时间,精确到毫秒 +} + +func GetJobLogById(id bson.ObjectId) (*JobLog, error) { + return nil, nil +} + +func GetJobLogList(query interface{}, skip, limit int) (list []*JobLog, err error) { + return +} diff --git a/web/job_log.go b/web/job_log.go new file mode 100644 index 0000000..53256fd --- /dev/null +++ b/web/job_log.go @@ -0,0 +1,70 @@ +package web + +import ( + "net/http" + "strings" + "time" + + "github.com/gorilla/mux" + "gopkg.in/mgo.v2" + "gopkg.in/mgo.v2/bson" + + "sunteng/cronsun/models" +) + +type JobLog struct{} + +func (jl *JobLog) GetDetail(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + id := strings.TrimSpace(vars["id"]) + if len(id) == 0 { + outJSONWithCode(w, http.StatusBadRequest, "empty log id.") + return + } + + logDetail, err := models.GetJobLogById(bson.ObjectIdHex(id)) + if err != nil { + statusCode := http.StatusInternalServerError + if err == mgo.ErrNotFound { + statusCode = http.StatusNotFound + err = nil + } + outJSONWithCode(w, statusCode, err) + return + } + + outJSON(w, logDetail) +} + +func (jl *JobLog) GetList(w http.ResponseWriter, r *http.Request) { + list := []*models.JobLog{ + &models.JobLog{ + Id: bson.NewObjectId(), + Name: "test1", + JobId: "test1", + Node: "192.168.1.2", + ExitCode: 0, + BeginTime: time.Now(), + EndTime: time.Now().Add(time.Duration(time.Minute)), + }, + &models.JobLog{ + Id: bson.NewObjectId(), + Name: "test2", + JobId: "test2", + Node: "192.168.1.2", + ExitCode: 1, + BeginTime: time.Now(), + EndTime: time.Now().Add(time.Duration(350 * time.Millisecond)), + }, + &models.JobLog{ + Id: bson.NewObjectId(), + Name: "test3", + JobId: "test3", + Node: "192.168.1.3", + ExitCode: 0, + BeginTime: time.Now(), + EndTime: time.Now().Add(time.Duration(time.Hour * 12)), + }, + } + outJSON(w, list) +} diff --git a/web/routers.go b/web/routers.go index 05b4611..e0920e0 100644 --- a/web/routers.go +++ b/web/routers.go @@ -12,6 +12,7 @@ import ( func InitRouters() (s *http.Server, err error) { jobHandler := &Job{} nodeHandler := &Node{} + jobLogHandler := &JobLog{} r := mux.NewRouter() subrouter := r.PathPrefix("/v1").Subrouter() @@ -35,6 +36,13 @@ func InitRouters() (s *http.Server, err error) { h = BaseHandler{Handle: jobHandler.DeleteJob} subrouter.Handle("/job/{group}-{id}", h).Methods("DELETE") + // get job log list + h = BaseHandler{Handle: jobLogHandler.GetList} + subrouter.Handle("/logs", h).Methods("GET") + // get job log + h = BaseHandler{Handle: jobLogHandler.GetDetail} + subrouter.Handle("/log/{id}", h).Methods("GET") + h = BaseHandler{Handle: nodeHandler.GetActivityNodeList} subrouter.Handle("/node/activitys", h).Methods("GET") // get node group list diff --git a/web/ui/src/components/JobEdit.vue b/web/ui/src/components/JobEdit.vue index 75bb66a..7d0492e 100644 --- a/web/ui/src/components/JobEdit.vue +++ b/web/ui/src/components/JobEdit.vue @@ -1,5 +1,8 @@