Commit 4f5a2280 authored by jingbo.wang's avatar jingbo.wang

报表代码优化

parent 128da8d6
...@@ -43,26 +43,9 @@ func RegularReport(dir string) { ...@@ -43,26 +43,9 @@ func RegularReport(dir string) {
for { for {
<-timer.C <-timer.C
now := time.Now() now := time.Now()
fileNamePrefix := now.Format("2006-01-02")
//周表 //报表
if now.Weekday() == time.Monday { run(now, dir)
fileName := dir + "/" + fileNamePrefix + "_week.txt"
if !util.Exists(fileName) {
body := GeneralTableRun(7)
if err := ioutil.WriteFile(fileName, []byte(body), 0644); err != nil {
glog.Error("报表写入文件失败:", fileName, err)
glog.Info(body)
}
email.SendEmail("服务监控状态总表-"+fileNamePrefix, body, receiverList...)
}
glog.Info("每周总表报表完成:", fileName)
}
//日表
//TODO
//fmt.Println("如果文件不存在,则报表:", fileNamePrefix)
next := now.Add(time.Hour * 24) next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location()) next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
...@@ -70,3 +53,30 @@ func RegularReport(dir string) { ...@@ -70,3 +53,30 @@ func RegularReport(dir string) {
} }
}() }()
} }
func run(now time.Time, dir string) {
fileNamePrefix := now.Format("2006-01-02")
//周表
if now.Weekday() == time.Monday {
sm := GeneralTableNewSM(7)
//总表
fileName := dir + "/" + fileNamePrefix + "_week.txt"
if !util.Exists(fileName) {
body := GeneralTableRun(sm)
if err := ioutil.WriteFile(fileName, []byte(body), 0644); err != nil {
glog.Error("报表写入文件失败:", fileName, err)
glog.Info(body)
}
email.SendEmail("服务监控状态总表-"+fileNamePrefix, body, receiverList...)
}
glog.Info("每周总表报表完成:", fileName)
//分表
//TODO
}
//日表
//TODO
}
...@@ -118,25 +118,13 @@ func (t *Table) AddRecord(values ...interface{}) error { ...@@ -118,25 +118,13 @@ func (t *Table) AddRecord(values ...interface{}) error {
return nil return nil
} }
func GeneralTableRun(n int) string { func GeneralTableRun(sm *ServiceMap) string {
const ( const (
cpuMin = 1 cpuMin = 1
memMin = 1 memMin = 1
diskMin = 1 diskMin = 1
) )
//求前n天的数据,按天取整
now := time.Now()
startTime := now.Add(-1 * time.Duration(now.Hour()) * time.Hour)
startTime = startTime.Add(-1 * time.Duration(now.Minute()) * time.Minute)
startTime = startTime.Add(-1 * time.Duration(now.Second()) * time.Second)
startTime = startTime.Add(-1 * time.Duration(now.Nanosecond()))
startTime = startTime.AddDate(0, 0, -n)
endTime := startTime.AddDate(0, 0, n)
//查询
sm := NewServiceMap(startTime, endTime)
//生成报表 //生成报表
rtn := new(strings.Builder) rtn := new(strings.Builder)
...@@ -144,7 +132,8 @@ func GeneralTableRun(n int) string { ...@@ -144,7 +132,8 @@ func GeneralTableRun(n int) string {
rtn.WriteString(" 服务健康状态总表\n") rtn.WriteString(" 服务健康状态总表\n")
//时间 //时间
rtn.WriteString(fmt.Sprintf("时间:%s ~ %s\n", startTime.Format(time.RFC3339), endTime.Format(time.RFC3339))) rtn.WriteString(fmt.Sprintf("时间:%s ~ %s\n",
sm.startTime.Format(time.RFC3339), sm.endTime.Format(time.RFC3339)))
//访问量 //访问量
rtn.WriteString(fmt.Sprintf("总访问量:%v 总响应时间:%v 平均响应时间:%v\n\n", rtn.WriteString(fmt.Sprintf("总访问量:%v 总响应时间:%v 平均响应时间:%v\n\n",
...@@ -236,3 +225,19 @@ func GeneralTableRun(n int) string { ...@@ -236,3 +225,19 @@ func GeneralTableRun(n int) string {
return rtn.String() return rtn.String()
} }
func GeneralTableNewSM(n int) *ServiceMap {
//求前n天的数据,按天取整
now := time.Now()
startTime := now.Add(-1 * time.Duration(now.Hour()) * time.Hour)
startTime = startTime.Add(-1 * time.Duration(now.Minute()) * time.Minute)
startTime = startTime.Add(-1 * time.Duration(now.Second()) * time.Second)
startTime = startTime.Add(-1 * time.Duration(now.Nanosecond()))
startTime = startTime.AddDate(0, 0, -n)
endTime := startTime.AddDate(0, 0, n)
//查询
sm := NewServiceMap(startTime, endTime)
return sm
}
...@@ -7,7 +7,8 @@ import ( ...@@ -7,7 +7,8 @@ import (
) )
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
fmt.Println(GeneralTableRun(10)) sm := GeneralTableNewSM(10)
fmt.Println(GeneralTableRun(sm))
} }
func TestUtf8StringLen(t *testing.T) { func TestUtf8StringLen(t *testing.T) {
......
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