changeset 4053:d7e85b85918f historization_ng

Merged default
author Sascha Wilde <wilde@intevation.de>
date Wed, 24 Jul 2019 17:36:31 +0200
parents 8ab4c6dc3d6c (current diff) 4a7c2140e44b (diff)
children 033a8e3fec8e
files
diffstat 2 files changed, 66 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/imports/agm.go	Wed Jul 24 17:35:36 2019 +0200
+++ b/pkg/imports/agm.go	Wed Jul 24 17:36:31 2019 +0200
@@ -26,6 +26,7 @@
 	"math"
 	"os"
 	"path/filepath"
+	"sort"
 	"strconv"
 	"strings"
 	"time"
@@ -497,9 +498,10 @@
 	var removed int
 
 	// Issue deletes
-	for _, old := range oldGMLines {
+	for _, key := range sortByLocationCode(oldGMLines) {
+		old := oldGMLines[key]
 		removed += len(old)
-		for _, line := range old {
+		for _, line := range sortByMeasureDate(old) {
 			if _, err := txTrackStmt.ExecContext(
 				ctx, importID, "waterway.gauge_measurements",
 				line.id,
@@ -507,6 +509,11 @@
 			); err != nil {
 				return nil, err
 			}
+			entries = append(entries, &agmSummaryEntry{
+				FKGaugeID:   line.Location,
+				MeasureDate: line.MeasureDate,
+				Versions:    []*agmLine{line, nil},
+			})
 		}
 	}
 
@@ -528,6 +535,32 @@
 	return entries, nil
 }
 
+func sortByLocationCode(data map[models.Isrs]map[int64]*agmLine) []models.Isrs {
+	keys := make([]models.Isrs, len(data))
+	var i int
+	for key := range data {
+		keys[i] = key
+		i++
+	}
+	sort.Slice(keys, func(i, j int) bool {
+		return keys[i].Less(&keys[j])
+	})
+	return keys
+}
+
+func sortByMeasureDate(data map[int64]*agmLine) []*agmLine {
+	lines := make([]*agmLine, len(data))
+	var i int
+	for _, line := range data {
+		lines[i] = line
+		i++
+	}
+	sort.Slice(lines, func(i, j int) bool {
+		return lines[i].MeasureDate.Before(lines[j].MeasureDate.Time)
+	})
+	return lines
+}
+
 func getOldGMLines(
 	ctx context.Context,
 	stmt *sql.Stmt,
--- a/pkg/models/isrs.go	Wed Jul 24 17:35:36 2019 +0200
+++ b/pkg/models/isrs.go	Wed Jul 24 17:36:31 2019 +0200
@@ -43,6 +43,37 @@
 	return nil
 }
 
+func (isrs *Isrs) Less(other *Isrs) bool {
+	if isrs.Hectometre < other.Hectometre {
+		return true
+	}
+	if isrs.Hectometre > other.Hectometre {
+		return false
+	}
+	if isrs.CountryCode < other.CountryCode {
+		return true
+	}
+	if isrs.CountryCode > other.CountryCode {
+		return false
+	}
+	if isrs.LoCode < other.LoCode {
+		return true
+	}
+	if isrs.LoCode > other.LoCode {
+		return false
+	}
+	if isrs.FairwaySection < other.FairwaySection {
+		return true
+	}
+	if isrs.FairwaySection > other.FairwaySection {
+		return false
+	}
+	if isrs.Orc < other.Orc {
+		return true
+	}
+	return false
+}
+
 func (isrs *Isrs) MarshalJSON() ([]byte, error) {
 	if isrs == nil {
 		return nil, nil