Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Commit

Permalink
Fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jan 31, 2020
1 parent 56ecb7b commit 0f7da0e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 5 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func allowed(f http.HandlerFunc, methods ...string) http.HandlerFunc {
return
}
}

http.Error(w, "Bad method; supported OPTIONS, POST", http.StatusBadRequest)
}
}
Expand All @@ -36,13 +37,16 @@ func logger(f http.HandlerFunc, debug bool) http.HandlerFunc {
start := time.Now()

var body []byte

if debug {
// get request body
var err error
body, err = ioutil.ReadAll(r.Body)

if err != nil {
log.Print(err)
}

r.Body = ioutil.NopCloser(bytes.NewBuffer(body))

// get response body
Expand All @@ -52,7 +56,7 @@ func logger(f http.HandlerFunc, debug bool) http.HandlerFunc {
f(w, r)

duration := time.Since(start)
log.Printf("%v %v (%dms)", r.Method, r.URL.Path, duration.Nanoseconds()/1e6)
log.Printf("%v %v (%dms)", r.Method, r.URL.Path, duration.Milliseconds())

if debug {
log.Println("Request:\n" + string(body))
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
var (
version = "development"
commit = "unknown commit"
timeout = 30 * time.Second
)

var apiURL = flag.String("api", "https://demo.volkszaehler.org/middleware.php", "volkszaehler api url")
var apiTimeout = flag.Duration("timeout", 30*time.Second, "volkszaehler api request timeout")
var apiTimeout = flag.Duration("timeout", timeout, "volkszaehler api request timeout")
var url = flag.String("url", "0.0.0.0:8000", "listening address")
var verbose = flag.Bool("verbose", false, "verbose logging")
var help = flag.Bool("help", false, "help")
Expand Down
5 changes: 4 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ func (server *Server) queryHandler(w http.ResponseWriter, r *http.Request) {
}

func roundTimestampMS(ts int64, group string) int64 {
t := time.Unix(ts/1000, 0)
const millisPerSecond = 1000
t := time.Unix(ts/millisPerSecond, 0)

switch group {
case "hour":
Expand All @@ -204,6 +205,7 @@ func (server *Server) executeQuery(qr grafana.QueryRequest) []grafana.QueryRespo

go func(idx int, target grafana.Target) {
var qres grafana.QueryResponse

context := strings.ToLower(target.Data.Context)
if context == "prognosis" {
qres = server.queryPrognosis(target)
Expand All @@ -223,6 +225,7 @@ func (server *Server) executeQuery(qr grafana.QueryRequest) []grafana.QueryRespo
}

res[idx] = qres

wg.Done()
}(idx, target)
}
Expand Down
7 changes: 3 additions & 4 deletions volkszaehler/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (api *client) Get(endpoint string) (io.ReadCloser, error) {
}

duration := time.Since(start)
log.Printf("GET %s (%dms)", url, duration.Nanoseconds()/1e6)
log.Printf("GET %s (%dms)", url, duration.Milliseconds())

if api.debug {
if err := api.debugResponseBody(resp); err != nil {
Expand Down Expand Up @@ -165,9 +165,8 @@ func (api *client) QueryEntity(entity string) (Entity, error) {
func (api *client) QueryData(uuid string, from time.Time, to time.Time,
group string, options string, tuples int,
) ([]Tuple, error) {
f := from.Unix()
t := to.Unix()
url := fmt.Sprintf("/data/%s.json?from=%d&to=%d", uuid, f*1000, t*1000)
const n2m = int64(time.Millisecond) // nano to milli seconds
url := fmt.Sprintf("/data/%s.json?from=%d&to=%d", uuid, from.UnixNano()/n2m, to.UnixNano()/n2m)

if tuples > 0 {
url += fmt.Sprintf("&tuples=%d", tuples)
Expand Down

0 comments on commit 0f7da0e

Please sign in to comment.