Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
enoch
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
enoch
Commits
044610a6
Commit
044610a6
authored
Dec 11, 2019
by
jingbo.wang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
每周10点 发送报表 OK
parent
0839b0e8
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
104 additions
and
73 deletions
+104
-73
regular_email.go
pkg/report-form/regular_email.go
+94
-0
regular_email_test.go
pkg/report-form/regular_email_test.go
+10
-0
regular_report.go
pkg/report-form/regular_report.go
+0
-73
No files found.
pkg/report-form/regular_email.go
0 → 100644
View file @
044610a6
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
}
}
pkg/report-form/regular_email_test.go
0 → 100644
View file @
044610a6
package
report_form
import
(
"testing"
"time"
)
func
TestCallSendEmail
(
t
*
testing
.
T
)
{
callSendEmailApi
(
"week"
,
time
.
Now
(),
"all"
,
[]
string
{
"jingbo.wang@quantgroup.cn"
})
}
pkg/report-form/regular_report.go
View file @
044610a6
...
...
@@ -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
})
}
*/
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment