/** * 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" "log" "net/http" "os" "strconv" "time" "yats-server/config" "yats-server/db" "yats-server/model" ) var ( //outfile, _ = os.Create(configuration.LogFile) LOG = log.New(os.Stdout, "[YATS] ", log.Ldate|log.Ltime) ) // @BasePath / // WriteMetric godoc // @Param X-SSL-Client-CN header string true "clientCN" // @Summary write metric to db // @Schemes // @Description store metric // @Tags Metrics // @Accept json // @Param metric2 body model.MetricModel true "Metric request body" // @Produce json // @Success 202 {string} WriteMetric // @Router /metric [post] func WriteMetric(cfg config.Configuration) gin.HandlerFunc { return func(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, cfg) 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"}) } } // SearchMetrics godoc // @Param X-SSL-Client-CN header string true "clientCN" // @Summary Get the metrics From ... and To when present // @Schemes // @Description search metrics in the specified timeframe // @Tags Metrics // @Accept json // @Param metric1 body model.MetricSearchRequest true "Metric query filters" // @Produce json // @Success 200 {string} WriteMetric // @Router /metric/search [post] func SearchMetrics(cfg config.Configuration) gin.HandlerFunc { return func(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, cfg) if metric.SourceApplication != "" { if !db.CanReadSourceMetric(clientCN, metric.SourceApplication, metric.Name) { c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "-2"}) return } clientCN = metric.SourceApplication } 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() fmt.Printf("PASSING HERE, from %s to %s \n", fromInBytes, toInBytes) LOG.Printf("PASSING HERE, from %s to %s\n", fromInBytes, toInBytes) metricsPack = db.MetricsBetween(db.Session, clientCN, metric.Name, string(fromInBytes), string(toInBytes), 100) } c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": metricsPack}) } } // SearchMetricsFrom SearchMetrics godoc // @Param X-SSL-Client-CN header string true "clientCN" // @Summary Get the specific metric {name} starting from the epoch {from} // @Schemes // @Description search metrics after the specified timestamp // @Tags Metrics // @Produce json // @Success 200 {string} []model.MetricModel // @Router /metric/{name}/{from} [get] // @Param name path string true "Metric Name :name" // @Param from path string true "Starting from epoch :from" func SearchMetricsFrom(cfg config.Configuration) gin.HandlerFunc { return func(c *gin.Context) { var metric model.MetricSearchRequest 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"}) } clientCN := GetClientCN(c, cfg) fmt.Printf("%s / %s ", clientCN, metric.Name) var metricsPack []model.MetricModel unixTimeUTC := time.Unix(fromParamInt64, 0) timeAsBytes, _ := unixTimeUTC.UTC().MarshalText() metricsPack = db.MetricsFrom(db.Session, clientCN, nameParam, string(timeAsBytes), 100) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK", "data": metricsPack}) } }