Author: Paolo Lulli <paolo@lulli.net>
Fix input time filters
server/rest/rest-event.go | 22 +++++++++++----------- server/rest/rest-metric.go | 25 +++++++++++++------------ server/rest/rest-position.go | 20 ++++++++++----------
diff --git a/server/rest/rest-event.go b/server/rest/rest-event.go index 93381e8a43f9b206aaf786b89833d648af56d313..101a8bf50e37f4a313cde300e07fe8ad845bcb2b 100644 --- a/server/rest/rest-event.go +++ b/server/rest/rest-event.go @@ -37,7 +37,7 @@ return func(c *gin.Context) { var event model.EventModel if err := c.BindJSON(&event); err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) return } @@ -70,7 +70,7 @@ return func(c *gin.Context) { var event model.EventSearchRequest if err := c.BindJSON(&event); err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) return } @@ -79,7 +79,7 @@ fmt.Printf("Client ID: %s\n ", clientCN) if event.SourceApplication != "" { if !db.CanReadSourceEvent(clientCN, event.SourceApplication) { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-2"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-2"}) return } clientCN = event.SourceApplication @@ -88,14 +88,14 @@ var eventsPack []model.EventModel if event.To == 0 { unixTimeUTC := time.Unix(event.From, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100) + timeAsBytes := unixTimeUTC.UTC().String() + eventsPack = db.EventsFrom(db.Session, clientCN, timeAsBytes, 100) } else if event.From != 0 { fromUnixTimeUTC := time.Unix(event.From, 0) toUnixTimeUTC := time.Unix(event.To, 0) - fromInBytes, _ := fromUnixTimeUTC.UTC().MarshalText() - toInBytes, _ := toUnixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsBetween(db.Session, clientCN, string(fromInBytes), string(toInBytes), 100) + fromString := fromUnixTimeUTC.UTC().String() + toString := toUnixTimeUTC.UTC().String() + eventsPack = db.EventsBetween(db.Session, clientCN, fromString, toString, 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } @@ -119,7 +119,7 @@ fromParam := c.Param("from") fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64) if err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) } clientCN := GetClientCN(c, cfg) @@ -128,8 +128,8 @@ var eventsPack []model.EventModel unixTimeUTC := time.Unix(fromParamInt64, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100) + timeAsString := unixTimeUTC.UTC().String() + eventsPack = db.EventsFrom(db.Session, clientCN, timeAsString, 100) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } diff --git a/server/rest/rest-metric.go b/server/rest/rest-metric.go index 03627d9f1714e0578c7a70ffa0556a966c684d94..762a6bc3dd5a8af04a1287f9949c631cb2e30fb8 100644 --- a/server/rest/rest-metric.go +++ b/server/rest/rest-metric.go @@ -80,7 +80,7 @@ return func(c *gin.Context) { var metric model.MetricSearchRequest if err := c.BindJSON(&metric); err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) return } @@ -88,7 +88,7 @@ clientCN := GetClientCN(c, cfg) if metric.SourceApplication != "" { if !db.CanReadSourceMetric(clientCN, metric.SourceApplication, metric.Name) { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-2"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-2"}) return } clientCN = metric.SourceApplication @@ -100,16 +100,16 @@ var metricsPack []model.MetricModel if metric.To == 0 { unixTimeUTC := time.Unix(metric.From, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - metricsPack = db.MetricsFrom(db.Session, clientCN, metric.Name, string(timeAsBytes), 100) + timeAsBytes := unixTimeUTC.UTC().String() + metricsPack = db.MetricsFrom(db.Session, clientCN, metric.Name, timeAsBytes, 100) } else if metric.From != 0 { fromUnixTimeUTC := time.Unix(metric.From, 0) toUnixTimeUTC := time.Unix(metric.To, 0) - fromInBytes := fromUnixTimeUTC.UTC().String() - toInBytes, _ := toUnixTimeUTC.UTC().MarshalText() - fmt.Println("PASSING HERE, from:" + fromInBytes + " to:" + string(toInBytes)) - //LOG.Printf("PASSING HERE, from %s to %s\n", fromInBytes, toInBytes) - metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, fromInBytes, string(toInBytes), 100) + fromString := fromUnixTimeUTC.UTC().String() + toString := toUnixTimeUTC.UTC().String() + fmt.Println("PASSING HERE, from:" + fromString + " to:" + toString) + //LOG.Printf("PASSING HERE, from %s to %s\n", fromString, toString) + metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, fromString, toString, 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": metricsPack}) } @@ -135,7 +135,8 @@ nameParam := c.Param("name") fromParam := c.Param("from") fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64) if err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) + return } clientCN := GetClientCN(c, cfg) @@ -144,8 +145,8 @@ var metricsPack []model.MetricModel unixTimeUTC := time.Unix(fromParamInt64, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, string(timeAsBytes), 100) + timeAsString := unixTimeUTC.UTC().String() + metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, timeAsString, 100) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": metricsPack}) } diff --git a/server/rest/rest-position.go b/server/rest/rest-position.go index 23590f9e5c4a9879854229dce94f112f37a7559f..b61c0a9b24829e12c44b585177266e1bc1985662 100644 --- a/server/rest/rest-position.go +++ b/server/rest/rest-position.go @@ -60,7 +60,7 @@ return func(c *gin.Context) { var position model.PositionSearchRequest if err := c.BindJSON(&position); err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) return } @@ -69,7 +69,7 @@ fmt.Printf("Client ID: %s\n ", clientCN) if position.SourceApplication != "" { if !db.CanReadSourcePosition(clientCN, position.SourceApplication) { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) return } clientCN = position.SourceApplication @@ -78,14 +78,14 @@ var eventsPack []model.EventModel if position.To == 0 { unixTimeUTC := time.Unix(position.From, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100) + timeString := unixTimeUTC.UTC().String() + eventsPack = db.EventsFrom(db.Session, clientCN, timeString, 100) } else if position.From != 0 { fromUnixTimeUTC := time.Unix(position.From, 0) toUnixTimeUTC := time.Unix(position.To, 0) - fromInBytes, _ := fromUnixTimeUTC.UTC().MarshalText() - toInBytes, _ := toUnixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsBetween(db.Session, clientCN, string(fromInBytes), string(toInBytes), 100) + fromString := fromUnixTimeUTC.UTC().String() + toString := toUnixTimeUTC.UTC().String() + eventsPack = db.EventsBetween(db.Session, clientCN, fromString, toString, 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } @@ -109,7 +109,7 @@ fromParam := c.Param("from") fromParamInt64, err := strconv.ParseInt(fromParam, 10, 64) if err != nil { - c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) + c.IndentedJSON(http.StatusBadRequest, gin.H{"ret": "-1"}) } clientCN := GetClientCN(c, cfg) @@ -118,8 +118,8 @@ var eventsPack []model.EventModel unixTimeUTC := time.Unix(fromParamInt64, 0) - timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() - eventsPack = db.EventsFrom(db.Session, clientCN, string(timeAsBytes), 100) + timeString := unixTimeUTC.UTC().String() + eventsPack = db.EventsFrom(db.Session, clientCN, timeString, 100) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) }