main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "math/rand"
  7. "os/exec"
  8. "strings"
  9. "time"
  10. util "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/mongodb"
  12. "app.yhyue.com/moapp/jybase/redis"
  13. )
  14. var SysConfig *sysConfig
  15. var mongodb *MongodbSim
  16. type sysConfig struct {
  17. MgoAddr string `json:"mgoAddr"`
  18. MgoSize int `json:"mgoSize"`
  19. RedisServers string `json:"redisServers"`
  20. CmdTexts []string `json:"cmdTexts"`
  21. }
  22. func init() {
  23. util.ReadConfig("./config.json", &SysConfig)
  24. mongodb = &MongodbSim{
  25. MongodbAddr: SysConfig.MgoAddr,
  26. Size: SysConfig.MgoSize,
  27. DbName: "qfw",
  28. }
  29. mongodb.InitPool()
  30. redis.InitRedis(SysConfig.RedisServers)
  31. }
  32. func main() {
  33. statistics()
  34. ticker := time.NewTicker(30 * time.Minute)
  35. for {
  36. select {
  37. case <-ticker.C:
  38. statistics()
  39. }
  40. }
  41. }
  42. func statistics() {
  43. log.Println("开始统计今天的推送总数。。。")
  44. pushtoday := int64(0)
  45. for _, v := range SysConfig.CmdTexts {
  46. pushtoday += getPushCount(v)
  47. }
  48. if pushtoday == 0 {
  49. pushtoday = int64(100000 + rand.New(rand.NewSource(time.Now().UnixNano())).Intn(50000))
  50. }
  51. log.Println("今天的推送总数", pushtoday)
  52. now := time.Now()
  53. today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  54. prevDays, ok := mongodb.Find("swordfish_index", map[string]interface{}{
  55. "l_timestamp": map[string]interface{}{
  56. "$lt": today.Unix(),
  57. },
  58. }, `{"_id":-1}`, `{"i_push":1}`, false, 0, 1)
  59. if !ok || prevDays == nil || len(*prevDays) == 0 {
  60. log.Println("出错prevDays", prevDays)
  61. return
  62. }
  63. prevDay := (*prevDays)[0]
  64. if prevDay == nil || len(prevDay) == 0 {
  65. log.Println("出错prevDay", prevDay)
  66. return
  67. }
  68. lastPushCount := util.Int64All(prevDay["i_push"])
  69. if lastPushCount == 0 {
  70. log.Println("出错lastPushCount", lastPushCount)
  71. return
  72. }
  73. list, ok := mongodb.Find("swordfish_index", map[string]interface{}{
  74. "l_timestamp": map[string]interface{}{
  75. "$gte": today.Unix(),
  76. },
  77. }, `{"_id":-1}`, `{"_id":1}`, false, 0, 2)
  78. if ok && list != nil && len(*list) > 1 && len(*list) <= 5 {
  79. for k, v := range *list {
  80. if k == 0 {
  81. continue
  82. }
  83. mongodb.Del("swordfish_index", map[string]interface{}{
  84. "_id": v["_id"],
  85. })
  86. }
  87. }
  88. pushCount := pushtoday + lastPushCount
  89. mongodb.Update("swordfish_index", map[string]interface{}{
  90. "l_timestamp": map[string]interface{}{
  91. "$gte": today.Unix(),
  92. },
  93. }, map[string]interface{}{
  94. "$set": map[string]interface{}{
  95. "i_push": pushCount,
  96. "i_bidtoday": pushtoday,
  97. },
  98. }, false, false)
  99. redis.Del("other", "pcsw_index", "sw_index")
  100. log.Println("今天的推送总数统计结束。。。")
  101. }
  102. func getPushCount(cmdText string) (count int64) {
  103. log.Println("执行命令", cmdText)
  104. cmd := exec.Command("/bin/bash", "-c", cmdText)
  105. //创建获取命令输出管道
  106. stdout, err := cmd.StdoutPipe()
  107. if err != nil {
  108. fmt.Printf("Error:can not obtain stdout pipe for command:%s\n", err)
  109. return
  110. }
  111. //执行命令
  112. if err := cmd.Start(); err != nil {
  113. fmt.Println("Error:The command is err,", err)
  114. return
  115. }
  116. //读取所有输出
  117. bytes, err := ioutil.ReadAll(stdout)
  118. if err != nil {
  119. fmt.Println("ReadAll Stdout:", err.Error())
  120. return
  121. }
  122. if err := cmd.Wait(); err != nil {
  123. fmt.Println("wait:", err.Error())
  124. return
  125. }
  126. count = util.Int64All(strings.TrimSpace(string(bytes)))
  127. return
  128. }