changeset 5195:d6710d29516b new-fwa

Started to move code around.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 May 2020 15:46:51 +0200
parents 6727ede03009
children 5bc8daa986d9
files pkg/common/time.go pkg/controllers/bottlenecks.go pkg/controllers/fwa.go pkg/controllers/misc.go
diffstat 4 files changed, 109 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/common/time.go	Wed May 06 17:35:40 2020 +0200
+++ b/pkg/common/time.go	Thu May 07 15:46:51 2020 +0200
@@ -97,3 +97,24 @@
 		return time.Unix(int64(secs), int64(nsecs))
 	}
 }
+
+func MinTime(a, b time.Time) time.Time {
+	if a.Before(b) {
+		return a
+	}
+	return b
+}
+
+func MaxTime(a, b time.Time) time.Time {
+	if a.After(b) {
+		return a
+	}
+	return b
+}
+
+func OrderTime(a, b time.Time) (time.Time, time.Time) {
+	if a.Before(b) {
+		return a, b
+	}
+	return b, a
+}
--- a/pkg/controllers/bottlenecks.go	Wed May 06 17:35:40 2020 +0200
+++ b/pkg/controllers/bottlenecks.go	Thu May 07 15:46:51 2020 +0200
@@ -221,7 +221,7 @@
 			continue
 		}
 
-		lo, hi := maxTime(p1.when, from), minTime(p2.when, to)
+		lo, hi := common.MaxTime(p1.when, from), common.MinTime(p2.when, to)
 
 		m1, m2 := access(p1), access(p2)
 		if m1 == m2 { // The whole interval is in only one class.
@@ -244,13 +244,13 @@
 		}
 
 		for j := 0; j < len(values)-1; j++ {
-			start, end := orderTime(values[j], values[j+1])
+			start, end := common.OrderTime(values[j], values[j+1])
 
 			if start.After(hi) || end.Before(lo) {
 				continue
 			}
 
-			start, end = maxTime(start, lo), minTime(end, hi)
+			start, end = common.MaxTime(start, lo), common.MinTime(end, hi)
 			result[j] += end.Sub(start)
 		}
 	}
@@ -258,27 +258,6 @@
 	return optimisticPadClassification(from, to, result)
 }
 
-func orderTime(a, b time.Time) (time.Time, time.Time) {
-	if a.Before(b) {
-		return a, b
-	}
-	return b, a
-}
-
-func minTime(a, b time.Time) time.Time {
-	if a.Before(b) {
-		return a
-	}
-	return b
-}
-
-func maxTime(a, b time.Time) time.Time {
-	if a.After(b) {
-		return a
-	}
-	return b
-}
-
 func durationsToPercentage(duration time.Duration, classes []time.Duration) []float64 {
 	percents := make([]float64, len(classes))
 	total := 100 / duration.Seconds()
@@ -288,48 +267,6 @@
 	return percents
 }
 
-func parseFormTime(
-	rw http.ResponseWriter,
-	req *http.Request,
-	field string,
-	def time.Time,
-) (time.Time, bool) {
-	f := req.FormValue(field)
-	if f == "" {
-		return def.UTC(), true
-	}
-	v, err := common.ParseTime(f)
-	if err != nil {
-		http.Error(
-			rw, fmt.Sprintf("Invalid format for '%s'.", field),
-			http.StatusBadRequest,
-		)
-		return time.Time{}, false
-	}
-	return v.UTC(), true
-}
-
-func parseFormInt(
-	rw http.ResponseWriter,
-	req *http.Request,
-	field string,
-	def int,
-) (int, bool) {
-	f := req.FormValue(field)
-	if f == "" {
-		return def, true
-	}
-	v, err := strconv.Atoi(f)
-	if err != nil {
-		http.Error(
-			rw, fmt.Sprintf("Invalid format for '%s'.", field),
-			http.StatusBadRequest,
-		)
-		return 0, false
-	}
-	return v, true
-}
-
 func intervalMode(mode string) int {
 	switch strings.ToLower(mode) {
 	case "monthly":
--- a/pkg/controllers/fwa.go	Wed May 06 17:35:40 2020 +0200
+++ b/pkg/controllers/fwa.go	Thu May 07 15:46:51 2020 +0200
@@ -14,20 +14,39 @@
 package controllers
 
 import (
+	"log"
 	"net/http"
 
 	"github.com/gorilla/mux"
 )
 
-func fairwayAvailability(res http.ResponseWriter, req *http.Request) {
+func fairwayAvailability(rw http.ResponseWriter, req *http.Request) {
 
 	vars := mux.Vars(req)
 
 	switch vars["kind"] {
 	case "bottleneck":
+		fairwayAvailabilityBottleneck(rw, req)
 	case "stretch":
 	case "section":
+	default:
+		http.Error(rw, "Invalid kind type.", http.StatusBadRequest)
+		return
 	}
 
 	// TODO: Implement me!
 }
+
+func fairwayAvailabilityBottleneck(rw http.ResponseWriter, req *http.Request) {
+
+	vars := mux.Vars(req)
+
+	bottleneckID := vars["name"]
+	if bottleneckID == "" {
+		http.Error(rw, "missing bottleneck_id", http.StatusBadRequest)
+		return
+	}
+	log.Printf("info: fairway availability for bottleneck_id '%s'\n", bottleneckID)
+
+	// TODO: Implement me!
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/misc.go	Thu May 07 15:46:51 2020 +0200
@@ -0,0 +1,65 @@
+// This is Free Software under GNU Affero General Public License v >= 3.0
+// without warranty, see README.md and license for details.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+// License-Filename: LICENSES/AGPL-3.0.txt
+//
+// Copyright (C) 2019, 2020 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package controllers
+
+import (
+	"fmt"
+	"net/http"
+	"strconv"
+	"time"
+
+	"gemma.intevation.de/gemma/pkg/common"
+)
+
+func parseFormTime(
+	rw http.ResponseWriter,
+	req *http.Request,
+	field string,
+	def time.Time,
+) (time.Time, bool) {
+	f := req.FormValue(field)
+	if f == "" {
+		return def.UTC(), true
+	}
+	v, err := common.ParseTime(f)
+	if err != nil {
+		http.Error(
+			rw, fmt.Sprintf("Invalid format for '%s'.", field),
+			http.StatusBadRequest,
+		)
+		return time.Time{}, false
+	}
+	return v.UTC(), true
+}
+
+func parseFormInt(
+	rw http.ResponseWriter,
+	req *http.Request,
+	field string,
+	def int,
+) (int, bool) {
+	f := req.FormValue(field)
+	if f == "" {
+		return def, true
+	}
+	v, err := strconv.Atoi(f)
+	if err != nil {
+		http.Error(
+			rw, fmt.Sprintf("Invalid format for '%s'.", field),
+			http.StatusBadRequest,
+		)
+		return 0, false
+	}
+	return v, true
+}