changeset 2570:c4242b9d59fe

Morphological differences: Added endpoint stub POST /api/diff
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 11 Mar 2019 13:15:28 +0100
parents 2d18b8d5b9db
children eec11d3d74f9
files 3rdpartylibs.sh pkg/controllers/diff.go pkg/controllers/routes.go pkg/models/diff.go
diffstat 4 files changed, 124 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/3rdpartylibs.sh	Mon Mar 11 12:39:39 2019 +0100
+++ b/3rdpartylibs.sh	Mon Mar 11 13:15:28 2019 +0100
@@ -38,6 +38,9 @@
 go get -u -v github.com/tidwall/rtree
 # MIT
 
+golang.org/x/sync/semaphore
+# Go License (aka BSD-3-Clause?)
+
 ## list of additional licenses that get fetched and installed as dependencies
 # github.com/fsnotify/fsnotify/ BSD-3-Clause
 # github.com/hashicorp/hcl/ MPL-2.0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/controllers/diff.go	Mon Mar 11 13:15:28 2019 +0100
@@ -0,0 +1,95 @@
+// 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) 2018, 2019 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 (
+	"database/sql"
+	"fmt"
+	"log"
+	"net/http"
+	"time"
+
+	"golang.org/x/sync/semaphore"
+
+	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/models"
+	"gemma.intevation.de/gemma/pkg/octree"
+)
+
+// Only allow three diffence calculation at once.
+// TODO: Make this configurable?
+var diffCalculationSemaphore = semaphore.NewWeighted(int64(3))
+
+func diffCalculation(
+	input interface{},
+	req *http.Request,
+	conn *sql.Conn,
+) (jr JSONResult, err error) {
+
+	start := time.Now()
+
+	ctx := req.Context()
+
+	// DoS counter measure.
+	if err = diffCalculationSemaphore.Acquire(ctx, 1); err != nil {
+		return
+	}
+	defer diffCalculationSemaphore.Release(1)
+
+	dci := input.(models.DiffCalculationInput)
+
+	minuendTree, err := octree.FromCache(
+		ctx, conn,
+		dci.Bottleneck, dci.Minuend.Time)
+
+	log.Printf("info: loading minuend octree took %s\n", time.Since(start))
+	if err != nil {
+		return
+	}
+
+	if minuendTree == nil {
+		err = JSONError{
+			Code: http.StatusNotFound,
+			Message: fmt.Sprintf("Cannot find survey for %s/%s.",
+				dci.Bottleneck,
+				dci.Minuend.Format(common.DateFormat)),
+		}
+		return
+	}
+
+	start = time.Now()
+
+	subtrahendTree, err := octree.FromCache(
+		ctx, conn,
+		dci.Bottleneck, dci.Subtrahend.Time)
+
+	log.Printf("info: loading subtrahend octree took %s\n", time.Since(start))
+	if err != nil {
+		return
+	}
+
+	if subtrahendTree == nil {
+		err = JSONError{
+			Code: http.StatusNotFound,
+			Message: fmt.Sprintf("Cannot find survey for %s/%s.",
+				dci.Bottleneck,
+				dci.Subtrahend.Format(common.DateFormat)),
+		}
+		return
+	}
+
+	// TODO: Implement me!
+
+	return
+}
--- a/pkg/controllers/routes.go	Mon Mar 11 12:39:39 2019 +0100
+++ b/pkg/controllers/routes.go	Mon Mar 11 13:15:28 2019 +0100
@@ -172,6 +172,12 @@
 		Handle: listBottlenecks,
 	})).Methods(http.MethodGet)
 
+	// difference calculation
+	api.Handle("/diff", any(&JSONHandler{
+		Input:  func(*http.Request) interface{} { return new(models.DiffCalculationInput) },
+		Handle: diffCalculation,
+	})).Methods(http.MethodPost)
+
 	// Cross sections
 	api.Handle("/cross", any(&JSONHandler{
 		Input:  func(*http.Request) interface{} { return new(models.CrossSectionInput) },
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/models/diff.go	Mon Mar 11 13:15:28 2019 +0100
@@ -0,0 +1,20 @@
+// 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) 2018, 2019 by via donau
+//   – Österreichische Wasserstraßen-Gesellschaft mbH
+// Software engineering by Intevation GmbH
+//
+// Author(s):
+//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>
+
+package models
+
+type DiffCalculationInput struct {
+	Minuend    Date   `json:"minuend"`
+	Subtrahend Date   `json:"subtrahend"`
+	Bottleneck string `json:"bottleneck"`
+}