yats.git

commit 0102aa21efe7d4d94a56fc48566c89232fb68064

Author: Paolo Lulli <paolo@lulli.net>

Unpack json payload - needs FIX

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


diff --git a/client/main.go b/client/main.go
index 76d35835d75502795614440eb9c2554058bc91ad..555934dd30787721332b1ed706095f22bd17f41e 100644
--- a/client/main.go
+++ b/client/main.go
@@ -26,6 +26,8 @@ }
 
 func main() {
 	var configPath string
+	recordSeparator := ","
+	rowSeparator := ";\n"
 
 	if len(os.Args) == 1 {
 		fmt.Printf("Usage: \n\n%s --help | -h\n", os.Args[0])
@@ -69,6 +71,7 @@
 	reformatOutput := flag.BoolP("format", "f", false, "format output")
 	logOutput := flag.BoolP("value-only", "l", false, "log value only output")
 	decodeBase64Output := flag.BoolP("base64-decode", "b", false, "decode base64 output")
+	unpackJsonPayload := flag.BoolP("unpack-json", "u", false, "unpack base64/json output")
 
 	flag.Parse()
 
@@ -146,10 +149,8 @@ 			err := json.Unmarshal(dataJson, &eventListModel)
 
 			if err == nil {
 				for _, event := range eventListModel {
-					unixTimeUTC := time.Unix(event.Etime/1000, 0) //gives unix time stamp in utc
-
-					unitTimeInRFC3339 := unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format
-
+					unixTimeUTC := time.Unix(event.Etime/1000, 0)
+					unitTimeInRFC3339 := unixTimeUTC.Format(time.RFC3339)
 					fmt.Printf("%s %s\n", unitTimeInRFC3339, event.Name)
 				}
 			} else {
@@ -214,6 +215,47 @@ 				}
 				os.Exit(0)
 			}
 
+			if *unpackJsonPayload {
+				dataJson, errDataPortion := getDataJson(listResponse)
+				if errDataPortion {
+					fmt.Printf("Could not parse data attribute: %v", errDataPortion)
+					return
+				}
+
+				var metricListModel []MetricModel
+				err := json.Unmarshal(dataJson, &metricListModel)
+
+				if err == nil {
+					metric := metricListModel[0]
+					dataMap, errNoValue := loadBase64ValueAsMap(metric)
+					if errNoValue {
+						return
+					}
+
+					for k := range dataMap {
+						fmt.Printf("\"%v\"%s", k, recordSeparator)
+					}
+					fmt.Printf("%s", rowSeparator)
+					for _, row := range metricListModel {
+						rowMap, errNoValue := loadBase64ValueAsMap(row)
+						if errNoValue {
+							return
+						}
+						for k := range dataMap {
+							if rowMap[k] != nil {
+								fmt.Printf("\"%v\"%s", rowMap[k], recordSeparator)
+							} else {
+								fmt.Printf("\"-\"%s", recordSeparator)
+							}
+						}
+						fmt.Printf("%s", rowSeparator)
+					}
+				} else {
+					fmt.Println(err)
+				}
+				os.Exit(0)
+			}
+
 			fmt.Printf("%s", listResponse)
 
 		} else {
@@ -227,6 +269,19 @@ 		listResponse := yatsClient.PositionList(*sourceOption, fromTimestamp, toTimestamp)
 		fmt.Printf("%s", listResponse)
 		os.Exit(0)
 	}
+}
+
+func loadBase64ValueAsMap(metric MetricModel) (map[string]interface{}, bool) {
+	decodedBytes, err := base64.StdEncoding.DecodeString(metric.Value)
+	if err != nil {
+		fmt.Println("Error decoding string:", err)
+		return nil, true
+	}
+
+	decoded := string(decodedBytes)
+	var dataMap map[string]interface{}
+	json.Unmarshal([]byte(decoded), &dataMap)
+	return dataMap, false
 }
 
 func getDataJson(list string) ([]byte, bool) {