Mejora en velocidad de busqueda

This commit is contained in:
Juan Pablo Vial
2024-02-28 21:44:37 -03:00
parent d9b5bc6507
commit 48bfe5d8ab
7 changed files with 269 additions and 38 deletions

View File

@ -1,4 +1,12 @@
<?php <?php
use Incoviba\Controller\API\Search; use Incoviba\Controller\API\Search;
$app->post('/search', [Search::class, 'query']); $app->group('/search', function($app) {
$app->group('/ventas', function($app) {
$app->post('/unidades', [Search::class, 'unidades']);
$app->get('/unidad/{unidad_id}', [Search::class, 'unidad']);
$app->post('[/]', [Search::class, 'ventas']);
});
$app->get('/venta/{venta_id}', [Search::class, 'venta']);
$app->post('[/]', [Search::class, 'query']);
});

View File

@ -96,6 +96,10 @@
id: '', id: '',
data: [], data: [],
table: null, table: null,
queues: {
unidades: [],
ventas: []
},
get: function() { get: function() {
return { return {
results: () => { results: () => {
@ -122,34 +126,33 @@
} }
const progress = this.draw().progress(data.results.length) const progress = this.draw().progress(data.results.length)
const promises = [] const promises = []
data.results.forEach(row => { this.queues.ventas = data.results.filter(row => row.tipo === 'venta').map(row => row.id)
if (row.tipo === 'venta') { this.queues.unidades = data.results.filter(row => row.tipo !== 'venta').map(row => row.id)
return promises.push(this.get().venta(row.id).then(json => { promises.push(this.get().ventas().then(arrays => {
if (json.venta === null) { arrays.forEach(json => {
console.debug(json) if (json.ventas.length === 0) {
return console.debug(json)
} return
const venta = json.venta }
json.ventas.forEach(venta => {
progress.progress('increment') progress.progress('increment')
const r = new Row({unidad: venta.propiedad.unidades[0], proyecto: venta.proyecto}) const r = new Row({unidad: venta.propiedad.unidades[0], proyecto: venta.proyecto})
r.venta = venta r.venta = venta
this.data.push(r) this.data.push(r)
}).catch(error => { })
})
}))
promises.push(this.get().unidades().then(arrays => {
arrays.forEach(json => {
if (json.unidades.length === 0) {
return
}
json.unidades.forEach(unidad => {
progress.progress('increment') progress.progress('increment')
console.error(row) this.data.push(new Row({unidad: unidad, proyecto: unidad.proyecto_tipo_unidad.proyecto}))
console.error(error) })
})) })
} }))
promises.push(this.get().unidad(row.id).then(json => {
const unidad = json.unidad
progress.progress('increment')
this.data.push(new Row({unidad: unidad, proyecto: unidad.proyecto_tipo_unidad.proyecto}))
}).catch(error => {
progress.progress('increment')
console.error(row)
console.error(error)
}))
})
Promise.all(promises).then(() => { Promise.all(promises).then(() => {
this.sort() this.sort()
this.draw().clear() this.draw().clear()
@ -157,22 +160,43 @@
}) })
}) })
}, },
unidad: id => { unidades: () => {
const url = '{{$urls->api}}/ventas/unidad/' + id const url = '{{$urls->api}}/search/ventas/unidades'
return fetchAPI(url).then(response => { const chunks = []
if (response.ok) { for (let i = 0; i < this.queues.unidades.length; i += 100) {
chunks.push(this.queues.unidades.slice(i, i + 100))
}
const promises = []
chunks.forEach(ids => {
const body = new FormData()
body.set('unidades', ids)
promises.push(fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json() return response.json()
} }))
}) })
return Promise.all(promises)
}, },
venta: id => { ventas: () => {
const url = '{{$urls->api}}/venta/' + id const url = '{{$urls->api}}/search/ventas'
return fetchAPI(url).then(response => { const chunks = []
if (!response) { for (let i = 0; i < this.queues.ventas.length; i += 100) {
return chunks.push(this.queues.ventas.slice(i, i + 100))
} }
return response.json() const promises = []
chunks.forEach(ids => {
const body = new FormData()
body.set('ventas', ids)
promises.push(fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json()
}))
}) })
return Promise.all(promises)
} }
} }
}, },

View File

@ -3,11 +3,13 @@ namespace Incoviba\Controller\API;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\withRedis;
use Incoviba\Common\Implement\Exception;
use Incoviba\Service; use Incoviba\Service;
class Search class Search
{ {
use withJson; use withJson, withRedis;
public function query(ServerRequestInterface $request, ResponseInterface $response, public function query(ServerRequestInterface $request, ResponseInterface $response,
Service\Search $service): ResponseInterface Service\Search $service): ResponseInterface
@ -17,4 +19,155 @@ class Search
$output = compact('results'); $output = compact('results');
return $this->withJson($response, $output); return $this->withJson($response, $output);
} }
public function unidad(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Unidad $unidadService, int $unidad_id): ResponseInterface
{
$output = [
'unidad_id' => $unidad_id,
'unidad' => null
];
$redisKey = "search:unidad:{$unidad_id}";
try {
$output['unidad'] = $this->fetchRedis($redisService, $redisKey);
} catch (Exception\EmptyRedis) {
try {
$unidad = $unidadService->getByIdForSearch($unidad_id);
$output['unidad'] = [
'id' => $unidad['id'],
'descripcion' => $unidad['descripcion'],
'proyecto_tipo_unidad' => [
'proyecto' => [
'id' => $unidad['proyecto_id'],
'descripcion' => $unidad['proyecto_descripcion']
],
'tipo_unidad' => [
'descripcion' => $unidad['tipo_unidad_descripcion']
],
'superficie' => $unidad['superficie']
],
'current_precio' => [
'valor' => $unidad['precio']
]
];
$this->saveRedis($redisService, $redisKey, $output['unidad']);
} catch (Exception\EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function unidades(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta\Unidad $unidadService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'unidades' => []
];
$unidades = explode(',', $body['unidades']);
foreach ($unidades as $unidad_id) {
$redisKey = "search:unidad:{$unidad_id}";
try {
$output['unidades'] []= $this->fetchRedis($redisService, $redisKey);
} catch (Exception\EmptyRedis) {
try {
$unidad = $unidadService->getByIdForSearch($unidad_id);
$unidad = [
'id' => $unidad['id'],
'descripcion' => $unidad['descripcion'],
'proyecto_tipo_unidad' => [
'proyecto' => [
'id' => $unidad['proyecto_id'],
'descripcion' => $unidad['proyecto_descripcion']
],
'tipo_unidad' => [
'descripcion' => $unidad['tipo_unidad_descripcion']
],
'superficie' => $unidad['superficie']
],
'current_precio' => [
'valor' => $unidad['precio']
]
];
$output['unidades'] []= $unidad;
$this->saveRedis($redisService, $redisKey, $unidad);
} catch (Exception\EmptyResult) {}
}
}
return $this->withJson($response, $output);
}
public function venta(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService, int $venta_id): ResponseInterface
{
$output = [
'venta_id' => $venta_id,
'venta' => null
];
$redisKey = "search:venta:{$venta_id}";
try {
$output['venta'] = $this->fetchRedis($redisService, $redisKey);
} catch (Exception\EmptyRedis) {
try {
$venta = $ventaService->getById($venta_id);
/*$output['venta'] = [
'id' => $venta->id,
''
];*/
$output['venta'] = $venta;
$this->saveRedis($redisService, $redisKey, $output['venta']);
} catch (Exception\EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function ventas(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Venta $ventaService): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'ventas' => []
];
$ventas = explode(',', $body['ventas']);
foreach ($ventas as $venta_id) {
$redisKey = "search:venta:{$venta_id}";
try {
$output['ventas'] []= $this->fetchRedis($redisService, $redisKey);
} catch (Exception\EmptyRedis) {
try {
$venta = $ventaService->getByIdForSearch($venta_id);
$venta = [
'id' => $venta['id'],
'proyecto' => [
'id' => $venta['proyecto_id'],
'descripcion' => $venta['proyecto_descripcion']
],
'propietario' => [
'nombre_completo' => $venta['propietario']
],
'propiedad' => [
'unidades' => [
[
'descripcion' => $venta['unidad_descripcion'],
'proyecto_tipo_unidad' => [
'tipo_unidad' => [
'descripcion' => $venta['tipo_unidad_descripcion']
],
'superficie' => $venta['superficie']
]
]
]
],
'fecha' => $venta['fecha'],
'current_estado' => [
'tipo_estado_venta' => [
'activa' => $venta['activa']
]
],
'valor' => $venta['valor']
];
$output['ventas'] []= $venta;
$this->saveRedis($redisService, $redisKey, json_encode($venta));
} catch (Exception\EmptyResult) {}
}
}
return $this->withJson($response, $output);
}
} }

View File

@ -298,7 +298,6 @@ class Venta extends Ideal\Repository
OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario OR CONCAT_WS(' ', `propietario`.`nombres`, `propietario`.`apellido_paterno`, `propietario`.`apellido_materno`) LIKE :propietario
OR rut = :rut OR rut = :rut
OR CONCAT_WS('-', rut, dv) = :rut"); OR CONCAT_WS('-', rut, dv) = :rut");
error_log($query.PHP_EOL,3,'/logs/debug');
return $this->fetchIds($query, [':propietario' => "%{$propietario}%", ':rut' => $propietario]); return $this->fetchIds($query, [':propietario' => "%{$propietario}%", ':rut' => $propietario]);
} }
public function fetchByPropietarioNombreCompleto(string $propietario): array public function fetchByPropietarioNombreCompleto(string $propietario): array
@ -356,6 +355,27 @@ class Venta extends Ideal\Repository
->where('bono_pie = ?'); ->where('bono_pie = ?');
return $this->fetchId($query, [$bono_id]); return $this->fetchId($query, [$bono_id]);
} }
public function fetchByIdForSearch(int $venta_id): array
{
$query = $this->connection->getQueryBuilder()
->select('venta.id AS id, venta.fecha AS fecha, venta.valor_uf AS valor')
->columns('proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion')
->columns('CONCAT_WS(" ", propietario.nombres, propietario.apellido_paterno, propietario.apellido_materno) AS propietario')
->columns('unidad.descripcion AS unidad_descripcion, tu.descripcion AS tipo_unidad_descripcion, ptu.m2 + ptu.logia + ptu.terraza AS superficie')
->columns('tev.activa')
->from($this->getTable())
->joined('JOIN propietario ON propietario.rut = venta.propietario')
->joined('JOIN propiedad_unidad pu ON pu.propiedad = venta.propiedad')
->joined('JOIN unidad ON unidad.id = pu.unidad')
->joined('JOIN proyecto_tipo_unidad ptu ON unidad.pt = ptu.id')
->joined('JOIN proyecto ON proyecto.id = ptu.proyecto')
->joined('JOIN tipo_unidad tu ON tu.id = ptu.tipo')
->joined('JOIN (SELECT ev1.* FROM estado_venta ev1 JOIN (SELECT MAX(id) AS id, venta FROM estado_venta GROUP BY venta) ev0 ON ev0.id = ev1.id) ev ON ev.venta = venta.id')
->joined('JOIN tipo_estado_venta tev ON ev.estado = tev.id')
->where('venta.id = ? AND tu.descripcion = "departamento"')
->group('venta.id');
return $this->connection->execute($query, [$venta_id])->fetch(PDO::FETCH_ASSOC);
}
protected function fetchIds(string $query, ?array $data = null): array protected function fetchIds(string $query, ?array $data = null): array
{ {

View File

@ -177,6 +177,24 @@ class Unidad extends Ideal\Repository
->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)"); ->where("a.`descripcion` LIKE ? AND tu.`descripcion` = ? AND (pu.`id` IS NULL OR `venta`.`id` IS NULL OR tev.`activa` = 0)");
return $this->connection->execute($query, [$descripcion, $tipo])->fetchAll(PDO::FETCH_ASSOC); return $this->connection->execute($query, [$descripcion, $tipo])->fetchAll(PDO::FETCH_ASSOC);
} }
public function fetchByIdForSearch(int $unidad_id): array
{
$query = $this->connection->getQueryBuilder()
->select('unidad.id AS id, unidad.descripcion AS descripcion')
->columns('proyecto.id AS proyecto_id, proyecto.descripcion AS proyecto_descripcion')
->columns('tu.descripcion AS tipo_unidad_descripcion')
->columns('ptu.m2 + ptu.logia + ptu.terraza AS superficie')
->columns('precio.valor AS precio')
->from($this->getTable())
->joined('JOIN proyecto_tipo_unidad ptu ON ptu.id = unidad.pt')
->joined('JOIN proyecto ON proyecto.id = ptu.proyecto')
->joined('JOIN tipo_unidad tu ON tu.id = ptu.tipo')
->joined('JOIN precio ON precio.unidad = unidad.id')
->joined('JOIN (SELECT ep1.* FROM estado_precio ep1 JOIN (SELECT MAX(id) AS id, precio FROM estado_precio GROUP BY precio) ep0 ON ep0.id = ep1.id) ep ON ep.precio = precio.id')
->where('unidad.id = ?')
->group('unidad.id');
return $this->connection->execute($query, [$unidad_id])->fetch(PDO::FETCH_ASSOC);
}
protected function joinProrrateo(): string protected function joinProrrateo(): string
{ {

View File

@ -75,6 +75,10 @@ class Venta extends Service
$ventas = $this->ventaRepository->fetchEscriturasByProyecto($proyecto_id); $ventas = $this->ventaRepository->fetchEscriturasByProyecto($proyecto_id);
return array_map([$this, 'process'], $ventas); return array_map([$this, 'process'], $ventas);
} }
public function getByIdForSearch(int $venta_id): array
{
return $this->ventaRepository->fetchByIdForSearch($venta_id);
}
protected function process(Model\Venta $venta): Model\Venta protected function process(Model\Venta $venta): Model\Venta
{ {

View File

@ -33,6 +33,10 @@ class Unidad
{ {
return $this->unidadRepository->fetchDisponiblesByProyecto($proyecto_id); return $this->unidadRepository->fetchDisponiblesByProyecto($proyecto_id);
} }
public function getByIdForSearch(int $unidad_id): array
{
return $this->unidadRepository->fetchByIdForSearch($unidad_id);
}
protected function process($unidad): Model\Venta\Unidad protected function process($unidad): Model\Venta\Unidad
{ {