diff pkg/imports/agm.go @ 2173:1b1cb6cbfbf0

Approved gauge measurement imports: Suppress diff lines when old and new are identical.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 08 Feb 2019 17:59:24 +0100
parents b868cb653c4d
children 7c83b5277c1c
line wrap: on
line diff
--- a/pkg/imports/agm.go	Fri Feb 08 17:55:42 2019 +0100
+++ b/pkg/imports/agm.go	Fri Feb 08 17:59:24 2019 +0100
@@ -21,6 +21,7 @@
 	"encoding/json"
 	"fmt"
 	"io"
+	"math"
 	"os"
 	"path/filepath"
 	"strconv"
@@ -136,6 +137,30 @@
 	SourceOrganization string   `json:"source-organization"`
 }
 
+func (a *agmLine) hasDiff(b *agmLine) bool {
+	const eps = 0.00001
+	fdiff := func(x, y *float64) bool {
+		if x == nil && y == nil {
+			return false
+		}
+		if (x == nil && y != nil) || (x != nil && y == nil) {
+			return true
+		}
+		return math.Abs(*x-*y) > eps
+	}
+	return a.CountryCode != b.CountryCode ||
+		a.Sender != b.Sender ||
+		a.LanguageCode != b.LanguageCode ||
+		!a.DateIssue.Time.Equal(b.DateIssue.Time) ||
+		a.ReferenceCode != b.ReferenceCode ||
+		math.Abs(a.WaterLevel-b.WaterLevel) > eps ||
+		a.Predicted != b.Predicted ||
+		fdiff(a.ValueMin, b.ValueMin) ||
+		fdiff(a.ValueMax, b.ValueMax) ||
+		!a.DateInfo.Time.Equal(b.DateInfo.Time) ||
+		a.SourceOrganization != b.SourceOrganization
+}
+
 type agmSummaryEntry struct {
 	FKGaugeID   models.Isrs `json:"fk-gauge-id"`
 	MeasureDate timetz      `json:"measure-date"`
@@ -481,43 +506,46 @@
 			return nil, err
 		}
 
+		n := newAGMLine(
+			newCountryCode,
+			newSender,
+			newLanguageCode,
+			newDateIssue,
+			newReferenceCode,
+			newValue,
+			newPredicted,
+			newValueMin,
+			newValueMax,
+			newDateInfo,
+			newSourceOrganization,
+		)
+
 		ase := &agmSummaryEntry{
 			FKGaugeID:   *gid,
 			MeasureDate: timetz{md},
-			Versions: []*agmLine{
-				newAGMLine(
-					newCountryCode,
-					newSender,
-					newLanguageCode,
-					newDateIssue,
-					newReferenceCode,
-					newValue,
-					newPredicted,
-					newValueMin,
-					newValueMax,
-					newDateInfo,
-					newSourceOrganization,
-				),
-			},
 		}
 
-		if !newEntry {
-			ase.Versions = []*agmLine{
-				newAGMLine(
-					oldCountryCode,
-					oldSender,
-					oldLanguageCode,
-					oldDateIssue,
-					oldReferenceCode,
-					oldValue,
-					oldPredicted,
-					oldValueMin,
-					oldValueMax,
-					oldDateInfo,
-					oldSourceOrganization,
-				),
-				ase.Versions[0],
+		if newEntry {
+			ase.Versions = []*agmLine{n}
+		} else {
+			o := newAGMLine(
+				oldCountryCode,
+				oldSender,
+				oldLanguageCode,
+				oldDateIssue,
+				oldReferenceCode,
+				oldValue,
+				oldPredicted,
+				oldValueMin,
+				oldValueMax,
+				oldDateInfo,
+				oldSourceOrganization,
+			)
+			// Ignore if there is no diff.
+			if !n.hasDiff(o) {
+				continue
 			}
+			ase.Versions = []*agmLine{o, n}
 		}
 		entries = append(entries, ase)
 	}