Redis service

This commit is contained in:
2023-10-19 20:46:52 -03:00
parent 742c0327c2
commit dc217d876a
10 changed files with 228 additions and 97 deletions

View File

@ -3,71 +3,94 @@ namespace Incoviba\Controller\API\Ventas;
use DateTimeImmutable;
use DateInterval;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Controller\API\withRedis;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyRedis;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Repository;
use Incoviba\Service;
class Cuotas
{
use withJson;
public function hoy(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
use withJson, withRedis;
public function hoy(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService): ResponseInterface
{
$today = new DateTimeImmutable();
$redisKey = "cuotas_hoy-{$today->format('Y-m-d')}";
$output = [
'cuotas' => 0
];
try {
$output['cuotas'] = count($cuotaRepository->fetchHoy());
} catch (EmptyResult) {}
$output['cuotas'] = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
try {
$output['cuotas'] = count($cuotaRepository->fetchHoy());
$this->saveRedis($redisService, $redisKey, $output['cuotas']);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function pendiente(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository): ResponseInterface
public function pendiente(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService): ResponseInterface
{
$today = new DateTimeImmutable();
$redisKey = "cuotas_pendientes-{$today->format('Y-m-d')}";
$output = [
'cuotas' => 0
];
try {
$output['cuotas'] = count($cuotaRepository->fetchPendientes());
} catch (EmptyResult) {}
$output['cuotas'] = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
try {
$output['cuotas'] = count($cuotaRepository->fetchPendientes());
$this->saveRedis($redisService, $redisKey, $output['cuotas']);
} catch (EmptyResult) {}
}
return $this->withJson($response, $output);
}
public function porVencer(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Format $formatService): ResponseInterface
public function porVencer(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Cuota $cuotaRepository, Service\Redis $redisService, Service\Format $formatService): ResponseInterface
{
$output = [];
$today = new DateTimeImmutable();
$redisKey = "cuotas_por_vencer-{$today->format('Y-m-d')}";
try {
$benchmark = [
'start' => microtime(true)
];
$cuotas = $cuotaRepository->fetchDatosPorVencer();
$benchmark['cuotas'] = microtime(true) - $benchmark['start'];
foreach ($cuotas as $row) {
$fecha = $row['Fecha'];
$date = new DateTimeImmutable($fecha);
if (($weekday = $date->format('N')) > 5) {
$day_diff = 7 - $weekday + 1;
$date = $date->add(new DateInterval("P{$day_diff}D"));
$fecha = $date->format('Y-m-d');
$output = $this->fetchRedis($redisService, $redisKey);
} catch (EmptyRedis) {
$output = [];
try {
$benchmark = [
'start' => microtime(true)
];
$cuotas = $cuotaRepository->fetchDatosPorVencer();
$benchmark['cuotas'] = microtime(true) - $benchmark['start'];
foreach ($cuotas as $row) {
$fecha = $row['Fecha'];
$date = new DateTimeImmutable($fecha);
if (($weekday = $date->format('N')) > 5) {
$day_diff = 7 - $weekday + 1;
$date = $date->add(new DateInterval("P{$day_diff}D"));
$fecha = $date->format('Y-m-d');
}
$key = $formatService->localDate($fecha, "EEE. dd 'de' MMMM 'de' yyyy", true);
if (!isset($output[$key])) {
$output[$key] = [];
}
if (!isset($output[$key][$row['Proyecto']])) {
$output[$key][$row['Proyecto']] = 0;
}
$output[$key][$row['Proyecto']] += $row['Cantidad'];
}
$key = $formatService->localDate($fecha, "EEE. dd 'de' MMMM 'de' yyyy", true);
if (!isset($output[$key])) {
$output[$key] = [];
foreach ($output as $key => $day) {
uksort($day, function($a, $b) {
return strcmp($a, $b);
});
$output[$key] = $day;
}
if (!isset($output[$key][$row['Proyecto']])) {
$output[$key][$row['Proyecto']] = 0;
}
$output[$key][$row['Proyecto']] += $row['Cantidad'];
}
foreach ($output as $key => $day) {
uksort($day, function($a, $b) {
return strcmp($a, $b);
});
$output[$key] = $day;
}
$benchmark['run'] = microtime(true) - $benchmark['start'];
error_log(var_export($benchmark,true));
} catch (EmptyResult) {}
$benchmark['run'] = microtime(true) - $benchmark['start'];
error_log(var_export($benchmark,true));
$this->saveRedis($redisService, $redisKey, $output);
} catch (EmptyResult) {}
}
return $this->withJson($response, ['cuotas' => $output]);
}
}