cronsun/web/info.go

60 lines
1.2 KiB
Go
Raw Normal View History

2017-02-20 09:57:22 +00:00
package web
import (
"time"
v3 "github.com/coreos/etcd/clientv3"
2017-05-12 06:48:24 +00:00
"github.com/shunfei/cronsun"
2017-05-12 07:38:50 +00:00
"github.com/shunfei/cronsun/conf"
2017-02-20 09:57:22 +00:00
)
type Info struct{}
func (inf *Info) Overview(ctx *Context) {
2017-02-20 09:57:22 +00:00
var overview = struct {
TotalJobs int64 `json:"totalJobs"`
JobExecuted *cronsun.StatExecuted `json:"jobExecuted"`
JobExecutedDaily []*cronsun.StatExecuted `json:"jobExecutedDaily"`
2017-02-20 09:57:22 +00:00
}{}
const day = 24 * time.Hour
days := 7
2017-05-12 06:48:24 +00:00
overview.JobExecuted, _ = cronsun.JobLogStat()
end := time.Now()
begin := end.Add(time.Duration(1-days) * day)
statList, _ := cronsun.JobLogDailyStat(begin, end)
list := make([]*cronsun.StatExecuted, days)
cur := begin
for i := 0; i < days; i++ {
date := cur.Format("2006-01-02")
var se *cronsun.StatExecuted
for j := range statList {
if statList[j].Date == date {
se = statList[j]
statList = statList[1:]
break
}
}
if se != nil {
list[i] = se
} else {
list[i] = &cronsun.StatExecuted{Date: date}
}
cur = cur.Add(day)
}
2017-02-20 09:57:22 +00:00
overview.JobExecutedDaily = list
2017-05-12 06:48:24 +00:00
gresp, err := cronsun.DefalutClient.Get(conf.Config.Cmd, v3.WithPrefix(), v3.WithCountOnly())
2017-02-20 09:57:22 +00:00
if err == nil {
overview.TotalJobs = gresp.Count
}
outJSON(ctx.W, overview)
2017-02-20 09:57:22 +00:00
}