Commit 6e3103bd authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Resultloader working again. Needs some tests

parent 0a6f5246
...@@ -18,7 +18,7 @@ class CacheHelper ...@@ -18,7 +18,7 @@ class CacheHelper
'key' => $key, 'key' => $key,
'value' => $value, 'value' => $value,
]; ];
Redis::rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, json_encode($cacherItem)); Redis::rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, base64_encode(serialize($cacherItem)));
} }
} }
...@@ -50,11 +50,11 @@ class RequestCacher extends Command ...@@ -50,11 +50,11 @@ class RequestCacher extends Command
while ($this->shouldRun) { while ($this->shouldRun) {
$cacheItem = Redis::blpop(self::CACHER_QUEUE, 1); $cacheItem = Redis::blpop(self::CACHER_QUEUE, 1);
if (!empty($cacheItem)) { if (!empty($cacheItem)) {
$cacheItem = json_decode($cacheItem[1], true); $cacheItem = unserialize(base64_decode($cacheItem[1]));
if (empty($cacheItem["body"])) { if (empty($cacheItem["value"])) {
$cacheItem["body"] = "no-result"; $cacheItem["value"] = "no-result";
} }
Cache::put($cacheItem["hash"], $cacheItem["body"], now()->addMinutes($cacheItem["cacheDuration"])); Cache::put($cacheItem["key"], $cacheItem["value"], now()->addSeconds($cacheItem["timeSeconds"]));
} }
} }
} }
......
...@@ -120,7 +120,7 @@ class RequestFetcher extends Command ...@@ -120,7 +120,7 @@ class RequestFetcher extends Command
'key' => $resulthash, 'key' => $resulthash,
'value' => $body, 'value' => $body,
]; ];
$pipe->rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, json_encode($cacherItem)); $pipe->rpush(\App\Console\Commands\RequestCacher::CACHER_QUEUE, base64_encode(serialize($cacherItem)));
}); });
\curl_multi_remove_handle($this->multicurl, $info["handle"]); \curl_multi_remove_handle($this->multicurl, $info["handle"]);
} }
......
...@@ -6,7 +6,6 @@ use App; ...@@ -6,7 +6,6 @@ use App;
use App\MetaGer; use App\MetaGer;
use Cache; use Cache;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use LaravelLocalization; use LaravelLocalization;
use View; use View;
...@@ -64,14 +63,7 @@ class MetaGerSearch extends Controller ...@@ -64,14 +63,7 @@ class MetaGerSearch extends Controller
# Ergebnisse der Suchmaschinen kombinieren: # Ergebnisse der Suchmaschinen kombinieren:
$metager->prepareResults(); $metager->prepareResults();
# Save the results in Redis \App\CacheHelper::put($metager->getSearchUid(), $metager->getEngines(), 1 * 60);
$redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));
$pipeline = $redis->pipeline();
foreach ($metager->getResults() as $result) {
$pipeline->rpush($metager->getRedisCurrentResultList(), base64_encode(serialize($result)));
}
$pipeline->expire($metager->getRedisCurrentResultList(), env('REDIS_RESULT_CACHE_DURATION'));
$pipeline->execute();
# Die Ausgabe erstellen: # Die Ausgabe erstellen:
$resultpage = $metager->createView($quicktipResults); $resultpage = $metager->createView($quicktipResults);
...@@ -106,83 +98,66 @@ class MetaGerSearch extends Controller ...@@ -106,83 +98,66 @@ class MetaGerSearch extends Controller
# Create a MetaGer Instance with the supplied hash # Create a MetaGer Instance with the supplied hash
$hash = $request->input('loadMore', ''); $hash = $request->input('loadMore', '');
$metager = new MetaGer($hash); # Parser Skripte einhängen
$redis = Redis::connection(env('REDIS_RESULT_CONNECTION')); $dir = app_path() . "/Models/parserSkripte/";
foreach (scandir($dir) as $filename) {
$result = []; $path = $dir . $filename;
# Check if there should be more results if (is_file($path)) {
$stats = $redis->hgetall($metager->getRedisEngineResult() . "status"); require_once $path;
$stats["startTime"] = floatval($stats["startTime"]);
$stats["engineCount"] = intval($stats["engineCount"]);
$stats["engineAnswered"] = intval($stats["engineAnswered"]);
$stats["engineDelivered"] = intval($stats["engineDelivered"]);
$result["finished"] = true;
$result["engineCount"] = $stats["engineCount"];
$result["engineAnswered"] = $stats["engineAnswered"];
$result["engineDelivered"] = $stats["engineDelivered"];
$result["timeWaiting"] = microtime(true) - $stats["startTime"];
# Check if we can abort
if ($stats["engineAnswered"] > $stats["engineDelivered"]/*&& $result["timeWaiting"] <= 10 */) {
$metager->parseFormData($request);
# Nach Spezialsuchen überprüfen:
$metager->checkSpecialSearches($request);
# Read which search engines are new
$newEngines = [];
while (($engine = $redis->lpop($metager->getRedisResultWaitingKey())) != null) {
$result["engineDelivered"]++;
$newEngines[$engine] = $metager->getSumaFile()->sumas->{$engine};
}
$cache = Cache::get($hash);
if ($cache != null) {
$metager->setNext(unserialize($cache)["engines"]);
} }
}
# Check if this request is not for page one $engines = Cache::get($hash);
$metager->setEngines($request, $newEngines); if ($engines === null) {
return response()->json(['finished' => true]);
}
# Add the results already delivered to the user // Mark all engines that are already loaded
$results = $redis->lrange($metager->getRedisCurrentResultList(), 0, -1); $finished = true;
foreach ($results as $index => $oldResult) { foreach ($engines as $engine) {
$results[$index] = unserialize(base64_decode($oldResult)); if ($engine->loaded) {
$results[$index]->new = false; $engine->setNew(false);
} else {
$finished = false;
$engine->setNew(true);
} }
$metager->setResults($results); }
$metager->retrieveResults();
$metager->rankAll(); $metager = new MetaGer($hash);
$metager->prepareResults();
$result["nextSearchLink"] = $metager->nextSearchLink(); $metager->parseFormData($request);
$results = $metager->getResults(); # Nach Spezialsuchen überprüfen:
foreach ($results as $index => $resultTmp) { $metager->checkSpecialSearches($request);
if ($resultTmp->new) { $metager->restoreEngines($engines);
if ($metager->getFokus() !== "bilder") {
$view = View::make('layouts.result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]); $metager->retrieveResults();
$html = $view->render(); $metager->rankAll();
$result['newResults'][$index] = $html; $metager->prepareResults();
$result["imagesearch"] = false;
} else { $result = [
$view = View::make('layouts.image_result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]); 'finished' => true,
$html = $view->render(); 'newResults' => [],
$result['newResults'][$index] = $html; ];
$result["imagesearch"] = true; $result["nextSearchLink"] = $metager->nextSearchLink();
}
foreach ($metager->getResults() as $index => $resultTmp) {
if ($resultTmp->new) {
if ($metager->getFokus() !== "bilder") {
$view = View::make('layouts.result', ['index' => $index, 'result' => $resultTmp, 'metager' => $metager]);
$html = $view->render();
$result['newResults'][$index] = $html;
$result["imagesearch"] = false;
} else {
$view = View::make('layouts.image_result', ['index' => $index, '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(), env('REDIS_RESULT_CACHE_DURATION'));
$pipeline->execute();
} }
// Update new Engines
\App\CacheHelper::put($metager->getSearchUid(), $metager->getEngines(), 1 * 60);
return response()->json($result); return response()->json($result);
} }
......
...@@ -1733,4 +1733,16 @@ class MetaGer ...@@ -1733,4 +1733,16 @@ class MetaGer
{ {
return $this->redisCurrentResultList; return $this->redisCurrentResultList;
} }
public function getEngines()
{
return $this->engines;
}
/**
* Used by JS result loader to restore MetaGer Object of previous request
*/
public function restoreEngines($engines)
{
$this->engines = $engines;
}
} }
...@@ -37,6 +37,7 @@ abstract class Searchengine ...@@ -37,6 +37,7 @@ abstract class Searchengine
public $write_time = 0; # Wird eventuell für Artefakte benötigt public $write_time = 0; # Wird eventuell für Artefakte benötigt
public $connection_time = 0; # Wird eventuell für Artefakte benötigt public $connection_time = 0; # Wird eventuell für Artefakte benötigt
public $cacheDuration = 60; # Wie lange soll das Ergebnis im Cache bleiben (Minuten) public $cacheDuration = 60; # Wie lange soll das Ergebnis im Cache bleiben (Minuten)
public $new = true; # Important for loading results by JS
public function __construct($name, \stdClass $engine, MetaGer $metager) public function __construct($name, \stdClass $engine, MetaGer $metager)
{ {
...@@ -183,6 +184,7 @@ abstract class Searchengine ...@@ -183,6 +184,7 @@ abstract class Searchengine
if ($body !== null) { if ($body !== null) {
$this->loadResults($body); $this->loadResults($body);
$this->getNext($metager, $body); $this->getNext($metager, $body);
$this->markNew();
$this->loaded = true; $this->loaded = true;
return true; return true;
} else { } else {
...@@ -190,6 +192,13 @@ abstract class Searchengine ...@@ -190,6 +192,13 @@ abstract class Searchengine
} }
} }
protected function markNew()
{
foreach ($this->results as $result) {
$result->new = $this->new;
}
}
# Erstellt den für die Get-Anfrage genutzten String # Erstellt den für die Get-Anfrage genutzten String
protected function generateGetString($query) protected function generateGetString($query)
{ {
...@@ -243,4 +252,9 @@ abstract class Searchengine ...@@ -243,4 +252,9 @@ abstract class Searchengine
{ {
return []; return [];
} }
public function setNew($new)
{
$this->new = $new;
}
} }
...@@ -59,9 +59,6 @@ function loadMoreResults() { ...@@ -59,9 +59,6 @@ function loadMoreResults() {
updateUrl = updateUrl.replace("/meta.ger3", "/loadMore"); updateUrl = updateUrl.replace("/meta.ger3", "/loadMore");
var expectedEngines = -1;
var deliveredEngines = -1;
var currentlyLoading = false; var currentlyLoading = false;
// Regularily check for not yet delivered Results // Regularily check for not yet delivered Results
...@@ -70,7 +67,7 @@ function loadMoreResults() { ...@@ -70,7 +67,7 @@ function loadMoreResults() {
currentlyLoading = true; currentlyLoading = true;
$.getJSON(updateUrl, function (data) { $.getJSON(updateUrl, function (data) {
// Check if we can clear the interval (once every searchengine has answered) // Check if we can clear the interval (once every searchengine has answered)
if (data.engineDelivered == data.engineCount || data.timeWaiting > 15) { if (!data || data.finished) {
clearInterval(resultLoader); clearInterval(resultLoader);
} }
// If there are new results we can add them // If there are new results we can add them
...@@ -109,13 +106,9 @@ function loadMoreResults() { ...@@ -109,13 +106,9 @@ function loadMoreResults() {
$(".alert.alert-danger").remove(); $(".alert.alert-danger").remove();
} }
} }
console.log(data);
} }
currentlyLoading = false; currentlyLoading = false;
}); });
} }
}, 1000); }, 1000);
//clearInterval(resultLoader);
console.log(updateUrl);
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment