Separate the uploading of files and the processing of files. #704

This commit is contained in:
Korina Cordero 2022-09-14 09:33:05 +00:00
parent 3c9bdc8152
commit a3e9c45ec3
4 changed files with 306 additions and 158 deletions

View file

@ -0,0 +1,205 @@
<?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\WarrantySerial;
use App\Entity\WarrantySerialUploadLog;
use App\Entity\WarrantySerialLoadLog;
use App\Service\WarrantySerialUploadLogger;
use App\Service\WarrantySerialLoadLogger;
class LoadWarrantySerialCommand extends Command
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this->setName('warrantyserial:load')
->setDescription('Load warranty serials from file.')
->setHelp('Load warranty serials from file.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
return 0;
}
protected function processWarrantySerialFile($csv_file, $upload_logger, $load_logger)
{
// attempt to open file
try
{
$fh = fopen($csv_file, "r");
}
catch (Exception $e)
{
$error = 'The file ' . $csv_file . 'could not be read.';
$log_data = [
'user_id' => $user_id,
'is_uploaded' => false,
'error' => $error,
];
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
return new APIResponse(false, $error);
}
while(($row = $fgetcsv($fh)) !== false)
{
$is_valid = $this->validateRow($row[0], $load_logger, $user_id);
if ($is_valid)
{
// valid entry, we parse and insert
$serial = trim(strtoupper($row[0]));
$sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]);
$str_date_create = trim($row[3]);
$inventory_status = trim($row[4]);
$cat_id = trim($row[5]);
$cat_name = trim(strtoupper($row[6]));
// we are sure that this is a valid date at this point
$created_date = $this->convertDateCreate($str_date_create);
$meta_info = [
'dispatch_status' => $dispatch_status,
'inventory_status' => $inventory_status,
'category_id' => $cat_id,
'category_name' => $cat_name,
];
$info = json_encode($meta_info);
// insert the data. Still need to figure out how to do this fast
// log the successful insert
$log_data = [
'user_id' => $user_id,
'is_loaded' => true,
'serial' => $serial,
'error' => '',
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
}
}
return true;
}
protected function validateRow($entry, $load_logger, $user_id)
{
// possible lines:
// (1) header in csv file - ignore
// SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName
// (2) No available data - ignore
// (3) CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF - valid
// (4) MG2000313690,N/A,1,2021-05-14T23:47:30.6430000+08:00,0,10,GOLD - valid
// (5) Empty line - ignore
// (6) empty sku - log
// check if the line is a header
if ($row[0] == 'SerialNumber')
{
// no need to log, just ignore and skip
return false;
}
// check if empty line
if ($row == array(null))
{
// no need to log, just ignore and skip
return false;
}
// check if empty serial
if (empty($row[0]))
{
// this one, we log
$error = 'Empty serial';
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => '',
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
return false;
}
// validate the date created
$str_date_create = trim($row[3]);
$date_create = $this->convertDateCreate($str_date_create);
if ($date_create == null)
{
// log
$error = 'Invalid date create';
$serial = trim($row[0]);
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => $serial,
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
return false;
}
// check if serial is a dupe
// valid entry
return true;
}
protected function convertDateCreate($str_date_create)
{
// since some people cannot follow simple instructions...
// check the date format on the string
// try 2021-05-15T08:35:46+08:00 format on str_date_create
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_date_create);
if ($date_create == false)
{
// try this format: 2021-05-15T08:47:20.3330000+08:00
// get the date, time and timezone from str_date_create
$str_date_time = substr($str_date_create, 0, 19);
$str_timezone = substr($str_date_create, 27);
$str_datetime_tz = $str_date_time . $str_timezone;
// create DateTime object
// sample: 2021-05-15T12:16:06+08:00
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_datetime_tz);
// check if datetime object was created
// if not, someone f*cked up and we have no date create
if ($date_create == false)
{
return null;
}
}
// TODO: think about how we're going to save this, if we need string or DateTime
// if you reach this part, then date string is valid and can be converted
$created_date = $date_create->format('Y-m-d H:i:s');
return $created_date;
}
}

View file

@ -13,12 +13,11 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\WarrantySerial;
use App\Entity\WarrantySerialUploadLog;
use App\Entity\WarrantySerialLoadLog;
use App\Entity\WarrantySerialiQueue;
use App\Service\WarrantySerialUploadLogger;
use App\Service\WarrantySerialLoadLogger;
use DateTime;
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
@ -32,8 +31,7 @@ class WarrantySerialController extends APIController
$this->acl_gen = $acl_gen;
}
public function upload(Request $req, EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger,
WarrantySerialLoadLogger $load_logger)
public function upload(Request $req, EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger)
{
$this->denyAccessUnlessGranted('warrantyserial.upload', null, 'No access.');
@ -51,172 +49,69 @@ class WarrantySerialController extends APIController
// get the csv file
$csv_file = $req->files->get('serial_file');
$res = $this->processWarrantySerialFile($csv_file, $upload_logger, $load_logger);
if (!$res)
return $res;
// process file upload
$upload_dir = $kernel->getProjectDir() . '/public/warranty_serial_uploads';
$serial_filename = $this->handleSerialFileUpload($csv_file, $upload_dir, $upload_logger);
// insert to warranty serial queue
$res = $this->registerWarrantySerialFile($em, $csv_file, $serial_filename, $upload_logger, $user_id);
// flush to db
$em->flush();
return $res;
}
protected function processWarrantySerialFile($csv_file, $upload_logger, $load_logger)
protected function registerWarrantySerialFile($em, $file, $serial_filename, $upload_logger, $user_id)
{
// attempt to open file
try
{
$fh = fopen($csv_file, "r");
}
catch (Exception $e)
{
$error = 'The file ' . $csv_file . 'could not be read.';
$log_data = [
'user_id' => $user_id,
'is_uploaded' => false,
'error' => $error,
];
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
return new APIResponse(false, $error);
}
$orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
while(($row = $fgetcsv($fh)) !== false)
$ws_file = new WarrantySerialQueue();
$ws_file->setFileSerial($serial_filename)
->setStatus('pending');
$em->persist($ws_file);
// log upload
$log_data = [
'user_id' => $user_id,
'is_uploaded' => true,
'orig_file_serial' => $orig_filename,
'uploaded_file_serial' => $serial_filename,
];
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
}
protected function handleSerialFileUpload($file, $upload_dir, $upload_logger)
{
// create target dir if it doesn't exist
if (!file_exists($target_dir))
{
$is_valid = $this->validateRow($row[0], $load_logger, $user_id);
if ($is_valid)
if (!mkdir($target_dir, 0744, true))
{
// valid entry, we parse and insert
$serial = trim(strtoupper($row[0]));
$sku = trim(strtoupper($row[1]));
$dispatch_status = trim($row[2]);
$str_date_create = trim($row[3]);
$inventory_status = trim($row[4]);
$cat_id = trim($row[5]);
$cat_name = trim(strtoupper($row[6]));
// we are sure that this is a valid date at this point
$created_date = $this->convertDateCreate($str_date_create);
$meta_info = [
'dispatch_status' => $dispatch_status,
'inventory_status' => $inventory_status,
'category_id' => $cat_id,
'category_name' => $cat_name,
];
$info = json_encode($meta_info);
// insert the data. Still need to figure out how to do this fast
// log the successful insert
$log_data = [
'user_id' => $user_id,
'is_loaded' => true,
'serial' => $serial,
'error' => '',
'is_uploaded' => false,
'error' => 'Failed to create folder for warranty serial files.'
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
}
}
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
return true;
}
protected function validateRow($entry, $load_logger, $user_id)
{
// possible lines:
// (1) header in csv file - ignore
// SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName
// (2) No available data - ignore
// (3) CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF - valid
// (4) MG2000313690,N/A,1,2021-05-14T23:47:30.6430000+08:00,0,10,GOLD - valid
// (5) Empty line - ignore
// (6) empty sku - log
// check if the line is a header
if ($row[0] == 'SerialNumber')
{
// no need to log, just ignore and skip
return false;
}
// check if empty line
if ($row == array(null))
{
// no need to log, just ignore and skip
return false;
}
// check if empty serial
if (empty($row[0]))
{
// this one, we log
$error = 'Empty serial';
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => '',
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
return false;
}
// validate the date created
$str_date_create = trim($row[3]);
$date_create = $this->convertDateCreate($str_date_create);
if ($date_create == null)
{
// log
$error = 'Invalid date create';
$serial = trim($row[0]);
$log_data = [
'user_id' => $user_id,
'is_loaded' => false,
'serial' => $serial,
'error' => $error,
];
$this->upload_logger->logWarrantySeriaLoadInfo($log_data);
return false;
}
// check if serial is a dupe
// valid entry
return true;
}
protected function convertDateCreate($str_date_create)
{
// since some people cannot follow simple instructions...
// check the date format on the string
// try 2021-05-15T08:35:46+08:00 format on str_date_create
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_date_create);
if ($date_create == false)
{
// try this format: 2021-05-15T08:47:20.3330000+08:00
// get the date, time and timezone from str_date_create
$str_date_time = substr($str_date_create, 0, 19);
$str_timezone = substr($str_date_create, 27);
$str_datetime_tz = $str_date_time . $str_timezone;
// create DateTime object
// sample: 2021-05-15T12:16:06+08:00
$date_create = DateTime::createFromFormat('Y-m-d\TH:i:sP', $str_datetime_tz);
// check if datetime object was created
// if not, someone f*cked up and we have no date create
if ($date_create == false)
{
return null;
}
}
// if you reach this part, then date string is valid and can be converted
$created_date = $date_create->format('Y-m-d H:i:s');
// get current date
$curr_date = new DateTime();
$str_curr_date = $curr_date->format('YmdHis');
return $created_date;
// move file
$filename = 'warranty_serial' . '.' . $file->getClientOriginalExtension();
$file->move($target_dir . '/' . $str_curr_date, $filename);
return $str_curr_date . '/' . $filename;
}
protected function checkRequiredParams(Request $req, $params, $upload_logger, $user_id)

View file

@ -32,6 +32,18 @@ class WarrantySerialUploadLog
*/
protected $api_user;
// original filename of warranty serial
/**
* @ORM\Column(type="string", length=80, nullable=true)
*/
protected $orig_file_serial;
// uploaded file name
/**
* @ORM\Column(type="string", length=80, nullable=true)
*/
protected $uploaded_file_serial;
// flag if uploaded
/**
* @ORM\Column(type="boolean")
@ -39,7 +51,7 @@ class WarrantySerialUploadLog
protected $flag_uploaded;
/**
* @ORM\Column(type="string", length=30, nullable=true)
* @ORM\Column(type="string", length=60, nullable=true)
*/
protected $error;
@ -47,6 +59,8 @@ class WarrantySerialUploadLog
{
$this->date_create = new DateTime();
$this->api_user = '';
$this->orig_file_serial = '';
$this->orig_file_serial = '';
$this->flag_uploaded = false;
$this->error = null;
}
@ -78,6 +92,28 @@ class WarrantySerialUploadLog
return $this->api_user;
}
public function setOrigFileSerial($file = null)
{
$this->orig_file_serial = $file;
return $this;
}
public function getOrigFileSerial()
{
return $this->orig_file_serial;
}
public function setUploadedFileSerial($file = null)
{
$this->uploaded_file_serial = $file;
return $this;
}
public function getUploadedFileSerial()
{
return $this->uploaded_file_serial;
}
public function setUploaded($flag_uploaded = true)
{
$this->flag_uploaded = $flag_uploaded;

View file

@ -21,10 +21,22 @@ class WarrantySerialUploadLogger
$user_id = $log_data['user_id'];
$is_uploaded = $log_data['is_uploaded'];
$error = $log_data['error'];
$error = '';
$orig_file_serial = '';
$uploaded_file_serial = '';
if (isset($log_data['error']))
$error = $log_data['error'];
if (isset($log_data['orig_file_serial']))
$orig_file_serial = $log_data['orig_file_serial'];
if (isset($log_data['uploaded_file_serial']))
$uploaded_file_serial = $log_data['uploaded_file_serial'];
$log_entry->setApiUser($user_id)
->setLoaded($is_uploaded)
->setOrigFileSerial($orig_file_serial)
->setUploadedFileSerial($uploaded_file_serial)
->setError($error);
$this->em->persist($log_entry);