Skip to content

Commit

Permalink
should search for a configurable number of days, defaults to 7
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Oct 25, 2017
1 parent 70fe784 commit c629056
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
16 changes: 9 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import (

// App is the struct that defines the application
type App struct {
Debug bool
Port int
Host string
API *echo.Echo
Engine engine.Server
RedisClient *redisclient.RedisClient
NewRelic newrelic.Application
Debug bool
Port int
Host string
API *echo.Echo
Engine engine.Server
RedisClient *redisclient.RedisClient
NewRelic newrelic.Application
NumberOfDaysToSearch int
}

// GetApp creates an app given the parameters
Expand Down Expand Up @@ -97,6 +98,7 @@ func (app *App) configureSentry() {

func (app *App) configureApplication() {
app.Engine = standard.New(fmt.Sprintf("%s:%d", app.Host, app.Port))
app.NumberOfDaysToSearch = viper.GetInt("numberOfDaysToSearch")
app.API = echo.New()
a := app.API
_, w, _ := os.Pipe()
Expand Down
2 changes: 1 addition & 1 deletion app/histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func HistoriesHandler(app *App) func(c echo.Context) error {

var searchResults *elastic.SearchResult
err = WithSegment("elasticsearch", c, func() error {
searchResults, err = DoESQuery(getLimitedIndexString(), boolQuery, from, limit)
searchResults, err = DoESQuery(app.NumberOfDaysToSearch, boolQuery, from, limit)
return err
})

Expand Down
4 changes: 2 additions & 2 deletions app/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func HistoryHandler(app *App) func(c echo.Context) error {

var searchResults *elastic.SearchResult
err = WithSegment("elasticsearch", c, func() error {
searchResults, err = DoESQuery(getLimitedIndexString(), boolQuery, from, limit)
searchResults, err = DoESQuery(app.NumberOfDaysToSearch, boolQuery, from, limit)
return err
})

Expand Down Expand Up @@ -124,7 +124,7 @@ func HistorySinceHandler(app *App) func(c echo.Context) error {

var searchResults *elastic.SearchResult
err = WithSegment("elasticsearch", c, func() error {
searchResults, err = DoESQuery(getLimitedIndexString(), boolQuery, from, limit)
searchResults, err = DoESQuery(app.NumberOfDaysToSearch, boolQuery, from, limit)
return err
})

Expand Down
18 changes: 5 additions & 13 deletions app/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,20 @@ import (
"gopkg.in/olivere/elastic.v5"
)

func getLimitedIndexString() string {
return fmt.Sprintf(
"chat-%s*,chat-%s*",
time.Now().Local().Format("2006-01-02"),
time.Now().Local().Add(-24*time.Hour).Format("2006-01-02"),
)
}

func getPastIndexString() string {
func getLimitedIndexString(days int) string {
var buffer bytes.Buffer
t := time.Now().Local().Add(time.Duration(2*-24) * time.Hour).Format("2006-01-02")
t := time.Now().Local().Format("2006-01-02")
buffer.WriteString(fmt.Sprintf("chat-%s*", t))
for cnt := 3; cnt <= 30; cnt++ {
for cnt := 1; cnt <= days; cnt++ {
t := time.Now().Local().Add(time.Duration(cnt*-24) * time.Hour).Format("2006-01-02")
buffer.WriteString(fmt.Sprintf(",chat-%s*", t))
}
return buffer.String()
}

// DoESQuery does a query
func DoESQuery(index string, boolQuery *elastic.BoolQuery, from, limit int) (*elastic.SearchResult, error) {
func DoESQuery(numberOfDaysToSearch int, boolQuery *elastic.BoolQuery, from, limit int) (*elastic.SearchResult, error) {
esclient := es.GetESClient()
return esclient.Search().Index(getLimitedIndexString()).Query(boolQuery).
return esclient.Search().Index(getLimitedIndexString(numberOfDaysToSearch)).Query(boolQuery).
Sort("timestamp", false).From(from).Size(limit).Do(context.TODO())
}
1 change: 1 addition & 0 deletions config/local.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
numberOfDaysToSearch: 7
healthcheck:
workingText: "WORKING"
elasticsearch:
Expand Down
1 change: 1 addition & 0 deletions config/test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
numberOfDaysToSearch: 7
healthcheck:
workingText: "WORKING"
elasticsearch:
Expand Down

0 comments on commit c629056

Please sign in to comment.