package main

import (
	"encoding/json"
	"fmt"
	"log"
	qu "qfw/util"
	"qfw/util/elastic"
	"time"
	es_elastic "qfw/common/src/gopkg.in/olivere/elastic.v1"
)


func dealWithSTData()  {

	log.Println("开始统计全量站点数据...")
	defer qu.Catch()

	start := int(time.Now().Unix())
	now:=time.Now() //当前天
	lt_time:= time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
	gte_time := lt_time-86400

	mgodata := toCalculateMgoData(gte_time,lt_time)
	esdata := toCalculateEsData(fmt.Sprintf("%d",gte_time),fmt.Sprintf("%d",lt_time))
	mgonum,esnum:= qu.IntAll(mgodata["totalnum"]),qu.IntAll(esdata["totalnum"])

	comeintime:=qu.Int64All(time.Now().Unix())
	date := qu.FormatDateByInt64(&gte_time, qu.DATEFORMAT)
	save_mgo.Save("monitor_site", map[string]interface{}{
		"comeintime":comeintime,
		"date":date,
		"name":"全量站点",
		"mgonum":mgonum,
		"esnum":esnum,
		"mgodata":mgodata["detail"],
		"esdata":esdata["detail"],
	})

	log.Println("站点-定时处理完毕...","用时：",int(time.Now().Unix())-start,"秒")
}












//es数据统计
func toCalculateEsData(gte_time string,lt_time string) map[string]interface{} {

	if gte_time == "" || lt_time == "" {
		return map[string]interface{}{
			"totalnum" : 0,
			"detail": map[string]interface{}{},
		}
	}

	dict:= make(map[string]interface{},0)
	total :=0

	elastic.InitElasticSize("http://172.17.145.170:9800", 10,)
	//elastic.InitElasticSize("http://127.0.0.1:12003", 10,)
	esclient := elastic.GetEsConn()
	defer elastic.DestoryEsConn(esclient)
	if esclient == nil {
		log.Println("连接池异常")
	}
	query :=es_elastic.NewRangeQuery("comeintime").Gte(gte_time).Lt(lt_time)
	cursor, err := esclient.Scan(es_index_st).Query(es_elastic.NewBoolQuery().Must(query)).
		Size(200).Do()
	if err != nil {
		log.Println("cursor",err)
	}
	if cursor.Results == nil {
		log.Println("results != nil; got nil")
	}
	if cursor.Results.Hits == nil {
		log.Println("expected results.Hits != nil; got nil")
	}
	//log.Println("es total ：",cursor.TotalHits(),"处理分析中......")
	for {
		searchResult, err := cursor.Next()
		if err != nil {
			if err.Error() == "EOS" {
				break
			}else {
				log.Println("cursor searchResult",err)
			}
		}
		for _, hit := range searchResult.Hits.Hits {
			tmp := make(map[string]interface{})
			err := json.Unmarshal(*hit.Source, &tmp)
			if err != nil {
				log.Println("json Unmarshal error")
				continue
			}
			total++
			site := qu.ObjToString(tmp["site"])	//统计站点
			if dict[site] == nil {
				dict[site] = 1
			}else {
				num := qu.IntAll(dict[site])+1
				dict[site] = num
			}
		}
	}


	return map[string]interface{}{
		"totalnum" : total,
		"detail": dict,
	}
}




//mongo数据统计
func toCalculateMgoData(gte_time int64,lt_time int64) map[string]interface{} {

	if gte_time == 0 || lt_time == 0 {
		return map[string]interface{}{
			"totalnum" : 0,
			"detail": map[string]interface{}{},
		}
	}

	query := map[string]interface{}{
		"comeintime": map[string]interface{}{
			"$gte":  gte_time,
			"$lt": lt_time,
		},
	}

	sess := st_mgo.GetMgoConn()
	defer st_mgo.DestoryMongoConn(sess)

	dict:= make(map[string]interface{},0)
	//细节才需要遍历
	it := sess.DB(st_mgo.DbName).C(st_c_name).Find(&query).Select(map[string]interface{}{
		"site":1,
	}).Iter()
	total:=0
	for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
		//统计站点
		site := qu.ObjToString(tmp["site"])
		if dict[site] == nil {
			dict[site] = 1
		}else {
			num := qu.IntAll(dict[site])+1
			dict[site] = num
		}

		tmp = make(map[string]interface{})
	}
	return map[string]interface{}{
		"totalnum" : total,
		"detail": dict,
	}
}