changeset 1315:24e4c60c2606

Simplified and fixed filtering of import queue listing.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 23 Nov 2018 19:41:31 +0100
parents 97d9e689520b
children 990e2b83b0e6
files pkg/controllers/importqueue.go pkg/controllers/routes.go pkg/misc/set.go
diffstat 3 files changed, 57 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Fri Nov 23 15:19:24 2018 +0100
+++ b/pkg/controllers/importqueue.go	Fri Nov 23 19:41:31 2018 +0100
@@ -14,7 +14,6 @@
 package controllers
 
 import (
-	"context"
 	"database/sql"
 	"fmt"
 	"log"
@@ -91,8 +90,7 @@
 
 func queryImportListStmt(
 	conn *sql.Conn,
-	ctx context.Context,
-	vars map[string]string,
+	req *http.Request,
 ) (*sql.Rows, error) {
 
 	var (
@@ -102,11 +100,16 @@
 		kinds  *pgtype.TextArray
 	)
 
-	if st, found := vars["states"]; found {
+	arg := func(format string, v interface{}) {
+		fmt.Fprintf(&stmt, format, len(args)+1)
+		args = append(args, v)
+	}
+
+	if st := req.FormValue("states"); st != "" {
 		states = toTextArray(st, imports.ImportStateNames)
 	}
 
-	if ks, found := vars["kinds"]; found {
+	if ks := req.FormValue("kinds"); ks != "" {
 		kinds = toTextArray(ks, imports.ImportKindNames)
 	}
 
@@ -116,8 +119,7 @@
 	}
 
 	if states != nil {
-		fmt.Fprintf(&stmt, " states = ANY($%d) ", len(args)+1)
-		args = append(args, states)
+		arg(" state = ANY($%d) ", states)
 	}
 
 	if states != nil && kinds != nil {
@@ -125,25 +127,28 @@
 	}
 
 	if kinds != nil {
-		fmt.Fprintf(&stmt, " kind = ANY($%d) ", len(args)+1)
-		args = append(args, kinds)
+		arg(" kind = ANY($%d) ", kinds)
 	}
 
 	stmt.WriteString(" ORDER BY enqueued DESC ")
 
-	if lim, found := vars["limit"]; found {
-		fmt.Fprintf(&stmt, " LIMIT $%d ", len(args)+1)
-		limit, _ := strconv.ParseInt(lim, 10, 64)
-		args = append(args, limit)
+	if lim := req.FormValue("limit"); lim != "" {
+		limit, err := strconv.ParseInt(lim, 10, 64)
+		if err != nil {
+			return nil, err
+		}
+		arg(" LIMIT $%d ", limit)
 	}
 
-	if ofs, found := vars["offset"]; found {
-		fmt.Fprintf(&stmt, " OFFSET $%d ", len(args)+1)
-		offset, _ := strconv.ParseInt(ofs, 10, 64)
-		args = append(args, offset)
+	if ofs := req.FormValue("offset"); ofs != "" {
+		offset, err := strconv.ParseInt(ofs, 10, 64)
+		if err != nil {
+			return nil, err
+		}
+		arg(" OFFSET $%d ", offset)
 	}
 
-	return conn.QueryContext(ctx, stmt.String(), args...)
+	return conn.QueryContext(req.Context(), stmt.String(), args...)
 }
 
 func listImports(
@@ -153,7 +158,7 @@
 ) (jr JSONResult, err error) {
 
 	var rows *sql.Rows
-	rows, err = queryImportListStmt(conn, req.Context(), mux.Vars(req))
+	rows, err = queryImportListStmt(conn, req)
 	if err != nil {
 		return
 	}
--- a/pkg/controllers/routes.go	Fri Nov 23 15:19:24 2018 +0100
+++ b/pkg/controllers/routes.go	Fri Nov 23 19:41:31 2018 +0100
@@ -176,14 +176,7 @@
 	})
 
 	api.Handle("/imports", lsImports).
-		Methods(http.MethodGet).
-		Queries(
-			"offset", "{offset:[0-9]+}",
-			"limit", "{limit:[0-9]+}",
-			"states", "",
-			"kinds", "")
-
-	api.Handle("/imports", lsImports).Methods(http.MethodGet)
+		Methods(http.MethodGet)
 
 	api.Handle("/imports/{id:[0-9]+}", waterwayAdmin(&JSONHandler{
 		Handle: importLogs,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pkg/misc/set.go	Fri Nov 23 19:41:31 2018 +0100
@@ -0,0 +1,32 @@
+// 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 L. Teichmann <sascha.teichmann@intevation.de>
+
+package misc
+
+import "math"
+
+func PowerSet(n int, fn func([]int)) {
+	powerSetSize := int(math.Pow(2, float64(n)))
+
+	subSet := make([]int, n)
+	for index := 0; index < powerSetSize; index++ {
+		subSet = subSet[:0]
+
+		for j := 0; j < n; j++ {
+			if index&(1<<uint(j)) > 0 {
+				subSet = append(subSet, j)
+			}
+		}
+		fn(subSet)
+	}
+}