changeset 2988:e1ccc8438529

DMV: separated fetching list of countries from database from requesting the RIS index. Also added some logging and better error handling.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 10 Apr 2019 11:00:23 +0200
parents 524f1382558a
children 87d34efafb3a
files pkg/imports/erdms.go
diffstat 1 files changed, 40 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/erdms.go	Wed Apr 10 10:02:07 2019 +0200
+++ b/pkg/imports/erdms.go	Wed Apr 10 11:00:23 2019 +0200
@@ -24,7 +24,36 @@
 	"gemma.intevation.de/gemma/pkg/soap/erdms"
 )
 
-const selectUserCountriesSQL = `SELECT DISTINCT country FROM users.list_users`
+const (
+	selectUserCountriesSQL = `
+SELECT DISTINCT country FROM users.list_users
+WHERE country <> '--'
+`
+)
+
+func userCountries(ctx context.Context, tx *sql.Tx) ([]string, error) {
+	rows, err := tx.QueryContext(ctx, selectUserCountriesSQL)
+	if err != nil {
+		return nil, err
+	}
+	defer rows.Close()
+
+	var countries []string
+
+	for rows.Next() {
+		var country string
+		if err = rows.Scan(&country); err != nil {
+			return nil, err
+		}
+		countries = append(countries, country)
+	}
+
+	if err := rows.Err(); err != nil {
+		return nil, err
+	}
+
+	return countries, nil
+}
 
 func getRisData(
 	tx *sql.Tx,
@@ -37,6 +66,11 @@
 	funcode string,
 ) ([]*erdms.GetRisDataXMLResponse, error) {
 
+	countries, err := userCountries(ctx, tx)
+	if err != nil {
+		return nil, err
+	}
+
 	var auth *soap.BasicAuth
 	if username != "" {
 		auth = &soap.BasicAuth{
@@ -47,21 +81,10 @@
 
 	client := erdms.NewRefService(URL, insecure, auth)
 
-	rows, err := tx.QueryContext(ctx, selectUserCountriesSQL)
-	if err != nil {
-		return nil, err
-	}
-	defer rows.Close()
+	var responseData []*erdms.GetRisDataXMLResponse
+	for _, country := range countries {
 
-	var country string
-	var countries []string
-	var responseData []*erdms.GetRisDataXMLResponse
-	for rows.Next() {
-		err = rows.Scan(&country)
-		if err != nil {
-			return nil, err
-		}
-		countries = append(countries, country)
+		feedback.Info("Request RIS index for country %s", country)
 
 		request := &erdms.GetRisDataXML{
 			GetRisDataXMLType: &erdms.GetRisDataXMLType{
@@ -76,7 +99,9 @@
 		}
 		responseData = append(responseData, data)
 	}
+
 	feedback.Info("Import data for countries: %s.",
 		strings.Join(countries, ", "))
+
 	return responseData, nil
 }