/** * Yats - yats * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2024 */ package rest import ( "fmt" "github.com/gin-gonic/gin" "net/http" "time" "yats-server/db" "yats-server/model" ) // @BasePath / // WriteMetric godoc // @Summary write metric to db // @Schemes // @Description store metric // @Tags Metrics // @Accept json // @Param data body model.MetricModel true "Metric request body" // @Produce json // @Success 202 {string} WriteMetric // @Router /metric [post] func WriteMetric(c *gin.Context) { var metric model.MetricModel 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) if metric.Mtime != 0 { unixTimeUTC := time.Unix(metric.Mtime, 0) db.SaveMetricAt(clientCN, unixTimeUTC, metric.Name, metric.Value) } else { db.SaveMetric(clientCN, metric.Name, metric.Value) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"}) } // GetMetrics godoc // @Summary Get the metrics From ... and To when present // @Schemes // @Description retrieve metric in the specified time window // @Tags Metrics // @Accept json // @Param data body model.MetricSearchRequest true "Metric query filters" // @Produce json // @Param data body []model.MetricModel true "Metric response" // @Success 200 {string} WriteMetric // @Router /metric/get [post] func GetMetrics(c *gin.Context) { var metric model.MetricSearchRequest if err := c.BindJSON(&metric); err != nil { c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-1"}) return } clientCN := GetClientCN(c) fmt.Printf("%s / %s ", clientCN, metric.Name) 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) } else if metric.From != 0 { fromUnixTimeUTC := time.Unix(metric.From, 0) toUnixTimeUTC := time.Unix(metric.To, 0) fromInBytes, _ := fromUnixTimeUTC.UTC().MarshalText() toInBytes, _ := toUnixTimeUTC.UTC().MarshalText() metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, string(fromInBytes), string(toInBytes), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "content": metricsPack}) }