diff --git a/metager/app/Http/Controllers/MetaGerSearch.php b/metager/app/Http/Controllers/MetaGerSearch.php index 0d2b744e3ac66279f93f6ba48fda93c2f0d1726d..bce600ae93f87721e9b2ddb1d831aa45336b294b 100644 --- a/metager/app/Http/Controllers/MetaGerSearch.php +++ b/metager/app/Http/Controllers/MetaGerSearch.php @@ -11,11 +11,14 @@ use Illuminate\Support\Facades\Log; use Prometheus\CollectorRegistry; use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\App; +use App\QueryTimer; +use App\SearchSettings; class MetaGerSearch extends Controller { public function search(Request $request, MetaGer $metager, $timing = false) { + $query_timer = \app()->make(QueryTimer::class); $locale = LaravelLocalization::getCurrentLocale(); $preferredLanguage = array($request->getPreferredLanguage()); if (!empty($preferredLanguage) && !empty($locale)) { @@ -25,11 +28,6 @@ class MetaGerSearch extends Controller if ($request->filled("chrome-plugin")) { return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), "/plugin")); } - $timings = null; - if ($timing) { - $timings = ['starttime' => microtime(true)]; - } - $time = microtime(true); $focus = $request->input("focus", "web"); @@ -45,16 +43,14 @@ class MetaGerSearch extends Controller } # Mit gelieferte Formulardaten parsen und abspeichern: + $query_timer->observeStart("Search_ParseFormData"); $metager->parseFormData($request); - if (!empty($timings)) { - $timings["parseFormData"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_ParseFormData"); # Nach Spezialsuchen überprüfen: + $query_timer->observeStart("Search_CheckSpecialSearches"); $metager->checkSpecialSearches($request); - if (!empty($timings)) { - $timings["checkSpecialSearches"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_CheckSpecialSearches"); # Search query can be empty after parsing the formdata # we will cancel the search in that case and show an error to the user @@ -62,47 +58,50 @@ class MetaGerSearch extends Controller return $metager->createView(); } + $query_timer->observeStart("Search_CreateQuicktips"); $quicktips = $metager->createQuicktips(); - if (!empty($timings)) { - $timings["createQuicktips"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_CreateQuicktips"); # Suche für alle zu verwendenden Suchmaschinen als Job erstellen, # auf Ergebnisse warten und die Ergebnisse laden - $metager->createSearchEngines($request, $timings); + $query_timer->observeStart("Search_CreateSearchEngines"); + $metager->createSearchEngines($request); + $query_timer->observeEnd("Search_CreateSearchEngines"); - $metager->startSearch($timings); + $query_timer->observeStart("Search_StartSearch"); + $metager->startSearch(); + $query_timer->observeEnd("Search_StartSearch"); # Versuchen die Ergebnisse der Quicktips zu laden if ($quicktips !== null) { + $query_timer->observeStart("Search_LoadQuicktips"); $quicktipResults = $quicktips->loadResults(); - if (!empty($timings)) { - $timings["Loaded Quicktip Results"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_LoadQuicktips"); } else { $quicktipResults = []; } + $query_timer->observeStart("Search_WaitForMainResults"); $metager->waitForMainResults(); - if (!empty($timings)) { - $timings["waitForMainResults"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_WaitForMainResults"); + $query_timer->observeStart("Search_RetrieveResults"); $metager->retrieveResults(); - if (!empty($timings)) { - $timings["retrieveResults"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_RetrieveResults"); # Alle Ergebnisse vor der Zusammenführung ranken: + $query_timer->observeStart("Search_RankAll"); $metager->rankAll(); - if (!empty($timings)) { - $timings["rankAll"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_RankAll"); # Ergebnisse der Suchmaschinen kombinieren: - $metager->prepareResults($timings); + $query_timer->observeStart("Search_PrepareResults"); + $metager->prepareResults(); + $query_timer->observeEnd("Search_PrepareResults"); $admitad = []; $adgoal = []; + + $query_timer->observeStart("Search_Affiliates"); if (!$metager->isApiAuthorized() && !$metager->isDummy()) { $newAdmitad = new \App\Models\Admitad($metager); if (!empty($newAdmitad->hash)) { @@ -121,14 +120,15 @@ class MetaGerSearch extends Controller if (!$metager->isApiAuthorized() && !$metager->isDummy()) { $metager->addDonationAdvertisement(); } + $query_timer->observeEnd("Search_Affiliates"); - $finished = true; foreach ($metager->getEngines() as $engine) { if ($engine->loaded) { $engine->setNew(false); $engine->markNew(); } } + $query_timer->observeStart("Search_CacheFiller"); try { Cache::put("loader_" . $metager->getSearchUid(), [ "metager" => [ @@ -141,21 +141,11 @@ class MetaGerSearch extends Controller } catch (\Exception $e) { Log::error($e->getMessage()); } - if (!empty($timings)) { - $timings["Filled resultloader Cache"] = microtime(true) - $time; - } + $query_timer->observeEnd("Search_CacheFiller"); # Die Ausgabe erstellen: $resultpage = $metager->createView($quicktipResults); - if (!empty($timings)) { - $timings["createView"] = microtime(true) - $time; - } - - if ($timings) { - dd($timings); - } - $registry = CollectorRegistry::getDefault(); $counter = $registry->getOrRegisterCounter('metager', 'result_counter', 'counts total number of returned results', []); $counter->incBy(sizeof($metager->getResults())); @@ -169,8 +159,7 @@ class MetaGerSearch extends Controller echo ($responsePart); flush(); } - $requestTime = microtime(true) - $time; - \App\PrometheusExporter::Duration($requestTime, "request_time"); + $query_timer->observeTotal(); } public function searchTimings(Request $request, MetaGer $metager) @@ -196,7 +185,7 @@ class MetaGerSearch extends Controller private function loadMoreJS(Request $request) { - $request->request->add(["javascript" => true]); + \app()->make(SearchSettings::class)->javascript_enabled = true; # Create a MetaGer Instance with the supplied hash $hash = $request->input('loadMore', ''); @@ -223,7 +212,6 @@ class MetaGerSearch extends Controller $metager->setApiAuthorized($mg["apiAuthorized"]); $metager->parseFormData($request, false); - $metager->setJsEnabled(true); # Nach Spezialsuchen überprüfen: $metager->checkSpecialSearches($request); $metager->restoreEngines($engines); diff --git a/metager/app/Http/Middleware/BrowserVerification.php b/metager/app/Http/Middleware/BrowserVerification.php index 470f82421580659835d2139bc83967372f5ff0af..5c8b4b22fee6e762c865d31ebff9b73dbec128e0 100644 --- a/metager/app/Http/Middleware/BrowserVerification.php +++ b/metager/app/Http/Middleware/BrowserVerification.php @@ -6,7 +6,10 @@ use Closure; use Illuminate\Support\Facades\Redis; use Jenssegers\Agent\Agent; use Illuminate\Http\Request; +use App\QueryTimer; use Cache; +use App\MetaGer; +use App\SearchSettings; class BrowserVerification { @@ -19,23 +22,26 @@ class BrowserVerification */ public function handle($request, Closure $next, $route = "resultpage") { - + \app()->make(QueryTimer::class)->observeStart(self::class); $bvEnabled = config("metager.metager.browserverification_enabled"); if (empty($bvEnabled) || !$bvEnabled) { + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } else { $whitelist = config("metager.metager.browserverification_whitelist"); $agent = new Agent(); foreach ($whitelist as $browser) { if ($agent->match($browser)) { + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } } } - if(($request->input("out", "") === "api" || $request->input("out", "") === "atom10") && app('App\Models\Key')->getStatus()) { + if (($request->input("out", "") === "api" || $request->input("out", "") === "atom10") && app('App\Models\Key')->getStatus()) { header('Content-type: application/xml; charset=utf-8'); - } elseif(($request->input("out", "") === "api" || $request->input("out", "") === "atom10") && !app('App\Models\Key')->getStatus()) { + } elseif (($request->input("out", "") === "api" || $request->input("out", "") === "atom10") && !app('App\Models\Key')->getStatus()) { + \app()->make(QueryTimer::class)->observeEnd(self::class); abort(403); } else { header('Content-type: text/html; charset=utf-8'); @@ -44,6 +50,7 @@ class BrowserVerification //use parameter for middleware to skip this when using associator if (($request->filled("loadMore") && Cache::has($request->input("loadMore"))) || app('App\Models\Key')->getStatus()) { + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } @@ -56,29 +63,37 @@ class BrowserVerification if (!empty($mgv)) { // Verify that key is a md5 checksum if (!preg_match("/^[a-f0-9]{32}$/", $mgv)) { + \app()->make(QueryTimer::class)->observeEnd(self::class); abort(404); } - $result = Redis::connection(config('cache.stores.redis.connection'))->blpop($mgv, 5); + $result = Redis::connection(config('cache.stores.redis.connection'))->blpop($mgv, 5); if ($result !== null) { - $request->request->add(["headerPrinted" => false, "jskey" => $mgv]); + $search_settings = \app()->make(SearchSettings::class); + $search_settings->jskey = $mgv; + $search_settings->header_printed = false; + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } else { # We are serving that request but log it for fail2ban self::logBrowserverification($request); + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } } $key = md5($request->ip() . microtime(true)); - echo(view('layouts.resultpage.verificationHeader')->with('key', $key)->render()); + echo (view('layouts.resultpage.verificationHeader')->with('key', $key)->render()); flush(); $answer = Redis::connection(config('cache.stores.redis.connection'))->blpop($key, 2); if ($answer !== null) { - echo(view('layouts.resultpage.resources')->render()); + echo (view('layouts.resultpage.resources')->render()); flush(); - $request->request->add(["headerPrinted" => true, "jskey" => $key]); + $search_settings = \app()->make(SearchSettings::class); + $search_settings->jskey = $key; + $search_settings->header_printed = true; + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } @@ -86,30 +101,33 @@ class BrowserVerification $params["mgv"] = $key; $url = route($route, $params); - echo(view('layouts.resultpage.unverifiedResultPage') - ->with('url', $url) - ->render()); + echo (view('layouts.resultpage.unverifiedResultPage') + ->with('url', $url) + ->render()); + \app()->make(QueryTimer::class)->observeEnd(self::class); + return $next($request); } - public static function logBrowserverification(Request $request) { + public static function logBrowserverification(Request $request) + { $fail2banEnabled = config("metager.metager.fail2ban.enabled"); - if(empty($fail2banEnabled) || !$fail2banEnabled || !config("metager.metager.fail2ban.url") || !config("metager.metager.fail2ban.user") || !config("metager.metager.fail2ban.password")){ + if (empty($fail2banEnabled) || !$fail2banEnabled || !config("metager.metager.fail2ban.url") || !config("metager.metager.fail2ban.user") || !config("metager.metager.fail2ban.password")) { return; } // Submit fetch job to worker $mission = [ - "resulthash" => "captcha", - "url" => config("metager.metager.fail2ban.url") . "/browserverification/", - "useragent" => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0", - "username" => config("metager.metager.fail2ban.user"), - "password" => config("metager.metager.fail2ban.password"), - "headers" => [ - "ip" => $request->ip() - ], - "cacheDuration" => 0, - "name" => "Captcha", - ]; + "resulthash" => "captcha", + "url" => config("metager.metager.fail2ban.url") . "/browserverification/", + "useragent" => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0", + "username" => config("metager.metager.fail2ban.user"), + "password" => config("metager.metager.fail2ban.password"), + "headers" => [ + "ip" => $request->ip() + ], + "cacheDuration" => 0, + "name" => "Captcha", + ]; $mission = json_encode($mission); Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission); } diff --git a/metager/app/Http/Middleware/HumanVerification.php b/metager/app/Http/Middleware/HumanVerification.php index cc8fdf7d75cb347063a27fa3109aa2dfe5036ace..71cb83f3545f4764bb6486f8d1fddc3909a4424c 100644 --- a/metager/app/Http/Middleware/HumanVerification.php +++ b/metager/app/Http/Middleware/HumanVerification.php @@ -7,6 +7,7 @@ use Closure; use Cookie; use Log; use URL; +use App\QueryTimer; class HumanVerification { @@ -19,7 +20,9 @@ class HumanVerification */ public function handle($request, Closure $next) { + \app()->make(QueryTimer::class)->observeStart(self::class); if ($request->filled("loadMore") && Cache::has($request->input("loadMore"))) { + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } @@ -33,14 +36,17 @@ class HumanVerification if (!empty($value) && intval($value) > 0) { Cache::decrement($token); + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } else { // Token is not valid. Remove it Cache::forget($token); - return redirect()->to(url()->current() . '?' . http_build_query($request->except(["token", "headerPrinted", "jskey"]))); + \app()->make(QueryTimer::class)->observeEnd(self::class); + return redirect()->to(url()->current() . '?' . http_build_query($request->except(["token"]))); } } else { - return redirect()->to(url()->current() . '?' . http_build_query($request->except(["token", "headerPrinted", "jskey"]))); + \app()->make(QueryTimer::class)->observeEnd(self::class); + return redirect()->to(url()->current() . '?' . http_build_query($request->except(["token"]))); } } @@ -73,6 +79,7 @@ class HumanVerification //use parameter for middleware to skip this when using associator if (!config("metager.metager.botprotection.enabled") || app('App\Models\Key')->getStatus()) { $update = false; + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } @@ -128,6 +135,7 @@ class HumanVerification # If the user is locked we will force a Captcha validation if ($user["locked"]) { \App\Http\Controllers\HumanVerification::logCaptcha($request); + \app()->make(QueryTimer::class)->observeEnd(self::class); return redirect()->route('captcha', ["id" => $id, "uid" => $uid, "url" => url()->full()]); } @@ -164,6 +172,7 @@ class HumanVerification } $request->request->add(['verification_id' => $user["uid"], 'verification_count' => $user["unusedResultPages"]]); + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } diff --git a/metager/app/Http/Middleware/RemoveKey.php b/metager/app/Http/Middleware/RemoveKey.php index a65dbf1daa5326666ace41380aa7260ef7f5e530..5aae84df712e076f24ec20abc30295b3261d57ff 100644 --- a/metager/app/Http/Middleware/RemoveKey.php +++ b/metager/app/Http/Middleware/RemoveKey.php @@ -4,6 +4,7 @@ namespace App\Http\Middleware; use Closure; use Cookie; +use \App\QueryTimer; class RemoveKey { @@ -16,10 +17,12 @@ class RemoveKey */ public function handle($request, Closure $next) { + \app()->make(QueryTimer::class)->observeStart(self::class); // Check if a wrong Key Cookie is set and if so remove it - if(Cookie::has("key") && app('App\Models\Key')->getStatus() === null){ + if (Cookie::has("key") && app('App\Models\Key')->getStatus() === null) { return redirect(route("removeCookie", ["ir" => url()->full()])); } + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } } diff --git a/metager/app/Http/Middleware/UserAgentMaster.php b/metager/app/Http/Middleware/UserAgentMaster.php index f19fdcd3a1091347e41e8cadeaa9e779b9151b6f..6be1c4a7b2ab9137f44230380acdf1933c02e4e9 100644 --- a/metager/app/Http/Middleware/UserAgentMaster.php +++ b/metager/app/Http/Middleware/UserAgentMaster.php @@ -4,6 +4,7 @@ namespace App\Http\Middleware; use Closure; use Illuminate\Support\Facades\Redis; +use App\QueryTimer; use Jenssegers\Agent\Agent; class UserAgentMaster @@ -21,6 +22,7 @@ class UserAgentMaster */ public function handle($request, Closure $next) { + \app()->make(QueryTimer::class)->observeStart(self::class); /** * Categorize the User-Agents by * 1. Platform (i.e. Ubuntu) @@ -65,6 +67,7 @@ class UserAgentMaster // Replace the User-Agent $_SERVER['HTTP_USER_AGENT'] = $newAgent; + \app()->make(QueryTimer::class)->observeEnd(self::class); return $next($request); } } diff --git a/metager/app/MetaGer.php b/metager/app/MetaGer.php index e33228ea073397c4e0dd6bdd3efa069324aad576..e39638982b8d850d8759f7cbab7e4e9116b66709 100644 --- a/metager/app/MetaGer.php +++ b/metager/app/MetaGer.php @@ -2,6 +2,7 @@ namespace App; +use App\Models\Searchengine; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Carbon\Carbon; @@ -254,14 +255,12 @@ class MetaGer } } - public function prepareResults(&$timings = null) + public function prepareResults() { $engines = $this->engines; // combine $this->combineResults($engines); - if (!empty($timings)) { - $timings["prepareResults"]["combined results"] = microtime(true) - $timings["starttime"]; - } + // misc (WiP) if ($this->fokus == "nachrichten") { $this->results = array_filter($this->results, function ($v, $k) { @@ -281,9 +280,7 @@ class MetaGer return ($a->getRank() < $b->getRank()) ? 1 : -1; }); } - if (!empty($timings)) { - $timings["prepareResults"]["sorted results"] = microtime(true) - $timings["starttime"]; - } + # Validate Results $newResults = []; foreach ($this->results as $result) { @@ -292,14 +289,9 @@ class MetaGer } } $this->results = $newResults; - if (!empty($timings)) { - $timings["prepareResults"]["validated results"] = microtime(true) - $timings["starttime"]; - } $this->duplicationCheck(); - if (!empty($timings)) { - $timings["prepareResults"]["duplications checked"] = microtime(true) - $timings["starttime"]; - } + # Validate Advertisements $newResults = []; foreach ($this->ads as $ad) { @@ -313,27 +305,23 @@ class MetaGer } $this->ads = $newResults; - if (!empty($timings)) { - $timings["prepareResults"]["validated ads"] = microtime(true) - $timings["starttime"]; - } + #Adgoal Implementation if (empty($this->adgoalLoaded)) { $this->adgoalLoaded = false; } - if (!empty($this->jskey)) { - $js = Redis::connection(config('cache.stores.redis.connection'))->lpop("js" . $this->jskey); + $search_settings = \app()->make(SearchSettings::class); + if (!empty($search_settings->jskey)) { + $js = Redis::connection(config('cache.stores.redis.connection'))->lpop("js" . $search_settings->jskey); if ($js !== null && boolval($js)) { - $this->javascript = true; + $search_settings->javascript_enabled = true; } } # Human Verification $this->humanVerification($this->results); $this->humanVerification($this->ads); - if (!empty($timings)) { - $timings["prepareResults"]["human verification"] = microtime(true) - $timings["starttime"]; - } $counter = 0; $firstRank = 0; @@ -354,9 +342,6 @@ class MetaGer 'engines' => $this->next, ]; Cache::put($this->getSearchUid(), serialize($this->next), 60 * 60); - if (!empty($timings)) { - $timings["prepareResults"]["filled cache"] = microtime(true) - $timings["starttime"]; - } } else { $this->next = []; } @@ -434,7 +419,7 @@ class MetaGer { $wait = false; $finished = true; - if (!$this->javascript) { + if (!\app()->make(SearchSettings::class)->javascript_enabled) { $wait = true; } foreach ($affiliates as $affiliate) { @@ -512,12 +497,8 @@ class MetaGer * Die Erstellung der Suchmaschinen bis die Ergebnisse da sind mit Unterfunktionen */ - public function createSearchEngines(Request $request, &$timings) + public function createSearchEngines(Request $request) { - if (!empty($timings)) { - $timings["createSearchEngines"]["start"] = microtime(true) - $timings["starttime"]; - } - # Wenn es kein Suchwort gibt if (!$request->filled("eingabe") || $this->q === "") { return; @@ -538,16 +519,8 @@ class MetaGer $sumas[$sumaName] = $this->sumaFile->sumas->{$sumaName}; } - if (!empty($timings)) { - $timings["createSearchEngines"]["created engine array"] = microtime(true) - $timings["starttime"]; - } - $this->removeAdsFromListIfAdfree($sumas); - if (!empty($timings)) { - $timings["createSearchEngines"]["removed ads"] = microtime(true) - $timings["starttime"]; - } - foreach ($sumas as $sumaName => $suma) { # Check if this engine is disabled and can't be used $disabled = empty($suma->disabled) ? false : $suma->disabled; @@ -601,10 +574,6 @@ class MetaGer } } - if (!empty($timings)) { - $timings["createSearchEngines"]["filtered invalid engines"] = microtime(true) - $timings["starttime"]; - } - # Include Yahoo Ads if Yahoo is not enabled as a searchengine if (!$this->apiAuthorized && $this->fokus != "bilder" && empty($this->enabledSearchengines["yahoo"]) && isset($this->sumaFile->sumas->{"yahoo-ads"})) { $this->enabledSearchengines["yahoo-ads"] = $this->sumaFile->sumas->{"yahoo-ads"}; @@ -629,9 +598,6 @@ class MetaGer $this->errors[] = $error; } $this->setEngines($request); - if (!empty($timings)) { - $timings["createSearchEngines"]["saved engines"] = microtime(true) - $timings["starttime"]; - } } private function removeAdsFromListIfAdfree(&$sumas) @@ -670,25 +636,15 @@ class MetaGer } } - public function startSearch(&$timings) + public function startSearch() { - if (!empty($timings)) { - $timings["startSearch"]["start"] = microtime(true) - $timings["starttime"]; - } - # Check all engines for Cached responses $this->checkCache(); - if (!empty($timings)) { - $timings["startSearch"]["cache checked"] = microtime(true) - $timings["starttime"]; - } - # Wir starten alle Suchen + /** @var Searchengine $engine */ foreach ($this->engines as $engine) { - $engine->startSearch($this, $timings); - } - if (!empty($timings)) { - $timings["startSearch"]["searches started"] = microtime(true) - $timings["starttime"]; + $engine->startSearch(); } } @@ -968,18 +924,6 @@ class MetaGer } $request->replace($input); } - $this->headerPrinted = $request->input("headerPrinted", false); - $request->request->remove("headerPrinted"); - - # Javascript option will be set by an asynchronious script we will check for it when we are fetching adgoal - # Until then javascript parameter will be false - $this->javascript = false; - if ($request->filled("javascript") && is_bool($request->input("javascript"))) { - $this->javascript = boolval($request->input("javascript")); - $request->request->remove("javascript"); - } - $this->jskey = $request->input('jskey', ''); - $request->request->remove("jskey"); $this->url = $request->url(); $this->fullUrl = $request->fullUrl(); @@ -1948,25 +1892,11 @@ class MetaGer return $this->framed; } - public function isHeaderPrinted() - { - return $this->headerPrinted; - } - public function isDummy() { return $this->dummy; } - public function jsEnabled() - { - return $this->javascript; - } - - public function setJsEnabled(bool $bool) - { - $this->javascript = $bool; - } /** * Used by JS result loader to restore MetaGer Object of previous request */ diff --git a/metager/app/Models/Adgoal.php b/metager/app/Models/Adgoal.php index 73418ce78377648cc05b5666e6ae93c0daffdc09..bdf4d427dab70e3c737417027fd7bd2830b2a08e 100644 --- a/metager/app/Models/Adgoal.php +++ b/metager/app/Models/Adgoal.php @@ -39,7 +39,6 @@ class Adgoal */ public function __construct(&$metager) { - $this->startTime = microtime(true); $publicKey = config("metager.metager.adgoal.public_key"); $privateKey = config("metager.metager.adgoal.private_key"); if ($publicKey === false) { @@ -210,8 +209,6 @@ class Adgoal } } - $requestTime = microtime(true) - $this->startTime; - \App\PrometheusExporter::Duration($requestTime, "adgoal"); $this->finished = true; } } diff --git a/metager/app/Models/Searchengine.php b/metager/app/Models/Searchengine.php index 31371721b8eb20f6b42e5bbb89a3eda75b54b002..0c1d97a7ff7bfdd915e9c321c7501689c2aa8048 100644 --- a/metager/app/Models/Searchengine.php +++ b/metager/app/Models/Searchengine.php @@ -114,17 +114,9 @@ abstract class Searchengine } # Prüft, ob die Suche bereits gecached ist, ansonsted wird sie als Job dispatched - public function startSearch(\App\MetaGer $metager, &$timings) + public function startSearch() { - if (!empty($timings)) { - $timings["startSearch"][$this->name]["start"] = microtime(true) - $timings["starttime"]; - } - if (!$this->cached) { - if (!empty($timings)) { - $timings["startSearch"][$this->name]["checked cache"] = microtime(true) - $timings["starttime"]; - } - // 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 // <ResultHash>;<URL to fetch> @@ -160,17 +152,11 @@ abstract class Searchengine // Since each Searcher is dedicated to one specific search engine // each Searcher has it's own queue lying under the redis key <name>.queue Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission); - if (!empty($timings)) { - $timings["startSearch"][$this->name]["pushed job"] = microtime(true) - $timings["starttime"]; - } // The request is not cached and will be submitted to the searchengine // We need to check if the number of requests to this engine are limited if (!empty($this->engine->{"monthly-requests"})) { Redis::incr("monthlyRequests:" . $this->name); - if (!empty($timings)) { - $timings["startSearch"][$this->name]["increased monthly requests"] = microtime(true) - $timings["starttime"]; - } } } } diff --git a/metager/app/Providers/MetaGerProvider.php b/metager/app/Providers/MetaGerProvider.php index aee981fe09bd69e47618f97717368f2c238982bc..d53480c8230bda32d67705f304070b29fb7b3a43 100644 --- a/metager/app/Providers/MetaGerProvider.php +++ b/metager/app/Providers/MetaGerProvider.php @@ -6,6 +6,9 @@ use App\MetaGer; use App\QueryLogger; use Illuminate\Support\Facades\App; use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Support\DeferrableProvider; +use App\QueryTimer; +use App\SearchSettings; class MetaGerProvider extends ServiceProvider { @@ -41,5 +44,22 @@ class MetaGerProvider extends ServiceProvider $this->app->singleton(QueryLogger::class, function ($app) { return new QueryLogger(); }); + + $this->app->singleton(QueryTimer::class, function ($app) { + return new QueryTimer(); + }); + + $this->app->singleton(SearchSettings::class, function ($app) { + return new SearchSettings(); + }); } + + // public function provides() + // { + // return [ + // MetaGer::class, + // QueryLogger::class, + // QueryTimer::class, + // ]; + // } } diff --git a/metager/app/QueryTimer.php b/metager/app/QueryTimer.php new file mode 100644 index 0000000000000000000000000000000000000000..8a7688a29754be279ca166ac3c3e3c939e72f94f --- /dev/null +++ b/metager/app/QueryTimer.php @@ -0,0 +1,54 @@ +<?php + +namespace App; + +use \Exception; + +class QueryTimer +{ + private $start_time; + + private $timings = []; + + public function __construct() + { + $this->start_time = microtime(true); + } + + /** + * Observes a start for a given name (Typically a function) + * It will store the name together with the current time + */ + public function observeStart(String $name) + { + if (!empty($this->timings[$name])) { + throw new Exception("Start Time for the event $name already registered"); + } + + $this->timings[$name]["start"] = microtime(true); + } + + /** + * Observes a end for a given name (Typically a function) + * It will store the name together with the current time + */ + public function observeEnd(String $name) + { + if (empty($this->timings[$name]["start"])) { + throw new Exception("Start Time for the event $name has not been registered yet"); + } + + $this->timings[$name]["end"] = microtime(true); + + PrometheusExporter::Duration($this->timings[$name]["end"] - $this->timings[$name]["start"], $name); + } + + /** + * Observes the total request time from start to finish + */ + public function observeTotal() + { + $total_time = \microtime(true) - $this->start_time; + PrometheusExporter::Duration($total_time, "Search_Total"); + } +} diff --git a/metager/app/SearchSettings.php b/metager/app/SearchSettings.php new file mode 100644 index 0000000000000000000000000000000000000000..bf575e3818cc534c45fa121aed1cceaed028f57e --- /dev/null +++ b/metager/app/SearchSettings.php @@ -0,0 +1,15 @@ +<?php + +namespace App; + +class SearchSettings +{ + + public $jskey = null; + public $javascript_enabled = false; + public $header_printed = false; + + public function __construct() + { + } +} diff --git a/metager/resources/views/layouts/resultPage.blade.php b/metager/resources/views/layouts/resultPage.blade.php index abcfa82d70e0d9c7b250fd16577e1c507585bef4..d3b75c462f1cf7ee430dfe9770700af1cd9b8fff 100644 --- a/metager/resources/views/layouts/resultPage.blade.php +++ b/metager/resources/views/layouts/resultPage.blade.php @@ -1,4 +1,4 @@ -@if(!$metager->isHeaderPrinted()) +@if(!\app()->make(\App\SearchSettings::class)->header_printed) <!DOCTYPE html> <html lang="{!! trans('staticPages.meta.language') !!}">