public function actionClean() { $query = "DELETE FROM " . FlightCache::model()->tableName() . " WHERE `dateFrom` < NOW();"; $deleted = Yii::app()->db->createCommand($query)->execute(); echo "Deleted flight rows: " . $deleted . "\n"; $query = "DELETE FROM " . HotelCache::model()->tableName() . " WHERE `dateFrom` < NOW();"; $deleted = Yii::app()->db->createCommand($query)->execute(); echo "Deleted hotel rows: " . $deleted . "\n"; }
/** * Do HotelSearch requests with all combinations of room types, * @param HotelSearchParams $hotelSearchParams * @return HotelSearchResponse */ public function fullHotelSearch(HotelSearchParams $hotelSearchParams) { $rooms = $hotelSearchParams->rooms; //Make combinations to combinations Array uasort($rooms, 'HotelBookClient::compareArrayAdultCount'); $combinations = array(); foreach ($rooms as $key => $room) { $rooms[$key]['sizeCount'] = count(self::$roomSizeRoomTypesMap[$room['adultCount']]); $rooms[$key]['sizeIndex'] = 0; } $allCombined = false; // Make ALL possible combinations while (!$allCombined) { $combination = array(); $allCombined = true; foreach ($rooms as $key => $room) { if ($room['sizeCount'] !== $room['sizeIndex'] + 1) { $allCombined = false; } $rooms[$key]['roomSizeId'] = self::$roomSizeRoomTypesMap[$room['adultCount']][$room['sizeIndex']]; $combination[] = array('roomSizeId' => $rooms[$key]['roomSizeId'], 'child' => $rooms[$key]['childCount'], 'cots' => $rooms[$key]['cots'], 'ChildAge' => $rooms[$key]['childAge']); } sort($combination); $combinations[] = $combination; if (!$allCombined) { //next possible state $overflow = false; $iterationComplete = false; foreach ($rooms as $key => $room) { if ($room['sizeCount'] == 1) { continue; } if ($iterationComplete) { if ($overflow) { if ($room['sizeCount'] == $room['sizeIndex'] + 1) { $rooms[$key]['sizeIndex'] = 0; } else { $rooms[$key]['sizeIndex']++; $overflow = false; break; } } else { break; } } else { if ($room['sizeCount'] == $room['sizeIndex'] + 1) { $rooms[$key]['sizeIndex'] = 0; $iterationComplete = true; $overflow = true; } else { $rooms[$key]['sizeIndex']++; break; } } } } } //delete same combinations sort($combinations); foreach ($combinations as $key => $combination) { if (!isset($prevComb)) { $prevComb = $combination; continue; } if ($prevComb == $combination) { unset($combinations[$key]); } $prevComb = $combination; } unset($prevComb); unset($combination); //add requests to queue self::$groupId = substr(md5(uniqid('', true)), 0, 10); $params = array('cityId' => $hotelSearchParams->city->hotelbookId, 'checkIn' => $hotelSearchParams->checkIn, 'duration' => $hotelSearchParams->duration); if (!$params['cityId']) { throw new CException('City ' . $hotelSearchParams->city->localEn . ' does not linked with hotelBookId. You cannot find any hotels there'); } foreach ($combinations as $key => $combination) { $params['rooms'] = array(); foreach ($combination as $i => $room) { if (!isset($prevInd)) { $prevInd = $i; $roomNumber = 1; continue; } if ($combination[$i] === $combination[$prevInd]) { $roomNumber++; continue; } else { $combination[$prevInd]['roomNumber'] = $roomNumber; $params['rooms'][] = $combination[$prevInd]; $prevInd = $i; $roomNumber = 1; } } $combination[$prevInd]['roomNumber'] = $roomNumber; $params['rooms'][] = $combination[$prevInd]; //print_r($params); unset($prevInd); $this->hotelSearch($params, true); } //run all requests $startTime = microtime(true); $this->processAsyncRequests(); $endTime = microtime(true); //Header('ExecutionTimeProcessAsync:'.($endTime - $startTime)); self::$groupId = null; $hotels = array(); $errorDescriptions = array(); foreach ($this->requests as $request) { //echo count($request['result']->hotels).'<br>'; //die(); if (isset($request['result'])) { if ($request['result']->hotels) { foreach ($request['result']->hotels as $hotel) { $key = $hotel->key; if (isset($hotels[$key])) { //echo '--duplicate'; //echo 'have:'; //VarDumper::dump($hotels[$key]); //echo 'new:'; //VarDumper::dump($hotel); //echo "key: $key"; } $hotels[$key] = $hotel; } } if ($request['result']->errorsDescriptions) { foreach ($request['result']->errorsDescriptions as $desc) { $errorDescriptions[] = $desc; } } } else { print_r($request); die; } } //die(); $response = new HotelSearchResponse(); if ($hotels) { $response->responseStatus = ResponseStatus::ERROR_CODE_NO_ERRORS; } else { if (!$errorDescriptions) { $response->responseStatus = ResponseStatus::ERROR_CODE_EMPTY; } else { $response->responseStatus = ResponseStatus::ERROR_CODE_EXTERNAL; $response->errorsDescriptions = $errorDescriptions; } } /* //print_r($combinations); print_r(count($hotels)); print_r($errorDescriptions);*/ if ($hotels) { //echo "pre results count:".count($hotels); if (count($hotelSearchParams->rooms) == 1) { // O_o foreach ($hotelSearchParams->rooms as $room) { break; } if ($room['adultCount'] == 2 && $room['childCount'] == 0 && $room['cots'] == 0) { $allHotelStack = new HotelStack(array('hotels' => $hotels)); //echo "post results count:".count($allHotelStack->_hotels); //die(); //VarDumper::dump($allHotelStack);die(); $allHotelStack->groupBy('categoryId')->groupBy('roomSizeId')->groupBy('roomTypeId')->groupBy('centerDistance')->groupBy('rubPrice'); //$allHotelStack->groupBy('categoryId')->groupBy('centerDistance')->groupBy('rubPrice'); //print_r($allHotelStack); //die(); //VarDumper::dump($allHotelStack);die(); //VarDumper::dump($hotelStack->hotelStacks); foreach ($allHotelStack->hotelStacks as $categoryId => $hotelStack) { //categoryId - star rating (we need 3..5 stars) if ($categoryId == Hotel::STARS_THREE || $categoryId == Hotel::STARS_FOUR || $categoryId == Hotel::STARS_FIVE) { //echo "category: $categoryId<br>"; //VarDumper::dump($hotelStack); die(); $haveStack = false; foreach ($hotelStack->hotelStacks as $i => $hotelStackSize) { //VarDumper::dump($i); //echo "roomSizeId: $i<br>"; //todo: move to room class if (!in_array($i, array(appParams('HotelBook.room.DBL'), appParams('HotelBook.room.TWIN')))) { $hotelStack->deleteStackWithIndex("{$i}"); //unset($hotelStack->hotelStacks[$i]); } else { //echo "in 2"; foreach ($hotelStack->hotelStacks[$i]->hotelStacks as $j => $hotelStackType) { //echo "roomTypeId: $j<br>"; if (!in_array($j, appParams('HotelBook.room.STD'))) { $hotelStack->deleteStackWithIndex("{$i},{$j}"); //unset($hotelStack->hotelStacks[$i]->hotelStacks[$j]); } else { //echo "in 3"; foreach ($hotelStack->hotelStacks[$i]->hotelStacks[$j]->hotelStacks as $k => $hotelStackDistance) { //echo "distance: $k"; if ($k > appParams('HotelBook.distanceFromCityCenter')) { //echo "out $k"; $hotelStack->deleteStackWithIndex("{$i},{$j},{$k}"); //unset($hotelStack->hotelStacks[$i]->hotelStacks[$j]->hotelStacks[$k]); } else { //echo "in 4"; $haveStack = true; } } } } } } if ($haveStack) { $jsonObject = $hotelStack->sortBy('rubPrice', 5)->getHotel()->getJsonObject(); $jsonObject['cityId'] = $hotelSearchParams->city->id; $jsonObject['dateFrom'] = $hotelSearchParams->checkIn; $from = DateTime::createFromFormat('Y-m-d', $hotelSearchParams->checkIn); $jsonObject['dateTo'] = $from->add(new DateInterval('P' . $hotelSearchParams->duration . 'D'))->format('Y-m-d'); Yii::log('TRYING TO SAVE HOTEL CACHE'); $hotelCache = new HotelCache(); $hotelCache->populateFromJsonObject($jsonObject); $hotelCache->save(); } else { //echo "havent"; //VarDumper::dump($hotelStack->getJsonObject(5)); } } } } } $response->hotels = $hotels; } return $response; }
public function merge() { echo "Batch insert incoming items\n"; $dir = Yii::getPathOfAlias('application.runtime'); $fileFlightName = "query_flight_" . time() . ".batch"; $fileHotelName = "query_hotel_" . time() . ".batch"; $this->fullFlightPath = $dir . DIRECTORY_SEPARATOR . $fileFlightName; $this->fullHotelPath = $dir . DIRECTORY_SEPARATOR . $fileHotelName; $counter = 0; $fileFlight = fopen($this->fullFlightPath, 'w'); $fileHotel = fopen($this->fullHotelPath, 'w'); foreach ($this->totalCache as $cache) { $item = @unserialize($cache); if (!$item instanceof FlightCacheDump and !$item instanceof HotelCacheDump) { continue; } if ($item instanceof FlightCacheDump) { $hash = $item->from . '_' . $item->to . '_' . $item->dateFrom . '_' . $item->dateBack; $flag = isset($result[$hash]); if ($flag) { if ($item->createdAt > $result[$hash]['time']) { $attr = @unserialize($item->attributes); if (!is_array($attr)) { continue; } $flightCache = new FlightCache(); $flightCache->scenario = 'restore'; $result[$hash]['time'] = $item->createdAt; $attr['updatedAt'] = date('Y-m-d H:i:s', $item->createdAt); $flightCache->setAttributes($attr, false); $part = $flightCache->buildRow(); fwrite($fileFlight, $part); unset($flightCache); } } else { $attr = @unserialize($item->attributes); if (!is_array($attr)) { continue; } $result[$hash]['time'] = $item->createdAt; $flightCache = new FlightCache(); $attr['updatedAt'] = date('Y-m-d H:i:s', $item->createdAt); $flightCache->setAttributes($attr, false); $part = $flightCache->buildRow(); $counter++; fwrite($fileFlight, $part); unset($flightCache); } } elseif ($item instanceof HotelCacheDump) { $hash = $item->cityId . '_' . $item->dateFrom . '_' . $item->dateTo . '_' . $item->stars; $flag = isset($result[$hash]); if ($flag) { if ($item->createdAt > $result[$hash]['time']) { $attr = @unserialize($item->attributes); if (!is_array($attr)) { continue; } $hotelCache = new HotelCache(); $result[$hash]['time'] = $item->createdAt; $attr['updatedAt'] = date('Y-m-d H:i:s', $item->createdAt); $hotelCache->setAttributes($attr, false); $part = $hotelCache->buildRow(); fwrite($fileHotel, $part); unset($hotelCache); } } else { $attr = @unserialize($item->attributes); if (!is_array($attr)) { continue; } $result[$hash]['time'] = $item->createdAt; $attr['updatedAt'] = date('Y-m-d H:i:s', $item->createdAt); $hotelCache = new HotelCache(); $hotelCache->setAttributes($attr, false); $part = $hotelCache->buildRow(); $counter++; fwrite($fileHotel, $part); unset($hotelCache); } } } fclose($fileFlight); fclose($fileHotel); }