|
@@ -0,0 +1,167 @@
|
|
|
+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()
|
|
|
+
|
|
|
+ now:=time.Now() //当前天
|
|
|
+ gte_time:= time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local).Unix()
|
|
|
+ lt_time := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
|
|
|
+ //临时测试
|
|
|
+ gte_time = 1618070400
|
|
|
+ lt_time = 1618156800
|
|
|
+ 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(&comeintime, qu.DATEFORMAT)
|
|
|
+ datamgo.Save("monitor_site", map[string]interface{}{
|
|
|
+ "comeintime":comeintime,
|
|
|
+ "updatedate":date,
|
|
|
+ "mgonum":mgonum,
|
|
|
+ "esnum":esnum,
|
|
|
+ "mgodata":mgodata["detail"],
|
|
|
+ "esdata":esdata["detail"],
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+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{}{},
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.Println("es 查询区间:",gte_time,lt_time)
|
|
|
+
|
|
|
+ //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(st_es_index).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(),"处理分析中......")
|
|
|
+
|
|
|
+ dict:= make(map[string]interface{},0)
|
|
|
+ total,start,isOK :=0, int(time.Now().Unix()),0
|
|
|
+ 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
|
|
|
+ isOK++
|
|
|
+ }else {
|
|
|
+ num := qu.IntAll(dict[site])+1
|
|
|
+ dict[site] = num
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Println("st is es over:",total,isOK,"有效:",len(dict),"用时:",int(time.Now().Unix())-start,"秒")
|
|
|
+
|
|
|
+
|
|
|
+ return map[string]interface{}{
|
|
|
+ "totalnum" : total,
|
|
|
+ "detail": dict,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+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,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ log.Println("mgo 查询区间",query)
|
|
|
+
|
|
|
+ sess := sitemgo.GetMgoConn()
|
|
|
+ defer sitemgo.DestoryMongoConn(sess)
|
|
|
+
|
|
|
+ dict:= make(map[string]interface{},0)
|
|
|
+ //细节才需要遍历
|
|
|
+ it := sess.DB(sitemgo.DbName).C(st_collname).Find(&query).Select(map[string]interface{}{
|
|
|
+ "site":1,
|
|
|
+ }).Iter()
|
|
|
+ total,start,isOK :=0, int(time.Now().Unix()),0
|
|
|
+ for tmp := make(map[string]interface{}); it.Next(&tmp); total++ {
|
|
|
+ if total%10000==0 {
|
|
|
+ //log.Println("current index",total,isOK)
|
|
|
+ }
|
|
|
+ //统计站点
|
|
|
+ site := qu.ObjToString(tmp["site"])
|
|
|
+ if dict[site] == nil {
|
|
|
+ dict[site] = 1
|
|
|
+ isOK++
|
|
|
+ }else {
|
|
|
+ num := qu.IntAll(dict[site])+1
|
|
|
+ dict[site] = num
|
|
|
+ }
|
|
|
+
|
|
|
+ tmp = make(map[string]interface{})
|
|
|
+ }
|
|
|
+
|
|
|
+ log.Println("st is mgo over:",total,isOK,"有效:",len(dict),"用时:",int(time.Now().Unix())-start,"秒")
|
|
|
+
|
|
|
+
|
|
|
+ return map[string]interface{}{
|
|
|
+ "totalnum" : total,
|
|
|
+ "detail": dict,
|
|
|
+ }
|
|
|
+}
|