123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package main
- import (
- "context"
- "fmt"
- "github.com/olivere/elastic/v7"
- "github.com/robfig/cron/v3"
- "github.com/spf13/viper"
- "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
- "log"
- "time"
- )
- var (
- GF GlobalConf
- Rest = make(map[string]interface{}, 0) //保存最终各个字段识别率
- MgoB *mongodb.MongodbSim
- )
- func main() {
- InitConfig()
- InitMgo()
- //getStatistic()
- local, _ := time.LoadLocation("Asia/Shanghai")
- c := cron.New(cron.WithLocation(local), cron.WithSeconds())
- _, err := c.AddFunc(GF.Env.Spec, getStatistic)
- if err != nil {
- log.Println(err)
- }
- c.Start()
- defer c.Stop()
- select {}
- }
- func InitConfig() (err error) {
- viper.SetConfigFile("config.toml") // 指定配置文件路径
- viper.SetConfigName("config") // 配置文件名称(无扩展名)
- viper.SetConfigType("toml") // 如果配置文件的名称中没有扩展名,则需要配置此项
- viper.AddConfigPath("./")
- viper.AddConfigPath("./conf/") // 还可以在工作目录中查找配置
- viper.AddConfigPath("../conf/") // 还可以在工作目录中查找配置
- err = viper.ReadInConfig() // 查找并读取配置文件
- if err != nil { // 处理读取配置文件的错误
- return
- }
- err = viper.Unmarshal(&GF)
- return err
- }
- func InitMgo() {
- MgoB = &mongodb.MongodbSim{
- MongodbAddr: GF.MongoB.Host,
- Size: 10,
- DbName: GF.MongoB.DB,
- UserName: GF.MongoB.Username,
- Password: GF.MongoB.Password,
- Direct: GF.MongoB.Direct,
- }
- MgoB.InitPool()
- }
- //getStatistic 获取统计
- func getStatistic() {
- log.Println("开始统计")
- // 创建 Elasticsearch 客户端
- client, err := elastic.NewClient(
- elastic.SetURL(GF.ES.URL),
- elastic.SetBasicAuth(GF.ES.Username, GF.ES.Password),
- elastic.SetSniff(false),
- )
- if err != nil {
- log.Fatalf("创建 Elasticsearch 客户端失败:%s", err)
- }
- // 获取昨天零点和今天零点的时间戳
- now := time.Now()
- yesterday := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
- today := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
- totalQuery := elastic.NewBoolQuery().Must(
- elastic.NewRangeQuery("comeintime").
- Gte(yesterday.Unix()). // 大于昨天零点
- Lt(today.Unix()), // 小于今天零点
- )
- totalNum, _ := client.Count().
- Index("bidding").
- Query(totalQuery).
- Do(context.Background())
- Rest["total"] = totalNum
- Rest["statistic_date"] = yesterday.Format("2006-01-02")
- for _, v := range GF.Fields {
- //log.Println(v.DataType)
- // 构建查询
- topQuery := elastic.NewBoolQuery().Must(
- elastic.NewTermQuery("toptype", v.Toptype),
- elastic.NewRangeQuery("comeintime").
- Gte(yesterday.Unix()). // 大于昨天零点
- Lt(today.Unix()), // 小于今天零点
- )
- topNum, _ := client.Count().
- Index("bidding").
- Query(topQuery).
- Do(context.Background())
- ra := make(map[string]interface{}, 0)
- for _, field := range v.Field {
- //fmt.Println(field)
- query := elastic.NewBoolQuery().
- Must(
- elastic.NewTermQuery("toptype", v.Toptype),
- elastic.NewExistsQuery(field),
- elastic.NewRangeQuery("comeintime").
- Gte(yesterday.Unix()). // 大于昨天零点
- Lt(today.Unix()), // 小于今天零点
- )
- // 执行查询
- count, _ := client.Count().
- Index("bidding").
- Query(query).
- Do(context.Background())
- // 将 int64 转换为 float64,并计算百分比
- percentage := (float64(count) / float64(topNum)) * 100.0
- ra[field] = fmt.Sprintf("%.2f%%", percentage)
- }
- Rest[v.DataType] = ra
- }
- //fmt.Println(Rest)
- MgoB.Save("bidding_statistic", Rest)
- log.Println("结束统计")
- }
|