yats.git

commit bd369765abe0b4b897bb923e6d5cec60338ce769

Author: Paolo Lulli <paolo@lulli.net>

Add flag to format event output

 client/config/cli-config.go | 6 ------
 client/duplicated_models.go | 25 +++++++++++++++++++++++++
 client/event-client-rest.go | 2 +-
 client/main.go | 30 +++++++++++++++++++++++++++++-
 client/position-client-rest.go | 2 +-


diff --git a/client/config/cli-config.go b/client/config/cli-config.go
index 9e44907dc20856bff8c6aed3cea73c6360af1b7a..6cd357b202469fdf2996f56b133c14ea1d1f1c4e 100644
--- a/client/config/cli-config.go
+++ b/client/config/cli-config.go
@@ -16,13 +16,7 @@ )
 
 type ClientConfiguration struct {
 	Endpoint string `json:"endpoint"`
-	/*
-			Service  string `json:"service"`
-			Client   string `json:"client"`
 
-		   clientName string, organization string, organizationalUnit string, emailAddress string
-
-	*/
 	TlsKeyFile      string `json:"tlsKeyFile"`
 	TlsCertificate  string `json:"tlsCertificate"`
 	TlsVerifyServer string `json:"tlsVerifyServer"`




diff --git a/client/duplicated_models.go b/client/duplicated_models.go
new file mode 100644
index 0000000000000000000000000000000000000000..240d929232bc7deb65a6f3339768742f82b85ee6
--- /dev/null
+++ b/client/duplicated_models.go
@@ -0,0 +1,25 @@
+package main
+
+// These are bloody duplicates, refactor project to use same
+// definitions between server and client
+
+type EventModel struct {
+	IdClient string `json:"id_client"`
+	Etime    int64  `json:"etime"`
+	Name     string `json:"name"`
+}
+
+type MetricModel struct {
+	IdClient string `json:"id_client" parquet:"name=id_client, type=BYTE_ARRAY, convertedtype=UTF8"`
+	Mtime    int64  `json:"mtime" parquet:"name=mtime, type=INT64, convertedtype=TIMESTAMP_MILLIS"`
+	Name     string `json:"name" parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
+	Value    string `json:"value" parquet:"name=value, type=BYTE_ARRAY, convertedtype=UTF8"`
+}
+
+type PositionModel struct {
+	IdClient string  `json:"id_client" parquet:"name=id_client, type=BYTE_ARRAY, convertedtype=UTF8"`
+	Ptime    int64   `json:"ptime" parquet:"name=ptime, type=INT64, convertedtype=TIMESTAMP_MILLIS"`
+	Lon      float64 `json:"lon" parquet:"name=lon, type=DOUBLE"` //TODO Check parquet types
+	Lat      float64 `json:"lat" parquet:"name=lat, type=DOUBLE"` //TODO Check parquet types
+	Name     string  `json:"name" parquet:"name=name, type=BYTE_ARRAY, convertedtype=UTF8"`
+}




diff --git a/client/event-client-rest.go b/client/event-client-rest.go
index 58543a128783d34c6e6255ee084802b4f970c409..c0274763e0c60c52d80cebec18ed511fa355c32e 100644
--- a/client/event-client-rest.go
+++ b/client/event-client-rest.go
@@ -16,7 +16,7 @@ 		body = "{\"source\": \"" + source + "\", \"from\":" + strconv.FormatInt(from, 10) + " }"
 	} else {
 		body = "{\"source\": \"" + source + "\", \"from\":" + strconv.FormatInt(from, 10) + ",\"to\":" + strconv.FormatInt(to, 10) + " }"
 	}
-	fmt.Printf("request: [%s]\n", body)
+	//fmt.Printf("request: [%s]\n", body)
 	return c.ApiPost(c.config.Endpoint+"/event/search", body)
 }
 




diff --git a/client/main.go b/client/main.go
index eab66021489a5589f34258e7d7ef2a5a08d0b1cb..2bb2451ab1bbf80a11349fe31cb860c3a8b05a2c 100644
--- a/client/main.go
+++ b/client/main.go
@@ -10,6 +10,7 @@  */
 package main
 
 import (
+	"encoding/json"
 	"fmt"
 	"os"
 	"time"
@@ -63,6 +64,8 @@ 	sinceMinutes := flag.BoolP("min", "", false, "minutes ago")
 	sinceHours := flag.BoolP("hour", "", false, "hours ago")
 	sinceDays := flag.BoolP("day", "", false, "days ago")
 	sinceYears := flag.BoolP("year", "", false, "years ago")
+
+	reformatOutput := flag.BoolP("format", "f", false, "format output")
 
 	flag.Parse()
 
@@ -128,7 +131,32 @@ 	}
 
 	if *listEvents {
 		listEvents := yatsClient.EventList(*sourceOption, fromTimestamp, toTimestamp)
-		fmt.Printf("%s", listEvents)
+		if *reformatOutput {
+
+			var result map[string]interface{}
+			json.Unmarshal([]byte(listEvents), &result)
+			innerData := result["data"]
+
+			dataJson, er := json.Marshal(innerData)
+			if er != nil {
+				fmt.Printf("Parsing response: %v", er)
+				return
+			}
+			//fmt.Printf("--->%s", dataJson)
+
+			var eventListModel []EventModel
+			err := json.Unmarshal(dataJson, &eventListModel)
+
+			if err == nil {
+				for _, event := range eventListModel {
+					fmt.Printf("%d %s\n", event.Etime, event.Name)
+				}
+			} else {
+				fmt.Println(err)
+			}
+		} else {
+			fmt.Printf("%s", listEvents)
+		}
 		os.Exit(0)
 	}
 




diff --git a/client/position-client-rest.go b/client/position-client-rest.go
index 3084ccd15b799448e5f2727f51709a86090d250b..55f0ea9a7ccff7df36863f82a1ffd10fa98fa93b 100644
--- a/client/position-client-rest.go
+++ b/client/position-client-rest.go
@@ -16,7 +16,7 @@ 		body = "{\"source\": \"" + source + "\", \"from\":" + strconv.FormatInt(from, 10) + " }"
 	} else {
 		body = "{\"source\": \"" + source + "\", \"from\":" + strconv.FormatInt(from, 10) + ",\"to\":" + strconv.FormatInt(to, 10) + " }"
 	}
-	fmt.Printf("request: [%s]\n", body)
+	//fmt.Printf("request: [%s]\n", body)
 	return c.ApiPost(c.config.Endpoint+"/position/search", body)
 }