main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "monitor/config"
  8. "net/http"
  9. "time"
  10. "github.com/robfig/cron"
  11. "github.com/spf13/cobra"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.uber.org/zap"
  14. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  15. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  16. )
  17. var (
  18. WebUrl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=45962efc-ca87-4996-9ffa-08bf6608ab7a"
  19. WebUrl1 = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1594c58d-c279-4fdc-8d44-c201160e3731"
  20. WarningStr = "数据采集bidding_file表数据,已累计%d条数据未处理"
  21. WarningStr1 = "标的物等字段识别提醒,bidding表数据已积累%d条未处理"
  22. WarningStr2 = "项目合并表数据,超过一个1小时(查询时间点: %d)未有合并新数据"
  23. )
  24. func init() {
  25. config.Init("./common.toml")
  26. InitLog()
  27. }
  28. func main() {
  29. rootCmd := &cobra.Command{Use: "my cmd"}
  30. rootCmd.AddCommand(biddingFile())
  31. if err := rootCmd.Execute(); err != nil {
  32. fmt.Println("rootCmd.Execute failed", err.Error())
  33. }
  34. }
  35. func biddingFile() *cobra.Command {
  36. cmdClient := &cobra.Command{
  37. Use: "bidding_warn",
  38. Short: "Start statistics bidding_file data",
  39. Run: func(cmd *cobra.Command, args []string) {
  40. InitMgo()
  41. log.Info("bidding_warn start")
  42. crn := cron.New()
  43. _ = crn.AddFunc("@hourly", func() {
  44. taskFile()
  45. taskProject()
  46. })
  47. _ = crn.AddFunc("0 */30 * * * ?", func() {
  48. taskPy()
  49. })
  50. crn.Start()
  51. c := make(chan bool, 1)
  52. <-c
  53. },
  54. }
  55. return cmdClient
  56. }
  57. func taskFile() {
  58. count := MgoB.Count("bidding_file", bson.M{"moveok": bson.M{"$exists": false}})
  59. if count > config.Conf.Serve.FileWarn {
  60. SendMsg(fmt.Sprintf(WarningStr, count))
  61. } else {
  62. log.Info("bidding_file", zap.Int("count", count))
  63. }
  64. }
  65. func taskPy() {
  66. info, _ := MgoB.Find("bidding_processing_ids", bson.M{"dataprocess": 1}, nil, bson.M{"count": 1, "dataprocess": 1}, false, -1, -1)
  67. count := 0
  68. for _, m := range *info {
  69. count += util.IntAll(m["count"])
  70. }
  71. if count > config.Conf.Serve.PyWarn {
  72. SendMsg(fmt.Sprintf(WarningStr1, count))
  73. } else {
  74. log.Info("bidding_py", zap.Int("count", count))
  75. }
  76. }
  77. func taskProject() {
  78. now := time.Now().Unix() - 1*60*60
  79. c := MgoP.Count(config.Conf.DB.MongoP.Coll, bson.M{"pici": bson.M{"$gte": now}})
  80. if c <= 0 {
  81. SendMsg1(fmt.Sprintf(WarningStr2, now))
  82. } else {
  83. log.Info("projectset", zap.Int("count", c))
  84. }
  85. }
  86. func SendMsg(content string) {
  87. client := &http.Client{}
  88. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  89. "content": content, "mentioned_mobile_list": []string{"13373929153", "15090279371", "15639297172"},
  90. }}
  91. bytesData, _ := json.Marshal(data)
  92. req, _ := http.NewRequest("POST", WebUrl, bytes.NewReader(bytesData))
  93. resp, _ := client.Do(req)
  94. body, _ := io.ReadAll(resp.Body)
  95. log.Info("SendMsg", zap.String("resp", string(body)))
  96. }
  97. func SendMsg1(content string) {
  98. client := &http.Client{}
  99. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  100. "content": content, "mentioned_mobile_list": []string{"15090279371", "15639297172"},
  101. }}
  102. bytesData, _ := json.Marshal(data)
  103. req, _ := http.NewRequest("POST", WebUrl1, bytes.NewReader(bytesData))
  104. resp, _ := client.Do(req)
  105. body, _ := io.ReadAll(resp.Body)
  106. log.Info("SendMsg", zap.String("resp", string(body)))
  107. }