diff pkg/controllers/diff.go @ 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
children 7686c7c23506
line wrap: on
line diff
--- /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
+}