yats.git

ref: f79a9d09c451653ff27fb34afb5b0c71dc758a32

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
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
	}

	fmt.Printf("%s / %s / %s", metric.ID_client, metric.Name, metric.Value)
	db.SaveMetric(metric.ID_client, 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
	}

	fmt.Printf("%s / %s / %s", metric.ID_client, metric.Name, metric.Value)

	unixTimeUTC := time.Unix(metric.Mtime, 0)
	db.SaveMetricAt(metric.ID_client, unixTimeUTC, metric.Name, metric.Value)

	c.IndentedJSON(http.StatusAccepted, gin.H{"ret": "OK"})
}