Resolve "Warranty Serial Upload Improvements" #1635

Merged
korina.cordero merged 6 commits from 708-warranty-serial-upload-improvements into master 2022-11-10 06:45:47 +00:00
4 changed files with 106 additions and 23 deletions

View file

@ -0,0 +1,54 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\WarrantySerialQueue;
class CountTotalPendingWarrantySerialFilesCommand extends Command
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('warrantyserial:count')
->setDescription('Count number of pending warranty serial files.')
->setHelp('Count number of pending warranty serial files.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->em;
$status = 'pending';
$db = $em->getConnection();
$ws_query_sql = 'SELECT COUNT(*) AS total FROM warranty_serial_queue
WHERE status = :status';
$ws_query_stmt = $db->prepare($ws_query_sql);
$ws_query_stmt->bindValue('status', $status);
$ws_results = $ws_query_stmt->executeQuery();
$results = $ws_results->fetchAssociative();
$output->write($results['total']);
return 0;
}
}

View file

@ -7,6 +7,7 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Filesystem\Filesystem;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
@ -32,15 +33,18 @@ class LoadWarrantySerialCommand extends Command
protected $project_dir; protected $project_dir;
protected $callback_url; protected $callback_url;
protected $log_data; protected $log_data;
protected $filesystem;
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,
FileSystem $filesystem)
{ {
$this->em = $em; $this->em = $em;
$this->upload_logger = $upload_logger; $this->upload_logger = $upload_logger;
$this->load_logger = $load_logger; $this->load_logger = $load_logger;
$this->project_dir = $kernel->getProjectDir(); $this->project_dir = $kernel->getProjectDir();
$this->callback_url = $callback_url; $this->callback_url = $callback_url;
$this->filesystem = $filesystem;
parent::__construct(); parent::__construct();
} }
@ -62,8 +66,8 @@ class LoadWarrantySerialCommand extends Command
// get the filenames from the queue table with status pending // get the filenames from the queue table with status pending
$db = $em->getConnection(); $db = $em->getConnection();
$ws_query_sql = 'SELECT id, file_serial, file_id, api_user FROM warranty_serial_queue $ws_query_sql = 'SELECT id, file_serial, file_id, api_user, orig_file_serial FROM warranty_serial_queue
WHERE status = :status'; WHERE status = :status ORDER BY id LIMIT 1';
$ws_query_stmt = $db->prepare($ws_query_sql); $ws_query_stmt = $db->prepare($ws_query_sql);
$ws_query_stmt->bindValue('status', $status); $ws_query_stmt->bindValue('status', $status);
@ -77,23 +81,31 @@ class LoadWarrantySerialCommand extends Command
$user_id = $row['api_user']; $user_id = $row['api_user'];
$id = $row['id']; $id = $row['id'];
$file_id = $row['file_id']; $file_id = $row['file_id'];
$orig_filename = $row['orig_file_serial'];
$output_info[] = $this->processWarrantySerialFile($filename, $user_id, $file_id); $output_info[] = $this->processWarrantySerialFile($filename, $user_id, $file_id, $orig_filename);
// remove entry from queue table
$this->updateWarrantySerialQueue($id); $this->updateWarrantySerialQueue($id);
// delete the uploaded csv file and directory
$this->deleteDirectoryAndFile($file_id);
} }
// error_log(print_r($this->log_data, true)); if (count($output_info) > 0)
// load log data into db {
$this->load_logger->logWarrantySerialLoadInfo($this->log_data); // 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);
}
return 0; return 0;
} }
protected function processWarrantySerialFile($filename, $user_id, $file_id) protected function processWarrantySerialFile($filename, $user_id, $file_id, $orig_filename)
{ {
$csv_file = $this->project_dir . '/public/warranty_serial_uploads/' . $filename; $csv_file = $this->project_dir . '/public/warranty_serial_uploads/' . $filename;
@ -114,7 +126,7 @@ class LoadWarrantySerialCommand extends Command
]; ];
$this->upload_logger->logWarrantySerialUploadInfo($log_data); $this->upload_logger->logWarrantySerialUploadInfo($log_data);
$output_info = $this->setOutputInfo($filename, $file_id, true, $error, $data); $output_info = $this->setOutputInfo($filename, $file_id, true, $error, $data, $orig_filename);
return $output_info; return $output_info;
} }
@ -200,7 +212,7 @@ class LoadWarrantySerialCommand extends Command
} }
// form what we output // form what we output
$output_info = $this->setOutputInfo($filename, $file_id, false, '', $data); $output_info = $this->setOutputInfo($filename, $file_id, false, '', $data, $orig_filename, $orig_filename);
return $output_info; return $output_info;
} }
@ -391,25 +403,32 @@ class LoadWarrantySerialCommand extends Command
{ {
// prepared statement // prepared statement
$db = $this->em->getConnection(); $db = $this->em->getConnection();
$update_stmt = $db->prepare('UPDATE warranty_serial_queue SET status = :status
// lock the warranty serial queue table
$db->exec('LOCK TABLES warranty_serial_queue WRITE;');
$delete_stmt = $db->prepare('DELETE FROM warranty_serial_queue
WHERE id = :id'); WHERE id = :id');
$res = $update_stmt->execute([ $res = $delete_stmt->execute([
':status' => 'done',
':id' => $id, ':id' => $id,
]); ]);
$db->exec('UNLOCK TABLES;');
} }
protected function setOutputInfo($filename, $file_id, $has_error, $error_message, $entries) protected function deleteDirectoryAndFile($filedir)
{ {
// need to get the original filename from warranty_serial_queue $csv_filedir = $this->project_dir . '/public/warranty_serial_uploads/' . $filedir;
// use the uploaded_file_serial which has the saved csv file
$upload_entry = $this->em->getRepository(WarrantySerialQueue::class)->findOneBy(['file_id' => $file_id]);
$original_filename = $upload_entry->getOrigFileSerial();
$this->filesystem->remove($csv_filedir);
}
protected function setOutputInfo($filename, $file_id, $has_error, $error_message, $entries, $orig_filename)
{
$info = [ $info = [
'id' => $file_id, 'id' => $file_id,
'filename' => $original_filename, 'filename' => $orig_filename,
'has_error' => $has_error, 'has_error' => $has_error,
'error_message' => $error_message, 'error_message' => $error_message,
'data' => $entries, 'data' => $entries,
@ -444,7 +463,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

@ -21,7 +21,7 @@ class WarrantySerialLoadLogger
$cache_dir = __DIR__ . '/../../var/cache'; $cache_dir = __DIR__ . '/../../var/cache';
$file = $cache_dir . '/warranty_serial_load_log.tab'; $file = $cache_dir . '/warranty_serial_load_log.tab';
error_log('opening file for warranty serial load log - ' . $file); // error_log('opening file for warranty serial load log - ' . $file);
$fp = fopen($file, 'w'); $fp = fopen($file, 'w');
if ($fp === false) if ($fp === false)

View file

@ -0,0 +1,10 @@
#!/bin/bash
# get the number fo warranty serial files to process
declare -i num_files
num_files=`/var/www/resq/bin/console warrantyserial:count`
for (( i = 1; i <= num_files; i++ ))
do
/usr/bin/php /var/www/resq/bin/console warrantyserial:load
done