Change the logging of serial. #704

This commit is contained in:
Korina Cordero 2022-09-20 08:19:43 +00:00
parent 6314b4097d
commit f40155e1f5
3 changed files with 46 additions and 22 deletions

View file

@ -28,6 +28,7 @@ class LoadWarrantySerialCommand extends Command
protected $load_logger; protected $load_logger;
protected $project_dir; protected $project_dir;
protected $callback_url; protected $callback_url;
protected $log_data;
public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger, public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel, $callback_url) WarrantySerialLoadLogger $load_logger, KernelInterface $kernel, $callback_url)
@ -51,6 +52,7 @@ class LoadWarrantySerialCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$em = $this->em; $em = $this->em;
$this->log_data = [];
$status = 'pending'; $status = 'pending';
@ -78,6 +80,10 @@ class LoadWarrantySerialCommand extends Command
$this->updateWarrantySerialQueue($id); $this->updateWarrantySerialQueue($id);
} }
// error_log(print_r($this->log_data, true));
// load log data into db
$this->load_logger->logWarrantySerialLoadInfo($this->log_data);
// send results back to third party // send results back to third party
$this->sendResults($output_info); $this->sendResults($output_info);
@ -337,14 +343,16 @@ class LoadWarrantySerialCommand extends Command
protected function logLoadInfo($user_id, $is_loaded, $serial, $error) protected function logLoadInfo($user_id, $is_loaded, $serial, $error)
{ {
$log_data = [ $date_create = new DateTime();
'user_id' => $user_id, $str_date_create = $date_create->format('Y-m-d H:i:s');
'is_loaded' => $is_loaded,
'serial' => $serial,
'error' => $error,
];
$this->load_logger->logWarrantySerialLoadInfo($log_data); $this->log_data[] = [
$str_date_create,
$user_id,
$serial,
$is_loaded,
$error,
];
} }
protected function updateWarrantySerialQueue($id) protected function updateWarrantySerialQueue($id)
@ -384,7 +392,7 @@ class LoadWarrantySerialCommand extends Command
// error_log(print_r($body, true)); // error_log(print_r($body, true));
error_log('Sending json output to ' . $this->callback_url); // error_log('Sending json output to ' . $this->callback_url);
$curl = curl_init(); $curl = curl_init();
@ -404,7 +412,7 @@ class LoadWarrantySerialCommand extends Command
curl_close($curl); curl_close($curl);
// check result // check result
error_log('Result ' . $res); // error_log('Result ' . $res);
} }
} }

View file

@ -27,7 +27,7 @@ class TestController extends APIController
// return $res; // return $res;
$data = [ $data = [
'status' => 'Test successful.', 'result' => $res,
]; ];
return new APIResponse(true, 'Test successful.', $data); return new APIResponse(true, 'Test successful.', $data);
} }

View file

@ -4,7 +4,7 @@ namespace App\Service;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\WarrantySerialLoadLog; use DateTime;
class WarrantySerialLoadLogger class WarrantySerialLoadLogger
{ {
@ -17,21 +17,37 @@ class WarrantySerialLoadLogger
public function logWarrantySerialLoadInfo($log_data) public function logWarrantySerialLoadInfo($log_data)
{ {
$log_entry = new WarrantySerialLoadLog(); // cache directory
$cache_dir = __DIR__ . '/../../var/cache';
$user_id = $log_data['user_id']; $file = $cache_dir . '/warranty_serial_load_log.tab';
$is_loaded = $log_data['is_loaded']; error_log('opening file for warranty serial load log - ' . $file);
$error = $log_data['error'];
$serial = $log_data['serial'];
$log_entry->setApiUser($user_id) $fp = fopen($file, 'w');
->setSerial($serial) if ($fp === false)
->setLoaded($is_loaded) {
->setError($error); error_log('could not open file for load data infile - ' . $file);
}
else
{
foreach ($log_data as $key => $data)
{
$line = implode('|', $data) . "\r\n";
fwrite($fp, $line);
}
}
$this->em->persist($log_entry); fclose($fp);
$this->em->flush();
// prepared statement
$db = $this->em->getConnection();
$stmt = $db->prepare('LOAD DATA LOCAL INFILE \''. $file . '\' INTO TABLE warranty_serial_load_log FIELDS TERMINATED BY \'|\' LINES TERMINATED BY \'\\r\\n\' (date_create, api_user, serial, flag_loaded, error)');
$result = $stmt->execute();
if (!$result)
error_log('Failed loading data.');
// TODO: delete file?
} }
} }