change metric format
This commit is contained in:
parent
7c9e198311
commit
e4001f2d44
45
metrics.go
45
metrics.go
|
|
@ -33,6 +33,14 @@ type OneMetric struct {
|
||||||
|
|
||||||
// metrics handler
|
// 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) {
|
func metricsHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
name := getUserName(request)
|
name := getUserName(request)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
|
|
@ -133,7 +141,16 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
|
|
||||||
scanner := bufio.NewScanner(strings.NewReader(string(body)))
|
scanner := bufio.NewScanner(strings.NewReader(string(body)))
|
||||||
first := true
|
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() {
|
for scanner.Scan() {
|
||||||
s := strings.Split(scanner.Text(), ",")
|
s := strings.Split(scanner.Text(), ",")
|
||||||
fmt.Printf("Scanned Line: %v\n", s)
|
fmt.Printf("Scanned Line: %v\n", s)
|
||||||
|
|
@ -144,13 +161,28 @@ func metricsHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
a := t.Unix()
|
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")
|
fmt.Println("skip 0 value for lon/lat")
|
||||||
} else {
|
} 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) {
|
if !(first) {
|
||||||
fmt.Fprintf(response, ",")
|
fmt.Fprintf(response, " ,")
|
||||||
} else {
|
} else {
|
||||||
first = false
|
first = false
|
||||||
|
fmt.Fprintf(response, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(response, "[%d, %s, %s, %s, %d]\n", a, s[8], s[9], s[10], vbat2percent(s[10]))
|
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 {
|
} else {
|
||||||
fmt.Fprintf(response, "{ \"msg\": \"Only available for logged in users\" }")
|
fmt.Fprintf(response, "{ \"msg\": \"Only available for logged in users\" }")
|
||||||
|
|
|
||||||
|
|
@ -75,14 +75,14 @@ function refreshDatapoints(deveui, start, stop) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
datapoints = mydata;
|
datapoints = mydata['data'];
|
||||||
$('#datetimeslider').attr('max', mydata.length - 1);
|
$('#datetimeslider').attr('max', datapoints.length - 1);
|
||||||
$('#datetimeslider').val(mydata.length - 1);
|
$('#datetimeslider').val(datapoints.length - 1);
|
||||||
last_slider_position = mydata.length - 1;
|
last_slider_position = datapoints.length - 1;
|
||||||
var unix_timestamp = mydata[mydata.length - 1][0];
|
var unix_timestamp = datapoints[datapoints.length - 1][0];
|
||||||
var date = new Date(unix_timestamp * 1000);
|
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);
|
console.log(myhtml);
|
||||||
$('#tooltiptext').html(myhtml);
|
$('#tooltiptext').html(myhtml);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue