|
@@ -0,0 +1,107 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "io/ioutil"
|
|
|
+ "log"
|
|
|
+ "os/exec"
|
|
|
+ "qfw/util"
|
|
|
+ "qfw/util/mongodb"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+var SysConfig *sysConfig
|
|
|
+
|
|
|
+type sysConfig struct {
|
|
|
+ MgoAddr string `json:"mgoAddr"`
|
|
|
+ MgoSize int `json:"mgoSize"`
|
|
|
+ CmdText string `json:"cmdText"`
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ util.ReadConfig("./config.json", &SysConfig)
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ mongodb.InitMongodbPool(SysConfig.MgoSize, SysConfig.MgoAddr, "qfw")
|
|
|
+ statistics()
|
|
|
+ ticker := time.NewTicker(3 * time.Minute)
|
|
|
+ for {
|
|
|
+ select {
|
|
|
+ case <-ticker.C:
|
|
|
+ statistics()
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+func statistics() {
|
|
|
+ log.Println("开始统计今天的推送总数。。。")
|
|
|
+ pushtoday := getPushCount()
|
|
|
+ log.Println("今天的推送总数", pushtoday)
|
|
|
+ if pushtoday > 0 {
|
|
|
+ now := time.Now()
|
|
|
+ today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
|
+ prevDays := mongodb.Find("swordfish_index", map[string]interface{}{
|
|
|
+ "l_timestamp": map[string]interface{}{
|
|
|
+ "$lt": today.Unix(),
|
|
|
+ },
|
|
|
+ }, `{"_id":-1}`, `{"i_push":1}`, false, 0, 1)
|
|
|
+ if 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
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+ log.Println("今天的推送总数统计结束。。。")
|
|
|
+}
|
|
|
+func getPushCount() (count int64) {
|
|
|
+ log.Println("执行命令", SysConfig.CmdText)
|
|
|
+ cmd := exec.Command("/bin/bash", "-c", SysConfig.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
|
|
|
+}
|