changeset 2682:9a493d27bf3f import-overview-rework

Added /api/imports?count=true to only count the results.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 15 Mar 2019 12:29:12 +0100
parents 097b4b964ed9
children 4d460b2b439f
files pkg/controllers/importqueue.go
diffstat 1 files changed, 37 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Fri Mar 15 12:15:37 2019 +0100
+++ b/pkg/controllers/importqueue.go	Fri Mar 15 12:29:12 2019 +0100
@@ -39,6 +39,11 @@
   FROM import.import_logs
   WHERE kind = 'warn'::log_type
 )`
+	selectImportsCountSQL = warningSQLPrefix + `
+SELECT count(*)
+FROM import.imports
+WHERE
+`
 	selectImportsSQL = warningSQLPrefix + `
 SELECT
   imports.id AS id,
@@ -159,7 +164,15 @@
 
 	var noBefore, noAfter bool
 
-	l.stmt.WriteString(selectImportsSQL)
+	var counting bool
+
+	switch count := strings.ToLower(req.FormValue("count")); count {
+	case "1", "t", "true":
+		counting = true
+		l.stmt.WriteString(selectImportsCountSQL)
+	default:
+		l.stmt.WriteString(selectImportsSQL)
+	}
 	a.stmt.WriteString(selectAfterSQL)
 	b.stmt.WriteString(selectBeforeSQL)
 
@@ -228,9 +241,11 @@
 		a.stmt.WriteString(" TRUE ")
 	}
 
-	l.stmt.WriteString(" ORDER BY enqueued DESC ")
-	a.stmt.WriteString(" ORDER BY enqueued LIMIT 1")
-	b.stmt.WriteString(" ORDER BY enqueued LIMIT 1")
+	if !counting {
+		l.stmt.WriteString(" ORDER BY enqueued DESC ")
+		a.stmt.WriteString(" ORDER BY enqueued LIMIT 1")
+		b.stmt.WriteString(" ORDER BY enqueued LIMIT 1")
+	}
 
 	if noBefore {
 		b = nil
@@ -269,6 +284,24 @@
 
 	ctx := req.Context()
 
+	// Fast path for counting
+
+	switch count := strings.ToLower(req.FormValue("count")); count {
+	case "1", "t", "true":
+		var count int64
+		err = conn.QueryRowContext(ctx, list.stmt.String(), list.args...).Scan(&count)
+		switch {
+		case err == sql.ErrNoRows:
+			count, err = 0, nil
+		case err != nil:
+			return
+		}
+		jr = JSONResult{Result: count}
+		return
+	}
+
+	// Generate the list
+
 	var rows *sql.Rows
 	if rows, err = conn.QueryContext(ctx, list.stmt.String(), list.args...); err != nil {
 		return