# HG changeset patch # User Sascha L. Teichmann # Date 1556483208 -7200 # Node ID 4dcbf9e9013c34f29264505871c11b8172d7e2be # Parent 6b5132fd385e83a10c0c10cb3a4b0a35f7e157f4 Added stub for GET /api/data/fairway-depth/{gauge} diff -r 6b5132fd385e -r 4dcbf9e9013c pkg/controllers/bottlenecks.go --- a/pkg/controllers/bottlenecks.go Sun Apr 28 22:03:37 2019 +0200 +++ b/pkg/controllers/bottlenecks.go Sun Apr 28 22:26:48 2019 +0200 @@ -97,6 +97,13 @@ availMeasurements []availMeasurement ) +// afdRefs are the typical available fairway depth reference values. +var afdRefs = []referenceValue{ + {0, 200}, + {1, 230}, + {2, 250}, +} + func (measurement *availMeasurement) getDepth() float64 { return float64(measurement.depth) } @@ -242,6 +249,18 @@ return t.UTC(), nil } +func parseInt(s, what string) (int, error) { + i, err := strconv.Atoi(s) + if err != nil { + return 0, JSONError{ + Code: http.StatusBadRequest, + Message: fmt.Sprintf( + "Invalid value for field '%s': %v", what, err), + } + } + return i, nil +} + func loadDepthValues( ctx context.Context, conn *sql.Conn, @@ -370,11 +389,7 @@ var los int if l := req.FormValue("los"); l != "" { - if los, err = strconv.Atoi(l); err != nil { - err = JSONError{ - Code: http.StatusBadRequest, - Message: fmt.Sprintf("Invalid value for field 'los': %v", err), - } + if los, err = parseInt(l, "los"); err != nil { return } } else { @@ -415,12 +430,6 @@ (*availMeasurement).getValue, ) - afdRefs := []referenceValue{ - {0, 200}, - {1, 230}, - {2, 250}, - } - afd := ms.classifyAvailMeasurements( from, to, afdRefs, @@ -475,3 +484,76 @@ jr = JSONResult{Result: &out} return } + +func bottleneckAvailableFairwayDepth( + _ interface{}, + req *http.Request, + conn *sql.Conn, +) (jr JSONResult, err error) { + bn := mux.Vars(req)["objnam"] + + if bn == "" { + err = JSONError{ + Code: http.StatusBadRequest, + Message: "Missing objnam of bottleneck", + } + return + } + + var mode int + if m := req.FormValue("mode"); m != "" { + switch strings.ToLower(m) { + case "monthly": + mode = 0 + case "quarterly": + mode = 1 + case "yearly": + mode = 2 + default: + err = JSONError{ + Code: http.StatusBadRequest, + Message: "Unknown 'mode' value", + } + return + } + } + + var from, to time.Time + + 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 t := req.FormValue("to"); t != "" { + if to, err = parseTime(t, "to"); err != nil { + return + } + } else { + to = from.AddDate(1, 0, 0).UTC() + } + + if to.Before(from) { + to, from = from, to + } + + log.Printf("info: time interval: (%v - %v)\n", from, to) + + var los int + if l := req.FormValue("los"); l != "" { + if los, err = parseInt(l, "los"); err != nil { + return + } + } else { + los = 1 + } + + // TODO: Implement me! + + _, _ = los, mode + + return +} diff -r 6b5132fd385e -r 4dcbf9e9013c pkg/controllers/routes.go --- a/pkg/controllers/routes.go Sun Apr 28 22:03:37 2019 +0200 +++ b/pkg/controllers/routes.go Sun Apr 28 22:26:48 2019 +0200 @@ -307,6 +307,10 @@ Handle: bottleneckAvailabilty, })).Methods(http.MethodGet) + api.Handle("/data/bottleneck/fairway-depth/{objnam}", any(&JSONHandler{ + Handle: bottleneckAvailableFairwayDepth, + })).Methods(http.MethodGet) + api.Handle("/data/waterlevels/{gauge}", any( middleware.DBConn(http.HandlerFunc(waterlevels)))).Methods(http.MethodGet)