Skip to content

Instantly share code, notes, and snippets.

@eiyaya
Created March 30, 2017 06:14
Show Gist options
  • Save eiyaya/0ff1dfa7cde86b23faad99cb0a05950f to your computer and use it in GitHub Desktop.
Save eiyaya/0ff1dfa7cde86b23faad99cb0a05950f to your computer and use it in GitHub Desktop.
calculate jenkins build time
package controllers
import (
"fmt"
"log"
"os"
xmlpath "gopkg.in/xmlpath.v1"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/revel/revel"
)
type Cis struct {
*revel.Controller
}
type BuildRecord struct {
Duration int64 `json:"duration"`
Timestamp int64 `json:"timestamp"`
Result string `json:"result"`
}
type Build struct {
Name string `db: name`
BuildNo string `db: buildno`
Inqueue string `db: inqueue`
StartTime string `db: starttime`
EndTime string `db: endtime`
Duration string `db: duration`
Successed string `db: successed`
}
func (c Cis) Index() revel.Result {
return c.Render("you got me!")
}
func (c Cis) BuildStart(name string, buildno string, inqueue string) revel.Result {
db, _ := sqlx.Connect("mysql", "root@tcp(localhost:3306)/jenkins_stat")
builds := []Build{}
db.Select(&builds, "select * from buildlog where name = ? and buildno = ?", name, buildno)
if len(builds) == 0 {
newbuild := Build{Name: name, BuildNo: buildno, Inqueue: inqueue, StartTime: "", EndTime: "", Duration: "", Successed: ""}
ql := "insert into buildlog (name, buildno, inqueue, starttime, endtime, duration, successed) values (:name, :buildno, :inqueue, :starttime, :endtime, :duration, :successed)"
_, err := db.NamedExec(ql, newbuild)
if err != nil {
fmt.Println("编译LOG插入失败, Name: " + name + "BuildNo: " + buildno)
//fmt.Println(err)
}
} else {
build := builds[0]
build.Inqueue = inqueue
ql := "update buildlog set inqueue = :inqueue where name = :name and buildno = :buildno"
_, err := db.NamedExec(ql, build)
if err != nil {
fmt.Println("编译LOG更新失败, Name: " + name + "BuildNo: " + buildno)
}
}
defer db.Close()
return c.RenderText("you got me!" + name + buildno + inqueue)
}
func (c Cis) BuildEnd(name string, buildno string, endtime string) revel.Result {
db, _ := sqlx.Connect("mysql", "root@tcp(localhost:3306)/jenkins_stat")
build := &Build{}
err := db.Get(build, db.Rebind("SELECT * FROM buildlog where name = ? and buildno = ?"), name, buildno)
if err != nil {
fmt.Println("编译LOG 查询失败, Name: " + name + "BuildNo: " + buildno)
//fmt.Println(err)
}
build.EndTime = endtime
ql := "update buildlog set endtime = :endtime where name = :name and buildno = :buildno"
_, err = db.NamedExec(ql, build)
if err != nil {
fmt.Println("编译LOG 更新失败, Name: " + name + "BuildNo: " + buildno)
//fmt.Println(err)
}
defer db.Close()
updateDurFromFile(name, buildno)
/*
//time.Sleep(15000 * time.Millisecond)
//fmt.Println(name + buildno)
jsonurl := "http://ci.jenkins.com/job/" + name + "/" + buildno + "/api/json"
//jsonurl = "http://ci.jenins.com/job/zhuzhan-infolist-house-web-1-0-9-BRANCH/844/api/json"
res, err := goreq.Request{Uri: jsonurl}.Do()
if err != nil {
fmt.Println(err)
fmt.Println("获取Jenkins_api" + jsonurl + "失败")
} else {
record := &BuildRecord{}
res.Body.FromJsonTo(&record)
build.StartTime = strconv.FormatInt(record.Timestamp, 10)
build.Duration = strconv.FormatInt(record.Duration, 10)
build.Successed = record.Result
ql = "update buildlog set starttime = :starttime, duration = :duration, successed = :successed where name = :name and buildno = :buildno"
_, err = db.NamedExec(ql, build)
if err != nil {
fmt.Println("编译LOG 更新2失败, Name: " + name + "BuildNo: " + buildno)
}
} */
return c.RenderText("you got me!" + build.Name + build.BuildNo + build.EndTime)
}
func updateDurFromFile(name string, buildno string) {
location := "/data/hudson/CI/jenkins/jobs/" + name + "/builds/" + buildno + "/build.xml"
xmlFile, err := os.Open(location)
defer xmlFile.Close()
if err != nil {
fmt.Println("Error opening file:", err)
return
}
root, err := xmlpath.Parse(xmlFile)
if err != nil {
log.Fatal(err)
}
var startTime, result, duration string
if value, ok := xmlpath.MustCompile("/build/startTime").String(root); ok {
startTime = value
//fmt.Println("Found:", startTime)
}
if value, ok := xmlpath.MustCompile("/build/result").String(root); ok {
result = value
//fmt.Println("Found:", result)
}
if value, ok := xmlpath.MustCompile("/build/duration").String(root); ok {
duration = value
//fmt.Println("Found:", duration)
}
db, _ := sqlx.Connect("mysql", "root@tcp(localhost:3306)/jenkins_stat")
defer db.Close()
build := &Build{}
build.BuildNo = buildno
build.Duration = duration
build.EndTime = ""
build.Name = name
build.StartTime = startTime
build.Successed = result
ql := "update buildlog set starttime = :starttime, duration = :duration, successed = :successed where name = :name and buildno = :buildno"
_, err = db.NamedExec(ql, build)
if err != nil {
fmt.Println("编译LOG 更新2失败, Name: " + name + "BuildNo: " + buildno)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment