main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/ioutil"
  11. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  13. "monitor/config"
  14. "net/http"
  15. "time"
  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. crn := cron.New()
  42. _ = crn.AddFunc("@hourly", func() {
  43. taskFile()
  44. })
  45. _ = crn.AddFunc("0 */30 * * * ?", func() {
  46. taskPy()
  47. })
  48. _ = crn.AddFunc("@hourly", func() {
  49. taskProject()
  50. })
  51. crn.Start()
  52. c := make(chan bool, 1)
  53. <-c
  54. },
  55. }
  56. return cmdClient
  57. }
  58. func taskFile() {
  59. count := MgoB.Count("bidding_file", bson.M{"moveok": bson.M{"$exists": false}})
  60. if count > config.Conf.Serve.FileWarn {
  61. SendMsg(fmt.Sprintf(WarningStr, count))
  62. } else {
  63. log.Info("bidding_file", zap.Int("count", count))
  64. }
  65. }
  66. func taskPy() {
  67. info, _ := MgoB.Find("bidding_processing_ids", bson.M{"dataprocess": bson.M{"$in": []int{1, 2}}}, nil, bson.M{"count": 1, "dataprocess": 1}, false, -1, -1)
  68. count := 0
  69. for _, m := range *info {
  70. count += util.IntAll(m["count"])
  71. }
  72. if count > config.Conf.Serve.PyWarn {
  73. SendMsg(fmt.Sprintf(WarningStr1, count))
  74. } else {
  75. log.Info("bidding_py", zap.Int("count", count))
  76. }
  77. }
  78. func taskProject() {
  79. now := time.Now().Unix() - 1*60*60
  80. c := MgoP.Count(config.Conf.DB.MongoP.Coll, bson.M{"pici": bson.M{"$gte": now}})
  81. if c <= 0 {
  82. SendMsg1(fmt.Sprintf(WarningStr2, now))
  83. } else {
  84. log.Info("projectset", zap.Int("count", c))
  85. }
  86. }
  87. func SendMsg(content string) {
  88. client := &http.Client{}
  89. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  90. "content": content, "mentioned_mobile_list": []string{"13373929153", "15090279371", "15639297172"},
  91. }}
  92. bytesData, _ := json.Marshal(data)
  93. req, _ := http.NewRequest("POST", WebUrl, bytes.NewReader(bytesData))
  94. resp, _ := client.Do(req)
  95. body, _ := ioutil.ReadAll(resp.Body)
  96. log.Info("SendMsg", zap.String("resp", string(body)))
  97. }
  98. func SendMsg1(content string) {
  99. client := &http.Client{}
  100. data := map[string]interface{}{"msgtype": "text", "text": map[string]interface{}{
  101. "content": content, "mentioned_mobile_list": []string{"15090279371", "15639297172"},
  102. }}
  103. bytesData, _ := json.Marshal(data)
  104. req, _ := http.NewRequest("POST", WebUrl1, bytes.NewReader(bytesData))
  105. resp, _ := client.Do(req)
  106. body, _ := ioutil.ReadAll(resp.Body)
  107. log.Info("SendMsg", zap.String("resp", string(body)))
  108. }