Refactor product history retrieval in Cron and Products classes; optimize date handling and query logic

This commit is contained in:
2024-12-21 21:32:38 +01:00
parent 730ff661c8
commit 33442a110a
3 changed files with 21 additions and 34 deletions

View File

@@ -35,8 +35,8 @@
},
"class.Products.php": {
"type": "-",
"size": 6164,
"lmtime": 1734294911481,
"size": 6779,
"lmtime": 1734386753537,
"modified": false
},
"class.Site.php": {
@@ -67,8 +67,8 @@
},
"class.Products.php": {
"type": "-",
"size": 3084,
"lmtime": 1733868265361,
"size": 3709,
"lmtime": 1734386591635,
"modified": false
},
"class.Users.php": {
@@ -2563,8 +2563,8 @@
},
"functions.js": {
"type": "-",
"size": 3168,
"lmtime": 0,
"size": 3400,
"lmtime": 1734382316193,
"modified": false
},
"grid": {},
@@ -2712,8 +2712,8 @@
"products": {
"main_view.php": {
"type": "-",
"size": 3909,
"lmtime": 1733868311355,
"size": 6819,
"lmtime": 1734385931827,
"modified": false
},
"product_history.php": {

View File

@@ -141,25 +141,12 @@ class Cron
$products = $mdb -> select( 'products', 'id', [ 'client_id' => $client_id ] );
foreach ( $products as $product )
{
for ( $i = 0; $i < 30; $i++ )
$dates = $mdb -> query( 'SELECT id, date_add FROM products_history WHERE product_id = ' . $product . ' AND updated = 1 ORDER BY date_add DESC' ) -> fetchAll( \PDO::FETCH_ASSOC );
foreach ( $dates as $date )
{
$date_to = date( 'Y-m-d', strtotime( '-' . (1 + $i) . ' days' ) );
$date_from = date( 'Y-m-d', strtotime( '-' . (31 + $i) . ' days' ) );
$data_updated = false;
if ( $mdb -> count( 'products_history', [ 'AND' => [ 'product_id' => $product, 'date_add[>=]' => $date_from, 'date_add[<=]' => $date_to, 'updated' => 1 ] ] ) > 0 )
{
$data_updated = true;
}
if ( $data_updated )
{
self::cron_product_history_30_save( $product, $date_from, $date_to );
}
self::cron_product_history_30_save( $product, $date['date_add'] );
$mdb -> update( 'products_history', [ 'updated' => 0 ], [ 'id' => $date['id'] ] );
}
// $mdb -> update( 'products_history', [ 'updated' => 0 ], [ 'AND' => [ 'product_id' => $product, 'updated' => 1 ] ] );
}
$end_time = microtime(true);
@@ -168,11 +155,11 @@ class Cron
exit;
}
static public function cron_product_history_30_save( $product_id, $date_from, $date_to )
static public function cron_product_history_30_save( $product_id, $date_to )
{
global $mdb;
$data = $mdb -> query( 'SELECT * FROM products_history WHERE product_id = ' . $product_id . ' AND date_add >= \'' . $date_from . '\' AND date_add <= \'' . $date_to . '\' ORDER BY date_add ASC' ) -> fetchAll( \PDO::FETCH_ASSOC );
$data = $mdb -> query( 'SELECT * FROM products_history WHERE product_id = ' . $product_id . ' AND date_add <= \'' . $date_to . '\' ORDER BY date_add DESC LIMIT 30' ) -> fetchAll( \PDO::FETCH_ASSOC );
// Inicjalizacja tablic do przechowywania danych
$offers_data = [];
@@ -206,15 +193,15 @@ class Cron
{
$day_count = count( $offer['days_counted'] );
$impressions = $offer['impressions'] / $day_count;
$clicks = $offer['clicks'] / $day_count;
$impressions = $offer['impressions'];
$clicks = $offer['clicks'];
$ctr = ( $clicks > 0 and $impressions ) ? round( $clicks / $impressions, 4 ) * 100 : 0;
$cost = $offer['cost'] / $day_count;
$conversions = $offer['conversions'] / $day_count;
$conversions_value = $offer['conversions_value'] / $day_count;
$cost = $offer['cost'];
$conversions = $offer['conversions'];
$conversions_value = $offer['conversions_value'];
$roas = ( $conversions_value > 0 and $cost ) ? round( $conversions_value / $cost, 2 ) * 100 : 0;
if ( $day_count > 14 )
if ( $day_count >= 30 )
{
if ( $mdb -> count( 'products_history_30', [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date_to ] ] ) > 0 )
{

View File

@@ -49,7 +49,7 @@ class Products
static public function get_product_history_30( $client_id, $product_id, $start, $limit )
{
global $mdb;
return $mdb -> query( 'SELECT * FROM products_history_30 AS ph3 WHERE ph3.product_id = \'' . $product_id . '\' ORDER BY ph3.date_add DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC );
return $mdb -> query( 'SELECT * FROM products_history_30 AS ph3 WHERE ph3.product_id = \'' . $product_id . '\' ORDER BY ph3.date_add ASC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC );
}
static public function get_impressions_30( $product_id )