Mercurial > gemma
comparison pkg/controllers/gauges.go @ 5490:5f47eeea988d logging
Use own logging package.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Mon, 20 Sep 2021 17:45:39 +0200 |
parents | 3b3cf2083730 |
children | 6270951dda28 |
comparison
equal
deleted
inserted
replaced
5488:a726a92ea5c9 | 5490:5f47eeea988d |
---|---|
16 import ( | 16 import ( |
17 "context" | 17 "context" |
18 "database/sql" | 18 "database/sql" |
19 "encoding/csv" | 19 "encoding/csv" |
20 "fmt" | 20 "fmt" |
21 "log" | |
22 "math" | 21 "math" |
23 "net/http" | 22 "net/http" |
24 "sort" | 23 "sort" |
25 "strconv" | 24 "strconv" |
26 "strings" | 25 "strings" |
29 "github.com/gorilla/mux" | 28 "github.com/gorilla/mux" |
30 "github.com/jackc/pgx/pgtype" | 29 "github.com/jackc/pgx/pgtype" |
31 "gonum.org/v1/gonum/stat" | 30 "gonum.org/v1/gonum/stat" |
32 | 31 |
33 "gemma.intevation.de/gemma/pkg/common" | 32 "gemma.intevation.de/gemma/pkg/common" |
33 "gemma.intevation.de/gemma/pkg/log" | |
34 "gemma.intevation.de/gemma/pkg/models" | 34 "gemma.intevation.de/gemma/pkg/models" |
35 | 35 |
36 mw "gemma.intevation.de/gemma/pkg/middleware" | 36 mw "gemma.intevation.de/gemma/pkg/middleware" |
37 ) | 37 ) |
38 | 38 |
191 ctx := req.Context() | 191 ctx := req.Context() |
192 | 192 |
193 begin := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC) | 193 begin := time.Date(year, time.January, 1, 0, 0, 0, 0, time.UTC) |
194 end := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC).Add(-time.Microsecond) | 194 end := time.Date(year+1, time.January, 1, 0, 0, 0, 0, time.UTC).Add(-time.Microsecond) |
195 | 195 |
196 log.Printf("info: begin %s\n", begin) | 196 log.Infof("begin %s\n", begin) |
197 log.Printf("info: end %s\n", end) | 197 log.Infof("end %s\n", end) |
198 | 198 |
199 rows, err := conn.QueryContext( | 199 rows, err := conn.QueryContext( |
200 ctx, | 200 ctx, |
201 selectYearWaterlevelsMeasuredSQL, | 201 selectYearWaterlevelsMeasuredSQL, |
202 isrs.CountryCode, | 202 isrs.CountryCode, |
233 | 233 |
234 for rows.Next() { | 234 for rows.Next() { |
235 var when time.Time | 235 var when time.Time |
236 var value float64 | 236 var value float64 |
237 if err := rows.Scan(&when, &value); err != nil { | 237 if err := rows.Scan(&when, &value); err != nil { |
238 log.Printf("error: %v", err) | 238 log.Errorf("%v", err) |
239 // Too late for an HTTP error code. | 239 // Too late for an HTTP error code. |
240 return | 240 return |
241 } | 241 } |
242 when = when.UTC() | 242 when = when.UTC() |
243 day, month := when.Day(), int(when.Month()) | 243 day, month := when.Day(), int(when.Month()) |
244 if day != lastDay || month != lastMonth { | 244 if day != lastDay || month != lastMonth { |
245 if err := write(); err != nil { | 245 if err := write(); err != nil { |
246 log.Printf("error: %v", err) | 246 log.Errorf("%v", err) |
247 // Too late for an HTTP error code. | 247 // Too late for an HTTP error code. |
248 return | 248 return |
249 } | 249 } |
250 lastDay, lastMonth = day, month | 250 lastDay, lastMonth = day, month |
251 } | 251 } |
252 values = append(values, value) | 252 values = append(values, value) |
253 } | 253 } |
254 | 254 |
255 if err := rows.Err(); err != nil { | 255 if err := rows.Err(); err != nil { |
256 log.Printf("error: %v", err) | 256 log.Errorf("%v", err) |
257 // Too late for an HTTP error code. | 257 // Too late for an HTTP error code. |
258 return | 258 return |
259 } | 259 } |
260 | 260 |
261 if err := write(); err != nil { | 261 if err := write(); err != nil { |
262 log.Printf("error: %v", err) | 262 log.Errorf("%v", err) |
263 // Too late for an HTTP error code. | 263 // Too late for an HTTP error code. |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 func longtermWaterlevels(rw http.ResponseWriter, req *http.Request) { | 267 func longtermWaterlevels(rw http.ResponseWriter, req *http.Request) { |
362 rw, fmt.Sprintf("error: %v", err), | 362 rw, fmt.Sprintf("error: %v", err), |
363 http.StatusInternalServerError) | 363 http.StatusInternalServerError) |
364 return | 364 return |
365 } | 365 } |
366 | 366 |
367 log.Printf("info: loading entries took %s\n", time.Since(start)) | 367 log.Infof("loading entries took %s\n", time.Since(start)) |
368 | 368 |
369 log.Printf("info: days found: %d\n", len(results)) | 369 log.Infof("days found: %d\n", len(results)) |
370 | 370 |
371 sort.Slice(results, func(i, j int) bool { | 371 sort.Slice(results, func(i, j int) bool { |
372 if d := results[i].month - results[j].month; d != 0 { | 372 if d := results[i].month - results[j].month; d != 0 { |
373 return d < 0 | 373 return d < 0 |
374 } | 374 } |
390 "", | 390 "", |
391 "", | 391 "", |
392 } | 392 } |
393 | 393 |
394 if err := out.Write(record); err != nil { | 394 if err := out.Write(record); err != nil { |
395 log.Printf("error: %v\n", err) | 395 log.Errorf("%v\n", err) |
396 // Too late for an HTTP error code. | 396 // Too late for an HTTP error code. |
397 return | 397 return |
398 } | 398 } |
399 | 399 |
400 record = []string{ | 400 record = []string{ |
406 "#q25", | 406 "#q25", |
407 "#q75", | 407 "#q75", |
408 } | 408 } |
409 | 409 |
410 if err := out.Write(record); err != nil { | 410 if err := out.Write(record); err != nil { |
411 log.Printf("error: %v\n", err) | 411 log.Errorf("%v\n", err) |
412 // Too late for an HTTP error code. | 412 // Too late for an HTTP error code. |
413 return | 413 return |
414 } | 414 } |
415 | 415 |
416 for i := range results { | 416 for i := range results { |
421 record[3] = float64format(r.mean) | 421 record[3] = float64format(r.mean) |
422 record[4] = float64format(r.median) | 422 record[4] = float64format(r.median) |
423 record[5] = float64format(r.q25) | 423 record[5] = float64format(r.q25) |
424 record[6] = float64format(r.q75) | 424 record[6] = float64format(r.q75) |
425 if err := out.Write(record); err != nil { | 425 if err := out.Write(record); err != nil { |
426 log.Printf("error: %v\n", err) | 426 log.Errorf("%v\n", err) |
427 // Too late for an HTTP error code. | 427 // Too late for an HTTP error code. |
428 return | 428 return |
429 } | 429 } |
430 } | 430 } |
431 | 431 |
432 out.Flush() | 432 out.Flush() |
433 if err := out.Error(); err != nil { | 433 if err := out.Error(); err != nil { |
434 log.Printf("error: %v", err) | 434 log.Errorf("%v", err) |
435 // Too late for an HTTP error code. | 435 // Too late for an HTTP error code. |
436 return | 436 return |
437 } | 437 } |
438 } | 438 } |
439 | 439 |
555 if hasCurrent && !math.IsNaN(current.observed) && len(current.predicted) > 0 { | 555 if hasCurrent && !math.IsNaN(current.observed) && len(current.predicted) > 0 { |
556 values = append(values, current) | 556 values = append(values, current) |
557 } | 557 } |
558 | 558 |
559 // for i := range values { | 559 // for i := range values { |
560 // log.Printf("%v %f %d\n", values[i].when, values[i].observed, len(values[i].predicted)) | 560 // log.Debugf("%v %f %d\n", values[i].when, values[i].observed, len(values[i].predicted)) |
561 // if len(values[i].predicted) > 0 { | 561 // if len(values[i].predicted) > 0 { |
562 // for j := range values[i].predicted { | 562 // for j := range values[i].predicted { |
563 // log.Printf("\t%v %f\n", values[i].predicted[j].When, values[i].predicted[j].Value) | 563 // log.Debugf("\t%v %f\n", values[i].predicted[j].When, values[i].predicted[j].Value) |
564 // } | 564 // } |
565 // } | 565 // } |
566 // } | 566 // } |
567 | 567 |
568 return values, nil | 568 return values, nil |
597 | 597 |
598 if values, err = loadNashSutcliffeData(ctx, mw.JSONConn(req), isrs, when); err != nil { | 598 if values, err = loadNashSutcliffeData(ctx, mw.JSONConn(req), isrs, when); err != nil { |
599 return | 599 return |
600 } | 600 } |
601 | 601 |
602 log.Printf("info: found %d value(s) for Nash Sutcliffe.\n", len(values)) | 602 log.Infof("found %d value(s) for Nash Sutcliffe.\n", len(values)) |
603 | 603 |
604 type coeff struct { | 604 type coeff struct { |
605 Value float64 `json:"value"` | 605 Value float64 `json:"value"` |
606 Samples int `json:"samples"` | 606 Samples int `json:"samples"` |
607 Hours int `json:"hours"` | 607 Hours int `json:"hours"` |
744 "#value_max", | 744 "#value_max", |
745 "#predicted", | 745 "#predicted", |
746 } | 746 } |
747 | 747 |
748 if err := out.Write(record); err != nil { | 748 if err := out.Write(record); err != nil { |
749 log.Printf("error: %v", err) | 749 log.Errorf("%v", err) |
750 // Too late for an HTTP error code. | 750 // Too late for an HTTP error code. |
751 return | 751 return |
752 } | 752 } |
753 | 753 |
754 for rows.Next() { | 754 for rows.Next() { |
764 &waterlevel, | 764 &waterlevel, |
765 &valueMin, | 765 &valueMin, |
766 &valueMax, | 766 &valueMax, |
767 &predicted, | 767 &predicted, |
768 ); err != nil { | 768 ); err != nil { |
769 log.Printf("error: %v\n", err) | 769 log.Errorf("%v\n", err) |
770 // Too late for an HTTP error code. | 770 // Too late for an HTTP error code. |
771 return | 771 return |
772 } | 772 } |
773 record[0] = measureDate.Format(common.TimeFormat) | 773 record[0] = measureDate.Format(common.TimeFormat) |
774 record[1] = float64format(waterlevel) | 774 record[1] = float64format(waterlevel) |
775 record[2] = nullFloat64format(valueMin) | 775 record[2] = nullFloat64format(valueMin) |
776 record[3] = nullFloat64format(valueMax) | 776 record[3] = nullFloat64format(valueMax) |
777 record[4] = boolFormat(predicted) | 777 record[4] = boolFormat(predicted) |
778 | 778 |
779 if err := out.Write(record); err != nil { | 779 if err := out.Write(record); err != nil { |
780 log.Printf("error: %v", err) | 780 log.Errorf("%v", err) |
781 // Too late for an HTTP error code. | 781 // Too late for an HTTP error code. |
782 return | 782 return |
783 } | 783 } |
784 } | 784 } |
785 | 785 |
786 if err := rows.Err(); err != nil { | 786 if err := rows.Err(); err != nil { |
787 log.Printf("error: %v", err) | 787 log.Errorf("%v", err) |
788 // Too late for an HTTP error code. | 788 // Too late for an HTTP error code. |
789 return | 789 return |
790 } | 790 } |
791 | 791 |
792 out.Flush() | 792 out.Flush() |
793 if err := out.Error(); err != nil { | 793 if err := out.Error(); err != nil { |
794 log.Printf("error: %v", err) | 794 log.Errorf("%v", err) |
795 // Too late for an HTTP error code. | 795 // Too late for an HTTP error code. |
796 return | 796 return |
797 } | 797 } |
798 } | 798 } |