# HG changeset patch # User Sascha L. Teichmann # Date 1549645164 -3600 # Node ID 1b1cb6cbfbf09a77bae79784e2591b521c7b6089 # Parent 7e2c77ccc02f20367c466664c11c9fa03f471fbd Approved gauge measurement imports: Suppress diff lines when old and new are identical. diff -r 7e2c77ccc02f -r 1b1cb6cbfbf0 pkg/imports/agm.go --- 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) }