namespace yii_app\services;
use DateTime;
+use Yii;
use yii\db\Expression;
use yii\db\Query;
use yii\helpers\ArrayHelper;
*/
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] = [
'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;
}
*/
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')
->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) {
} 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];
}