diff --git a/main.go b/main.go index 8450820..0a6347b 100644 --- a/main.go +++ b/main.go @@ -84,6 +84,7 @@ func main() { http.HandleFunc("/set_password", setPasswordHandler) http.HandleFunc("/logout", logoutHandler) http.HandleFunc("/confirm", confirmHandler) + http.HandleFunc("/metrics", metricsHandler) logit("Starting Web Application...") http.ListenAndServe("127.0.0.1:4000", nil) diff --git a/metrics.go b/metrics.go new file mode 100644 index 0000000..fcc6e49 --- /dev/null +++ b/metrics.go @@ -0,0 +1,75 @@ +package main + +import ( + "bytes" + "fmt" + "log" + "net/http" + "time" + "io/ioutil" + "bufio" + "strings" +) + +// metrics handler + +func metricsHandler(response http.ResponseWriter, request *http.Request) { + name := getUserName(request) + if name != "" { + url := "http://localhost:9999/api/v2/query?org=beieliorg" + data := []byte(`from(bucket:"beielibucket") |> range(start:-24h) |> filter(fn: (r) => r._field == "v") |> filter(fn: (r) => r.devaddr == "084B3824")`) + + req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) + if err != nil { + log.Fatal("Error reading request. ", err) + } + + // Set headers + req.Header.Set("Authorization", "Token xXnq8ADcDygAyM_L0B_10c9yuoOv-cYcUfIXkJunRrIDMhB5ZNmza-Whr1ELcbKNzW8GzUMMYD85QohUHQdAmg==") + req.Header.Set("accept", "application/csv") + req.Header.Set("content-type", "application/vnd.flux") + + // Set client timeout + client := &http.Client{Timeout: time.Second * 10} + + // Send request + resp, err := client.Do(req) + if err != nil { + log.Fatal("Error reading response. ", err) + } + defer resp.Body.Close() + + fmt.Println("response Status:", resp.Status) + fmt.Println("response Headers:", resp.Header) + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Fatal("Error reading body. ", err) + } + + fmt.Fprintf(response,"[\n") + scanner := bufio.NewScanner(strings.NewReader(string(body))) + first := true + for scanner.Scan() { + s := strings.Split(scanner.Text(),",") + if ((len(s) >= 7) && !(strings.HasPrefix(s[5],"_"))) { + t,err := time.Parse(time.RFC3339,s[5]) + if err != nil { + continue + } + a := t.Unix() + b := s[6] + if !(first) { + fmt.Fprintf(response,",") + } else { + first = false + } + fmt.Fprintf(response,"[%d000,%s]\n",a,b) + } + } + fmt.Fprintf(response,"]\n") + + } else { + fmt.Fprintf(response, "{ \"msg\": \"Only available for logged in users\" }") + } +} diff --git a/snippets/scales.html b/snippets/scales.html index cbf766d..ef3b1a8 100644 --- a/snippets/scales.html +++ b/snippets/scales.html @@ -1,6 +1,95 @@ {{define "body_content"}} {{ if ne .UserName "" }} -
Datenauswertung
+ + + +Modal title
+ +