changeset 2783:2806821cfd63

Added min and max values to CSV output of /api/data/waterlevels/{gauge} .
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 22 Mar 2019 21:55:30 +0100
parents 615b0a9b8098
children 387fe9f088a6 9a7d4caa1b77
files pkg/controllers/gauges.go
diffstat 1 files changed, 53 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/gauges.go	Fri Mar 22 16:23:40 2019 +0100
+++ b/pkg/controllers/gauges.go	Fri Mar 22 21:55:30 2019 +0100
@@ -58,6 +58,8 @@
 SELECT
   measure_date,
   water_level,
+  value_min,
+  value_max,
   predicted
 FROM waterway.gauge_measurements
 WHERE
@@ -83,6 +85,24 @@
 `
 )
 
+func float64format(v float64) string {
+	return strconv.FormatFloat(v, 'f', -1, 64)
+}
+
+func nullFloat64format(v sql.NullFloat64) string {
+	if v.Valid {
+		return float64format(v.Float64)
+	}
+	return ""
+}
+
+func boolFormat(b bool) string {
+	if b {
+		return "t"
+	}
+	return "f"
+}
+
 func averageWaterlevels(rw http.ResponseWriter, req *http.Request) {
 	gauge := mux.Vars(req)["gauge"]
 
@@ -172,28 +192,24 @@
 		return
 	}
 
-	format := func(v float64) string {
-		return strconv.FormatFloat(v, 'f', -1, 64)
-	}
-
 	write := func() error {
 		if len(values) > 0 {
 			sort.Float64s(values)
 			// date
 			record[0] = last.Format(common.DateFormat)
 			// min
-			record[1] = format(values[0])
+			record[1] = float64format(values[0])
 			// max
-			record[2] = format(values[len(values)-1])
+			record[2] = float64format(values[len(values)-1])
 			// mean
-			record[3] = format(stat.Mean(values, nil))
+			record[3] = float64format(stat.Mean(values, nil))
 			// median
-			record[4] = format(values[len(values)/2])
+			record[4] = float64format(values[len(values)/2])
 			// Q25
-			record[5] = format(
+			record[5] = float64format(
 				stat.Quantile(0.25, stat.Empirical, values, nil))
 			// Q75
-			record[6] = format(
+			record[6] = float64format(
 				stat.Quantile(0.75, stat.Empirical, values, nil))
 
 			err := out.Write(record)
@@ -398,26 +414,45 @@
 
 	out := csv.NewWriter(rw)
 
-	record := make([]string, 3)
+	record := []string{
+		"#date",
+		"#water_level",
+		"#value_min",
+		"#value_max",
+		"#predicted",
+	}
+
+	if err := out.Write(record); err != nil {
+		log.Printf("error: %v", err)
+		// Too late for an HTTP error code.
+		return
+	}
 
 	for rows.Next() {
 		var (
 			measureDate time.Time
 			waterlevel  float64
+			valueMin    sql.NullFloat64
+			valueMax    sql.NullFloat64
 			predicted   bool
 		)
-		if err := rows.Scan(&measureDate, &waterlevel, &predicted); err != nil {
+		if err := rows.Scan(
+			&measureDate,
+			&waterlevel,
+			&valueMin,
+			&valueMax,
+			&predicted,
+		); err != nil {
 			log.Printf("error: %v\n", err)
 			// Too late for an HTTP error code.
 			return
 		}
 		record[0] = measureDate.Format(models.ImportTimeFormat)
-		record[1] = strconv.FormatFloat(waterlevel, 'f', -1, 64)
-		if predicted {
-			record[2] = "t"
-		} else {
-			record[2] = "f"
-		}
+		record[1] = float64format(waterlevel)
+		record[2] = nullFloat64format(valueMin)
+		record[3] = nullFloat64format(valueMax)
+		record[4] = boolFormat(predicted)
+
 		if err := out.Write(record); err != nil {
 			log.Printf("error: %v", err)
 			// Too late for an HTTP error code.