Commit 6f581e80 authored by jingbo.wang's avatar jingbo.wang

每天0点定时拉取数据生成报表,完成

parent 463e2b06
package report_form package report_form
/* import (
var ( "git.quantgroup.cn/DevOps/enoch/pkg/glog"
receiverList = make([]string, 0) "github.com/vrg0/go-common/util"
serviceOwner = make(map[string][]string) "io/ioutil"
"os"
"runtime/debug"
"time"
) )
func init() { //定期报表,保存到指定文件夹
global.Config.Watch(global.NamespaceApplication, "report.form.receiver", func(oldCfg string, newCfg string) {
rList := make([]string, 0)
if err := json.Unmarshal([]byte(newCfg), &rList); err != nil {
glog.Error("can not unmarshal json:", err, " ", newCfg)
return
}
receiverList = rList
})
global.Config.Watch(global.NamespaceApplication, "service.owner", func(oldCfg string, newCfg string) {
obj := make(map[string][]string)
if err := json.Unmarshal([]byte(newCfg), &obj); err != nil {
glog.Error("can not unmarshal json:", err, " ", newCfg)
return
}
serviceOwner = obj
})
}
//保存服务报表的目录
func RegularReport(dir string) { func RegularReport(dir string) {
if !util.IsDir(dir) { if !util.IsDir(dir) {
return return
...@@ -37,6 +20,7 @@ func RegularReport(dir string) { ...@@ -37,6 +20,7 @@ func RegularReport(dir string) {
if err := recover(); err != nil { if err := recover(); err != nil {
glog.Error(err, "\n", string(debug.Stack())) glog.Error(err, "\n", string(debug.Stack()))
} }
}() }()
timer := time.NewTimer(0) timer := time.NewTimer(0)
...@@ -44,7 +28,6 @@ func RegularReport(dir string) { ...@@ -44,7 +28,6 @@ func RegularReport(dir string) {
<-timer.C <-timer.C
now := time.Now() now := time.Now()
//报表
run(now, dir) run(now, dir)
next := now.Add(time.Hour * 24) next := now.Add(time.Hour * 24)
...@@ -56,7 +39,59 @@ func RegularReport(dir string) { ...@@ -56,7 +39,59 @@ func RegularReport(dir string) {
func run(now time.Time, dir string) { func run(now time.Time, dir string) {
fileNamePrefix := now.Format("2006-01-02") fileNamePrefix := now.Format("2006-01-02")
dir = dir + "/" + fileNamePrefix
dirDay := dir + "/" + "day"
dirWeek := dir + "/" + "week"
if util.Exists(fileNamePrefix) {
return
} else {
if err := os.Mkdir(dir, os.ModePerm); err != nil {
glog.Error("创建dir失败:", dir, ":", err)
return
}
}
//周表
if err := os.Mkdir(dirWeek, os.ModePerm); err != nil {
glog.Error("创建dir失败:", dirWeek, ":", err)
return
} else {
reportForm(now, dirWeek, 7)
}
//日表
if err := os.Mkdir(dirDay, os.ModePerm); err != nil {
glog.Error("创建dir失败:", dirDay, ":", err)
return
} else {
reportForm(now, dirDay, 1)
}
}
func reportForm(t time.Time, dir string, n int) {
startTime := t.Add(-1 * time.Duration(t.Hour()) * time.Hour)
startTime = startTime.Add(-1 * time.Duration(t.Minute()) * time.Minute)
startTime = startTime.Add(-1 * time.Duration(t.Second()) * time.Second)
startTime = startTime.Add(-1 * time.Duration(t.Nanosecond()))
startTime = startTime.AddDate(0, 0, -n)
endTime := startTime.AddDate(0, 0, n)
sm := NewServiceMap(startTime, endTime)
all := sm.ServiceMapReportForm()
if err := ioutil.WriteFile(dir+"/all.html", []byte(all), os.ModePerm); err != nil {
glog.Error("写入文件失败", dir+"/all.html", " ", err)
return
}
}
// fileNamePrefix := now.Format("2006-01-02")
//周表
// if now.Weekday() == time.Monday {
// week()
// }
/*
//周表 //周表
if now.Weekday() == time.Tuesday { if now.Weekday() == time.Tuesday {
sm := GeneralTableNewSM(7) sm := GeneralTableNewSM(7)
...@@ -92,5 +127,32 @@ func run(now time.Time, dir string) { ...@@ -92,5 +127,32 @@ func run(now time.Time, dir string) {
} }
} }
} }
*/
/*
var (
receiverList = make([]string, 0)
serviceOwner = make(map[string][]string)
)
func init() {
global.Config.Watch(global.NamespaceApplication, "report.form.receiver", func(oldCfg string, newCfg string) {
rList := make([]string, 0)
if err := json.Unmarshal([]byte(newCfg), &rList); err != nil {
glog.Error("can not unmarshal json:", err, " ", newCfg)
return
}
receiverList = rList
})
global.Config.Watch(global.NamespaceApplication, "service.owner", func(oldCfg string, newCfg string) {
obj := make(map[string][]string)
if err := json.Unmarshal([]byte(newCfg), &obj); err != nil {
glog.Error("can not unmarshal json:", err, " ", newCfg)
return
}
serviceOwner = obj
})
} }
*/ */
package report_form package report_form
/* import "testing"
func TestRegularReport(t *testing.T) { func TestRegularReport(t *testing.T) {
RegularReport("/var/enoch_health_table") RegularReport("/var/enoch_health_table")
select {} select {}
} }
*/
This diff is collapsed.
...@@ -7,7 +7,8 @@ import ( ...@@ -7,7 +7,8 @@ import (
) )
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
smForm := ServiceMapReportForm(time.Now().AddDate(0, 0, -10), time.Now()) sm := NewServiceMap(time.Now().AddDate(0, 0, -10), time.Now())
smForm := sm.ServiceMapReportForm()
fmt.Println(smForm) fmt.Println(smForm)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment