cache v1 + debug mode + buttons on text results
This commit is contained in:
parent
9208104ff7
commit
d5bbfe118d
9 changed files with 228 additions and 65 deletions
103
text.go
103
text.go
|
@ -1,3 +1,4 @@
|
|||
// text.go
|
||||
package main
|
||||
|
||||
import (
|
||||
|
@ -11,31 +12,49 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type TextSearchResult struct {
|
||||
URL string
|
||||
Header string
|
||||
Description string
|
||||
Source string
|
||||
}
|
||||
|
||||
var debugMode bool
|
||||
var (
|
||||
debugMode bool
|
||||
resultsCache = NewResultsCache()
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&debugMode, "debug", false, "enable debug mode")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string) {
|
||||
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int) {
|
||||
startTime := time.Now()
|
||||
const resultsPerPage = 10
|
||||
|
||||
cacheKey := CacheKey{Query: query, Page: page, Safe: safe, Lang: lang}
|
||||
|
||||
// Try to get results from cache
|
||||
combinedResults, exists := resultsCache.Get(cacheKey)
|
||||
if !exists {
|
||||
// Fetch results for the current page
|
||||
combinedResults = fetchAndCacheResults(query, safe, lang, page, resultsPerPage)
|
||||
resultsCache.Set(cacheKey, combinedResults)
|
||||
}
|
||||
|
||||
// Pre-fetch and cache results for the next page
|
||||
nextPageResults := fetchAndCacheResults(query, safe, lang, page+1, resultsPerPage)
|
||||
resultsCache.Set(CacheKey{Query: query, Page: page + 1, Safe: safe, Lang: lang}, nextPageResults)
|
||||
|
||||
hasPrevPage := page > 1
|
||||
hasNextPage := len(nextPageResults) > 0
|
||||
|
||||
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
|
||||
}
|
||||
|
||||
func fetchAndCacheResults(query, safe, lang string, page, resultsPerPage int) []TextSearchResult {
|
||||
var combinedResults []TextSearchResult
|
||||
var resultMap = make(map[string]TextSearchResult)
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
resultsChan := make(chan []TextSearchResult)
|
||||
|
||||
searchFuncs := []struct {
|
||||
Func func(string, string, string) ([]TextSearchResult, error)
|
||||
Func func(string, string, string, int) ([]TextSearchResult, error)
|
||||
Source string
|
||||
}{
|
||||
{PerformGoogleTextSearch, "Google"},
|
||||
|
@ -46,9 +65,9 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string) {
|
|||
wg.Add(len(searchFuncs))
|
||||
|
||||
for _, searchFunc := range searchFuncs {
|
||||
go func(searchFunc func(string, string, string) ([]TextSearchResult, error), source string) {
|
||||
go func(searchFunc func(string, string, string, int) ([]TextSearchResult, error), source string) {
|
||||
defer wg.Done()
|
||||
results, err := searchFunc(query, safe, lang)
|
||||
results, err := searchFunc(query, safe, lang, page)
|
||||
if err == nil {
|
||||
for i := range results {
|
||||
results[i].Source = source
|
||||
|
@ -67,50 +86,52 @@ func HandleTextSearch(w http.ResponseWriter, query, safe, lang string) {
|
|||
|
||||
for results := range resultsChan {
|
||||
mu.Lock()
|
||||
for _, result := range results {
|
||||
existingResult, exists := resultMap[result.URL]
|
||||
if !exists || shouldReplace(existingResult.Source, result.Source) {
|
||||
resultMap[result.URL] = result
|
||||
}
|
||||
if debugMode {
|
||||
log.Printf("Result from %s: %+v\n", result.Source, result)
|
||||
}
|
||||
}
|
||||
combinedResults = append(combinedResults, results...)
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
// Convert the map back to a slice
|
||||
for _, result := range resultMap {
|
||||
combinedResults = append(combinedResults, result)
|
||||
}
|
||||
|
||||
// Custom sorting: Google first, DuckDuckGo second, Qwant third
|
||||
// Sort combinedResults by source priority: Google first, DuckDuckGo second, Qwant third
|
||||
sort.SliceStable(combinedResults, func(i, j int) bool {
|
||||
return sourceOrder(combinedResults[i].Source) < sourceOrder(combinedResults[j].Source)
|
||||
})
|
||||
|
||||
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds())
|
||||
}
|
||||
// Paginate results
|
||||
startIndex := (page - 1) * resultsPerPage
|
||||
endIndex := startIndex + resultsPerPage
|
||||
|
||||
func shouldReplace(existingSource, newSource string) bool {
|
||||
return sourceOrder(newSource) < sourceOrder(existingSource)
|
||||
// Ensure startIndex and endIndex are within bounds
|
||||
if startIndex >= len(combinedResults) {
|
||||
return []TextSearchResult{}
|
||||
}
|
||||
if endIndex > len(combinedResults) {
|
||||
endIndex = len(combinedResults)
|
||||
}
|
||||
|
||||
return combinedResults[startIndex:endIndex]
|
||||
}
|
||||
|
||||
func sourceOrder(source string) int {
|
||||
switch source {
|
||||
case "Qwant":
|
||||
return 3
|
||||
case "DuckDuckGo":
|
||||
return 2
|
||||
case "Google":
|
||||
return 1
|
||||
case "DuckDuckGo":
|
||||
return 2
|
||||
case "Qwant":
|
||||
return 3
|
||||
default:
|
||||
return 4
|
||||
}
|
||||
}
|
||||
|
||||
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64) {
|
||||
tmpl, err := template.ParseFiles("templates/text.html")
|
||||
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64, page int, hasPrevPage, hasNextPage bool) {
|
||||
tmpl, err := template.New("text.html").Funcs(template.FuncMap{
|
||||
"sub": func(a, b int) int {
|
||||
return a - b
|
||||
},
|
||||
"add": func(a, b int) int {
|
||||
return a + b
|
||||
},
|
||||
}).ParseFiles("templates/text.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
return
|
||||
|
@ -120,12 +141,18 @@ func displayResults(w http.ResponseWriter, results []TextSearchResult, query, la
|
|||
Results []TextSearchResult
|
||||
Query string
|
||||
Fetched string
|
||||
Page int
|
||||
HasPrevPage bool
|
||||
HasNextPage bool
|
||||
LanguageOptions []LanguageOption
|
||||
CurrentLang string
|
||||
}{
|
||||
Results: results,
|
||||
Query: query,
|
||||
Fetched: fmt.Sprintf("%.2f seconds", elapsed),
|
||||
Page: page,
|
||||
HasPrevPage: hasPrevPage,
|
||||
HasNextPage: hasNextPage,
|
||||
LanguageOptions: languageOptions,
|
||||
CurrentLang: lang,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue