package main import ( "bytes" "encoding/json" "fmt" "github.com/robfig/cron" "github.com/spf13/cobra" "go.mongodb.org/mongo-driver/bson" "go.uber.org/zap" "io" "jygit.jydev.jianyu360.cn/data_processing/common_utils/log" "monitor/config" "net/http" "time" ) var ( WebUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a" WebUrl1 = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1594c58d-c279-4fdc-8d44-c201160e3731" WarningStr = "数据采集bidding_file表数据,已累计%d条数据待处理" WarningStr1 = "标的物等字段识别提醒,bidding_normal表数据已积累%d条待处理" WarningStr2 = "项目合并表数据,超过一个1小时(查询时间点: %d)未有合并新数据" ) func init() { config.Init("./common.toml") InitLog() } func main() { rootCmd := &cobra.Command{Use: "my cmd"} rootCmd.AddCommand(biddingFile()) if err := rootCmd.Execute(); err != nil { fmt.Println("rootCmd.Execute failed", err.Error()) } } func biddingFile() *cobra.Command { cmdClient := &cobra.Command{ Use: "bidding_warn", Short: "Start statistics bidding_file data", Run: func(cmd *cobra.Command, args []string) { InitMgo() log.Info("bidding_warn start") crn := cron.New() _ = crn.AddFunc("@hourly", func() { taskFile() taskProject() }) _ = crn.AddFunc("0 */30 * * * ?", func() { taskPy() }) crn.Start() c := make(chan bool, 1) <-c }, } return cmdClient } func taskFile() { count := MgoB.Count("bidding_file", bson.M{"moveok": bson.M{"$exists": false}}) if count > config.Conf.Serve.FileWarn { SendMsg(fmt.Sprintf(WarningStr, count)) } else { log.Info("bidding_file", zap.Int("count", count)) } } func taskPy() { count := MgoB.Count("bidding_nomal", bson.M{"bid_completetime": bson.M{"$exists": false}}) if count > config.Conf.Serve.PyWarn { SendMsg(fmt.Sprintf(WarningStr1, count)) } else { log.Info("bidding_py", zap.Int("count", count)) } } func taskProject() { now := time.Now().Unix() - 1*60*60 c := MgoP.Count(config.Conf.DB.MongoP.Coll, bson.M{"pici": bson.M{"$gte": now}}) if c <= 0 { SendMsg1(fmt.Sprintf(WarningStr2, now)) } else { log.Info("projectset", zap.Int("count", c)) } } func SendMsg(content string) { client := &http.Client{} data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{ "content": content, "mentioned_mobile_list": []string{"13373929153", "17639376639", "18530014520"}, }} bytesData, _ := json.Marshal(data) req, _ := http.NewRequest("POST", WebUrl, bytes.NewReader(bytesData)) resp, _ := client.Do(req) body, _ := io.ReadAll(resp.Body) log.Info("SendMsg", zap.String("resp", string(body))) } func SendMsg1(content string) { client := &http.Client{} data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{ "content": content, "mentioned_mobile_list": []string{"15090279371", "15639297172"}, }} bytesData, _ := json.Marshal(data) req, _ := http.NewRequest("POST", WebUrl1, bytes.NewReader(bytesData)) resp, _ := client.Do(req) body, _ := io.ReadAll(resp.Body) log.Info("SendMsg", zap.String("resp", string(body))) }