comparison pkg/controllers/fwa.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 d71ebe576c76
children
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 "net/http" 21 "net/http"
23 "sort" 22 "sort"
24 "strconv" 23 "strconv"
25 "strings" 24 "strings"
26 "time" 25 "time"
27 26
28 "github.com/gorilla/mux" 27 "github.com/gorilla/mux"
29 28
30 "gemma.intevation.de/gemma/pkg/common" 29 "gemma.intevation.de/gemma/pkg/common"
30 "gemma.intevation.de/gemma/pkg/log"
31 "gemma.intevation.de/gemma/pkg/middleware" 31 "gemma.intevation.de/gemma/pkg/middleware"
32 ) 32 )
33 33
34 const ( 34 const (
35 selectBottlenecksLimitingSQL = ` 35 selectBottlenecksLimitingSQL = `
217 http.Error(rw, "Invalid kind type.", http.StatusBadRequest) 217 http.Error(rw, "Invalid kind type.", http.StatusBadRequest)
218 return 218 return
219 } 219 }
220 220
221 if err != nil { 221 if err != nil {
222 log.Printf("error: %v\n", err) 222 log.Errorf("%v\n", err)
223 http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest) 223 http.Error(rw, "cannot extract bottlenecks", http.StatusBadRequest)
224 return 224 return
225 } 225 }
226 226
227 // If there are no bottlenecks there is nothing to do. 227 // If there are no bottlenecks there is nothing to do.
231 } 231 }
232 232
233 // load validities and limiting factors 233 // load validities and limiting factors
234 for i := range bns { 234 for i := range bns {
235 if err := bns[i].loadLimitingValidities(ctx, conn, from, to); err != nil { 235 if err := bns[i].loadLimitingValidities(ctx, conn, from, to); err != nil {
236 log.Printf("error: %v\n", err) 236 log.Errorf("%v\n", err)
237 http.Error(rw, "cannot load validities", http.StatusInternalServerError) 237 http.Error(rw, "cannot load validities", http.StatusInternalServerError)
238 return 238 return
239 } 239 }
240 // load LCDs 240 // load LCDs
241 if err := bns[i].loadLDCs(ctx, conn, from, to); err != nil { 241 if err := bns[i].loadLDCs(ctx, conn, from, to); err != nil {
242 log.Printf("error: %v\n", err) 242 log.Errorf("%v\n", err)
243 http.Error(rw, "cannot load LDCs", http.StatusInternalServerError) 243 http.Error(rw, "cannot load LDCs", http.StatusInternalServerError)
244 return 244 return
245 } 245 }
246 // load values 246 // load values
247 if err := bns[i].loadValues(ctx, conn, from, to, los); err != nil { 247 if err := bns[i].loadValues(ctx, conn, from, to, los); err != nil {
248 log.Printf("error: %v\n", err) 248 log.Errorf("%v\n", err)
249 http.Error(rw, "cannot load values", http.StatusInternalServerError) 249 http.Error(rw, "cannot load values", http.StatusInternalServerError)
250 return 250 return
251 } 251 }
252 } 252 }
253 253
298 298
299 out := csv.NewWriter(rw) 299 out := csv.NewWriter(rw)
300 300
301 if err := out.Write(record); err != nil { 301 if err := out.Write(record); err != nil {
302 // Too late for HTTP status message. 302 // Too late for HTTP status message.
303 log.Printf("error: %v\n", err) 303 log.Errorf("%v\n", err)
304 return 304 return
305 } 305 }
306 306
307 for i := range record[1:] { 307 for i := range record[1:] {
308 record[i+1] = "0" 308 record[i+1] = "0"
415 totalDays++ 415 totalDays++
416 416
417 if finish(next) { 417 if finish(next) {
418 if err := write(); err != nil { 418 if err := write(); err != nil {
419 // Too late for HTTP status message. 419 // Too late for HTTP status message.
420 log.Printf("error: %v\n", err) 420 log.Errorf("%v\n", err)
421 return 421 return
422 } 422 }
423 423
424 // Reset counters 424 // Reset counters
425 overLDCDays, totalDays = 0, 0 425 overLDCDays, totalDays = 0, 0
433 433
434 // Write rest if last period was not finished. 434 // Write rest if last period was not finished.
435 if totalDays > 0 { 435 if totalDays > 0 {
436 if err := write(); err != nil { 436 if err := write(); err != nil {
437 // Too late for HTTP status message. 437 // Too late for HTTP status message.
438 log.Printf("error: %v\n", err) 438 log.Errorf("%v\n", err)
439 return 439 return
440 } 440 }
441 } 441 }
442 442
443 for i, days := range missingLDCs { 443 for i, days := range missingLDCs {
444 if missingLDCs[i] > 0 { 444 if missingLDCs[i] > 0 {
445 log.Printf("warn: Missing LDCs for %s on %d days.\n", 445 log.Warnf("warn: Missing LDCs for %s on %d days.\n",
446 bns[i].id, days) 446 bns[i].id, days)
447 } 447 }
448 } 448 }
449 449
450 out.Flush() 450 out.Flush()
451 if err := out.Error(); err != nil { 451 if err := out.Error(); err != nil {
452 // Too late for HTTP status message. 452 // Too late for HTTP status message.
453 log.Printf("error: %v\n", err) 453 log.Errorf("%v\n", err)
454 } 454 }
455 } 455 }
456 456
457 func minClass(classes []time.Duration, threshold time.Duration) int { 457 func minClass(classes []time.Duration, threshold time.Duration) int {
458 var sum time.Duration 458 var sum time.Duration
524 } 524 }
525 525
526 defaults, err := breaksToReferenceValue(breaks) 526 defaults, err := breaksToReferenceValue(breaks)
527 if err != nil { 527 if err != nil {
528 msg := fmt.Sprintf("Parameter '%s' is invalid: %s.", parameter, err) 528 msg := fmt.Sprintf("Parameter '%s' is invalid: %s.", parameter, err)
529 log.Printf("error: %s\n", msg) 529 log.Errorf("%s\n", msg)
530 http.Error(rw, msg, http.StatusBadRequest) 530 http.Error(rw, msg, http.StatusBadRequest)
531 return nil, false 531 return nil, false
532 } 532 }
533 533
534 return defaults, true 534 return defaults, true
584 case "depth": 584 case "depth":
585 return limitingDepth 585 return limitingDepth
586 case "width": 586 case "width":
587 return limitingWidth 587 return limitingWidth
588 default: 588 default:
589 log.Printf("warn: unknown limitation '%s'. default to 'depth'\n", limiting) 589 log.Warnf("unknown limitation '%s'. default to 'depth'\n", limiting)
590 return limitingDepth 590 return limitingDepth
591 } 591 }
592 } 592 }
593 593
594 func loadLimitingValidities( 594 func loadLimitingValidities(