diff --git a/.env.dist b/.env.dist index f6d35eec..06731d8f 100644 --- a/.env.dist +++ b/.env.dist @@ -80,3 +80,7 @@ API_VERSION=insert_api_version_here #SSL_ENABLE for websockets SSL_ENABLE=set_to_true_or_false + +# db +DB_USER=db_username +DB_PASSWORD=db_password diff --git a/utils/get_warranty_serial/get_serials.php b/utils/get_warranty_serial/get_serials.php index 237ce266..e9538e3f 100644 --- a/utils/get_warranty_serial/get_serials.php +++ b/utils/get_warranty_serial/get_serials.php @@ -20,14 +20,20 @@ $conn_string = "BlobEndpoint=$blob_url;\nSharedAccessSignature=$sas_token"; $blob_client = BlobRestProxy::createBlobService($conn_string); -$current_date = new DateTime(); -$current_date->modify("-1 day"); +// get date argument +$proc_date = $argv[1]; -$date = $current_date->format('m-d-y'); +error_log($proc_date); -$filename = 'warrantylogs' . $date . '.csv'; -//print_r($filename); +//$current_date = new DateTime(); +//$current_date->modify("-1 day"); +//$date = $current_date->format('m-d-Y'); + +$filename = 'warrantylogs' . $proc_date . '.csv'; +error_log($filename); + +/* try { // NOTE: via download blob $res = $blob_client->getBlob('warranty', $filename); @@ -36,8 +42,7 @@ try { file_put_contents("/tmp/warranty_download_serial.txt", $res->getContentStream()); } catch (Exception $e) { file_put_contents("/tmp/serial_download_error.txt", $filename . "\n" . $e->getMessage() . "\n" . "\n", FILE_APPEND); -} - +} */ /* // NOTE: getting via url diff --git a/utils/get_warranty_serial/new_get_serials.php b/utils/get_warranty_serial/new_get_serials.php new file mode 100644 index 00000000..45161f3a --- /dev/null +++ b/utils/get_warranty_serial/new_get_serials.php @@ -0,0 +1,49 @@ +getBlob('warranty', $filename); + // print_r($res); + + if ($flag_overwrite > 0) + { + file_put_contents($output_file, $res->getContentStream()); + } + else + { + file_put_contents($output_file, "\r\n", FILE_APPEND); + file_put_contents($output_file, $res->getContentStream(), FILE_APPEND); + } +} catch (Exception $e) { + file_put_contents("/tmp/serial_download_error.txt", $filename . "\n" . $e->getMessage() . "\n" . "\n", FILE_APPEND); +} + + + + + diff --git a/utils/load_warranty_serial/load_serials.php b/utils/load_warranty_serial/load_serials.php index cd1cce90..812b151c 100644 --- a/utils/load_warranty_serial/load_serials.php +++ b/utils/load_warranty_serial/load_serials.php @@ -1,10 +1,48 @@ loadEnv(__DIR__.'/../../.env'); + +$db_info = $_ENV['DATABASE_URL']; + +// sample format of db_info: mysql://db_user:db_password@127.0.0.1:3306/resq +// dsn for PDO needs to be: mysql:host=127.0.0.1:3306;dbname=resq;charset=UTF8 + +preg_match('/^mysql:\/\/(.*):(.*)@(.*):(.*)\/(.*)/', $db_info, $result); + +$db_type = 'mysql:host='; +$user = $result[1]; +$pass = $result[2]; +$ip_port = $result[3] . ':' . $result[4] . ';'; +$db_name = 'dbname=' . $result[5] . ';'; +$charset = 'charset=UTF8'; + +$dsn = $db_type . $ip_port . $db_name . $charset; + +//error_log($dsn); +//error_log($user); +//error_log($pass); // connect to db $db = new PDO($dsn, $user, $pass); @@ -13,33 +51,90 @@ $db = new PDO($dsn, $user, $pass); $sth = $db->prepare('insert into warranty_serial (id, sku, date_create, source, meta_info) values (:serial, :sku, :date_create, :source, :meta_info)'); // go through rows -$counter = 0; $source = 'motiv'; while (($row = fgetcsv($csv)) !== false) { - // skip first line - if ($counter == 0) + // TODO: verify if these are still the headers if there are headers + // possible lines in output file: + // (1) header in csv file + // SerialNumber,Sku,DispatchStatus,CreatedDate,InventoryStatus,CategoryID,CategoryName + // (2) No available data + // (3) CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF + // (4) Empty line + // (5) empty sku + + // check if No available data + if ($row[0] == 'No available data') { - $counter++; + // skip the line + error_log('No available data, skipping the line...'); continue; } - /* - $serial = trim(strtoupper($row[0])); - $sku = trim($row[1]); - $date_create = $row[2]; - $ref_id = $row[3]; */ + // check if empty line + if ($row == array(null)) + { + // skip + error_log('Skipping empty line...'); + continue; + } + + // check if empty serial + if (empty($row[0])) + { + $err_message = "Empty serial. " . "\n"; + fwrite($output_fh, $err_message); + continue; + } // sample of line in output file: // serial number, sku, dispatch status, created date, inventory status, category id, category name // CH2000012071,WCHD23BL-CPN00-LX,0,2020-08-11 04:05:27.090,0,4,CHAMPION MF + // MG2000313690,N/A,1,2021-05-14T23:47:30.6430000+08:00,0,10,GOLD $serial = trim(strtoupper($row[0])); - $sku = trim($row[1]); + $sku = trim(strtoupper($row[1])); $dispatch_status = trim($row[2]); - $date_create = $row[3]; + $str_date_create = trim($row[3]); $inventory_status = trim($row[4]); $cat_id = trim($row[5]); - $cat_name = trim($row[6]); + $cat_name = trim(strtoupper($row[6])); + + error_log('Processing ' . $serial . ' and ' . $sku); + + // 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) + { + // log the serial + $message = "$serial - ERROR - " . "Invalid date format for create date." . "\n"; + fwrite($output_fh, $message); + continue; + } + } + + $created_date = $date_create->format('Y-m-d H:i:s'); + + //error_log($str_date_time); + //error_log($str_timezone); + //error_log($str_datetime_tz); + //error_log($date_create->format('Y-m-d H:i:s')); $meta_info = [ 'dispatch_status' => $dispatch_status, @@ -56,19 +151,27 @@ while (($row = fgetcsv($csv)) !== false) $res = $sth->execute([ ':serial' => $serial, ':sku' => $sku, - ':date_create' => $date_create, + ':date_create' => $created_date, ':source' => $source, ':meta_info' => $info, ]); if (!$res) { + // log the error $err = $sth->errorInfo(); - echo "Error ($serial) - " . $err[2] . "\n"; + $err_message = "$serial - ERROR - " . $err[2] . "\n"; + fwrite($output_fh, $err_message); + } + else + { + // log successful adding of serial + $message = "$serial - SUCCESS - " . "\n"; + fwrite($output_fh, $message); } } // close file fclose($csv); - +fclose($output_fh); diff --git a/utils/warranty_motiv_dev.sh b/utils/warranty_motiv_dev.sh index 00c73418..95f687d6 100755 --- a/utils/warranty_motiv_dev.sh +++ b/utils/warranty_motiv_dev.sh @@ -1,4 +1,4 @@ #!/bin/bash touch /tmp/warranty_download_serial.txt -/usr/bin/php /usr/share/nginx/html/resqapi/utils/get_warranty_serial/get_serials.php -/usr/bin/php /usr/share/nginx/html/resqapi/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=localhost;dbname=resq;charset=UTF8" resq Motolite456 +/usr/bin/php /usr/share/nginx/html/resqapi/utils/get_warranty_serial/get_serials.php `date +%m-%d-%Y` +/usr/bin/php /usr/share/nginx/html/resqapi/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=localhost;dbname=resq;charset=UTF8" diff --git a/utils/warranty_motiv_local.sh b/utils/warranty_motiv_local.sh new file mode 100755 index 00000000..e99c7f64 --- /dev/null +++ b/utils/warranty_motiv_local.sh @@ -0,0 +1,9 @@ +#!/bin/bash +#touch /tmp/warranty_download_serial.csv +proc_date=`date +%m-%d-%y -d "1 day ago"` +download_file="/tmp/warranty_download_serial_$proc_date.csv" +load_status_file="/tmp/warranty_load_status_$proc_date.txt" +echo $download_file +echo $load_status_file +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php $proc_date $download_file 1 +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php $download_file $load_status_file diff --git a/utils/warranty_motiv_local_bulk.sh b/utils/warranty_motiv_local_bulk.sh new file mode 100755 index 00000000..c4f2a551 --- /dev/null +++ b/utils/warranty_motiv_local_bulk.sh @@ -0,0 +1,40 @@ +#!/bin/bash +touch /tmp/warranty_download_serial.csv +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-03-21 /tmp/warranty_download_serial.csv 1 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-07-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-08-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-09-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-10-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-11-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-12-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-13-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-14-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-15-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-16-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-17-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-18-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-19-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-20-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-21-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-22-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-23-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-24-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-25-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-26-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-27-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-28-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-29-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-30-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-31-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-01-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-02-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-03-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-07-21 /tmp/warranty_download_serial.csv 0 +touch /tmp/warranty_load_status.txt +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.csv /tmp/warranty_load_status.txt diff --git a/utils/warranty_motiv_prod.sh b/utils/warranty_motiv_prod.sh index f13a7d7d..124f6e96 100755 --- a/utils/warranty_motiv_prod.sh +++ b/utils/warranty_motiv_prod.sh @@ -1,4 +1,9 @@ #!/bin/bash -touch /tmp/warranty_download_serial.txt -/usr/bin/php /var/www/resq/utils/get_warranty_serial/get_serials.php -/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.txt "mysql:host=172.18.203.191:3306;dbname=resq;charset=UTF8" resq Motolite456 +proc_date=`date +%m-%d-%y -d "1 day ago"` +download_file="/tmp/warranty_download_serial_$proc_date.csv" +load_status_file="/tmp/warranty_load_status_$proc_date.txt" +#echo $download_file +#echo $load_status_file +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php $proc_date $download_file 1 +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php $download_file $load_status_file + diff --git a/utils/warranty_motiv_prod_bulk.sh b/utils/warranty_motiv_prod_bulk.sh new file mode 100755 index 00000000..c4f2a551 --- /dev/null +++ b/utils/warranty_motiv_prod_bulk.sh @@ -0,0 +1,40 @@ +#!/bin/bash +touch /tmp/warranty_download_serial.csv +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-03-21 /tmp/warranty_download_serial.csv 1 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-07-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-08-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-09-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-10-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-11-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-12-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-13-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-14-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-15-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-16-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-17-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-18-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-19-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-20-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-21-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-22-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-23-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-24-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-25-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-26-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-27-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-28-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-29-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-30-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 05-31-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-01-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-02-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-03-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-04-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-05-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-06-21 /tmp/warranty_download_serial.csv 0 +/usr/bin/php /var/www/resq/utils/get_warranty_serial/new_get_serials.php 06-07-21 /tmp/warranty_download_serial.csv 0 +touch /tmp/warranty_load_status.txt +/usr/bin/php /var/www/resq/utils/load_warranty_serial/load_serials.php /tmp/warranty_download_serial.csv /tmp/warranty_load_status.txt