From c3f7425ceeabbd9f4e65a110afa7e486ad9bb86c Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期三, 26 八月 2020 16:01:19 +0800
Subject: [PATCH] fix stay time

---
 EsClient.go |   80 +++++++++++++++++++++++++++++++---------
 1 files changed, 62 insertions(+), 18 deletions(-)

diff --git a/EsClient.go b/EsClient.go
index 8481e19..79c7c0d 100644
--- a/EsClient.go
+++ b/EsClient.go
@@ -506,9 +506,12 @@
 		topAttentionHits := inf.(map[string]interface{})["top_attention_hits"].(map[string]interface{})
 		middleHits := topAttentionHits["hits"].(map[string]interface{})
 		finalHits := middleHits["hits"].([]interface{})
-		tmpHitSource := make(map[string]interface{})
 		startTime := ""
+		indexLength := len(finalHits)
+		point := 0
 		for _, in := range finalHits {
+			point = point+1
+			tmpHitSource := make(map[string]interface{})
 			tmpbuf, ok := in.(map[string]interface{})
 			if !ok {
 				fmt.Println("change to source error!")
@@ -528,23 +531,42 @@
 			}
 
 			sTime := tmpTime
-			eTime := tmpTime
-			stayTime := 0.0
-			if startTime != "" {
+			eTime := mTime.Add(time.Second*1).Format("2006-01-02 15:04:05")
+			stayTime := 1.0
+			if startTime != "" && point < indexLength{
 				sinTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, loc)
-				stayTime = math.Abs(sinTime.Sub(mTime).Seconds())
-				if stayTime <= thresholdTime {
+				passTime := math.Abs(mTime.Sub(sinTime).Seconds())
+				hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+				//fmt.Println("passTime:   ", passTime)
+				if passTime <= thresholdTime || point == indexLength{
 					startTime = tmpTime
 					hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
+					if point == indexLength{
+						hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
+						realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
+						stayTime = math.Abs(mTime.Sub(realStartTime).Seconds())
+						hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+					}
 					continue
 				} else {
+					hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
+					hitEndTime := hitsSources[len(hitsSources)-1]["endTime"].(string)
+					realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
+					realEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitEndTime, loc)
+					stayTime = math.Abs(realEndTime.Sub(realStartTime).Seconds())
 					if sinTime.Sub(mTime).Seconds() == 0 {
 						sinTime.Add(time.Second * 1)
 						sinTime.Format("2006-01-02 15:04:05")
-						hitsSources[len(hitsSources)-1]["endTime"] = sinTime
+						hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
+						stayTime =  1
 					}
+					hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+					startTime = ""
+					continue
+					//fmt.Println(hitsSources[len(hitsSources)-1])
 				}
 			}
+			//fmt.Println("========================================================")
 			startTime = tmpTime
 			tmpHitSource["personId"] = baseInfo["targetId"].(string)
 			tmpHitSource["cameraId"] = source["cameraId"].(string)
@@ -565,7 +587,7 @@
 			tmpHitSource["endTBackGroundPicUrl"] = source["picMaxUrl"].([]interface{})
 			tmpHitSource["endTFacePicUrl"] = targetInfo["picSmUrl"].(string)
 			if source["picWH"] != nil {
-			tmpHitSource["picWH"] = source["picWH"].(map[string]interface{})
+				tmpHitSource["picWH"] = source["picWH"].(map[string]interface{})
 			}
 			hitsSources = append(hitsSources, tmpHitSource)
 		}
@@ -610,9 +632,13 @@
 		topAttentionHits := inf.(map[string]interface{})["top_attention_hits"].(map[string]interface{})
 		middleHits := topAttentionHits["hits"].(map[string]interface{})
 		finalHits := middleHits["hits"].([]interface{})
-		tmpHitSource := make(map[string]interface{})
+		indexLength := len(finalHits)
+		point := 0
 		startTime := ""
+		//fmt.Println("finalHits: ",finalHits)
 		for _, in := range finalHits {
+			point = point+1
+			tmpHitSource := make(map[string]interface{})
 			tmpbuf, ok := in.(map[string]interface{})
 			if !ok {
 				fmt.Println("change to source error!")
@@ -632,21 +658,39 @@
 			}
 
 			sTime := tmpTime
-			eTime := tmpTime
-			stayTime := 0.0
-			if startTime != "" {
+			eTime := mTime.Add(time.Second*1).Format("2006-01-02 15:04:05")
+			stayTime := 1.0
+			if startTime != "" && point < indexLength{
 				sinTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startTime, loc)
-				stayTime = math.Abs(sinTime.Sub(mTime).Seconds())
-				if stayTime <= thresholdTime {
+				passTime := math.Abs(mTime.Sub(sinTime).Seconds())
+				hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+				//fmt.Println("passTime:   ", passTime)
+				if passTime <= thresholdTime || point == indexLength{
 					startTime = tmpTime
 					hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
+					if point == indexLength{
+						hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
+						realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
+						stayTime = math.Abs(mTime.Sub(realStartTime).Seconds())
+						hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+					}
 					continue
 				} else {
+					hitStartTime := hitsSources[len(hitsSources)-1]["startTime"].(string)
+					hitEndTime := hitsSources[len(hitsSources)-1]["endTime"].(string)
+					realStartTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitStartTime, loc)
+					realEndTime, _ := time.ParseInLocation("2006-01-02 15:04:05", hitEndTime, loc)
+					stayTime = math.Abs(realEndTime.Sub(realStartTime).Seconds())
 					if sinTime.Sub(mTime).Seconds() == 0 {
 						sinTime.Add(time.Second * 1)
 						sinTime.Format("2006-01-02 15:04:05")
-						hitsSources[len(hitsSources)-1]["endTime"] = sinTime
+						hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
+						stayTime =  1
 					}
+					hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
+					startTime = ""
+					continue
+					//fmt.Println(hitsSources[len(hitsSources)-1])
 				}
 			}
 			startTime = tmpTime
@@ -673,9 +717,9 @@
 			}
 			hitsSources = append(hitsSources, tmpHitSource)
 		}
-			tmpSources["groupKey"] = groupKey
-			tmpSources["hits_sources"] = hitsSources
-			sources = append(sources, tmpSources)
+		tmpSources["groupKey"] = groupKey
+		tmpSources["hits_sources"] = hitsSources
+		sources = append(sources, tmpSources)
 	}
 	return sources, nil
 }

--
Gitblit v1.8.0