Optimizacion de monedas

This commit is contained in:
Juan Pablo Vial
2024-05-16 21:23:24 -04:00
parent 256a3d2459
commit fa978728ce
5 changed files with 228 additions and 96 deletions

View File

@ -97,4 +97,45 @@ class Money
$output['ipc'] = $ipcService->get($start, $end);
return $this->withJson($response, $output);
}
public function getMany(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService,
Service\Money $moneyService): ResponseInterface
{
$input = $request->getParsedBody();
$output = [
'input' => $input,
'values' => []
];
$data = json_decode($input['dates']);
$ufs = [];
$ipcs = [];
try {
$ufs = (array) $this->fetchRedis($redisService, 'ufs');
} catch (EmptyRedis) {}
try {
$ipcs = (array) $this->fetchRedis($redisService, 'ipcs');
} catch (EmptyRedis) {}
foreach ($data as $date) {
if ($date->type === 'uf') {
if (!in_array($date->date, $ufs)) {
$uf = $moneyService->getUF(new DateTimeImmutable($date->date));
$ufs[$date->date] = $uf;
}
$date->value = $ufs[$date->date];
$output['values'] []= $date;
continue;
}
if ($date->type === 'ipc') {
$dateString = "{$date->start}|{$date->end}";
if (!in_array($dateString, $ipcs)) {
$ipc = $moneyService->getIPC(new DateTimeImmutable($date->start), new DateTimeImmutable($date->end));
$ipcs[$dateString] = $ipc;
}
$date->value = $ipcs[$dateString];
$output['values'] []= $date;
}
}
$this->saveRedis($redisService, 'ufs', $ufs, $this->time);
$this->saveRedis($redisService, 'ipcs', $ipcs, $this->time);
return $this->withJson($response, $output);
}
}