API: fix startDate/endDate query param

This commit is contained in:
Aloïs Micard 2020-09-23 09:09:08 +02:00
parent 62b54bf385
commit b85e9944a2
No known key found for this signature in database
GPG Key ID: 1A0EB82F071F5EFE
3 changed files with 18 additions and 9 deletions

View File

@ -125,7 +125,7 @@ func searchResources(es *elastic.Client) echo.HandlerFunc {
startDate := time.Time{}
if val := c.QueryParam("start-date"); val != "" {
d, err := time.Parse(time.RFC3339, val)
if err != nil {
if err == nil {
startDate = d
}
}
@ -133,7 +133,7 @@ func searchResources(es *elastic.Client) echo.HandlerFunc {
endDate := time.Time{}
if val := c.QueryParam("end-date"); val != "" {
d, err := time.Parse(time.RFC3339, val)
if err != nil {
if err == nil {
endDate = d
}
}
@ -231,19 +231,23 @@ func addResource(es *elastic.Client) echo.HandlerFunc {
func buildSearchQuery(url, keyword string, startDate, endDate time.Time) elastic.Query {
var queries []elastic.Query
if url != "" {
log.Trace().Str("url", url).Msg("SearchQuery: Setting url")
queries = append(queries, elastic.NewTermQuery("url", url))
}
if keyword != "" {
log.Trace().Str("body", keyword).Msg("SearchQuery: Setting body")
queries = append(queries, elastic.NewTermQuery("body", keyword))
}
if !startDate.IsZero() || !endDate.IsZero() {
timeQuery := elastic.NewRangeQuery("time")
if !startDate.IsZero() {
timeQuery.Gte(startDate)
log.Trace().Str("startDate", startDate.Format(time.RFC3339)).Msg("SearchQuery: Setting startDate")
timeQuery.Gte(startDate.Format(time.RFC3339))
}
if !endDate.IsZero() {
timeQuery.Lte(endDate)
log.Trace().Str("endDate", endDate.Format(time.RFC3339)).Msg("SearchQuery: Setting endDate")
timeQuery.Lte(endDate.Format(time.RFC3339))
}
queries = append(queries, timeQuery)
}

View File

@ -51,6 +51,13 @@ func execute(ctx *cli.Context) error {
log.Debug().Str("uri", ctx.String("nats-uri")).Msg("Using NATS server")
log.Debug().Str("uri", ctx.String("api-uri")).Msg("Using API server")
refreshDelay := parseRefreshDelay(ctx.String("refresh-delay"))
if refreshDelay != -1 {
log.Debug().Stringer("delay", refreshDelay).Msg("Existing resources will be crawled again")
} else {
log.Debug().Msg("Existing resources will NOT be crawled again")
}
// Create the API client
apiClient := api.NewClient(ctx.String("api-uri"))
@ -63,8 +70,7 @@ func execute(ctx *cli.Context) error {
log.Info().Msg("Successfully initialized tdsh-scheduler. Waiting for URLs")
if err := sub.QueueSubscribe(messaging.URLFoundSubject, "schedulers",
handleMessage(apiClient, parseRefreshDelay(ctx.String("refresh-delay")))); err != nil {
if err := sub.QueueSubscribe(messaging.URLFoundSubject, "schedulers", handleMessage(apiClient, refreshDelay)); err != nil {
return err
}
@ -96,10 +102,7 @@ func handleMessage(apiClient api.Client, refreshDelay time.Duration) natsutil.Ms
// that are newer than now-refreshDelay.
endDate := time.Time{}
if refreshDelay != -1 {
log.Debug().Stringer("delay", refreshDelay).Msg("Existing resources will be crawled again")
endDate = time.Now().Add(-refreshDelay)
} else {
log.Debug().Msg("Existing resources will NOT be crawled again")
}
b64URI := base64.URLEncoding.EncodeToString([]byte(u.String()))

View File

@ -26,4 +26,6 @@ func ConfigureLogger(ctx *cli.Context) {
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Debug().Stringer("lvl", zerolog.GlobalLevel()).Msg("Setting log level")
}