From 6d09d7170c99f8d16acdbcb0a2280ccab950c84c Mon Sep 17 00:00:00 2001
From: 554325746@qq.com <554325746@qq.com>
Date: 星期六, 01 八月 2020 10:33:00 +0800
Subject: [PATCH] add systemd service

---
 esutil/EsClient.go |  278 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 250 insertions(+), 28 deletions(-)

diff --git a/esutil/EsClient.go b/esutil/EsClient.go
index 57fdb75..f0239fc 100644
--- a/esutil/EsClient.go
+++ b/esutil/EsClient.go
@@ -1,7 +1,9 @@
 package esutil
 
 import (
+	"bytes"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -10,15 +12,42 @@
 	"strings"
 	"time"
 
-	log "github.com/long/test/log"
+	log "andriodServer/log"
 )
 
-func GetEsDataReq(url string, parama string, isSource bool) (error, map[string]interface{}) {
-	log.Log.Infoln("es 鏌ヨ璇锋眰璺緞" + url) //  閰嶇疆淇℃伅 鑾峰彇
-	req, err := http.NewRequest("POST", url, strings.NewReader(parama))
+func EsReq(method string, url string, parama []byte) (buf []byte, err error) {
+	//defer elapsed("page")()
+	timeout := time.Duration(100 * time.Second)
+	client := http.Client{
+		Timeout: timeout,
+	}
+	request, err := http.NewRequest(method, url, bytes.NewBuffer(parama))
+	request.Header.Set("Content-type", "application/json")
 
 	if err != nil {
-		log.Log.Errorln("build request error! ")
+		fmt.Println("build request fail !")
+		return nil, err
+	}
+
+	resp, err := client.Do(request)
+	if err != nil {
+		fmt.Println("request error: ", err)
+		return nil, err
+	}
+
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		fmt.Println(err)
+		return nil, err
+	}
+	return body, nil
+}
+
+func GetEsDataReq(url string, param string, isSource bool) (error, map[string]interface{}) {
+	req, err := http.NewRequest("POST", url, strings.NewReader(param))
+
+	if err != nil {
 		return err, nil
 	}
 
@@ -28,7 +57,7 @@
 	resp, err := client.Do(req)
 
 	if err != nil {
-		return err, nil
+        return err, nil
 	}
 
 	defer resp.Body.Close()
@@ -42,27 +71,106 @@
 	dec := json.NewDecoder(strings.NewReader(jsonStr))
 
 	if err := dec.Decode(&dat); err == io.EOF {
-		log.Log.Infoln(err.Error())
 		return err, nil
 	} else if err != nil {
-		log.Log.Errorln(err.Error())
 		return err, nil
 	}
 	// 鏄惁闇�瑕� 瑙f瀽 es 杩斿洖鐨� source
 	if isSource {
-		dat = dat["hits"].(map[string]interface{})
+        dat, ok := dat["hits"].(map[string]interface{})
+		if !ok {
+			return errors.New("data is not type of  map[string]interface{}"), nil
+		}
+
 		var data = make(map[string]interface{}, 2)
 		data["total"] = dat["total"]
 		sources := []interface{}{}
 		for _, value := range dat["hits"].([]interface{}) {
-			source := value.(map[string]interface{})["_source"].(map[string]interface{})
-			source["id"] = value.(map[string]interface{})["_id"]
-			sdkType := source["sdkType"]
-			if sdkType != nil {
-				sdk, _ := strconv.Atoi(sdkType.(string))
-				source["sdkType"] = sdkTypeToValue(sdk)
+			d := make(map[string]interface{})
+			source, ok := value.(map[string]interface{})["_source"].(map[string]interface{})
+			if !ok {
+				return errors.New("value is not type of map[string]interface{}"), nil
 			}
-			sources = append(sources, source)
+
+			d["id"] = value.(map[string]interface{})["_id"]
+			d["picDate"] = source["picDate"]
+			pmax, exist := source["picMaxUrl"]
+			if !exist {
+				continue
+			}
+			alarmRules, exist := source["alarmRules"]
+			if exist {
+				if alarmB,ae := json.Marshal(alarmRules);ae ==nil {
+					var alarmRArr []AlarmRule
+					if ae = json.Unmarshal(alarmB, &alarmRArr);ae ==nil && len(alarmRArr) >0 {
+						d["alarmLevel"] = alarmRArr[0].AlarmLevel
+					}
+				}
+			} else {
+				d["alarmLevel"] = ""
+			}
+			pmArr := pmax.([]interface{})
+			if len(pmArr) > 0 {
+				d["picMaxUrl"] = pmArr[0]
+			} else {
+				d["picMaxUrl"] = ""
+			}
+			d["picAddress"] = source["cameraAddr"]
+
+			tB, err := json.Marshal(source["targetInfo"])
+			if err != nil {
+			    log.Log.Infoln("err:", err)
+                continue
+			}
+
+			tiArr := []TargetInfo{}
+			err = json.Unmarshal(tB, &tiArr)
+			if err !=nil {
+			    log.Log.Infoln("err:", err)
+				continue
+			}
+			if len(tiArr) > 0 {
+				ti := tiArr[0]
+				d["picSmUrl"] = ti.PicSmUrl
+				d["targetType"] = ti.TargetType
+				if ti.TargetType == "FaceDetect" {
+					sLabelStr, ok := source["showLabels"]
+					if ok {
+						//labelArr := strings.Split(sLabelStr.(string), "/")
+						//if len(labelArr) == 3 {
+						//	d["gender"] = labelArr[0]
+						//	d["ageDescription"] = labelArr[1]
+						//	d["race"] = labelArr[2]
+						//}
+						d["showLabels"] = sLabelStr
+					} else {
+						d["showLabels"] = ""
+					}
+
+
+					if bInfos,ok := source["baseInfo"]; ok && bInfos != nil {
+						bd := getSourceBaseInfo(bInfos)
+						d["baseInfo"] = bd //姣斿鍒扮殑搴曞簱鐨勪汉
+					} else {
+						d["baseInfo"] = []interface{}{}
+					}
+					d["sdkType"] = source["taskName"]
+				} else {
+					d["sdkType"] = source["taskName"]
+					d["picSmUrl"] = d["picMaxUrl"]
+				}
+			} else {
+				d["picSmUrl"] = d["picMaxUrl"]
+			}
+
+			vUri := source["videoUrl"]
+			if vUri != nil && vUri.(string) != "" {
+				d["videoNum"] = "http://"+vUri.(string)
+			} else {
+				d["videoNum"] = ""
+			}
+
+			sources = append(sources, d)
 		}
 		data["datalist"] = sources
 		return nil, data
@@ -71,25 +179,139 @@
 	}
 }
 
-//sdk绫诲瀷
-func sdkTypeToValue(i int) string {
-	value := []string{"浜鸿劯", "杞﹁締", "浜轰綋", "鍏ヤ镜", "鎷ユ尋", "闈犲彸琛�", "浜哄憳寮傚父", "涓綋闈欐"}
-
-	return value[i-1]
+type AlarmRule struct {
+	GroupId 			string 			`json:"groupId"`
+	AlarmLevel 			string 			`json:"alarmLevel"`
 }
 
-func PostAction(sec int, Eurl string) []byte {
-	index := "videopersons,personaction"
+type TargetInfo struct {
+	TargetId 		string  		`json:"targetId"`
+	TargetType 		string 			`json:"targetType"`
+	PicSmUrl 		string 			`json:"picSmUrl"`
+	TargetScore 	float32 		`json:"targetScore"`
+}
+
+type BaseInfo struct {
+	TaskId 			string 		`json:"taskId"`
+	TaskName 		string 		`json:"taskName"`
+	LikePer 		float32 	`json:"likePer"`
+	TableId 		string 		`json:"tableId"`
+	TableName 		string 		`json:"tableName"`
+	PersonId 		string 		`json:"personId"`
+	PersonPicUrl 	string 		`json:"personPicUrl"`
+	PersonName 		string 		`json:"personName"`
+	Gender 			string 		`json:"gender"`
+	PhoneNum 		string 		`json:"phoneNum"`
+	IDCard 			string 		`json:"IDCard"`
+	MonitorLevel 	string 		`json:"monitorLevel"`
+	Content 		string 		`json:"content"`
+}
+
+type TI struct {
+	BwType       	string  	`json:"bwType"`
+	TargetPicUrl 	string 		`json:"targetPicUrl"`
+	TargetName 		string 		`json:"targetName"`
+	TargetId 		string 		`json:"targetId"`
+	TableId 		string 		`json:"tableId"`
+	CompareScore 	float32 	`json:"compareScore"`
+	MonitorLevel 	string 		`json:"monitorLevel"`
+	Content 		string 		`json:"content"`
+	TableName 		string 		`json:"tableName"`
+	Labels 			string 		`json:"labels"`
+}
+
+func getSourceBaseInfo(bInfos interface{}) []BaseInfo {
+	baseInfoArr := make([]BaseInfo,0)
+
+	b, err := json.Marshal(bInfos)
+	if err == nil {
+		var targetArr []TI
+		if err = json.Unmarshal(b, &targetArr); err == nil && len(targetArr) >0 {
+			for _,t := range targetArr {
+				idCard,sex := "",""
+				if t.Labels != "" {
+					arr := strings.Split(t.Labels, "/")
+					if len(arr) > 0 {
+						for _,str := range arr {
+							if str == "鐢�" || str == "濂�" {
+								sex = str
+								break
+							}
+						}
+						for _,str := range arr {
+							if len(str) == 18 {
+								idCard = str
+								break
+							}
+						}
+					}
+				}
+				baseInfoArr = append(baseInfoArr, BaseInfo{
+					TaskId: "",//2.0鏂板瓧娈�
+					TaskName: "",//2.0鏂板瓧娈�
+					LikePer: t.CompareScore,
+					TableId: "",//2.0鏂板瓧娈�
+					TableName: t.TableName,
+					PersonId: t.TargetId,
+					PersonName: t.TargetName,//浜哄憳濮撳悕锛屼粠绠$悊骞冲彴鑾峰彇
+					PersonPicUrl: t.TargetPicUrl,
+					Gender: sex,
+					PhoneNum: "",//鎵嬫満鍙凤紝浠庣鐞嗗钩鍙拌幏鍙�
+					IDCard: idCard,
+					MonitorLevel: t.MonitorLevel,//2.0鏂板瓧娈�
+					Content: t.Content,
+				})
+			}
+		} else {
+			fmt.Println("unmarshal bInfos err:", err)
+		}
+	} else {
+		fmt.Println("marshal bInfos err:", err)
+	}
+
+	return baseInfoArr
+}
+
+func PostAction(sec int, Eurl string, ishub string, size int, lastT time.Time, curTime time.Time) []byte {
+	index := "ai_ocean"
 	url := fmt.Sprintf("%s%s%s", Eurl, index, "/_search")
 
-	seccond := strconv.Itoa(sec)
+	sizeStr :=""
+	if size <=0 {
+		sizeStr = "1000"
+	} else {
+		sizeStr = strconv.Itoa(size)
+	}
+	preSec := "10"
+	if sec <=0 {
+		preSec = "10"
+	} else {
+		preSec = strconv.Itoa(sec)
+	}
+	var filterArr []string
+	var mustNotArr []string
+	//鏄惁鏌ユ姤璀︽暟鎹�
+	if ishub == "hub" {
+		mustNotArr = append(mustNotArr,"{\"term\":{\"alarmRules.alarmLevel.raw\":\"浜旂骇\"}}")
+	}
 
-	prama := "{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"personIsHub\":\"1\"}},{\"range\":{\"picDate\":{\"gte\":\"now+8h-" + seccond + "s\",\"lt\":\"now+8h\"}}}]}},\"size\":\"1000\",\"sort\":[{\"picDate\":{\"order\":\"desc\"}}]," +
-		"\"_source\":[\"BaseName\",\"Gender\",\"Race\",\"content\",\"idcard\",\"picAddress\",\"picDate\",\"sdkType\",\"Age\",\"personId\",\"personIsHub\",\"personPicUrl\",\"picLocalUrl\",\"picSmUrl\",\"videoIp\",\"videoNum\",\"videoReqNum\",\"ageDescription\",\"likePer\",\"picMaxUrl\"]" + "}"
+	filterArr = append(filterArr, "{\"range\":{\"picDate\":{\"gte\":\"now+8h-"+preSec+"s\",\"lt\":\"now+8h\"}}}")
 
-	err, tokenRes := GetEsDataReq(url, prama, true)
+	filterStr := ""
+	mustNotStr := ""
+	if len(filterArr) >0 {
+		filterStr = strings.Join(filterArr, ",")
+	}
+	if len(mustNotArr) > 0 {
+		mustNotStr = strings.Join(mustNotArr, ",")
+	}
+	param := "{\"query\":{\"bool\":{\"filter\":["+filterStr+"],\"must_not\":["+mustNotStr+"]}},\"size\":\""+sizeStr+"\",\"sort\":[{\"picDate\":{\"order\":\"desc\"}}]," +
+		"\"_source\":{\"includes\":[\"cameraAddr\",\"baseInfo\",\"targetInfo\",\"content\",\"id\",\"picMaxUrl\",\"picDate\",\"showLabels\",\"taskName\",\"sdkName\",\"videoUrl\",\"alarmRules\"],\"excludes\":[\"*.feature\",\"*.attachTarget\",\"*.targetLocation\"]}" +
+		"}"
+	err, tokenRes := GetEsDataReq(url, param, true)
+
 	if err != nil {
-		log.Log.Errorln(err)
+		log.Log.Errorln("GetEsDataReq err:", err)
 		return nil
 	}
 	jsonstring, _ := json.Marshal(tokenRes)

--
Gitblit v1.8.0