2019-03-26 05:46:08 +01:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-05-11 05:32:07 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-05-11 05:32:07 +02:00
|
|
|
*/
|
2019-03-26 05:46:08 +01:00
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2021-04-29 01:19:00 +02:00
|
|
|
use App\DataMapper\Analytics\DbQuery;
|
|
|
|
use App\Utils\Ninja;
|
2019-03-26 05:46:08 +01:00
|
|
|
use Closure;
|
|
|
|
use Illuminate\Http\Request;
|
2022-01-30 00:46:39 +01:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-04-29 01:19:00 +02:00
|
|
|
use Turbo124\Beacon\Facades\LightLogs;
|
2019-03-26 05:46:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class QueryLogging.
|
|
|
|
*/
|
|
|
|
class QueryLogging
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2023-05-29 00:07:39 +02:00
|
|
|
|
2021-10-08 07:29:06 +02:00
|
|
|
// Enable query logging for development
|
2022-08-05 03:28:29 +02:00
|
|
|
if (! Ninja::isHosted() || ! config('beacon.enabled')) {
|
|
|
|
return $next($request);
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-05-28 23:58:00 +02:00
|
|
|
DB::enableQueryLog();
|
|
|
|
return $next($request);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function terminate($request, $response)
|
|
|
|
{
|
2023-10-26 04:57:44 +02:00
|
|
|
if (! Ninja::isHosted() || ! config('beacon.enabled')) {
|
2023-05-29 00:07:39 +02:00
|
|
|
return;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2019-03-26 05:46:08 +01:00
|
|
|
|
2021-04-29 01:19:00 +02:00
|
|
|
// hide requests made by debugbar
|
|
|
|
if (strstr($request->url(), '_debugbar') === false) {
|
2023-05-28 23:58:00 +02:00
|
|
|
|
2021-04-29 01:19:00 +02:00
|
|
|
$queries = DB::getQueryLog();
|
|
|
|
$count = count($queries);
|
|
|
|
$timeEnd = microtime(true);
|
2023-05-29 00:07:39 +02:00
|
|
|
$time = $timeEnd - LARAVEL_START;
|
2021-09-05 06:03:21 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($count > 175) {
|
|
|
|
nlog("Query count = {$count}");
|
|
|
|
nlog($queries);
|
|
|
|
}
|
|
|
|
|
2021-06-22 02:51:43 +02:00
|
|
|
$ip = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2023-05-28 23:58:00 +02:00
|
|
|
if ($request->hasHeader('Cf-Connecting-Ip')) {
|
|
|
|
$ip = $request->header('Cf-Connecting-Ip');
|
|
|
|
} elseif ($request->hasHeader('X-Forwarded-For')) {
|
|
|
|
$ip = $request->header('Cf-Connecting-Ip');
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2023-05-28 23:58:00 +02:00
|
|
|
$ip = $request->ip();
|
2021-06-22 02:51:43 +02:00
|
|
|
}
|
|
|
|
|
2024-05-19 11:43:43 +02:00
|
|
|
$client_version = $request->server('HTTP_USER_AGENT');
|
2024-05-19 11:42:20 +02:00
|
|
|
$platform = '';
|
|
|
|
|
|
|
|
if ($request->hasHeader('X-CLIENT-PLATFORM')) {
|
|
|
|
$platform = $request->header('X-CLIENT-PLATFORM');
|
2024-06-14 09:09:44 +02:00
|
|
|
} elseif($request->hasHeader('X-React')) {
|
2024-05-19 11:42:20 +02:00
|
|
|
$platform = 'react';
|
|
|
|
}
|
|
|
|
|
2024-06-14 09:09:44 +02:00
|
|
|
if ($request->hasHeader('X-CLIENT-VERSION')) {
|
2024-05-19 11:42:20 +02:00
|
|
|
$client_version = $request->header('X-CLIENT-VERSION');
|
|
|
|
}
|
|
|
|
|
|
|
|
LightLogs::create(new DbQuery($request->method(), substr(urldecode($request->url()), 0, 180), $count, $time, $ip, $client_version, $platform))
|
2023-05-28 23:58:00 +02:00
|
|
|
->batch();
|
2019-03-26 05:46:08 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2019-03-26 05:46:08 +01:00
|
|
|
}
|
|
|
|
}
|