view pkg/controllers/search.go @ 3666:db87f34805fb

Align bottleneck validity at gauges Ensuring the validity of a bottleneck version is always contained by the validity of the referenced gauge version allows to reliably determine matching reference values of the gauge at a point in time. Since this implies that a bottleneck version might be cut into more than one time ranges, the concept of having only one non-erased version is no longer applicable and replaced by using the 'current' version of a bottleneck. Fairway availability data are always kept with the 'current' bottleneck version to have them at hand alltogether for analyses over longer time ranges.
author Tom Gottfried <tom@intevation.de>
date Sat, 15 Jun 2019 14:36:50 +0200
parents 02951a62e8c6
children 1504856c9378
line wrap: on
line source

// 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 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha Wilde <sascha.wilde@intevation.de>
//  * Sascha Teichmann <sascha.teichmann@intevation.de>

package controllers

import (
	"database/sql"
	"net/http"
	"regexp"
	"strconv"
	"strings"

	"gemma.intevation.de/gemma/pkg/models"
)

const (
	searchHectometreSQL = `SELECT COALESCE(json_agg(r),'[]')
FROM (SELECT (location_code).hectometre || ' rhm' AS name,
			replace(concat(location_code), ',','') AS locationcode,
             ST_AsGeoJSON(geom)::json AS geom, 'rhm' AS type
      FROM waterway.distance_marks_virtual
      WHERE (location_code).hectometre = $1) r`

	searchMostSQL = `SELECT search_most($1)::text`

	listBottlenecksSQL = `
SELECT COALESCE(json_agg(r),'[]')
FROM (
  SELECT
    objnam AS name,
    ST_AsGeoJSON(ST_Centroid(area))::json AS geom,
    'bottleneck' AS type
  FROM waterway.bottlenecks
  WHERE validity @> current_timestamp
ORDER BY objnam) r
`
)

var rkmRegex = regexp.MustCompile(
	"^[[:space:]]*([0-9]+)([,.]([0-9]))?[[:space:]]*$",
)

func searchFeature(
	input interface{},
	req *http.Request,
	db *sql.Conn,
) (jr JSONResult, err error) {

	s := input.(*models.SearchRequest)

	if len(s.SearchString) == 0 {
		err = JSONError{http.StatusBadRequest,
			"error: empty search string"}
		return
	}

	var result string

	m := rkmRegex.FindStringSubmatch(s.SearchString)
	if len(m) != 0 {
		// Handle search for river kilometre:
		var hectometre int
		if hectometre, err = strconv.Atoi(m[1]); err != nil {
			return
		}

		hectometre *= 10
		if m[3] != "" {
			var h int
			if h, err = strconv.Atoi(m[3]); err != nil {
				return
			}
			hectometre += h
		}

		err = db.QueryRowContext(
			req.Context(),
			searchHectometreSQL,
			hectometre,
		).Scan(&result)
	} else {
		// Hande search for bottlencks:
		err = db.QueryRowContext(
			req.Context(),
			searchMostSQL,
			s.SearchString,
		).Scan(&result)
	}

	if err != nil {
		return
	}

	jr.Result = strings.NewReader(result)
	return
}

func listBottlenecks(
	_ interface{},
	req *http.Request,
	conn *sql.Conn,
) (jr JSONResult, err error) {

	var result string
	err = conn.QueryRowContext(
		req.Context(), listBottlenecksSQL).Scan(&result)

	switch {
	case err == sql.ErrNoRows:
		err = JSONError{
			Code:    http.StatusNotFound,
			Message: "Cannot find any bottleneck.",
		}
		return
	case err != nil:
		return
	}

	jr = JSONResult{Result: strings.NewReader(result)}
	return
}