diff pkg/imports/sr.go @ 5560:f2204f91d286

Join the log lines of imports to the log exports to recover data from them. Used in SR export to extract information that where in the meta json but now are only found in the log.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Feb 2022 18:34:40 +0100
parents 728b58946c34
children b91716d2acc6
line wrap: on
line diff
--- a/pkg/imports/sr.go	Thu Dec 02 12:37:33 2021 +0100
+++ b/pkg/imports/sr.go	Wed Feb 09 18:34:40 2022 +0100
@@ -28,13 +28,16 @@
 	"os"
 	"path"
 	"path/filepath"
+	"regexp"
 	"strconv"
 	"strings"
+	"sync"
 	"time"
 
 	shp "github.com/jonas-p/go-shp"
 
 	"gemma.intevation.de/gemma/pkg/common"
+	"gemma.intevation.de/gemma/pkg/log"
 	"gemma.intevation.de/gemma/pkg/mesh"
 	"gemma.intevation.de/gemma/pkg/models"
 	"gemma.intevation.de/gemma/pkg/wkb"
@@ -239,23 +242,81 @@
 `
 )
 
+var (
+	bottleneckRe,
+	surveyTypeRe,
+	dateRe,
+	negateZRe *regexp.Regexp
+	recoverRegsOnce sync.Once
+)
+
+func compileRecoverRegs() {
+	bottleneckRe = regexp.MustCompile(`Bottleneck:\s*(.+)\s*$`)
+	surveyTypeRe = regexp.MustCompile(`Processing as\s+([^\s]+)\s+beam scan.`)
+	dateRe = regexp.MustCompile(`Survey date:\s*(\d{4}-\d{2}-\d{2}).`)
+	negateZRe = regexp.MustCompile(`Z values will be negated.`)
+}
+
 // Description gives a short info about relevant facts of this import.
-func (sr *SoundingResult) Description() (string, error) {
+func (sr *SoundingResult) Description(msgs []string) (string, error) {
+
+	recoverRegsOnce.Do(compileRecoverRegs)
+
+	//log.Debugln(strings.Join(msgs, "\n") + "\n\n")
+
+	var (
+		bottleneck, st, date string
+		negZ                 *bool
+	)
+
+	for _, msg := range msgs {
+		if m := bottleneckRe.FindStringSubmatch(msg); m != nil {
+			bottleneck = m[1]
+			continue
+		}
+		if m := surveyTypeRe.FindStringSubmatch(msg); m != nil {
+			st = m[1]
+			continue
+		}
+		if m := dateRe.FindStringSubmatch(msg); m != nil {
+			date = m[1]
+			continue
+		}
+		if negateZRe.MatchString(msg) {
+			t := true
+			negZ = &t
+		}
+	}
 
 	var descs []string
 
 	if sr.Bottleneck != nil {
 		descs = append(descs, *sr.Bottleneck)
+	} else if bottleneck != "" {
+		log.Debugf("bottleneck recovered: %s\n", bottleneck)
+		descs = append(descs, bottleneck)
 	}
+
 	if sr.Date != nil {
 		descs = append(descs, (*sr).Date.Format(common.DateFormat))
+	} else if date != "" {
+		log.Debugf("date recovered: %s\n", date)
+		descs = append(descs, date)
 	}
+
 	if sr.NegateZ != nil && *sr.NegateZ {
+	} else if negZ != nil && *negZ {
+		log.Debugln("negateZ recovered")
 		descs = append(descs, "negateZ")
 	}
+
 	if sr.SurveyType != nil {
 		descs = append(descs, string(*sr.SurveyType))
+	} else if st != "" {
+		log.Debugf("survey type recovered: %s\n", st)
+		descs = append(descs, st)
 	}
+
 	return strings.Join(descs, "|"), nil
 }