changeset 612:d127856e689d

Sounding results: Sort the data sets by name and date to make the output deterministic.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 10 Sep 2018 17:48:56 +0200
parents effd22c0ae5a
children 99cd44f19e86
files cmd/soundingresults/main.go
diffstat 1 files changed, 32 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/cmd/soundingresults/main.go	Mon Sep 10 17:32:51 2018 +0200
+++ b/cmd/soundingresults/main.go	Mon Sep 10 17:48:56 2018 +0200
@@ -11,6 +11,7 @@
 	"os"
 	"path/filepath"
 	"runtime"
+	"sort"
 	"strings"
 	"sync"
 	"time"
@@ -194,29 +195,40 @@
 	flush := func() {}
 
 	if *dump {
-		out := bufio.NewWriter(os.Stdout)
-		fmt.Fprintln(out, "BEGIN;")
-		flush = func() {
-			fmt.Fprintln(out, "COMMIT;")
-			out.Flush()
-		}
+		var results []result
 		handler = func(r result) {
 			if r.m.depthReference != "ADR" {
 				return
 			}
-			fmt.Fprintln(out, "INSERT INTO waterway.sounding_results (")
-			fmt.Fprintln(out, "  bottleneck_id,")
-			fmt.Fprintln(out, "  date_info,")
-			fmt.Fprintln(out, "  depth_reference,")
-			fmt.Fprintln(out, "  point_cloud")
-			fmt.Fprintln(out, ") VALUES (")
-			fmt.Fprintf(out, "  %s,\n", quote(r.m.name))
-			fmt.Fprintf(out, "  '%s'::date,\n", r.m.date.Format("2006-01-02"))
-			fmt.Fprintf(out, "  %s,\n", quote(r.m.depthReference))
-			fmt.Fprintf(out, "  '")
-			io.Copy(hex.NewEncoder(out), strings.NewReader(r.wkb))
-			fmt.Fprintln(out, "'")
-			fmt.Fprintln(out, ");")
+			results = append(results, r)
+		}
+		flush = func() {
+			sort.Slice(results, func(i, j int) bool {
+				if a, b := results[i].m.name, results[j].m.name; a != b {
+					return a < b
+				}
+				return results[i].m.date.Before(results[j].m.date)
+			})
+			out := bufio.NewWriter(os.Stdout)
+			fmt.Fprintln(out, "BEGIN;")
+			for i := range results {
+				r := &results[i]
+				fmt.Fprintln(out, "INSERT INTO waterway.sounding_results (")
+				fmt.Fprintln(out, "  bottleneck_id,")
+				fmt.Fprintln(out, "  date_info,")
+				fmt.Fprintln(out, "  depth_reference,")
+				fmt.Fprintln(out, "  point_cloud")
+				fmt.Fprintln(out, ") VALUES (")
+				fmt.Fprintf(out, "  %s,\n", quote(r.m.name))
+				fmt.Fprintf(out, "  '%s'::date,\n", r.m.date.Format("2006-01-02"))
+				fmt.Fprintf(out, "  %s,\n", quote(r.m.depthReference))
+				fmt.Fprintf(out, "  '")
+				io.Copy(hex.NewEncoder(out), strings.NewReader(r.wkb))
+				fmt.Fprintln(out, "'")
+				fmt.Fprintln(out, ");")
+			}
+			fmt.Fprintln(out, "COMMIT;")
+			out.Flush()
 		}
 	} else {
 		// TODO: Implement database stuff.
@@ -225,12 +237,10 @@
 	fin := make(chan struct{})
 
 	go func() {
-		defer flush()
-		defer close(fin)
+		defer func() { flush(); close(fin) }()
 		for {
 			select {
 			case <-done:
-				log.Println("xxx")
 				return
 			case r := <-results:
 				handler(r)