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

Resultloader working again. Needs some tests

parent 0a6f5246
......@@ -18,7 +18,7 @@ class CacheHelper
'key' => $key,
'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
while ($this->shouldRun) {
$cacheItem = Redis::blpop(self::CACHER_QUEUE, 1);
if (!empty($cacheItem)) {
$cacheItem = json_decode($cacheItem[1], true);
if (empty($cacheItem["body"])) {
$cacheItem["body"] = "no-result";
$cacheItem = unserialize(base64_decode($cacheItem[1]));
if (empty($cacheItem["value"])) {
$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
'key' => $resulthash,
'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"]);
}
......
......@@ -6,7 +6,6 @@ use App;
use App\MetaGer;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use LaravelLocalization;
use View;
......@@ -64,14 +63,7 @@ class MetaGerSearch extends Controller
# Ergebnisse der Suchmaschinen kombinieren:
$metager->prepareResults();
# Save the results in Redis
$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();
\App\CacheHelper::put($metager->getSearchUid(), $metager->getEngines(), 1 * 60);
# Die Ausgabe erstellen:
$resultpage = $metager->createView($quicktipResults);
......@@ -106,83 +98,66 @@ class MetaGerSearch extends Controller
# Create a MetaGer Instance with the supplied hash
$hash = $request->input('loadMore', '');
$metager = new MetaGer($hash);
$redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));
$result = [];
# Check if there should be more results
$stats = $redis->hgetall($metager->getRedisEngineResult() . "status");
$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"]);
# Parser Skripte einhängen
$dir = app_path() . "/Models/parserSkripte/";
foreach (scandir($dir) as $filename) {
$path = $dir . $filename;
if (is_file($path)) {
require_once $path;
}
}
# Check if this request is not for page one
$metager->setEngines($request, $newEngines);
$engines = Cache::get($hash);
if ($engines === null) {
return response()->json(['finished' => true]);
}
# 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));
$results[$index]->new = false;
// Mark all engines that are already loaded
$finished = true;
foreach ($engines as $engine) {
if ($engine->loaded) {
$engine->setNew(false);
} else {
$finished = false;
$engine->setNew(true);
}
$metager->setResults($results);
$metager->retrieveResults();
$metager->rankAll();
$metager->prepareResults();
$result["nextSearchLink"] = $metager->nextSearchLink();
$results = $metager->getResults();
foreach ($results 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;
}
}
$metager = new MetaGer($hash);
$metager->parseFormData($request);
# Nach Spezialsuchen überprüfen:
$metager->checkSpecialSearches($request);
$metager->restoreEngines($engines);
$metager->retrieveResults();
$metager->rankAll();
$metager->prepareResults();
$result = [
'finished' => true,
'newResults' => [],
];
$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);
}
......
......@@ -1733,4 +1733,16 @@ class MetaGer
{
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
public $write_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 $new = true; # Important for loading results by JS
public function __construct($name, \stdClass $engine, MetaGer $metager)
{
......@@ -183,6 +184,7 @@ abstract class Searchengine
if ($body !== null) {
$this->loadResults($body);
$this->getNext($metager, $body);
$this->markNew();
$this->loaded = true;
return true;
} else {
......@@ -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
protected function generateGetString($query)
{
......@@ -243,4 +252,9 @@ abstract class Searchengine
{
return [];
}
public function setNew($new)
{
$this->new = $new;
}
}
......@@ -59,9 +59,6 @@ function loadMoreResults() {
updateUrl = updateUrl.replace("/meta.ger3", "/loadMore");
var expectedEngines = -1;
var deliveredEngines = -1;
var currentlyLoading = false;
// Regularily check for not yet delivered Results
......@@ -70,7 +67,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 > 15) {
if (!data || data.finished) {
clearInterval(resultLoader);
}
// If there are new results we can add them
......@@ -109,13 +106,9 @@ function loadMoreResults() {
$(".alert.alert-danger").remove();
}
}
console.log(data);
}
currentlyLoading = false;
});
}
}, 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