From 568a3fb2c4bfbca6c2d7f70db66f1e1250c5f834 Mon Sep 17 00:00:00 2001 From: Dominik Hebeler <dominik@suma-ev.de> Date: Wed, 6 Mar 2019 14:11:13 +0100 Subject: [PATCH] loading the new results with ajax is now woking --- app/Http/Controllers/MetaGerSearch.php | 40 ++++++++++--- app/Jobs/Searcher.php | 12 ++-- app/MetaGer.php | 57 ++++++++++--------- app/Models/Quicktips/Quicktips.php | 11 ++-- app/Models/Searchengine.php | 20 ++----- resources/js/scriptResultPage.js | 39 ++++++++++--- .../views/layouts/image_result.blade.php | 8 +++ resources/views/parts/errors.blade.php | 2 +- resources/views/parts/pager.blade.php | 4 +- .../resultpages/results_images.blade.php | 11 +--- 10 files changed, 124 insertions(+), 80 deletions(-) create mode 100644 resources/views/layouts/image_result.blade.php diff --git a/app/Http/Controllers/MetaGerSearch.php b/app/Http/Controllers/MetaGerSearch.php index 9b992a763..f46c4331c 100644 --- a/app/Http/Controllers/MetaGerSearch.php +++ b/app/Http/Controllers/MetaGerSearch.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App; use App\MetaGer; +use Cache; use Illuminate\Http\Request; use Illuminate\Support\Facades\Redis; use View; @@ -106,12 +107,19 @@ class MetaGerSearch extends Controller # Read which search engines are new $newEngines = []; - foreach ($redis->lrange($metager->getRedisResultWaitingKey(), 0, -1) as $engine) { + while (($engine = $redis->lpop($metager->getRedisResultWaitingKey())) != null) { $result["engineDelivered"]++; $newEngines[$engine] = $metager->getSumaFile()->sumas->{$engine}; } - $metager->actuallyCreateSearchEngines($newEngines); - # Add the results already delivered to the suer + $cache = Cache::get($hash); + if ($cache != null) { + $metager->setNext(unserialize($cache)["engines"]); + } + + # Check if this request is not for page one + $metager->setEngines($request, $newEngines); + + # Add the results already delivered to the user $results = $redis->lrange($metager->getRedisCurrentResultList(), 0, -1); foreach ($results as $index => $oldResult) { $results[$index] = unserialize(base64_decode($oldResult)); @@ -121,15 +129,33 @@ class MetaGerSearch extends Controller $metager->retrieveResults(); $metager->rankAll(); $metager->prepareResults(); - + $result["nextSearchLink"] = $metager->nextSearchLink(); $results = $metager->getResults(); foreach ($results as $index => $resultTmp) { if ($resultTmp->new) { - $view = View::make('layouts.result', ['result' => $resultTmp, 'metager' => $metager]); - $html = $view->render(); - $result['newResults'][$index] = $html; + if ($metager->getFokus() !== "bilder") { + $view = View::make('layouts.result', ['result' => $resultTmp, 'metager' => $metager]); + $html = $view->render(); + $result['newResults'][$index] = $html; + $result["imagesearch"] = false; + } else { + $view = View::make('layouts.image_result', ['result' => $resultTmp, 'metager' => $metager]); + $html = $view->render(); + $result['newResults'][$index] = $html; + $result["imagesearch"] = true; + } } } + # Save the results in Redis + $pipeline = $redis->pipeline(); + $pipeline->hincrby($metager->getRedisEngineResult() . "status", "engineDelivered", sizeof($newEngines)); + $pipeline->hset($metager->getRedisEngineResult() . "status", "nextSearchLink", $result["nextSearchLink"]); + foreach ($metager->getResults() as $resultTmp) { + $resultTmp->new = false; + $pipeline->rpush($metager->getRedisCurrentResultList(), base64_encode(serialize($resultTmp))); + } + $pipeline->expire($metager->getRedisCurrentResultList(), 6000); + $pipeline->execute(); } return response()->json($result); diff --git a/app/Jobs/Searcher.php b/app/Jobs/Searcher.php index e911713af..2271c7bde 100644 --- a/app/Jobs/Searcher.php +++ b/app/Jobs/Searcher.php @@ -166,19 +166,17 @@ class Searcher implements ShouldQueue private function storeResult($result, $poptime, $hashValue) { - if ($this->name === "zeitde") { - sleep(3); - } - $pipeline = Redis::pipeline(); + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); + $pipeline = $redis->pipeline(); $pipeline->hset('search.' . $hashValue . ".results." . $this->name, "response", $result); $pipeline->hset('search.' . $hashValue . ".results." . $this->name, "delivered", "0"); $pipeline->hincrby('search.' . $hashValue . ".results.status", "engineAnswered", 1); // After 60 seconds the results should be read by the MetaGer Process and stored in the Cache instead - $pipeline->expire('search.' . $hashValue . ".results." . $this->name, 6000); + $pipeline->expire('search.' . $hashValue . ".results." . $this->name, env('REDIS_RESULT_CACHE_DURATION')); $pipeline->rpush('search.' . $hashValue . ".ready", $this->name); - $pipeline->expire('search.' . $hashValue . ".ready", 6000); + $pipeline->expire('search.' . $hashValue . ".ready", env('REDIS_RESULT_CACHE_DURATION')); $pipeline->sadd('search.' . $hashValue . ".engines", $this->name); - $pipeline->expire('search.' . $hashValue . ".engines", 6000); + $pipeline->expire('search.' . $hashValue . ".engines", env('REDIS_RESULT_CACHE_DURATION')); $pipeline->execute(); $this->lastTime = microtime(true); } diff --git a/app/MetaGer.php b/app/MetaGer.php index ac12cfbb4..26425376e 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -46,6 +46,7 @@ class MetaGer protected $agent; protected $apiKey = ""; protected $apiAuthorized = false; + protected $next = []; # Konfigurationseinstellungen: protected $sumaFile; protected $mobile; @@ -92,7 +93,6 @@ class MetaGer } catch (ConnectionException $e) { $this->canCache = false; } - $this->canCache = false; if ($hash === "") { $this->searchUid = md5(uniqid()); } else { @@ -235,7 +235,6 @@ class MetaGer public function prepareResults() { $engines = $this->engines; - // combine $this->combineResults($engines); // misc (WiP) @@ -280,14 +279,6 @@ class MetaGer $counter = 0; $firstRank = 0; - if (isset($this->startForwards)) { - $this->startCount = $this->startForwards; - } elseif (isset($this->startBackwards)) { - $this->startCount = $this->startBackwards - count($this->results) - 1; - } else { - $this->startCount = 0; - } - if (count($this->results) <= 0) { if (strlen($this->site) > 0) { $no_sitesearch_query = str_replace(urlencode("site:" . $this->site), "", $this->fullUrl); @@ -301,10 +292,9 @@ class MetaGer $page = $this->page + 1; $this->next = [ 'page' => $page, - 'startForwards' => $this->results[count($this->results) - 1]->number, 'engines' => $this->next, ]; - Cache::put(md5(serialize($this->next)), serialize($this->next), 60); + Cache::put($this->getSearchUid(), serialize($this->next), 60); } else { $this->next = []; } @@ -538,15 +528,23 @@ class MetaGer $engines = []; $typeslist = []; $counter = 0; + $this->setEngines($request); + } + public function setEngines(Request $request, $enabledSearchengines = []) + { if ($this->requestIsCached($request)) { # If this is a page other than 1 the request is "cached" $engines = $this->getCachedEngines($request); # We need to edit some Options of the Cached Search Engines foreach ($engines as $engine) { - $engine->setResultHash($this->getHashCode()); + $engine->setResultHash($this->getSearchUid()); } + $this->engines = $engines; } else { + if (sizeof($enabledSearchengines) > 0) { + $this->enabledSearchengines = $enabledSearchengines; + } $this->actuallyCreateSearchEngines($this->enabledSearchengines); } } @@ -688,31 +686,28 @@ class MetaGer $next = unserialize(Cache::get($request->input('next'))); $this->page = $next['page']; $engines = $next['engines']; - if (isset($next['startForwards'])) { - $this->startForwards = $next['startForwards']; - } - if (isset($next['startBackwards'])) { - $this->startBackwards = $next['startBackwards']; - } return $engines; } public function waitForMainResults() { + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); $engines = $this->engines; $enginesToWaitFor = []; - foreach ($engines as $engine) { - if ($engine->cached || !isset($engine->engine->main) || !$engine->engine->main) { - continue; + $mainEngines = $this->sumaFile->foki->{$this->fokus}->main; + foreach ($mainEngines as $mainEngine) { + foreach ($engines as $engine) { + if (!$engine->cached && $engine->name === $mainEngine) { + $enginesToWaitFor[] = $engine; + } } - $enginesToWaitFor[] = $engine; } $timeStart = microtime(true); $answered = []; $results = null; while (sizeof($enginesToWaitFor) > 0) { - $newEngine = Redis::blpop($this->redisResultWaitingKey, 5); + $newEngine = $redis->blpop($this->redisResultWaitingKey, 5); if ($newEngine === null || sizeof($newEngine) !== 2) { continue; } else { @@ -731,7 +726,7 @@ class MetaGer } # Now we can add an entry to Redis which defines the starting time and how many engines should answer this request - $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); + $pipeline = $redis->pipeline(); $pipeline->hset($this->getRedisEngineResult() . "status", "startTime", $timeStart); $pipeline->hset($this->getRedisEngineResult() . "status", "engineCount", sizeof($engines)); @@ -1111,7 +1106,7 @@ class MetaGer if ($this->request->input('out', '') !== "results" && $this->request->input('out', '') !== '') { $requestData["out"] = $this->request->input('out'); } - $requestData['next'] = md5(serialize($this->next)); + $requestData['next'] = $this->getSearchUid(); $link = action('MetaGerSearch@search', $requestData); } else { $link = "#"; @@ -1207,6 +1202,11 @@ class MetaGer } } + public function setNext($next) + { + $this->next = $next; + } + public function addLink($link) { if (strpos($link, "http://") === 0) { @@ -1333,6 +1333,11 @@ class MetaGer return $this->verificationId; } + public function getNext() + { + return $this->next; + } + public function getVerificationCount() { return $this->verificationCount; diff --git a/app/Models/Quicktips/Quicktips.php b/app/Models/Quicktips/Quicktips.php index 4aca5151b..1b7b7e046 100644 --- a/app/Models/Quicktips/Quicktips.php +++ b/app/Models/Quicktips/Quicktips.php @@ -30,7 +30,9 @@ class Quicktips # TODO cache wieder einbauen (eventuell) if ( /*!Cache::has($hash)*/true) { - Redis::hset("search." . $this->hash . ".results." . self::QUICKTIP_NAME, "status", "waiting"); + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); + + $redis->hset("search." . $this->hash . ".results." . self::QUICKTIP_NAME, "status", "waiting"); // Queue this search $mission = $this->hash . ";" . base64_encode($url) . ";" . $max_time; @@ -81,10 +83,11 @@ class Quicktips public function retrieveResults($hash) { $body = ""; - $body = Redis::hget('search.' . $hash . ".results." . self::QUICKTIP_NAME, "response"); + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); + $body = $redis->hget('search.' . $hash . ".results." . self::QUICKTIP_NAME, "response"); - Redis::del('search.' . $hash . ".results." . self::QUICKTIP_NAME); - Redis::del('search.' . $hash . ".ready"); + $redis->del('search.' . $hash . ".results." . self::QUICKTIP_NAME); + $redis->del('search.' . $hash . ".ready"); if ($body !== "") { return $body; } else { diff --git a/app/Models/Searchengine.php b/app/Models/Searchengine.php index 66c486295..991da0595 100644 --- a/app/Models/Searchengine.php +++ b/app/Models/Searchengine.php @@ -106,9 +106,10 @@ abstract class Searchengine $this->cached = true; $this->retrieveResults($metager); } else { + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); // We will push the confirmation of the submission to the Result Hash - Redis::hset($metager->getRedisEngineResult() . $this->name, "status", "waiting"); - Redis::expire($metager->getRedisEngineResult() . $this->name, 60); + $redis->hset($metager->getRedisEngineResult() . $this->name, "status", "waiting"); + $redis->expire($metager->getRedisEngineResult() . $this->name, 60); // We need to submit a action that one of our workers can understand // The missions are submitted to a redis queue in the following string format @@ -191,10 +192,6 @@ abstract class Searchengine { foreach ($this->results as $result) { $result->rank($eingabe); - if (str_contains($this->engine->{"display-name"}, "Yahoo")) { - #die(var_dump($result)); - } - } } @@ -211,11 +208,12 @@ abstract class Searchengine } $body = ""; + $redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); if ($this->canCache && $this->cacheDuration > 0 && Cache::has($this->hash)) { $body = Cache::get($this->hash); - } elseif (Redis::hexists($metager->getRedisEngineResult() . $this->name, "response")) { - $body = Redis::hget($metager->getRedisEngineResult() . $this->name, "response"); + } elseif ($redis->hexists($metager->getRedisEngineResult() . $this->name, "response")) { + $body = $redis->hget($metager->getRedisEngineResult() . $this->name, "response"); if ($this->canCache && $this->cacheDuration > 0) { Cache::put($this->hash, $body, $this->cacheDuration); } @@ -251,13 +249,7 @@ abstract class Searchengine # Append the Query String $getString .= "&" . $this->engine->{"query-parameter"} . "=" . $this->urlEncode($query); -/* -die(var_dump($getString)); -# Affildata -if (strpos($getString, "<<AFFILDATA>>")) { -$getString = str_replace("<<AFFILDATA>>", $this->getOvertureAffilData($url), $getString); -}*/ return $getString; } diff --git a/resources/js/scriptResultPage.js b/resources/js/scriptResultPage.js index 9bfd5fc65..aea5a2efc 100644 --- a/resources/js/scriptResultPage.js +++ b/resources/js/scriptResultPage.js @@ -70,7 +70,7 @@ function loadMoreResults() { currentlyLoading = true; $.getJSON(updateUrl, function (data) { // Check if we can clear the interval (once every searchengine has answered) - if (data.engineDelivered == data.engineCount || data.timeWaiting > 5) { + if (data.engineDelivered == data.engineCount || data.timeWaiting > 15) { clearInterval(resultLoader); } // If there are new results we can add them @@ -79,22 +79,43 @@ function loadMoreResults() { var value = data.newResults[key]; // If there are more results than the given index we will prepend otherwise we will append the result - var results = $(".result:not(.ad)"); - if (typeof results[key] != "undefined") { - console.log("inserting before " + key); - $(results[key]).insertBefore(value); + if (!data.imagesearch) { + var results = $(".result:not(.ad)"); + if (key == 0) { + if ($(".result.ad").length > 0) { + $(value).insertAfter($($(".result.ad")[$(".result.ad").length - 1])); + } else { + $("#results").prepend(value); + } + } else if (typeof results[key] != "undefined") { + $(value).insertBefore($(results[key])); + } else if (typeof results[key - 1] != "undefined") { + $(value).insertAfter($(results[key - 1])); + } } else { - $(results[key - 1]).insertAfter(value); + var results = $(".image-container > .image"); + if (key == 0) { + $(".image-container").prepend(value); + } else if (typeof results[key] != "undefined") { + $(value).insertBefore($(results[key])); + } else if (typeof results[key - 1] != "undefined") { + $(value).insertAfter($(results[key - 1])); + } + } + } + if ($(".no-results-error").length > 0 && $(".image-container > .image").length > 0) { + $(".no-results-error").remove(); + if ($(".alert.alert-danger > ul").children().length == 0) { + $(".alert.alert-danger").remove(); } - - console.log(key + "=>" + value); } + console.log(data); } currentlyLoading = false; }); } }, 1000); - + //clearInterval(resultLoader); console.log(updateUrl); } \ No newline at end of file diff --git a/resources/views/layouts/image_result.blade.php b/resources/views/layouts/image_result.blade.php new file mode 100644 index 000000000..01c489413 --- /dev/null +++ b/resources/views/layouts/image_result.blade.php @@ -0,0 +1,8 @@ +<div class="image"> + <a href="{{ $result->link }}" target="_blank"> + <div title="{{ $result->titel }}"> + <img src="{{ $metager->getImageProxyLink($result->image)}}" alt="{{ $result->titel }}"/> + <div>{{ $result->gefVon }}</div> + </div> + </a> +</div> diff --git a/resources/views/parts/errors.blade.php b/resources/views/parts/errors.blade.php index f40b8b0ef..cdd230715 100644 --- a/resources/views/parts/errors.blade.php +++ b/resources/views/parts/errors.blade.php @@ -3,7 +3,7 @@ <div class="alert alert-danger"> <ul> @foreach($errors as $error) - <li>{!! $error !!}</li> + <li @if($error === trans('metaGer.results.failed')) class="no-results-error" @endif>{!! $error !!}</li> @endforeach </ul> </div> diff --git a/resources/views/parts/pager.blade.php b/resources/views/parts/pager.blade.php index 5dd39e50c..5bb3011ca 100644 --- a/resources/views/parts/pager.blade.php +++ b/resources/views/parts/pager.blade.php @@ -3,7 +3,7 @@ <div @if($metager->getPage() === 1) class="disabled" @endif> <a @if($metager->getPage() !== 1) href="javascript:history.back()" @endif>{{ trans('results.zurueck') }}</a> </div> - <div @if($metager->nextSearchLink() === "#") class="disabled" @endif> + <div id="next-search-link" @if($metager->nextSearchLink() === "#") class="disabled" @endif> <a @if($metager->nextSearchLink() !== "#") href="{{ $metager->nextSearchLink() }}" @endif>{{ trans('results.weiter') }}</a> </div> -</nav> \ No newline at end of file +</nav> diff --git a/resources/views/resultpages/results_images.blade.php b/resources/views/resultpages/results_images.blade.php index 2eedcf651..78c4338e5 100644 --- a/resources/views/resultpages/results_images.blade.php +++ b/resources/views/resultpages/results_images.blade.php @@ -1,15 +1,6 @@ -@include('parts.errors') -@include('parts.warnings') <div id="container" class="image-container"> @foreach($metager->getResults() as $result) - <div class="image"> - <a href="{{ $result->link }}" target="_blank"> - <div title="{{ $result->titel }}"> - <img src="{{ $metager->getImageProxyLink($result->image)}}" alt="{{ $result->titel }}"/> - <div>{{ $result->gefVon }}</div> - </div> - </a> - </div> + @include('layouts.image_result', ['result' => $result]) @endforeach </div> @include('parts.pager') -- GitLab