From f26948b76f6abfb77588e77671d6dc5e40af8fe7 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 29 Apr 2021 09:19:00 +1000 Subject: [PATCH] Log query analytics --- app/DataMapper/Analytics/DbQuery.php | 65 ++++++++++++++++++++++++++++ app/Http/Middleware/QueryLogging.php | 32 ++++++++------ app/Models/Invoice.php | 5 --- 3 files changed, 83 insertions(+), 19 deletions(-) create mode 100644 app/DataMapper/Analytics/DbQuery.php diff --git a/app/DataMapper/Analytics/DbQuery.php b/app/DataMapper/Analytics/DbQuery.php new file mode 100644 index 000000000000..74a7470fa0a7 --- /dev/null +++ b/app/DataMapper/Analytics/DbQuery.php @@ -0,0 +1,65 @@ +string_metric5 = $string_metric5; + $this->double_metric2 = $double_metric2; + } +} diff --git a/app/Http/Middleware/QueryLogging.php b/app/Http/Middleware/QueryLogging.php index b898814f6855..034c13b2376a 100644 --- a/app/Http/Middleware/QueryLogging.php +++ b/app/Http/Middleware/QueryLogging.php @@ -11,10 +11,13 @@ namespace App\Http\Middleware; +use App\DataMapper\Analytics\DbQuery; +use App\Utils\Ninja; use Closure; use DB; use Illuminate\Http\Request; use Log; +use Turbo124\Beacon\Facades\LightLogs; /** * Class QueryLogging. @@ -31,32 +34,33 @@ class QueryLogging */ public function handle(Request $request, Closure $next) { - $timeStart = microtime(true); // Enable query logging for development - if (config('ninja.app_env') == 'production') { + if (!Ninja::isHosted() || !config('beacon.enabled')) { return $next($request); } + $timeStart = microtime(true); DB::enableQueryLog(); - $response = $next($request); - if (config('ninja.app_env') != 'production') { + // hide requests made by debugbar + if (strstr($request->url(), '_debugbar') === false) { - // hide requests made by debugbar - if (strstr($request->url(), '_debugbar') === false) { - $queries = DB::getQueryLog(); - $count = count($queries); - $timeEnd = microtime(true); - $time = $timeEnd - $timeStart; + $queries = DB::getQueryLog(); + $count = count($queries); + $timeEnd = microtime(true); + $time = $timeEnd - $timeStart; - nlog($request->method().' - '.$request->url().": $count queries - ".$time); + nlog($request->method().' - '.$request->url().": $count queries - ".$time); - // if($count > 50) - //nlog($queries); - } + // if($count > 50) + //nlog($queries); + + LightLogs::create(new DbQuery($request->method(), $request->url(), $count, $time)) + ->batch(); } + return $response; } diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index ab11b83696b3..d210552e2033 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -249,19 +249,14 @@ class Invoice extends BaseModel $partial_due_date = $this->partial_due_Date ? Carbon::parse($this->partial_due_date) : false; if ($this->status_id == self::STATUS_SENT && $due_date && $due_date->gt(now())) { - nlog("1 unpaid"); return self::STATUS_UNPAID; } elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->gt(now())) { - nlog("2 partial"); return self::STATUS_PARTIAL; } elseif ($this->status_id == self::STATUS_SENT && $due_date && $due_date->lt(now())) { - nlog("3 overdue"); return self::STATUS_OVERDUE; } elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->lt(now())) { - nlog("4 overdue"); return self::STATUS_OVERDUE; } else { - nlog("status id "); return $this->status_id; } }