*/
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']));
- $initTime = (hrtime(true) - $t0) / 1e6; // миллисекунды
- Yii::warning( "Init (calculateMedianSalesForPeriod): {$initTime} ms\n");
- $t1 = hrtime(true);
$rows = (new Query())
- ->select(['sp.product_id', 'cnt' => 'COUNT(*)'])
+ ->select(['sp.product_id', 'cnt' => 'SUM(sp.quantity)'])
->from(['s' => 'sales'])
->innerJoin(['sp' => 'sales_products'], 's.id = sp.check_id')
->where(['sp.product_id' => $similarProductIds])
->groupBy('sp.product_id')
->indexBy('product_id')
->all();
- $dur = (hrtime(true) - $t1) / 1e6;
- Yii::warning( "Query {$storeId}: {$dur} ms\n");
- $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];
}