Skip to content

Commit

Permalink
feat(suggestions): config
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksasiriski committed Jun 23, 2024
1 parent e8d9566 commit 7945e9b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
9 changes: 9 additions & 0 deletions src/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func New() Config {
},
},
Categories: map[category.Name]Category{
category.SUGGESTIONS: {
Engines: suggestionsEngines,
RequiredEngines: suggestionsRequiredEngines,
RequiredByOriginEngines: suggestionsRequiredByOriginEngines,
PreferredEngines: suggestionsPreferredEngines,
PreferredByOriginEngines: suggestionsPreferredByOriginEngines,
Ranking: suggestionsRanking(),
Timings: suggestionsTimings,
},
category.GENERAL: {
Engines: generalEngines,
RequiredEngines: generalRequiredEngines,
Expand Down
32 changes: 32 additions & 0 deletions src/config/defaults_cat_suggestions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package config

import (
"time"

"github.com/hearchco/agent/src/search/engines"
)

var suggestionsEngines = []engines.Name{
engines.DUCKDUCKGO,
engines.GOOGLE,
}

var suggestionsRequiredEngines = []engines.Name{
engines.DUCKDUCKGO,
engines.GOOGLE,
}

var suggestionsRequiredByOriginEngines = []engines.Name{}

var suggestionsPreferredEngines = []engines.Name{}

var suggestionsPreferredByOriginEngines = []engines.Name{}

func suggestionsRanking() CategoryRanking {
return EmptyRanking(suggestionsEngines)
}

var suggestionsTimings = CategoryTimings{
PreferredTimeout: 300 * time.Millisecond,
HardTimeout: 500 * time.Millisecond,
}
9 changes: 4 additions & 5 deletions src/router/routes/route_suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/hearchco/agent/src/config"
"github.com/hearchco/agent/src/search"
"github.com/hearchco/agent/src/search/engines/options"
"github.com/hearchco/agent/src/search/result"
)

func routeSuggest(w http.ResponseWriter, r *http.Request) error {
func routeSuggest(w http.ResponseWriter, r *http.Request, catConf config.Category) error {
// Parse form data (including query params).
if err := r.ParseForm(); err != nil {
// Server error.
Expand Down Expand Up @@ -46,8 +46,7 @@ func routeSuggest(w http.ResponseWriter, r *http.Request) error {
}

// Search for suggestions.
// TODO: Make timeout configurable.
scrapedSugs, err := search.Suggest(query, locale, 1*time.Second)
scrapedSugs, err := search.Suggest(query, locale, catConf)
if err != nil {
// Server error.
werr := writeResponseJSON(w, http.StatusInternalServerError, ErrorResponse{
Expand All @@ -60,7 +59,7 @@ func routeSuggest(w http.ResponseWriter, r *http.Request) error {
return err
}

// Rank the suggestions.
// TODO: Rank the suggestions.
// rankedSugs := rank.Rank(scrapedSugs, Ranking)

// Convert the suggestions to output format.
Expand Down
18 changes: 11 additions & 7 deletions src/router/routes/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (

"github.com/hearchco/agent/src/cache"
"github.com/hearchco/agent/src/config"
"github.com/hearchco/agent/src/search/category"
)

func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
// /healthz
mux.Get("/healthz", func(w http.ResponseWriter, r *http.Request) {
err := writeResponse(w, http.StatusOK, "OK")
if err != nil {
Expand All @@ -22,6 +24,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
}
})

// /versionz
mux.Get("/versionz", func(w http.ResponseWriter, r *http.Request) {
err := writeResponse(w, http.StatusOK, ver)
if err != nil {
Expand All @@ -33,6 +36,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
}
})

// /search
mux.Get("/search", func(w http.ResponseWriter, r *http.Request) {
err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt)
if err != nil {
Expand All @@ -43,9 +47,8 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
Msg("Failed to send response")
}
})

mux.Post("/suggestions", func(w http.ResponseWriter, r *http.Request) {
err := routeSuggest(w, r)
mux.Post("/search", func(w http.ResponseWriter, r *http.Request) {
err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt)
if err != nil {
log.Error().
Err(err).
Expand All @@ -55,8 +58,9 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
}
})

// /suggestions
mux.Get("/suggestions", func(w http.ResponseWriter, r *http.Request) {
err := routeSuggest(w, r)
err := routeSuggest(w, r, conf.Categories[category.SUGGESTIONS])
if err != nil {
log.Error().
Err(err).
Expand All @@ -65,9 +69,8 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
Msg("Failed to send response")
}
})

mux.Post("/search", func(w http.ResponseWriter, r *http.Request) {
err := routeSearch(w, r, ver, conf.Categories, conf.Server.Cache.TTL, db, conf.Server.ImageProxy.Salt)
mux.Post("/suggestions", func(w http.ResponseWriter, r *http.Request) {
err := routeSuggest(w, r, conf.Categories[category.SUGGESTIONS])
if err != nil {
log.Error().
Err(err).
Expand All @@ -77,6 +80,7 @@ func Setup(mux *chi.Mux, ver string, db cache.DB, conf config.Config) {
}
})

// /proxy
mux.Get("/proxy", func(w http.ResponseWriter, r *http.Request) {
err := routeProxy(w, r, conf.Server.ImageProxy.Salt, conf.Server.ImageProxy.Timeouts)
if err != nil {
Expand Down
13 changes: 8 additions & 5 deletions src/search/category/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
type Name string

const (
UNDEFINED Name = "undefined"
GENERAL Name = "general"
IMAGES Name = "images"
SCIENCE Name = "science"
THOROUGH Name = "thorough"
UNDEFINED Name = "undefined"
SUGGESTIONS Name = "suggestions"
GENERAL Name = "general"
IMAGES Name = "images"
SCIENCE Name = "science"
THOROUGH Name = "thorough"
)

func (cat Name) String() string {
Expand All @@ -31,6 +32,8 @@ func FromString(cat string) (Name, error) {
return SCIENCE, nil
case THOROUGH.String():
return THOROUGH, nil
case SUGGESTIONS.String():
return UNDEFINED, fmt.Errorf("category %q is not allowed", cat)
default:
return UNDEFINED, fmt.Errorf("category %q is not defined", cat)
}
Expand Down
17 changes: 1 addition & 16 deletions src/search/suggest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,12 @@ import (
"github.com/rs/zerolog/log"

"github.com/hearchco/agent/src/config"
"github.com/hearchco/agent/src/search/engines"
"github.com/hearchco/agent/src/search/engines/options"
"github.com/hearchco/agent/src/search/result"
"github.com/hearchco/agent/src/utils/anonymize"
)

func Suggest(query string, locale options.Locale, hardTimeout time.Duration) ([]result.Suggestion, error) {
// TODO: Implement category for this.
catConf := config.Category{
Engines: []engines.Name{engines.DUCKDUCKGO, engines.GOOGLE},
RequiredEngines: []engines.Name{engines.DUCKDUCKGO, engines.GOOGLE},
RequiredByOriginEngines: []engines.Name{},
PreferredEngines: []engines.Name{},
PreferredByOriginEngines: []engines.Name{},
Ranking: config.CategoryRanking{},
Timings: config.CategoryTimings{
PreferredTimeout: hardTimeout,
HardTimeout: hardTimeout,
},
}

func Suggest(query string, locale options.Locale, catConf config.Category) ([]result.Suggestion, error) {
// Capture start time.
startTime := time.Now()

Expand Down

0 comments on commit 7945e9b

Please sign in to comment.