1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00

Support query counter in webapp as well as API

This commit is contained in:
Hillel Coren 2016-05-13 12:08:41 +03:00
parent 02b0840215
commit fba37171ae
3 changed files with 53 additions and 24 deletions

View File

@ -61,40 +61,36 @@ class BaseAPIController extends Controller
}
$this->serializer = Request::get('serializer') ?: API_SERIALIZER_ARRAY;
if ($this->serializer === API_SERIALIZER_JSON) {
$this->manager->setSerializer(new JsonApiSerializer());
} else {
$this->manager->setSerializer(new ArraySerializer());
}
if (Utils::isNinjaDev()) {
\DB::enableQueryLog();
}
}
protected function handleAction($request)
{
{
$entity = $request->entity();
$action = $request->action;
$repo = Utils::toCamelCase($this->entityType) . 'Repo';
$this->$repo->$action($entity);
return $this->itemResponse($entity);
}
protected function listResponse($query)
{
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$includes = $transformer->getDefaultIncludes();
$includes = $this->getRequestIncludes($includes);
$query->with($includes);
if ($updatedAt = Input::get('updated_at')) {
$updatedAt = date('Y-m-d H:i:s', $updatedAt);
$query->where(function($query) use ($includes, $updatedAt) {
@ -106,14 +102,14 @@ class BaseAPIController extends Controller
}
});
}
if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId);
};
$query->whereHas('client', $filter);
}
if ( ! Utils::hasPermission('view_all')){
if ($this->entityType == ENTITY_USER) {
$query->where('id', '=', Auth::user()->id);
@ -121,7 +117,7 @@ class BaseAPIController extends Controller
$query->where('user_id', '=', Auth::user()->id);
}
}
$data = $this->createCollection($query, $transformer, $this->entityType);
return $this->response($data);
@ -130,10 +126,10 @@ class BaseAPIController extends Controller
protected function itemResponse($item)
{
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
$data = $this->createItem($item, $transformer, $this->entityType);
return $this->response($data);
}
@ -160,18 +156,12 @@ class BaseAPIController extends Controller
} else {
$resource = new Collection($query, $transformer, $entityType);
}
return $this->manager->createData($resource)->toArray();
}
protected function response($response)
{
if (Utils::isNinjaDev()) {
$count = count(\DB::getQueryLog());
Log::info(Request::method() . ' - ' . Request::url() . ": $count queries");
Log::info(json_encode(\DB::getQueryLog()));
}
$index = Request::get('index') ?: 'data';
if ($index == 'none') {
@ -222,7 +212,7 @@ class BaseAPIController extends Controller
$data[] = $include;
}
}
return $data;
}
}

View File

@ -17,6 +17,7 @@ class Kernel extends HttpKernel {
'Illuminate\View\Middleware\ShareErrorsFromSession',
'App\Http\Middleware\VerifyCsrfToken',
'App\Http\Middleware\DuplicateSubmissionCheck',
'App\Http\Middleware\QueryLogging',
'App\Http\Middleware\StartupCheck',
];

View File

@ -0,0 +1,38 @@
<?php namespace App\Http\Middleware;
use DB;
use Log;
use Utils;
use Closure;
class QueryLogging
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// Enable query logging for development
if (Utils::isNinjaDev()) {
DB::enableQueryLog();
}
$response = $next($request);
if (Utils::isNinjaDev()) {
// hide requests made by debugbar
if (strstr($request->url(), '_debugbar') === false) {
$queries = DB::getQueryLog();
$count = count($queries);
Log::info($request->method() . ' - ' . $request->url() . ": $count queries");
//Log::info(json_encode($queries));
}
}
return $response;
}
}