123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package main
- import (
- "fmt"
- "io/ioutil"
- "log"
- "math/rand"
- "os/exec"
- "strings"
- "time"
- util "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/mongodb"
- "app.yhyue.com/moapp/jybase/redis"
- )
- var SysConfig *sysConfig
- var mongodb *MongodbSim
- type sysConfig struct {
- MgoAddr string `json:"mgoAddr"`
- MgoSize int `json:"mgoSize"`
- RedisServers string `json:"redisServers"`
- CmdTexts []string `json:"cmdTexts"`
- }
- func init() {
- util.ReadConfig("./config.json", &SysConfig)
- mongodb = &MongodbSim{
- MongodbAddr: SysConfig.MgoAddr,
- Size: SysConfig.MgoSize,
- DbName: "qfw",
- }
- mongodb.InitPool()
- redis.InitRedis(SysConfig.RedisServers)
- }
- func main() {
- statistics()
- ticker := time.NewTicker(30 * time.Minute)
- for {
- select {
- case <-ticker.C:
- statistics()
- }
- }
- }
- func statistics() {
- log.Println("开始统计今天的推送总数。。。")
- pushtoday := int64(0)
- for _, v := range SysConfig.CmdTexts {
- pushtoday += getPushCount(v)
- }
- if pushtoday == 0 {
- pushtoday = int64(100000 + rand.New(rand.NewSource(time.Now().UnixNano())).Intn(50000))
- }
- log.Println("今天的推送总数", pushtoday)
- now := time.Now()
- today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
- prevDays, ok := mongodb.Find("swordfish_index", map[string]interface{}{
- "l_timestamp": map[string]interface{}{
- "$lt": today.Unix(),
- },
- }, `{"_id":-1}`, `{"i_push":1}`, false, 0, 1)
- if !ok || prevDays == nil || len(*prevDays) == 0 {
- log.Println("出错prevDays", prevDays)
- return
- }
- prevDay := (*prevDays)[0]
- if prevDay == nil || len(prevDay) == 0 {
- log.Println("出错prevDay", prevDay)
- return
- }
- lastPushCount := util.Int64All(prevDay["i_push"])
- if lastPushCount == 0 {
- log.Println("出错lastPushCount", lastPushCount)
- return
- }
- list, ok := mongodb.Find("swordfish_index", map[string]interface{}{
- "l_timestamp": map[string]interface{}{
- "$gte": today.Unix(),
- },
- }, `{"_id":-1}`, `{"_id":1}`, false, 0, 2)
- if ok && list != nil && len(*list) > 1 && len(*list) <= 5 {
- for k, v := range *list {
- if k == 0 {
- continue
- }
- mongodb.Del("swordfish_index", map[string]interface{}{
- "_id": v["_id"],
- })
- }
- }
- pushCount := pushtoday + lastPushCount
- mongodb.Update("swordfish_index", map[string]interface{}{
- "l_timestamp": map[string]interface{}{
- "$gte": today.Unix(),
- },
- }, map[string]interface{}{
- "$set": map[string]interface{}{
- "i_push": pushCount,
- "i_bidtoday": pushtoday,
- },
- }, false, false)
- redis.Del("other", "pcsw_index", "sw_index")
- log.Println("今天的推送总数统计结束。。。")
- }
- func getPushCount(cmdText string) (count int64) {
- log.Println("执行命令", cmdText)
- cmd := exec.Command("/bin/bash", "-c", cmdText)
- //创建获取命令输出管道
- stdout, err := cmd.StdoutPipe()
- if err != nil {
- fmt.Printf("Error:can not obtain stdout pipe for command:%s\n", err)
- return
- }
- //执行命令
- if err := cmd.Start(); err != nil {
- fmt.Println("Error:The command is err,", err)
- return
- }
- //读取所有输出
- bytes, err := ioutil.ReadAll(stdout)
- if err != nil {
- fmt.Println("ReadAll Stdout:", err.Error())
- return
- }
- if err := cmd.Wait(); err != nil {
- fmt.Println("wait:", err.Error())
- return
- }
- count = util.Int64All(strings.TrimSpace(string(bytes)))
- return
- }
|