comparison pkg/imports/fa.go @ 4281:c470d2202823

Limit time range requested from fa services. This is a workaround, so that services which limit the amount of results in there responses don't bail out, when the latest data is very old.
author Sascha Wilde <wilde@intevation.de>
date Thu, 29 Aug 2019 16:59:47 +0200
parents 5466562cca60
children c811d5da69bd
comparison
equal deleted inserted replaced
4280:2f2dbd3ebaea 4281:c470d2202823
36 // Insecure indicates if HTTPS traffic 36 // Insecure indicates if HTTPS traffic
37 // should validate certificates or not. 37 // should validate certificates or not.
38 Insecure bool `json:"insecure"` 38 Insecure bool `json:"insecure"`
39 } 39 }
40 40
41 // FAJobKind is import queue type identifier. 41 const (
42 const FAJobKind JobKind = "fa" 42 // FAJobKind is import queue type identifier.
43 FAJobKind JobKind = "fa"
44 // Upper limit for days to request
45 MaxHistoryDays int = 7
46 )
43 47
44 const ( 48 const (
45 listBottlenecksSQL = ` 49 listBottlenecksSQL = `
46 SELECT DISTINCT 50 SELECT DISTINCT
47 bottleneck_id 51 bottleneck_id
212 var date pgtype.Timestamp 216 var date pgtype.Timestamp
213 err := tx.QueryRowContext(ctx, latestMeasureDateSQL).Scan(&date) 217 err := tx.QueryRowContext(ctx, latestMeasureDateSQL).Scan(&date)
214 switch { 218 switch {
215 case err == sql.ErrNoRows: 219 case err == sql.ErrNoRows:
216 date = pgtype.Timestamp{ 220 date = pgtype.Timestamp{
217 // Fill Database with data of the last 5 days. Change this to a more useful value. 221 Time: time.Now().AddDate(0, 0, MaxHistoryDays*-1),
218 Time: time.Now().AddDate(0, 0, -5),
219 } 222 }
220 case err != nil: 223 case err != nil:
221 return pgtype.Timestamp{}, err 224 return pgtype.Timestamp{}, err
222 } 225 }
226 // Limit request range to MaxHistoryDays. Otherwise the Request might
227 // fail, e.g. the AT service has an upper liimit of 10000 results.
228 //
229 // FIXME: the better solution would be to detect such errors and
230 // dynamically and implement some kind of chunking...
231 if time.Since(date.Time).Hours() > float64(MaxHistoryDays*24) {
232 date = pgtype.Timestamp{
233 Time: time.Now().AddDate(0, 0, MaxHistoryDays*-1),
234 }
235 }
236
223 return date, nil 237 return date, nil
224 } 238 }
225 239
226 func storeFairwayAvailability( 240 func storeFairwayAvailability(
227 ctx context.Context, 241 ctx context.Context,