yats.git

commit b362bac3fb09192f0bb24608910e2ce6f5aa8959

Author: Paolo Lulli <paolo@lulli.net>

Fix client position,metric,event

 client/event-client-rest.go | 17 +++++++++++++++
 client/main.go | 30 +++++++++++++++++++++-----
 client/metric-client-rest.go | 20 ++++++++++++++++++
 client/position-client-rest.go | 40 ++++++++++++++++++++++++++++++++++++
 client/position-client-test.go | 20 ------------------


diff --git a/client/event-client-rest.go b/client/event-client-rest.go
index b8c4d5c53905a5f6cc89fe16c23930b762f82848..58543a128783d34c6e6255ee084802b4f970c409 100644
--- a/client/event-client-rest.go
+++ b/client/event-client-rest.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"errors"
 	"fmt"
 	"strconv"
 )
@@ -18,3 +19,19 @@ 	}
 	fmt.Printf("request: [%s]\n", body)
 	return c.ApiPost(c.config.Endpoint+"/event/search", body)
 }
+
+func (c *YatsClient) EventSave(eventName string, etime int64) (string, error) {
+	if eventName == "" {
+		//fmt.Println("Metric Name is empty")
+		return "", errors.New("Event Name is empty")
+	}
+
+	var body string
+	if etime == 0 {
+		body = "{ \"name\":\"" + eventName + "\" }"
+	} else {
+		body = "{ \"name\":\"" + eventName + "\", \"etime\":\"" + strconv.FormatInt(etime, 10) + "\" }"
+	}
+	fmt.Print("payload: " + body)
+	return c.ApiPost(c.config.Endpoint+"/event", body), nil
+}




diff --git a/client/main.go b/client/main.go
index 56e86c2c87231b002a85d4c765e6bc503513af18..ec24c4bd83b5ec841fc9757f5200143d54e78127 100644
--- a/client/main.go
+++ b/client/main.go
@@ -45,7 +45,16 @@ 	writeModeOption := flag.BoolP("write", "w", false, "Write Mode")
 
 	fromOption := flag.Int64("from", 0, "From tstamp")
 	toOption := flag.Int64("to", 0, "To tstamp")
-	metricNameOption := flag.StringP("metricName", "n", "", "Metric name")
+
+	metricNameOption := flag.StringP("metricName", "n", "", "Metric Name")
+	metricValueOption := flag.StringP("value", "v", "", "Metric Value")
+	timestampOption := flag.Int64("timestamp", 0, "Metric/Event TimeStamp")
+
+	eventNameOption := flag.StringP("eventName", "e", "", "Event Name")
+	positionNameOption := flag.StringP("positionName", "x", "", "Position Name")
+
+	longitudeOption := flag.Float64P("longitude", "o", 0, "Longitude")
+	latitudeOption := flag.Float64P("latitude", "a", 0, "Latitude")
 
 	flag.Parse()
 
@@ -79,18 +88,27 @@ 	} else {
 		// Write Mode
 		if *isMetricOption {
 			fmt.Printf("metricNameOption: %s", *metricNameOption)
-			listResponse, err := yatsClient.MetricList(*sourceOption, *metricNameOption, *fromOption, *toOption)
+
+			listResponse, err := yatsClient.MetricSave(*metricNameOption, *metricValueOption, *timestampOption)
 			if err == nil {
 				fmt.Printf("%s", listResponse)
 			} else {
 				fmt.Println(err)
 			}
 		} else if *isPositionOption {
-			listResponse := yatsClient.PositionList(*sourceOption, *fromOption, *toOption)
-			fmt.Printf("%s", listResponse)
+			savePositionResponse, err := yatsClient.PositionSave(*positionNameOption, *latitudeOption, *longitudeOption, *timestampOption)
+			if err == nil {
+				fmt.Printf("%s", savePositionResponse)
+			} else {
+				fmt.Println(err)
+			}
 		} else {
-			listEvents := yatsClient.EventList(*sourceOption, *fromOption, *toOption)
-			fmt.Printf("%s", listEvents)
+			saveEventResponse, err := yatsClient.EventSave(*eventNameOption, *timestampOption)
+			if err == nil {
+				fmt.Printf("%s", saveEventResponse)
+			} else {
+				fmt.Println(err)
+			}
 		}
 	}
 }




diff --git a/client/metric-client-rest.go b/client/metric-client-rest.go
index be4140886fbb0c326ca64086d8e4d2ceb07dd958..e59baec3e9576d88e39354e530ee2a2396699cb8 100644
--- a/client/metric-client-rest.go
+++ b/client/metric-client-rest.go
@@ -11,6 +11,7 @@ package main
 
 import (
 	"errors"
+	"fmt"
 	"strconv"
 )
 
@@ -27,3 +28,22 @@ 		body = "{\"source\": \"" + source + "\",\"name\":\"" + metricName + "\", \"from\":" + strconv.FormatInt(from, 10) + ",\"to\":" + strconv.FormatInt(to, 10) + " }"
 	}
 	return c.ApiPost(c.config.Endpoint+"/metric/search", body), nil
 }
+
+func (c *YatsClient) MetricSave(metricName string, value string, mtime int64) (string, error) {
+	if metricName == "" {
+		//fmt.Println("Metric Name is empty")
+		return "", errors.New("Metric Name is empty")
+	}
+	if value == "" {
+		//fmt.Println("Metric Name is empty")
+		return "", errors.New("Metric Value is empty")
+	}
+	var body string
+	if mtime == 0 {
+		body = "{ \"name\":\"" + metricName + "\", \"value\": \"" + value + "\" }"
+	} else {
+		body = "{ \"name\":\"" + metricName + "\", \"value\": \"" + value + "\", \"mtime\":\"" + strconv.FormatInt(mtime, 10) + "\" }"
+	}
+	fmt.Print("payload: " + body)
+	return c.ApiPost(c.config.Endpoint+"/metric", body), nil
+}




diff --git a/client/position-client-rest.go b/client/position-client-rest.go
new file mode 100644
index 0000000000000000000000000000000000000000..3084ccd15b799448e5f2727f51709a86090d250b
--- /dev/null
+++ b/client/position-client-rest.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+	"errors"
+	"fmt"
+	"strconv"
+)
+
+func (c *YatsClient) PositionList(source string, from int64, to int64) string {
+	if from == 0 {
+		panic("From is empty")
+	}
+	var body string
+	if to == 0 {
+		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)
+	return c.ApiPost(c.config.Endpoint+"/position/search", body)
+}
+
+func (c *YatsClient) PositionSave(positionName string, lat float64, lon float64, ptime int64) (string, error) {
+	if positionName == "" {
+		//fmt.Println("Metric Name is empty")
+		return "", errors.New("position name is empty")
+	}
+	if lat == 0 && lon == 0 {
+		//fmt.Println("Metric Name is empty")
+		return "", errors.New("Lat/Lon was 0,0")
+	}
+	var body string
+	if ptime == 0 {
+		body = "{ \"name\":\"" + positionName + "\", \"lat\": " + strconv.FormatFloat(lat, 'f', -1, 64) + ", \"lon\":" + strconv.FormatFloat(lon, 'f', -1, 64) + " }"
+	} else {
+		body = "{ \"name\":\"" + positionName + ", \"lat\": " + strconv.FormatFloat(lat, 'f', -1, 64) + ", \"lon\":" + strconv.FormatFloat(lon, 'f', -1, 64) + ", \"ptime\":\"" + strconv.FormatInt(ptime, 10) + "\" }"
+	}
+	fmt.Printf("payload: [%s]\n", body)
+	return c.ApiPost(c.config.Endpoint+"/position", body), nil
+}




diff --git a/client/position-client-test.go b/client/position-client-test.go
deleted file mode 100644
index b39ab11c7c447f5ed00c488282d46b9c419206bf..0000000000000000000000000000000000000000
--- a/client/position-client-test.go
+++ /dev/null
@@ -1,20 +0,0 @@
-package main
-
-import (
-	"fmt"
-	"strconv"
-)
-
-func (c *YatsClient) PositionList(source string, from int64, to int64) string {
-	if from == 0 {
-		panic("From is empty")
-	}
-	var body string
-	if to == 0 {
-		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)
-	return c.ApiPost(c.config.Endpoint+"/position/search", body)
-}