change metric format

This commit is contained in:
Joerg Lehmann 2021-03-17 18:09:49 +01:00
parent 7c9e198311
commit e4001f2d44
2 changed files with 49 additions and 12 deletions

View File

@ -33,6 +33,14 @@ type OneMetric struct {
// metrics handler
func string2float64(s string) float64 {
f, err := strconv.ParseFloat(s, 64)
if err == nil {
return f
}
return 0
}
func metricsHandler(response http.ResponseWriter, request *http.Request) {
name := getUserName(request)
if name != "" {
@ -133,7 +141,16 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
scanner := bufio.NewScanner(strings.NewReader(string(body)))
first := true
fmt.Fprintf(response, "[\n")
var lat float64
var lon float64
var min_lat float64 = 9999999999.9
var min_lon float64 = 9999999999.9
var max_lat float64 = 0
var max_lon float64 = 0
fmt.Fprintf(response, "{\n")
fmt.Fprintf(response, " \"data\": [\n")
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
fmt.Printf("Scanned Line: %v\n", s)
@ -144,13 +161,28 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
continue
}
a := t.Unix()
if s[8] == "0" {
lat = string2float64(s[8])
lon = string2float64(s[9])
if (lat == 0) || (lon == 0) {
fmt.Println("skip 0 value for lon/lat")
} else {
if lat < min_lat {
min_lat = lat
}
if lon < min_lon {
min_lon = lon
}
if lat > max_lat {
max_lat = lat
}
if lon > max_lon {
max_lon = lon
}
if !(first) {
fmt.Fprintf(response, ",")
fmt.Fprintf(response, " ,")
} else {
first = false
fmt.Fprintf(response, " ")
}
fmt.Fprintf(response, "[%d, %s, %s, %s, %d]\n", a, s[8], s[9], s[10], vbat2percent(s[10]))
@ -159,7 +191,12 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
}
}
fmt.Fprintf(response, "]\n")
fmt.Fprintf(response, " ],\n")
fmt.Fprintf(response, " \"min_lat\": %f,\n", min_lat)
fmt.Fprintf(response, " \"min_lon\": %f,\n", min_lon)
fmt.Fprintf(response, " \"max_lat\": %f,\n", max_lat)
fmt.Fprintf(response, " \"max_lon\": %f\n", max_lon)
fmt.Fprintf(response, "}\n")
} else {
fmt.Fprintf(response, "{ \"msg\": \"Only available for logged in users\" }")
@ -270,8 +307,8 @@ func getLastMetrics(deveui string) OneMetric {
res.Deveui = deveui
res.DeveuiJS = template.JS(deveui)
res.LatJS = template.JS(res.Lat)
res.LonJS = template.JS(res.Lon)
res.LatJS = template.JS(res.Lat)
res.LonJS = template.JS(res.Lon)
res.Alias = getDevAlias(deveui)
res.Readonly = false

View File

@ -75,14 +75,14 @@ function refreshDatapoints(deveui, start, stop) {
return;
}
datapoints = mydata;
$('#datetimeslider').attr('max', mydata.length - 1);
$('#datetimeslider').val(mydata.length - 1);
last_slider_position = mydata.length - 1;
var unix_timestamp = mydata[mydata.length - 1][0];
datapoints = mydata['data'];
$('#datetimeslider').attr('max', datapoints.length - 1);
$('#datetimeslider').val(datapoints.length - 1);
last_slider_position = datapoints.length - 1;
var unix_timestamp = datapoints[datapoints.length - 1][0];
var date = new Date(unix_timestamp * 1000);
var myhtml = moment(date).format('DD.MM.YYYY HH:mm') + ' / ' + vbat2icon(mydata[mydata.length - 1][4]) + ' (' + mydata[mydata.length - 1][3] + ' mV)';
var myhtml = moment(date).format('DD.MM.YYYY HH:mm') + ' / ' + vbat2icon(datapoints[datapoints.length - 1][4]) + ' (' + datapoints[datapoints.length - 1][3] + ' mV)';
console.log(myhtml);
$('#tooltiptext').html(myhtml);
});