Fix returned status in /search/result

This commit is contained in:
Kleissner
2021-12-14 16:08:02 +01:00
parent 9262acba88
commit a67c67b7f9
3 changed files with 29 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
job = CreateSearchJob(Timeout, input.MaxResults, Filter) job = CreateSearchJob(Timeout, input.MaxResults, Filter)
// todo: create actual search clients! // todo: create actual search clients!
job.Status = SearchStatusLive
go job.localSearch(api, input.Term) go job.localSearch(api, input.Term)
@@ -33,6 +34,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
func (job *SearchJob) localSearch(api *WebapiInstance, term string) { func (job *SearchJob) localSearch(api *WebapiInstance, term string) {
if api.backend.SearchIndex == nil || api.backend.GlobalBlockchainCache == nil { if api.backend.SearchIndex == nil || api.backend.GlobalBlockchainCache == nil {
job.Status = SearchStatusNoIndex
return return
} }
@@ -75,6 +77,8 @@ resultLoop:
job.statsAdd(&newFile) job.statsAdd(&newFile)
} }
job.Status = SearchStatusTerminated
job.ResultSync.Unlock() job.ResultSync.Unlock()
job.Terminate() job.Terminate()
} }

View File

@@ -48,6 +48,9 @@ type SearchJob struct {
// -- result data -- // -- result data --
// Status indicates the overall search status. This will be removed later when relying on search clients.
Status int
// runtime data // runtime data
//clients []*SearchClient // all search clients //clients []*SearchClient // all search clients
clientsMutex sync.Mutex // mutex for manipulating client list clientsMutex sync.Mutex // mutex for manipulating client list
@@ -67,10 +70,18 @@ type SearchJob struct {
currentOffset int // for always getting the next results currentOffset int // for always getting the next results
} }
const (
SearchStatusNotStarted = iota // Search was not yet started
SearchStatusLive // Search running
SearchStatusTerminated // Search is terminated. No more results are expected.
SearchStatusNoIndex // Search is terminated. No search index to use.
)
// CreateSearchJob creates a new search job and adds it to the lookup list. // CreateSearchJob creates a new search job and adds it to the lookup list.
// Timeout and MaxResults must be set and must not be 0. // Timeout and MaxResults must be set and must not be 0.
func CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) { func CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) {
job = &SearchJob{} job = &SearchJob{}
job.Status = SearchStatusNotStarted
job.id = uuid.New() job.id = uuid.New()
job.timeout = Timeout job.timeout = Timeout
job.maxResult = MaxResults job.maxResult = MaxResults

View File

@@ -169,12 +169,23 @@ func apiSearchResult(w http.ResponseWriter, r *http.Request) {
result.Files = append(result.Files, *resultFiles[n]) result.Files = append(result.Files, *resultFiles[n])
} }
if len(result.Files) == 0 { // set the status
result.Status = 3 // No results yet available keep trying if len(result.Files) > 0 {
result.Status = 0 // 0 = Success with results
} else {
switch job.Status {
case SearchStatusLive:
result.Status = 3 // No results yet available keep trying
case SearchStatusTerminated:
result.Status = 1 // No more results to expect
default: // SearchStatusNoIndex, SearchStatusNotStarted
result.Status = 1 // No more results to expect
}
} }
//if !job.IsSearchResults() { // if no fresh results to be expected //if !job.IsSearchResults() { // if no fresh results to be expected
result.Status = 1 // No more results to expect
// embedded statistics? // embedded statistics?
if returnStats, _ := strconv.ParseBool(r.Form.Get("stats")); returnStats { if returnStats, _ := strconv.ParseBool(r.Form.Get("stats")); returnStats {