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 $project_dir;
protected $callback_url;
protected $log_data;
public function __construct(EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger, KernelInterface $kernel, $callback_url)
@ -51,6 +52,7 @@ class LoadWarrantySerialCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
$this->log_data = [];
$status = 'pending';
@ -78,6 +80,10 @@ class LoadWarrantySerialCommand extends Command
$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
$this->sendResults($output_info);
@ -337,14 +343,16 @@ class LoadWarrantySerialCommand extends Command
protected function logLoadInfo($user_id, $is_loaded, $serial, $error)
{
$log_data = [
'user_id' => $user_id,
'is_loaded' => $is_loaded,
'serial' => $serial,
'error' => $error,
];
$date_create = new DateTime();
$str_date_create = $date_create->format('Y-m-d H:i:s');
$this->load_logger->logWarrantySerialLoadInfo($log_data);
$this->log_data[] = [
$str_date_create,
$user_id,
$serial,
$is_loaded,
$error,
];
}
protected function updateWarrantySerialQueue($id)
@ -384,7 +392,7 @@ class LoadWarrantySerialCommand extends Command
// 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();
@ -404,7 +412,7 @@ class LoadWarrantySerialCommand extends Command
curl_close($curl);
// check result
error_log('Result ' . $res);
// error_log('Result ' . $res);
}
}

View file

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

View file

@ -4,7 +4,7 @@ namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\WarrantySerialLoadLog;
use DateTime;
class WarrantySerialLoadLogger
{
@ -17,21 +17,37 @@ class WarrantySerialLoadLogger
public function logWarrantySerialLoadInfo($log_data)
{
$log_entry = new WarrantySerialLoadLog();
// cache directory
$cache_dir = __DIR__ . '/../../var/cache';
$user_id = $log_data['user_id'];
$is_loaded = $log_data['is_loaded'];
$error = $log_data['error'];
$serial = $log_data['serial'];
$file = $cache_dir . '/warranty_serial_load_log.tab';
error_log('opening file for warranty serial load log - ' . $file);
$log_entry->setApiUser($user_id)
->setSerial($serial)
->setLoaded($is_loaded)
->setError($error);
$fp = fopen($file, 'w');
if ($fp === false)
{
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);
$this->em->flush();
fclose($fp);
// 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?
}
}