Mercurial > gemma
diff pkg/controllers/stretches.go @ 3439:d7ddb21f7017 fairway-avail-csv
Prepared to write the fairway availibilty as CSV, too. WIP.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 24 May 2019 11:20:15 +0200 |
parents | 6994602d2935 |
children | ae6c09fbc590 |
line wrap: on
line diff
--- a/pkg/controllers/stretches.go Fri May 24 11:15:58 2019 +0200 +++ b/pkg/controllers/stretches.go Fri May 24 11:20:15 2019 +0200 @@ -21,12 +21,10 @@ "log" "net/http" "runtime" - "strconv" "strings" "sync" "time" - "gemma.intevation.de/gemma/pkg/common" "gemma.intevation.de/gemma/pkg/middleware" "github.com/gorilla/mux" ) @@ -163,51 +161,26 @@ name := vars["name"] mode := intervalMode(req.FormValue("mode")) - var from, to time.Time - var los int depthbreaks, widthbreaks := afdRefs, afdRefs - if f := req.FormValue("from"); f != "" { - var err error - if from, err = time.Parse(common.TimeFormat, f); err != nil { - http.Error( - rw, fmt.Sprintf("Invalid format for 'from': %v.", err), - http.StatusBadRequest) - return - } - } else { - from = time.Now().AddDate(-1, 0, 0) + from, ok := parseFormTime(rw, req, "from", time.Now().AddDate(-1, 0, 0)) + if !ok { + return } - from = from.UTC() - if t := req.FormValue("to"); t != "" { - var err error - if to, err = time.Parse(common.TimeFormat, t); err != nil { - http.Error( - rw, fmt.Sprintf("Invalid format for 'to': %v.", err), - http.StatusBadRequest) - return - } - } else { - to = from.AddDate(1, 0, 0) + to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0)) + if !ok { + return } - to = to.UTC() if to.Before(from) { to, from = from, to } - if l := req.FormValue("los"); l != "" { - var err error - if los, err = strconv.Atoi(l); err != nil { - http.Error( - rw, fmt.Sprintf("Invalid format for 'los': %v.", err), - http.StatusBadRequest) - return - } - } else { - los = 1 + los, ok := parseFormInt(rw, req, "los", 1) + if !ok { + return } conn := middleware.GetDBConn(req) @@ -220,6 +193,7 @@ http.StatusInternalServerError) return } + if len(bns) == 0 { http.Error(rw, "No bottlenecks found.", http.StatusNotFound) return @@ -441,49 +415,42 @@ return b.String() } -func stretchAvailabilty( - _ interface{}, - req *http.Request, - conn *sql.Conn, -) (jr JSONResult, err error) { +func stretchAvailabilty(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) stretch := vars["kind"] == "stretch" name := vars["name"] - var from, to time.Time - var los int - - depthbreaks, widthbreaks := afdRefs, afdRefs - - if f := req.FormValue("from"); f != "" { - if from, err = parseTime(f, "from"); err != nil { - return - } - } else { - from = time.Now().AddDate(-1, 0, 0).UTC() + if name == "" { + http.Error( + rw, + fmt.Sprintf("Missing %s name", vars["kind"]), + http.StatusBadRequest, + ) + return } - if t := req.FormValue("to"); t != "" { - if to, err = parseTime(t, "to"); err != nil { - return - } - } else { - to = from.AddDate(1, 0, 0).UTC() + from, ok := parseFormTime(rw, req, "form", time.Now().AddDate(-1, 0, 0)) + if !ok { + return + } + + to, ok := parseFormTime(rw, req, "to", from.AddDate(1, 0, 0)) + if !ok { + return } if to.Before(from) { to, from = from, to } - if l := req.FormValue("los"); l != "" { - if los, err = parseInt(l, "los"); err != nil { - return - } - } else { - los = 1 + los, ok := parseFormInt(rw, req, "los", 1) + if !ok { + return } + depthbreaks, widthbreaks := afdRefs, afdRefs + if b := req.FormValue("depthbreaks"); b != "" { depthbreaks = breaksToReferenceValue(b) } @@ -492,29 +459,35 @@ widthbreaks = breaksToReferenceValue(b) } + conn := middleware.GetDBConn(req) ctx := req.Context() - var bns stretchBottlenecks - if bns, err = loadStretchBottlenecks(ctx, conn, stretch, name); err != nil { + bns, err := loadStretchBottlenecks(ctx, conn, stretch, name) + if err != nil { + http.Error( + rw, fmt.Sprintf("DB error: %v.", err), + http.StatusInternalServerError) return } if len(bns) == 0 { - err = JSONError{ - Code: http.StatusNotFound, - Message: "No bottlenecks found.", - } + http.Error( + rw, + "No bottlenecks found.", + http.StatusNotFound, + ) return } useDepth, useWidth := bns.contains("depth"), bns.contains("width") if useDepth && useWidth && len(widthbreaks) != len(depthbreaks) { - err = JSONError{ - Code: http.StatusBadRequest, - Message: fmt.Sprintf("class breaks lengths differ: %d != %d", + http.Error( + rw, + fmt.Sprintf("class breaks lengths differ: %d != %d", len(widthbreaks), len(depthbreaks)), - } + http.StatusBadRequest, + ) return } @@ -541,10 +514,11 @@ } if len(loaded) == 0 { - err = JSONError{ - Code: http.StatusInternalServerError, - Message: fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)), - } + http.Error( + rw, + fmt.Sprintf("No bottleneck loaded: %v", joinErrors(errors)), + http.StatusInternalServerError, + ) return } @@ -620,6 +594,11 @@ lnwlPercents := durationsToPercentage(duration, ldc) afdPercents := durationsToPercentage(duration, afd) + _ = lnwlPercents + _ = afdPercents + + /* // TODO: Rewrite this + type ldcOutput struct { Value float64 `json:"value"` Below float64 `json:"below"` @@ -666,4 +645,5 @@ jr = JSONResult{Result: &out} return + */ }