API optimizations

This commit is contained in:
Hillel Coren 2016-05-03 09:46:24 +03:00
parent 95fdda6e1b
commit 6cd4ff025e
10 changed files with 38 additions and 17 deletions

View File

@ -3,6 +3,7 @@
use Session; use Session;
use Utils; use Utils;
use Auth; use Auth;
use Log;
use Input; use Input;
use Response; use Response;
use Request; use Request;
@ -66,6 +67,10 @@ class BaseAPIController extends Controller
} else { } else {
$this->manager->setSerializer(new ArraySerializer()); $this->manager->setSerializer(new ArraySerializer());
} }
if (Utils::isNinjaDev()) {
\DB::enableQueryLog();
}
} }
protected function handleAction($request) protected function handleAction($request)
@ -82,7 +87,8 @@ class BaseAPIController extends Controller
protected function listResponse($query) protected function listResponse($query)
{ {
//\DB::enableQueryLog(); $query->with($this->getIncluded());
if ($clientPublicId = Input::get('client_id')) { if ($clientPublicId = Input::get('client_id')) {
$filter = function($query) use ($clientPublicId) { $filter = function($query) use ($clientPublicId) {
$query->where('public_id', '=', $clientPublicId); $query->where('public_id', '=', $clientPublicId);
@ -103,7 +109,6 @@ class BaseAPIController extends Controller
$data = $this->createCollection($query, $transformer, $this->entityType); $data = $this->createCollection($query, $transformer, $this->entityType);
//return \DB::getQueryLog();
return $this->response($data); return $this->response($data);
} }
@ -146,6 +151,11 @@ class BaseAPIController extends Controller
protected function response($response) protected function response($response)
{ {
if (Utils::isNinjaDev()) {
$count = count(\DB::getQueryLog());
Log::info(Request::method() . ' - ' . Request::url() . ": $count queries");
}
$index = Request::get('index') ?: 'data'; $index = Request::get('index') ?: 'data';
if ($index == 'none') { if ($index == 'none') {

View File

@ -44,7 +44,6 @@ class ClientApiController extends BaseAPIController
public function index() public function index()
{ {
$clients = Client::scope() $clients = Client::scope()
->with($this->getIncluded())
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->withTrashed(); ->withTrashed();

View File

@ -60,7 +60,7 @@ class InvoiceApiController extends BaseAPIController
{ {
$invoices = Invoice::scope() $invoices = Invoice::scope()
->withTrashed() ->withTrashed()
->with(array_merge(['invoice_items'], $this->getIncluded())) ->with('invoice_items')
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($invoices); return $this->listResponse($invoices);

View File

@ -49,7 +49,7 @@ class PaymentApiController extends BaseAPIController
{ {
$payments = Payment::scope() $payments = Payment::scope()
->withTrashed() ->withTrashed()
->with(array_merge(['client.contacts', 'invitation', 'user', 'invoice'], $this->getIncluded())) ->with(['client.contacts', 'invitation', 'user', 'invoice'])
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($payments); return $this->listResponse($payments);

View File

@ -42,7 +42,6 @@ class TaskApiController extends BaseAPIController
{ {
$payments = Task::scope() $payments = Task::scope()
->withTrashed() ->withTrashed()
->with($this->getIncluded())
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');
return $this->listResponse($payments); return $this->listResponse($payments);

View File

@ -49,7 +49,6 @@ class VendorApiController extends BaseAPIController
public function index() public function index()
{ {
$vendors = Vendor::scope() $vendors = Vendor::scope()
->with($this->getIncluded())
->withTrashed() ->withTrashed()
->orderBy('created_at', 'desc'); ->orderBy('created_at', 'desc');

View File

@ -58,7 +58,7 @@ class ClientTransformer extends EntityTransformer
public function includeInvoices(Client $client) public function includeInvoices(Client $client)
{ {
$transformer = new InvoiceTransformer($this->account, $this->serializer); $transformer = new InvoiceTransformer($this->account, $this->serializer, $client);
return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE); return $this->includeCollection($client->invoices, $transformer, ENTITY_INVOICE);
} }

View File

@ -6,9 +6,15 @@ use League\Fractal;
class ExpenseTransformer extends EntityTransformer class ExpenseTransformer extends EntityTransformer
{ {
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function transform(Expense $expense) public function transform(Expense $expense)
{ {
return [ return [
'id' => (int) $expense->public_id, 'id' => (int) $expense->public_id,
'private_notes' => $expense->private_notes, 'private_notes' => $expense->private_notes,
@ -25,7 +31,7 @@ class ExpenseTransformer extends EntityTransformer
'exchange_rate' => (float) $expense->exchange_rate, 'exchange_rate' => (float) $expense->exchange_rate,
'invoice_currency_id' => (int) $expense->invoice_currency_id, 'invoice_currency_id' => (int) $expense->invoice_currency_id,
'is_deleted' => (bool) $expense->is_deleted, 'is_deleted' => (bool) $expense->is_deleted,
'client_id' => isset($expense->client->public_id) ? (int) $expense->client->public_id : null, 'client_id' => $this->client ? $this->client->public_id : (isset($expense->client->public_id) ? (int) $expense->client->public_id : null),
'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null, 'invoice_id' => isset($expense->invoice->public_id) ? (int) $expense->invoice->public_id : null,
'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null, 'vendor_id' => isset($expense->vendor->public_id) ? (int) $expense->vendor->public_id : null,
]; ];

View File

@ -31,6 +31,13 @@ class InvoiceTransformer extends EntityTransformer
'expenses', 'expenses',
]; ];
public function __construct($account = null, $serializer = null, $client = null)
{
parent::__construct($account, $serializer);
$this->client = $client;
}
public function includeInvoiceItems(Invoice $invoice) public function includeInvoiceItems(Invoice $invoice)
{ {
$transformer = new InvoiceItemTransformer($this->account, $this->serializer); $transformer = new InvoiceItemTransformer($this->account, $this->serializer);
@ -45,7 +52,7 @@ class InvoiceTransformer extends EntityTransformer
public function includePayments(Invoice $invoice) public function includePayments(Invoice $invoice)
{ {
$transformer = new PaymentTransformer($this->account, $this->serializer); $transformer = new PaymentTransformer($this->account, $this->serializer, $invoice);
return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT); return $this->includeCollection($invoice->payments, $transformer, ENTITY_PAYMENT);
} }
@ -68,7 +75,7 @@ class InvoiceTransformer extends EntityTransformer
'id' => (int) $invoice->public_id, 'id' => (int) $invoice->public_id,
'amount' => (float) $invoice->amount, 'amount' => (float) $invoice->amount,
'balance' => (float) $invoice->balance, 'balance' => (float) $invoice->balance,
'client_id' => (int) $invoice->client->public_id, 'client_id' => (int) ($this->client ? $this->client->public_id : $invoice->client->public_id),
'invoice_status_id' => (int) $invoice->invoice_status_id, 'invoice_status_id' => (int) $invoice->invoice_status_id,
'updated_at' => $this->getTimestamp($invoice->updated_at), 'updated_at' => $this->getTimestamp($invoice->updated_at),
'archived_at' => $this->getTimestamp($invoice->deleted_at), 'archived_at' => $this->getTimestamp($invoice->deleted_at),

View File

@ -26,10 +26,11 @@ class PaymentTransformer extends EntityTransformer
]; ];
public function __construct(Account $account) public function __construct($account = null, $serializer = null, $invoice = null)
{ {
parent::__construct($account); parent::__construct($account, $serializer);
$this->invoice = $invoice;
} }
public function includeInvoice(Payment $payment) public function includeInvoice(Payment $payment)
@ -57,7 +58,7 @@ class PaymentTransformer extends EntityTransformer
'archived_at' => $this->getTimestamp($payment->deleted_at), 'archived_at' => $this->getTimestamp($payment->deleted_at),
'is_deleted' => (bool) $payment->is_deleted, 'is_deleted' => (bool) $payment->is_deleted,
'payment_type_id' => (int) $payment->payment_type_id, 'payment_type_id' => (int) $payment->payment_type_id,
'invoice_id' => (int) $payment->invoice->public_id, 'invoice_id' => (int) ($this->invoice ? $this->invoice->public_id : $payment->invoice->public_id),
]; ];
} }
} }