Author: Paolo Lulli <paolo@lulli.net>
Refactor time input filter
server/rest/rest-event.go | 16 ++++------------ server/rest/rest-metric.go | 19 ++++--------------- server/rest/rest-position.go | 17 ++++------------- server/util/time.go | 20 ++++++++++++++++++++
diff --git a/server/rest/rest-event.go b/server/rest/rest-event.go index 101a8bf50e37f4a313cde300e07fe8ad845bcb2b..4ef1255e76742abb2d2c0d68bb7833e4d99c97aa 100644 --- a/server/rest/rest-event.go +++ b/server/rest/rest-event.go @@ -19,6 +19,7 @@ "time" "yats-server/config" "yats-server/db" "yats-server/model" + "yats-server/util" ) // WriteEvent godoc @@ -87,15 +88,9 @@ } var eventsPack []model.EventModel if event.To == 0 { - unixTimeUTC := time.Unix(event.From, 0) - timeAsBytes := unixTimeUTC.UTC().String() - eventsPack = db.EventsFrom(db.Session, clientCN, timeAsBytes, 100) + eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(event.From), 100) } else if event.From != 0 { - fromUnixTimeUTC := time.Unix(event.From, 0) - toUnixTimeUTC := time.Unix(event.To, 0) - fromString := fromUnixTimeUTC.UTC().String() - toString := toUnixTimeUTC.UTC().String() - eventsPack = db.EventsBetween(db.Session, clientCN, fromString, toString, 100) + eventsPack = db.EventsBetween(db.Session, clientCN, util.Int64ToTimeUTC(event.From), util.Int64ToTimeUTC(event.To), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } @@ -126,10 +121,7 @@ clientCN := GetClientCN(c, cfg) fmt.Printf("Client ID: %s\n ", clientCN) var eventsPack []model.EventModel - - unixTimeUTC := time.Unix(fromParamInt64, 0) - timeAsString := unixTimeUTC.UTC().String() - eventsPack = db.EventsFrom(db.Session, clientCN, timeAsString, 100) + eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(fromParamInt64), 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 063a91f22e2163a2966e187639f90eebd5ca5356..4134388f156b45037b6bab4fcb10ac7363025b36 100644 --- a/server/rest/rest-metric.go +++ b/server/rest/rest-metric.go @@ -17,11 +17,11 @@ "log" "net/http" "os" "strconv" - "strings" "time" "yats-server/config" "yats-server/db" "yats-server/model" + "yats-server/util" ) var ( @@ -100,17 +100,9 @@ var metricsPack []model.MetricModel if metric.To == 0 { - unixTimeUTC := time.Unix(metric.From, 0) - timeString := unixTimeUTC.UTC().String() - metricsPack = db.MetricsFrom(db.Session, clientCN, metric.Name, strings.TrimSpace(timeString[0:19]), 100) + metricsPack = db.MetricsFrom(db.Session, clientCN, metric.Name, util.Int64ToTimeUTC(metric.From), 100) } else if metric.From != 0 { - fromUnixTimeUTC := time.Unix(metric.From, 0) - toUnixTimeUTC := time.Unix(metric.To, 0) - 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, strings.TrimSpace(fromString[0:19]), strings.TrimSpace(toString[0:19]), 100) + metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, util.Int64ToTimeUTC(metric.From), util.Int64ToTimeUTC(metric.To), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": metricsPack}) } @@ -144,10 +136,7 @@ clientCN := GetClientCN(c, cfg) fmt.Printf("%s / %s ", clientCN, metric.Name) var metricsPack []model.MetricModel - - unixTimeUTC := time.Unix(fromParamInt64, 0) - timeAsString := unixTimeUTC.UTC().String() - metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, timeAsString, 100) + metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, util.Int64ToTimeUTC(fromParamInt64), 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 b144c10f35d9727a65bb2333d8497bb0c9cded27..86b65c30ea90a5993ddd0e07b43684c7e20deedd 100644 --- a/server/rest/rest-position.go +++ b/server/rest/rest-position.go @@ -5,11 +5,11 @@ "fmt" "github.com/gin-gonic/gin" "net/http" "strconv" - "strings" "time" "yats-server/config" "yats-server/db" "yats-server/model" + "yats-server/util" ) // WritePosition godoc @@ -78,15 +78,9 @@ } var eventsPack []model.EventModel if position.To == 0 { - unixTimeUTC := time.Unix(position.From, 0) - timeString := unixTimeUTC.UTC().String() - eventsPack = db.EventsFrom(db.Session, clientCN, timeString, 100) + eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(position.From), 100) } else if position.From != 0 { - fromUnixTimeUTC := time.Unix(position.From, 0) - toUnixTimeUTC := time.Unix(position.To, 0) - fromString := fromUnixTimeUTC.UTC().String() - toString := toUnixTimeUTC.UTC().String() - eventsPack = db.EventsBetween(db.Session, clientCN, fromString, toString, 100) + eventsPack = db.EventsBetween(db.Session, clientCN, util.Int64ToTimeUTC(position.From), util.Int64ToTimeUTC(position.To), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } @@ -117,10 +111,7 @@ clientCN := GetClientCN(c, cfg) fmt.Printf("Client ID: %s\n ", clientCN) var eventsPack []model.EventModel - - unixTimeUTC := time.Unix(fromParamInt64, 0) - timeString := unixTimeUTC.UTC().String() - eventsPack = db.EventsFrom(db.Session, clientCN, strings.TrimSpace(timeString[0:19]), 100) + eventsPack = db.EventsFrom(db.Session, clientCN, util.Int64ToTimeUTC(fromParamInt64), 100) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": eventsPack}) } diff --git a/server/util/time.go b/server/util/time.go new file mode 100644 index 0000000000000000000000000000000000000000..17b57edfbef5bc076fba44114d333b33db30582a --- /dev/null +++ b/server/util/time.go @@ -0,0 +1,20 @@ +/** + * Yats - yats + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Paolo Lulli <kevwe.com> + * @copyright Paolo Lulli 2024 + */ + +package util + +import ( + "strings" + "time" +) + +func Int64ToTimeUTC(timeAsInt64 int64) string { + return strings.TrimSpace(time.Unix(timeAsInt64, 0).UTC().String()[0:19]) +}