yats.git

commit 00fcd89516cc04563f6699ec107b081803b20df6

Author: Paolo Lulli <paolo@lulli.net>

Fix time filter bug

 client/main.go | 71 ++++++++++++++++++++++++++++++---------------------


diff --git a/client/main.go b/client/main.go
index 7c0400db4aa9903f4e11172827fc3176409649c7..2dbcc18ddcb9bca8f5dacf53da8eb82cdde1ac54 100644
--- a/client/main.go
+++ b/client/main.go
@@ -170,7 +170,7 @@ 		eventListModel, maxpage, _ := responseToEventArray(listEvents)
 		var composedList []EventModel
 		composedList = append(composedList, eventListModel...)
 
-		for (len(eventListModel) == MaxPageSize) && !stopSearching(maxpage, toTimestamp) {
+		for continueSearching(len(eventListModel), maxpage, toTimestamp) {
 			listEvents = yatsClient.EventList(*sourceOption, maxpage/1000, toTimestamp)
 			eventListModel, maxpage, _ = responseToEventArray(listEvents)
 			composedList = append(composedList, eventListModel...)
@@ -198,7 +198,7 @@ 		metricListModel, maxpage, _ := responseToMetricArray(listResponse)
 		var composedList []MetricModel
 		composedList = append(composedList, metricListModel...)
 
-		for (len(metricListModel) == MaxPageSize) && !stopSearching(maxpage, toTimestamp) {
+		for continueSearching(len(metricListModel), maxpage, toTimestamp) {
 			listResponse, _ = yatsClient.MetricList(*sourceOption, *listMetrics, maxpage/1000, toTimestamp)
 			metricListModel, maxpage, _ = responseToMetricArray(listResponse)
 			composedList = append(composedList, metricListModel...)
@@ -236,36 +236,37 @@ 		}
 
 		if *unpackJsonPayload {
 			if err == nil {
-				for _, metric := range composedList {
-					dataMap, errNoValue := mapFromBase64(metric.Value)
+				metric := composedList[0]
+				dataMap, errNoValue := mapFromBase64(metric.Value)
+				if errNoValue {
+					return
+				}
+
+				var keys []string
+				for k := range dataMap {
+					keys = append(keys, k)
+				}
+				sort.Strings(keys)
+
+				for _, k := range keys {
+					fmt.Printf("\"%s\"%s", k, recordSeparator)
+				}
+				for indexRow, row := range composedList {
+					rowMap, errNoValue := mapFromBase64(row.Value)
 					if errNoValue {
 						return
 					}
 
-					var keys []string
-					for k := range dataMap {
-						keys = append(keys, k)
-					}
-					sort.Strings(keys)
-
+					metricTime := formattedUtcTimeInSec(composedList[indexRow].Mtime)
+					//metricTime := formattedUtcTimeInSec(metric.Mtime)
+					fmt.Printf("%s", metricTime)
+					//fmt.Printf("%d%s", metric.Mtime, recordSeparator)
 					for _, k := range keys {
-						fmt.Printf("\"%s\"%s", k, recordSeparator)
+						fmt.Printf("%s\"%v\"", recordSeparator, rowMap[k])
 					}
-					for _, row := range composedList {
-						rowMap, errNoValue := mapFromBase64(row.Value)
-						if errNoValue {
-							return
-						}
+					fmt.Printf("%s", rowSeparator)
+				}
 
-						metricTime := formattedUtcTimeInSec(metric.Mtime)
-						fmt.Printf("%s", metricTime)
-						//fmt.Printf("%d%s", metric.Mtime, recordSeparator)
-						for _, k := range keys {
-							fmt.Printf("%s\"%v\"", recordSeparator, rowMap[k])
-						}
-						fmt.Printf("%s", rowSeparator)
-					}
-				}
 			} else {
 				fmt.Println(err)
 			}
@@ -314,12 +315,22 @@ 		return eventListModel, maxpage, true
 	}
 	return eventListModel, maxpage, false
 }
-func stopSearching(maxpage int64, toTimestamp int64) bool {
-	if toTimestamp == 0 {
+
+func continueSearching(arrayLen int, maxpage int64, toTimestamp int64) bool {
+	//fmt.Printf("DEBUG::arrayLen: %d maxpage: %d toTimestamp: %d\n", arrayLen, maxpage, toTimestamp)
+	if arrayLen < MaxPageSize {
+		//fmt.Printf("DEBUG::false -1\n")
 		return false
 	}
-	if maxpage >= toTimestamp {
-		return false
+
+	if toTimestamp == 0 {
+		//fmt.Printf("DEBUG::false 0\n")
+		return true
 	}
-	return true
+	if toTimestamp > maxpage {
+		//fmt.Printf("DEBUG::false 1\n")
+		return true
+	}
+	//fmt.Printf("DEBUG::true\n")
+	return false
 }