yats.git

commit 512911d5667044e9aced988aff8194700262a724

Author: Paolo Lulli <paolo@lulli.net>

Add endpoint for listing metrics FROM

 server/db/queries.go | 30 ++++++++++++++++++++++++++++++
 server/rest/mtls.go | 10 ++++++++++
 server/rest/rest-metric.go | 18 ++++++++++++++++++
 server/service-rest.go | 2 ++


diff --git a/server/db/queries.go b/server/db/queries.go
new file mode 100644
index 0000000000000000000000000000000000000000..b0871e78bc2637b4333a4cdd133321afae45445a
--- /dev/null
+++ b/server/db/queries.go
@@ -0,0 +1,30 @@
+package db
+
+import (
+	"fmt"
+	"time"
+	"yats-server/model"
+
+	"github.com/gocql/gocql"
+)
+
+func MetricsFrom(session *gocql.Session, idClient string, metricName string, fromTime string, limit int32) []model.MetricRequest {
+	var metrics []model.MetricRequest
+	m := map[string]interface{}{}
+
+	q := fmt.Sprintf("SELECT id_client, mtime, name, value FROM metric where id_client='%s' and name ='%s' and mtime > '%s' order by mtime limit %d", idClient, metricName, fromTime, limit)
+	fmt.Println(q)
+	iter := session.Query(q).Iter()
+	for iter.MapScan(m) {
+		metrics = append(metrics, model.MetricRequest{
+			ID_client: m["id_client"].(string),
+			Mtime:     m["mtime"].(time.Time).UnixMilli(),
+			Name:      m["name"].(string),
+			Value:     m["value"].(string),
+		})
+
+		m = map[string]interface{}{}
+
+	}
+	return metrics
+}




diff --git a/server/rest/mtls.go b/server/rest/mtls.go
index cf8c8593f4fb24e445ea8237dbb4da0ff60c5307..bc68bb775d3d5954b4d960b3c41436338a0a917e 100644
--- a/server/rest/mtls.go
+++ b/server/rest/mtls.go
@@ -1,3 +1,13 @@
+/**
+ * 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 rest
 
 import "github.com/gin-gonic/gin"




diff --git a/server/rest/rest-metric.go b/server/rest/rest-metric.go
index 9ea5ffdac0ecd2776c5edb64b114e9444a4cba3f..2b695a68dd9d722264f37e92044fbfef232d6234 100644
--- a/server/rest/rest-metric.go
+++ b/server/rest/rest-metric.go
@@ -51,3 +51,21 @@ 	db.SaveMetricAt(clientCN, unixTimeUTC, metric.Name, metric.Value)
 
 	c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
 }
+
+func GetMetricsFrom(c *gin.Context) {
+	var metric model.MetricRequest
+
+	if err := c.BindJSON(&metric); err != nil {
+		c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"})
+		return
+	}
+
+	clientCN := GetClientCN(c)
+	fmt.Printf("%s / %s / %s", clientCN, metric.Name, metric.Value)
+
+	unixTimeUTC := time.Unix(metric.Mtime, 0)
+	timeAsBytes, _ := unixTimeUTC.UTC().MarshalText()
+	metricsPack := db.MetricsFrom(db.Session, clientCN, metric.Name, string(timeAsBytes), 100)
+
+	c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": metricsPack})
+}




diff --git a/server/service-rest.go b/server/service-rest.go
index d76461e89157f4ce1005bbf2e97c5f1cd212d1eb..1ab7c9f26d993aeea513200e0c2ea0dc9b5eea8d 100644
--- a/server/service-rest.go
+++ b/server/service-rest.go
@@ -31,6 +31,8 @@
 	router.POST("/rt/metric", rest.WriteMetricNow)
 	router.POST("/metric", rest.WriteMetricAt)
 
+	router.POST("/metric/get", rest.GetMetricsFrom)
+
 	router.POST("/rt/event", rest.WriteEventNow)
 	router.POST("/event", rest.WriteEventcAt)