Commit 044610a6 authored by jingbo.wang's avatar jingbo.wang

每周10点 发送报表 OK

parent 0839b0e8
package report_form
import (
"encoding/json"
"fmt"
"git.quantgroup.cn/DevOps/enoch/pkg/global"
"git.quantgroup.cn/DevOps/enoch/pkg/glog"
"github.com/valyala/fasthttp"
"github.com/vrg0/go-common/util"
"runtime/debug"
"time"
)
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
})
}
func RegularMail(dir string) {
if !util.IsDir(dir) {
return
}
go func() {
defer func() {
if err := recover(); err != nil {
glog.Error(err, "\n", string(debug.Stack()))
}
}()
for {
now := time.Now()
next := now.Add(time.Hour * 24)
next = time.Date(next.Year(), next.Month(), next.Day(), 10, 0, 0, 0, next.Location())
timer := time.NewTimer(next.Sub(now))
<-timer.C
timer.Stop()
//发送邮件
if next.Weekday() == time.Monday {
callSendEmailApi("week", next, "all", receiverList)
for name, emailList := range serviceOwner {
callSendEmailApi("week", next, name, emailList)
}
}
}
}()
}
func callSendEmailApi(long string, t time.Time, name string, emailList []string) {
url := fmt.Sprintf("http://127.0.0.1:%d/send-email/%s/%02d/%02d/%02d/%s",
global.HttpPort, long, t.Year(), t.Month(), t.Day(), name)
body, err := json.Marshal(emailList)
if err != nil {
glog.Error("can not marshal json", receiverList)
return
}
req := &fasthttp.Request{}
req.SetRequestURI(url)
req.Header.SetContentType("application/json")
req.Header.SetMethod("POST")
req.SetBody(body)
resp := &fasthttp.Response{}
client := &fasthttp.Client{}
if err := client.Do(req, resp); err != nil {
glog.Error("请求失败:", err)
return
}
if resp.StatusCode() != 200 {
glog.Error("发送失败:", resp.StatusCode())
return
}
}
package report_form
import (
"testing"
"time"
)
func TestCallSendEmail(t *testing.T) {
callSendEmailApi("week", time.Now(), "all", []string{"jingbo.wang@quantgroup.cn"})
}
......@@ -83,76 +83,3 @@ func reportForm(t time.Time, dir string, n int) {
return
}
}
// fileNamePrefix := now.Format("2006-01-02")
//周表
// if now.Weekday() == time.Monday {
// week()
// }
/*
//周表
if now.Weekday() == time.Tuesday {
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)
//分表
for name, service := range sm.serviceMap {
fileName := dir + "/" + fileNamePrefix + "_" + name + "_week.txt"
if !util.Exists(fileName) {
body := SubTableRun(service)
if err := ioutil.WriteFile(fileName, []byte(body), 0644); err != nil {
glog.Error("报表写入文件失败:", fileName, err)
glog.Info(body)
}
//如果存在,则发送邮件
serviceOwnerEmailList, ok := serviceOwner[name]
if !ok {
continue
}
email.SendEmail(fileNamePrefix+"-服务健康状态表:"+name, body, serviceOwnerEmailList...)
}
}
}
*/
/*
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
})
}
*/
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