ref: 512911d5667044e9aced988aff8194700262a724
server/rest/rest-metric.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/** * 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 ( "fmt" "net/http" "time" "yats-server/db" "yats-server/model" "github.com/gin-gonic/gin" ) func WriteMetricNow(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) db.SaveMetric(clientCN, metric.Name, metric.Value) c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "0"}) } func WriteMetricAt(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) 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}) } |