Mercurial > gemma
view pkg/controllers/search.go @ 3302:ec6163c6687d
'Historicise' gauges on import
Gauge data sets will be updated or a new version will be inserted
depending on temporal validity and a timestamp marking the last
update in the RIS-Index of a data set. The trigger on date_info is
removed because the value is actually an attribut coming from the
RIS-Index.
Gauge measurements and predictions are associated to the version with
matching temporal validity. Bottlenecks are always associated to the
actual version of the gauge, although this might change as soon as
bottlenecks are 'historicised', too.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Thu, 16 May 2019 18:41:43 +0200 |
parents | 5bb1e3c1f364 |
children | 571801483a70 |
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, 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 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 }