]> gitweb.erp-flowers.ru Git - erp24_rep/yii-erp24/.git/commitdiff
Отладка запросов
authorfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 21 May 2025 10:17:20 +0000 (13:17 +0300)
committerfomichev <vladimir.fomichev@erp-flowers.ru>
Wed, 21 May 2025 10:17:20 +0000 (13:17 +0300)
erp24/services/StorePlanService.php

index 200f810ed1e332393614136f98ccbf5a426a2c42..6b7208a11a7112497dc6ae68e25c9289db9be2d3 100755 (executable)
@@ -3,6 +3,7 @@
 namespace yii_app\services;
 
 use DateTime;
+use Yii;
 use yii\db\Expression;
 use yii\db\Query;
 use yii\helpers\ArrayHelper;
@@ -469,29 +470,35 @@ class StorePlanService
      */
     public static function calculateWeightedSalesForProductsWithoutHistory($storeId, $selectedMonth, $selectedYear, $productsWithoutHistory)
     {
+        $t0 = hrtime(true);
         $targetDate = strtotime("{$selectedYear}-{$selectedMonth}-01");
         $periods = self::getPeriods($targetDate, 3);
 
         $weightedResults = [];
-
+        $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды
+        Yii::warning( "Init (periods): {$initTime} ms\n");
         foreach ($productsWithoutHistory as $product) {
             $guid = $product['guid'];
-
+            $t1 = hrtime(true);
             $similarProductIds = self::getSimilarProductIDs($guid);
             if (empty($similarProductIds)) {
                 $weightedResults[$guid] = 0;
                 continue;
             }
-
+            $dur = (hrtime(true) - $t1) / 1e6;
+            Yii::warning( "getSimilarProductIDs for product {$guid} {$storeId}: {$dur} ms\n");
             $medianSales = [];
             $salesValuesForEachMonth = [];
+            $t2 = hrtime(true);
             foreach ($periods as $periodKey => $monthInfo) {
                 list($median, $salesValues) = self::calculateMedianSalesForPeriod($storeId, $similarProductIds, $monthInfo);
                 $medianSales[$periodKey]             = $median;
                 $salesValuesForEachMonth[$periodKey]   = $salesValues;
             }
-
+            $dur = (hrtime(true) - $t2) / 1e6;
+            Yii::warning("calculateMedianSalesForPeriod  for product {$guid} {$storeId}: {$dur} ms\n");
             $weights = [3, 2, 1];
+            $t3 = hrtime(true);
             $weightedValue = self::computeWeightedValue($medianSales, $weights);
 
             $weightedResults[$guid] = [
@@ -499,8 +506,13 @@ class StorePlanService
                 'medianSales'   => $medianSales,
                 'salesValues'   => $salesValuesForEachMonth,
             ];
+            $dur = (hrtime(true) - $t3) / 1e6;
+            Yii::warning( "computeWeightedValue for product {$guid} {$storeId}: {$dur} ms\n");
         }
 
+        $totalTime = (hrtime(true) - $t0) / 1e6;
+        Yii::warning( "Total  calculateWeightedSalesForProductsWithoutHistory: {$totalTime} ms\n");
+
         return $weightedResults;
     }
 
@@ -695,13 +707,14 @@ class StorePlanService
      */
     private static function calculateMedianSalesForPeriod($storeId, $similarProductIds, $monthInfo)
     {
+        $t0 = hrtime(true);
         $startDate = sprintf('%04d-%02d-01', $monthInfo['year'], $monthInfo['month']);
         $endDate   = sprintf('%04d-%02d-%02d',
             $monthInfo['year'],
             $monthInfo['month'],
             cal_days_in_month(CAL_GREGORIAN, $monthInfo['month'], $monthInfo['year']));
 
-        $salesValues = [];
+/*        $salesValues = [];
 
         foreach ($similarProductIds as $simProdId) {
             $sales = Sales::find()->alias('s')
@@ -713,29 +726,37 @@ class StorePlanService
                 ->andWhere(['order_id' => ['', '0']])
                 ->count();
             $salesValues[] = (int)$sales;
-        }
-
-       /* $rows = (new Query())
+        }*/
+        $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды
+        Yii::warning( "Init (calculateMedianSalesForPeriod): {$initTime} ms\n");
+        $t1 = hrtime(true);
+      $rows = (new Query())
             ->select(['sp.product_id', 'cnt' => 'COUNT(*)'])
             ->from(['s' => 'sales'])
             ->innerJoin(['sp' => 'sales_products'], 's.id = sp.check_id')
             ->where(['sp.product_id' => $similarProductIds])
             ->andWhere(['s.store_id' => $storeId])
             ->andWhere(['between', 's.date', $startDate . ' 00:00:00', $endDate . ' 23:59:59'])
-            ->andWhere(['not', ['s.order_id' => ['','0']]])
+            ->andWhere(['s.order_id' => ['','0']])
             ->groupBy('sp.product_id')
             ->indexBy('product_id')
-            ->all();*/
-
+            ->all();
+        $dur = (hrtime(true) - $t1) / 1e6;
+        Yii::warning( "Query {$storeId}: {$dur} ms\n");
  //var_dump($similarProductIds, $storeId, $startDate, $endDate);die();
-//        $salesValues = [];
-//        foreach ($similarProductIds as $id) {
-//            $salesValues[] = isset($rows[$id]) ? (int)$rows[$id]['cnt'] : 0;
-//        }
+        $t2 = hrtime(true);
+        $salesValues = [];
+        foreach ($similarProductIds as $id) {
+            $salesValues[] = isset($rows[$id]) ? (int)$rows[$id]['cnt'] : 0;
+        }
 
         $nonZeroSales = array_filter($salesValues, function($val) {
             return $val > 0;
         });
+        $dur = (hrtime(true) - $t2) / 1e6;
+        Yii::warning("count for store {$storeId}: {$dur} ms\n");
+
+        $t3 = hrtime(true);
         sort($nonZeroSales, SORT_NUMERIC);
         $n = count($nonZeroSales);
         if ($n === 0) {
@@ -745,7 +766,11 @@ class StorePlanService
         } else {
             $median = ($nonZeroSales[$n / 2 - 1] + $nonZeroSales[$n / 2]) / 2;
         }
+        $dur = (hrtime(true) - $t3) / 1e6;
+        Yii::warning( "sort for store {$storeId}: {$dur} ms\n");
 
+        $totalTime = (hrtime(true) - $t0) / 1e6;
+        Yii::warning( "Total calculateMedianSalesForPeriod: {$totalTime} ms\n");
         return [$median, $salesValues];
     }