Skip to content

Commit

Permalink
fix: scheduled tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
paterleng committed Sep 14, 2024
1 parent f06e55d commit 1817cde
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 76 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# bug-notify

# redmine通过钉钉进行消息通知
## 介绍

本项目独立于你的项目之外,不影响原系统的使用,通过使用go-mysql库来检测mysql binlog日志变化,触发go-mysql库中的事件回调来处理mysql表中的数据变化。


## 部署

通过Dockerfile打包成docker镜像,然后通过编写的yaml文件部署到kubernetes集群中运行
### kubernetes集群中部署
```
kubectl apply -f yaml文件路径
```
### docker部署
```
docker build
docker run
```
### 二进制运行
Expand All @@ -23,7 +19,7 @@ cd 二进制包所在目录
```

## 使用
在部署前你可以编辑你的conf.yaml文件来监控你的多个数据库中的多张表的变化
在部署前你可以编辑你的conf.yaml文件来监控你的多个数据库中的多张表的变化,钉钉机器人的webhook地址及密钥需要定义一个自定义字段配置使用, 地址与密钥通过@符分隔,同时需要在项目层面配置自定义字段用于存储用户的手机号
## 源代码获取
```
cd existing_repo
Expand Down
Binary file modified bug-notify
Binary file not shown.
14 changes: 12 additions & 2 deletions dao/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ func GetUserInfoByUserID(id int32) (name model.UserName, err error) {
return
}

func GetStatusNumByID(status_id []int) ([]model.TimeData, error) {
func GetStatusNumByID(statusId []int, projectId int64) ([]model.TimeData, error) {
var a []model.TimeData
err := init_tool.DB.Table("issues").Select("status_id, priority_id, count(*) as count").Where("status_id in ?", status_id).Group("priority_id").Group("status_id").Find(&a).Error
err := init_tool.DB.Table("issues").Select("status_id, priority_id, count(*) as count").Where("status_id in ? and project_id = ?", statusId, projectId).Group("priority_id").Group("status_id").Find(&a).Error
return a, err
}

func GetWatchUserID(watchid int32, watchtype string) (userid []int32, err error) {
err = init_tool.DB.Table("watchers").Where("watchable_id = ? and watchable_type = ?", watchid, watchtype).Select("user_id").Find(&userid).Error
return
}

func GetAllProjectID() (ids []int64, err error) {
err = init_tool.DB.Table("projects").Select("id").Find(&ids).Error
return
}
func GetURLByProjectId(ids []int64) (urls []model.RobotUrl, err error) {
err = init_tool.DB.Table("custom_values").Where("customized_id in ? and customized_type = ?", ids, "Principal").Select("customized_id,value").Find(&urls).Error
return

}
117 changes: 59 additions & 58 deletions handle/timeing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,90 +4,91 @@ import (
"bug-notify/api"
"bug-notify/dao"
"bug-notify/model"
"bug-notify/utils"
"fmt"
"github.com/andeya/goutil/calendar/cron"
"go.uber.org/zap"
"strings"
"time"
)

const (
// 未处理 处理中的事务id
NOTPROCESSEDID = 2
PROCESSINGID = 3

//// bug处理等级
//LEVEL_HIGHT = 1
//LEVEL_MIDDLE = 2
//LEVEL_TAIL = 3
)

var P = []int{NOTPROCESSEDID, PROCESSINGID}

func TimeingTasks() {
c := cron.New()
c.AddFunc("0 21 * * *", func() {
a, err := dao.GetStatusNumByID(P)
//获取所有项目id
ids, err := dao.GetAllProjectID()
if err != nil {
zap.L().Error("获取项目id失败:", zap.Error(err))
return
}
//根据项目id获取相对应的回调地址并加签
urls, err := dao.GetURLByProjectId(ids)
if err != nil {
zap.L().Error("获取status_id为2的数量失败:", zap.Error(err))
zap.L().Error("获取钉钉webhook失败:", zap.Error(err))
return
}
maps := map[int]map[int]int{}
for _, value := range a {
childMap := make(map[int]int)
childMap[value.StatusId] = int(value.Count)
maps[value.PriorityId] = childMap
urlMap := make(map[int64]string)
for i := 0; i < len(urls); i++ {
urlMap[urls[i].CustomizedId] = urls[i].Value
}
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
if _, ok := maps[i+1][j+1]; !ok {
maps[i+1][j+1] = 0
for _, id := range ids {
a, err := dao.GetStatusNumByID(P, id)
if err != nil {
zap.L().Error("查询数量失败:", zap.Error(err))
return
}
maps := map[int]map[int]int{}
for _, value := range a {
childMap := make(map[int]int)
childMap[value.StatusId] = int(value.Count)
maps[value.PriorityId] = childMap
}
for i := 0; i < 3; i++ {
for j := 0; j < 2; j++ {
if _, ok := maps[i+1][j+1]; !ok {
maps[i+1][j+1] = 0
}
}
}
}
//使用字符串builders方式 高效切割
//var content strings.Builder
//content.WriteString("## status_id nums \n")
//content.WriteString("**未处理**:")

// 使用markdown格式
content := "# %s 任务状态统计 \n" +
"\n| **级别** | **未处理** | **处理中** | " +
"\n| :--: | :--: | :--: | " +
"\n| **重要** | %d | %d | " +
"\n| **中等** | %d | %d | " +
"\n| **普通** | %d | %d |" +
"\n\n <font color=005EFF>[@所有人](#)\n</font>"
nowTime := time.Now().Format("2006-01-02")

content = fmt.Sprintf(content, nowTime, maps[1][2], maps[1][3], maps[2][2], maps[2][3], maps[3][2], maps[3][3])
// 使用markdown格式
content := "# %s 任务状态统计 \n" +
"\n| **级别** | **未处理** | **处理中** | " +
"\n| :--: | :--: | :--: | " +
"\n| **重要** | %d | %d | " +
"\n| **中等** | %d | %d | " +
"\n| **普通** | %d | %d |" +
"\n\n <font color=005EFF>[@所有人](#)\n</font>"
nowTime := time.Now().Format("2006-01-02")

data := model.SendMsg{
Content: content,
IsAtAll: true,
MsgType: "markdown",
content = fmt.Sprintf(content, nowTime, maps[1][2], maps[1][3], maps[2][2], maps[2][3], maps[3][2], maps[3][3])
//分割字符串
split := strings.Split(urlMap[id], "@")
var secret string
if len(split) >= 2 {
secret = split[1]
}
sign := utils.DingSecret(secret)
data := model.SendMsg{
DingRobotURL: split[0] + sign,
Content: content,
IsAtAll: true,
MsgType: "markdown",
}
err = api.SendMessage(data)
if err != nil {
zap.L().Error(" 定时任务发送消息失败:", zap.Error(err))
return
}
}
api.SendMessage(data)
})
c.Start()
}

// // 返回查出的一个切片
//
// func selectBugLevel() (a [3][2]int64, err error) {
// // 简单便利 处理等级和遍历关联 没有关联也可自行转换关联
// //for i, _ := range a {
// // for j, _ := range a[i] {
// // a[i][j], err = dao.GetStatusNumByID(j+2, i+1)
// // }
// //}
// // 笨方法
// //a[0][0], err = dao.GetStatusNumByID(NOTPROCESSEDID, LEVEL_HIGHT)
// //a[0][1], err = dao.GetStatusNumByID(PROCESSINGID, LEVEL_HIGHT)
// //
// //a[1][0], err = dao.GetStatusNumByID(NOTPROCESSEDID, LEVEL_MIDDLE)
// //a[1][1], err = dao.GetStatusNumByID(PROCESSINGID, LEVEL_MIDDLE)
// //
// //a[2][0], err = dao.GetStatusNumByID(NOTPROCESSEDID, LEVEL_TAIL)
// //a[2][1], err = dao.GetStatusNumByID(PROCESSINGID, LEVEL_TAIL)
// return a, err
// }
5 changes: 5 additions & 0 deletions model/DataModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ type ActionBtns struct {
ActionURL string `json:"actionURL"`
Title string `json:"title"`
}

type RobotUrl struct {
Value string `json:"value"`
CustomizedId int64 `json:"customized_id"`
}
9 changes: 0 additions & 9 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ func SplicingString(str []string, s string) (newstr string) {
}

func DingSecret(secret string) (sign string) {
// 1. 获取当前时间戳,单位是毫秒
timestamp := fmt.Sprintf("%d", time.Now().UnixNano()/1e6)

// 2. 把时间戳和密钥拼接成字符串
stringToSign := timestamp + "\n" + secret

// 3. 使用 HMAC-SHA256 进行加密
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(stringToSign))
signData := h.Sum(nil)

// 4. 进行 Base64 编码
signature := base64.StdEncoding.EncodeToString(signData)

// 5. 进行 URL 编码
signature = url.QueryEscape(signature)
sign = "&timestamp=" + timestamp + "&sign=" + signature
return
Expand Down

0 comments on commit 1817cde

Please sign in to comment.