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 |   38 ++++++++++++++++++--------------------
 1 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/EsClient.go b/EsClient.go
index dcf9f04..79c7c0d 100644
--- a/EsClient.go
+++ b/EsClient.go
@@ -531,13 +531,14 @@
 			}
 
 			sTime := tmpTime
-			eTime := tmpTime
-			stayTime := 0.0
-			if startTime != "" && point <= indexLength{
+			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)
 				passTime := math.Abs(mTime.Sub(sinTime).Seconds())
+				hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
 				//fmt.Println("passTime:   ", passTime)
-				if passTime <= thresholdTime {
+				if passTime <= thresholdTime || point == indexLength{
 					startTime = tmpTime
 					hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
 					if point == indexLength{
@@ -549,16 +550,15 @@
 					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)
-					stayTime = math.Abs(mTime.Sub(realStartTime).Seconds())
-					//fmt.Println("stayTime:    ",stayTime)
-					hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
-					//fmt.Println("stayTime",stayTime)
+					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"] = tmpTime
-						stayTime = stayTime + 1
+						stayTime =  1
 					}
 					hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
 					startTime = ""
@@ -635,6 +635,7 @@
 		indexLength := len(finalHits)
 		point := 0
 		startTime := ""
+		//fmt.Println("finalHits: ",finalHits)
 		for _, in := range finalHits {
 			point = point+1
 			tmpHitSource := make(map[string]interface{})
@@ -657,11 +658,12 @@
 			}
 
 			sTime := tmpTime
-			eTime := tmpTime
-			stayTime := 0.0
-			if startTime != "" && point <= indexLength{
+			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)
 				passTime := math.Abs(mTime.Sub(sinTime).Seconds())
+				hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
 				//fmt.Println("passTime:   ", passTime)
 				if passTime <= thresholdTime || point == indexLength{
 					startTime = tmpTime
@@ -675,19 +677,15 @@
 					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)
-					//fmt.Println("hitStartTime:    ",hitStartTime)
-					//fmt.Println("realStartTime:    ",realStartTime)
-					//fmt.Println("mTime:    ",mTime)
-					stayTime = math.Abs(mTime.Sub(realStartTime).Seconds())
-					//fmt.Println("stayTime:    ",stayTime)
-					hitsSources[len(hitsSources)-1]["endTime"] = tmpTime
-					//fmt.Println("stayTime",stayTime)
+					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"] = tmpTime
-						stayTime = stayTime + 1
+						stayTime =  1
 					}
 					hitsSources[len(hitsSources)-1]["stayTime"] = stayTime
 					startTime = ""

--
Gitblit v1.8.0