main.go 3.1 KB

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