Search/text.go

235 lines
6.4 KiB
Go
Raw Normal View History

2024-05-16 16:29:26 +00:00
package main
import (
2024-05-17 23:59:29 +00:00
"flag"
2024-05-17 12:26:28 +00:00
"fmt"
"html/template"
"log"
2024-05-16 16:29:26 +00:00
"net/http"
2024-05-17 12:26:28 +00:00
"sort"
2024-05-17 23:59:29 +00:00
"sync"
2024-05-17 12:26:28 +00:00
"time"
2024-05-16 16:29:26 +00:00
)
var (
debugMode bool
)
2024-05-17 23:59:29 +00:00
func init() {
flag.BoolVar(&debugMode, "debug", false, "enable debug mode")
flag.Parse()
}
2024-05-17 12:26:28 +00:00
func HandleTextSearch(w http.ResponseWriter, query, safe, lang string, page int) {
2024-05-17 23:59:29 +00:00
startTime := time.Now()
const resultsPerPage = 10
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
combinedResults := getTextResultsFromCacheOrFetch(cacheKey, query, safe, lang, page, resultsPerPage)
2024-05-21 06:48:09 +00:00
hasPrevPage := page > 1
hasNextPage := len(combinedResults) == resultsPerPage
displayResults(w, combinedResults, query, lang, time.Since(startTime).Seconds(), page, hasPrevPage, hasNextPage)
2024-05-21 08:19:40 +00:00
// Always check and cache the next page if not enough results
if hasNextPage {
go cacheNextPageIfNotCached(query, safe, lang, page+1, resultsPerPage)
}
2024-05-21 06:48:09 +00:00
}
func getTextResultsFromCacheOrFetch(cacheKey CacheKey, query, safe, lang string, page, resultsPerPage int) []TextSearchResult {
cacheChan := make(chan []SearchResult)
2024-05-20 20:14:48 +00:00
var combinedResults []TextSearchResult
2024-05-20 20:14:48 +00:00
go func() {
results, exists := resultsCache.Get(cacheKey)
if exists {
log.Println("Cache hit")
cacheChan <- results
} else {
log.Println("Cache miss")
cacheChan <- nil
}
}()
select {
case results := <-cacheChan:
if results == nil {
combinedResults = fetchTextResultsUntilFull(query, safe, lang, page, resultsPerPage)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
} else {
textResults, _, _ := convertToSpecificResults(results)
combinedResults = textResults
2024-05-20 20:14:48 +00:00
}
case <-time.After(2 * time.Second):
log.Println("Cache check timeout")
combinedResults = fetchTextResultsUntilFull(query, safe, lang, page, resultsPerPage)
resultsCache.Set(cacheKey, convertToSearchResults(combinedResults))
}
2024-05-21 06:48:09 +00:00
return combinedResults
}
func cacheNextPageIfNotCached(query, safe, lang string, page, resultsPerPage int) {
cacheKey := CacheKey{Query: query, Page: page, Safe: safe == "true", Lang: lang, Type: "text"}
2024-05-21 06:48:09 +00:00
if _, exists := resultsCache.Get(cacheKey); !exists {
log.Printf("Next page %d not cached, caching now...", page)
nextPageResults := fetchTextResultsUntilFull(query, safe, lang, page, resultsPerPage)
resultsCache.Set(cacheKey, convertToSearchResults(nextPageResults))
2024-05-21 06:48:09 +00:00
} else {
log.Printf("Next page %d already cached", page)
2024-05-20 20:14:48 +00:00
}
2024-05-21 06:48:09 +00:00
}
func fetchTextResultsUntilFull(query, safe, lang string, targetPage, resultsPerPage int) []TextSearchResult {
2024-05-21 06:48:09 +00:00
var combinedResults []TextSearchResult
currentPage := 1
2024-05-21 08:19:40 +00:00
resultsNeeded := targetPage * resultsPerPage
2024-05-21 06:48:09 +00:00
for len(combinedResults) < resultsNeeded {
cacheKey := CacheKey{Query: query, Page: targetPage, Safe: safe == "true", Lang: lang, Type: "text"}
2024-05-21 08:19:40 +00:00
cachedResults, exists := resultsCache.Get(cacheKey)
if exists {
textResults, _, _ := convertToSpecificResults(cachedResults)
combinedResults = append(combinedResults, textResults...)
2024-05-21 08:19:40 +00:00
} else {
results := fetchAndCacheTextResults(query, safe, lang, currentPage, resultsPerPage)
2024-05-21 08:19:40 +00:00
if len(results) == 0 {
break
}
combinedResults = append(combinedResults, results...)
resultsCache.Set(cacheKey, convertToSearchResults(results))
2024-05-21 06:48:09 +00:00
}
2024-05-21 08:19:40 +00:00
2024-05-21 06:48:09 +00:00
currentPage++
2024-05-21 08:19:40 +00:00
// Stop fetching if we have enough results for the target page and the next page
if len(combinedResults) >= resultsNeeded+resultsPerPage {
break
}
2024-05-21 06:48:09 +00:00
}
2024-05-21 08:19:40 +00:00
startIndex := (targetPage - 1) * resultsPerPage
2024-05-21 06:48:09 +00:00
endIndex := startIndex + resultsPerPage
if startIndex >= len(combinedResults) {
return []TextSearchResult{}
}
if endIndex > len(combinedResults) {
endIndex = len(combinedResults)
}
return combinedResults[startIndex:endIndex]
}
func fetchAndCacheTextResults(query, safe, lang string, page, resultsPerPage int) []TextSearchResult {
2024-05-17 23:59:29 +00:00
var combinedResults []TextSearchResult
var wg sync.WaitGroup
var mu sync.Mutex
resultsChan := make(chan []TextSearchResult)
searchFuncs := []struct {
Func func(string, string, string, int) ([]TextSearchResult, error)
2024-05-17 23:59:29 +00:00
Source string
}{
{PerformGoogleTextSearch, "Google"},
{PerformDuckDuckGoTextSearch, "DuckDuckGo"},
{PerformQwantTextSearch, "Qwant"},
2024-05-17 12:26:28 +00:00
}
2024-05-17 23:59:29 +00:00
wg.Add(len(searchFuncs))
2024-05-17 12:26:28 +00:00
2024-05-17 23:59:29 +00:00
for _, searchFunc := range searchFuncs {
go func(searchFunc func(string, string, string, int) ([]TextSearchResult, error), source string) {
2024-05-17 23:59:29 +00:00
defer wg.Done()
results, err := searchFunc(query, safe, lang, page)
2024-05-17 23:59:29 +00:00
if err == nil {
for i := range results {
results[i].Source = source
}
resultsChan <- results
} else {
log.Printf("Error performing search from %s: %v", source, err)
}
}(searchFunc.Func, searchFunc.Source)
2024-05-17 12:26:28 +00:00
}
2024-05-17 23:59:29 +00:00
go func() {
wg.Wait()
close(resultsChan)
}()
for results := range resultsChan {
mu.Lock()
combinedResults = append(combinedResults, results...)
2024-05-17 23:59:29 +00:00
mu.Unlock()
2024-05-17 12:26:28 +00:00
}
sort.SliceStable(combinedResults, func(i, j int) bool {
2024-05-17 23:59:29 +00:00
return sourceOrder(combinedResults[i].Source) < sourceOrder(combinedResults[j].Source)
2024-05-17 12:26:28 +00:00
})
2024-05-21 06:48:09 +00:00
log.Printf("Fetched %d results for page %d", len(combinedResults), page)
return combinedResults
}
2024-05-17 23:59:29 +00:00
func sourceOrder(source string) int {
switch source {
case "Google":
2024-05-18 11:23:39 +00:00
return 1
case "DuckDuckGo":
return 2
case "Qwant":
return 3
2024-05-17 23:59:29 +00:00
default:
return 4
}
2024-05-16 16:29:26 +00:00
}
func displayResults(w http.ResponseWriter, results []TextSearchResult, query, lang string, elapsed float64, page int, hasPrevPage, hasNextPage bool) {
2024-05-21 06:48:09 +00:00
log.Printf("Displaying results for page %d", page)
log.Printf("Total results: %d", len(results))
log.Printf("Has previous page: %t, Has next page: %t", hasPrevPage, hasNextPage)
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")
2024-05-17 12:26:28 +00:00
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
data := struct {
Results []TextSearchResult
Query string
Fetched string
Page int
HasPrevPage bool
HasNextPage bool
2024-05-17 12:26:28 +00:00
LanguageOptions []LanguageOption
CurrentLang string
}{
Results: results,
Query: query,
2024-05-17 23:59:29 +00:00
Fetched: fmt.Sprintf("%.2f seconds", elapsed),
Page: page,
HasPrevPage: hasPrevPage,
HasNextPage: hasNextPage,
2024-05-17 12:26:28 +00:00
LanguageOptions: languageOptions,
CurrentLang: lang,
}
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
2024-05-16 16:29:26 +00:00
}