Log query analytics

This commit is contained in:
David Bomba 2021-04-29 09:19:00 +10:00
parent 61828d412a
commit f26948b76f
3 changed files with 83 additions and 19 deletions

View File

@ -0,0 +1,65 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\DataMapper\Analytics;
class DbQuery
{
/**
* The type of Sample.
*
* Monotonically incrementing counter
*
* - counter
*
* @var string
*/
public $type = 'mixed_metric';
/**
* The name of the counter.
* @var string
*/
public $name = 'db.queries';
/**
* The datetime of the counter measurement.
*
* date("Y-m-d H:i:s")
*
* @var DateTime
*/
public $datetime;
/**
* The Class failure name
* set to 0.
*
* @var string
*/
public $string_metric5 = 'uri';
public $string_metric6 = 'uri';
/**
* The counter
* set to 1.
*
* @var string
*/
public $int_metric1 = 1;
public $double_metric2 = 1;
public function __construct($string_metric5, $string_metric6, $int_metric1, $double_metric2) {
$this->string_metric5 = $string_metric5;
$this->double_metric2 = $double_metric2;
}
}

View File

@ -11,10 +11,13 @@
namespace App\Http\Middleware; namespace App\Http\Middleware;
use App\DataMapper\Analytics\DbQuery;
use App\Utils\Ninja;
use Closure; use Closure;
use DB; use DB;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Log; use Log;
use Turbo124\Beacon\Facades\LightLogs;
/** /**
* Class QueryLogging. * Class QueryLogging.
@ -31,32 +34,33 @@ class QueryLogging
*/ */
public function handle(Request $request, Closure $next) public function handle(Request $request, Closure $next)
{ {
$timeStart = microtime(true);
// Enable query logging for development // Enable query logging for development
if (config('ninja.app_env') == 'production') { if (!Ninja::isHosted() || !config('beacon.enabled')) {
return $next($request); return $next($request);
} }
$timeStart = microtime(true);
DB::enableQueryLog(); DB::enableQueryLog();
$response = $next($request); $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 $queries = DB::getQueryLog();
if (strstr($request->url(), '_debugbar') === false) { $count = count($queries);
$queries = DB::getQueryLog(); $timeEnd = microtime(true);
$count = count($queries); $time = $timeEnd - $timeStart;
$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) // if($count > 50)
//nlog($queries); //nlog($queries);
}
LightLogs::create(new DbQuery($request->method(), $request->url(), $count, $time))
->batch();
} }
return $response; return $response;
} }

View File

@ -249,19 +249,14 @@ class Invoice extends BaseModel
$partial_due_date = $this->partial_due_Date ? Carbon::parse($this->partial_due_date) : false; $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())) { if ($this->status_id == self::STATUS_SENT && $due_date && $due_date->gt(now())) {
nlog("1 unpaid");
return self::STATUS_UNPAID; return self::STATUS_UNPAID;
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->gt(now())) { } elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->gt(now())) {
nlog("2 partial");
return self::STATUS_PARTIAL; return self::STATUS_PARTIAL;
} elseif ($this->status_id == self::STATUS_SENT && $due_date && $due_date->lt(now())) { } elseif ($this->status_id == self::STATUS_SENT && $due_date && $due_date->lt(now())) {
nlog("3 overdue");
return self::STATUS_OVERDUE; return self::STATUS_OVERDUE;
} elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->lt(now())) { } elseif ($this->status_id == self::STATUS_PARTIAL && $partial_due_date && $partial_due_date->lt(now())) {
nlog("4 overdue");
return self::STATUS_OVERDUE; return self::STATUS_OVERDUE;
} else { } else {
nlog("status id ");
return $this->status_id; return $this->status_id;
} }
} }