Exemplo n.º 1
0
 public function actionFullCache2()
 {
     try {
         //Yii::app()->sharedMemory->erase();
         $cache = FlightCache::model()->findAll(array('limit' => 100));
         for ($i = 0; $i < 20; $i++) {
             foreach ($cache as $c) {
                 $from = rand(1, 6000);
                 $to = rand(1, 6000);
                 $c->from = $from;
                 $c->to = $to;
                 $c->save();
             }
         }
         $result = Yii::app()->sharedMemory->read(true);
     } catch (Exception $e) {
         $this->sendError(500, $e->getMessage());
     }
 }
Exemplo n.º 2
0
 public static function getOptimalPrice($fromCityId, $toCityId, $date, $returnDate = false, $forceUpdate = false)
 {
     $flightForm = new FlightForm();
     $flightForm->adultCount = 1;
     $flightForm->childCount = 0;
     $flightForm->infantCount = 0;
     $route = new RouteForm();
     $route->departureCityId = $fromCityId;
     $route->arrivalCityId = $toCityId;
     $route->departureDate = $date;
     if ($returnDate) {
         $route->isRoundTrip = true;
         $route->backDate = $returnDate;
     }
     $flightForm->routes[] = $route;
     $flightSearchParams = self::buildSearchParams($flightForm);
     $fs = new FlightSearch();
     $fs->status = 1;
     $fs->requestId = '1';
     $fs->data = '{}';
     $criteria = new CDbCriteria();
     $criteria->addColumnCondition(array('`from`' => $fromCityId, '`to`' => $toCityId));
     $criteria->addCondition('`dateFrom` = STR_TO_DATE("' . $date . '", "%d.%m.%Y")');
     if ($returnDate) {
         $criteria->addCondition('`dateBack` = STR_TO_DATE("' . $returnDate . '", "%d.%m.%Y")');
     } else {
         $criteria->addCondition('`dateBack` = "0000-00-00"');
     }
     if ($forceUpdate) {
         $result = $fs->sendRequest($flightSearchParams);
     } else {
         $result = FlightCache::model()->find($criteria);
     }
     if ($result) {
         $return = (int) $result->priceBestPriceTime;
         if ($return == 0) {
             $return = (int) $result->priceBestPrice;
         }
         return $return;
     } else {
         throw new CException('Can\'t get best pricetime');
     }
 }
Exemplo n.º 3
0
 public function addRoundTripToCache($row, $url)
 {
     $price = ceil($row->ReturnPrice);
     $codesFrom = explode('*', $row->OutboundWebsiteFlightCode);
     $codesTo = explode('*', $row->InboundWebsiteFlightCode);
     if (sizeof($codesFrom) > 1 && sizeof($codesTo) > 1) {
         $airline = $codesFrom[2];
         $depAirportDate = $codesFrom[1];
         $arrAirportDate = end($codesFrom);
         $fromAirp = Airport::model()->findByAttributes(array('code' => substr($depAirportDate, 0, 3)));
         if (!$fromAirp) {
             echo "Airport {$depAirportDate} not defined \n";
             return false;
         }
         $toAirp = Airport::model()->findByAttributes(array('code' => substr($arrAirportDate, 0, 3)));
         if (!$toAirp) {
             echo "Airport {$arrAirportDate} not defined \n";
             return false;
         }
         $from = $fromAirp->cityId;
         $to = $toAirp->cityId;
     } else {
         $airline = '';
         $from = $this->intRouters[$url][0];
         $to = $this->intRouters[$url][1];
         if (!$from || !$to) {
             echo "Cities not defined \n";
             return false;
         }
     }
     $timeFrom = strtotime($row->OutboundDepartureDate);
     $timeBack = strtotime($row->InboundDepartureDate);
     if ($timeFrom == 0 || $timeBack == 0) {
         echo "Time does not determined\n";
         return false;
     }
     $dateFrom = date('Y-m-d', $timeFrom);
     $dateBack = date('Y-m-d', $timeBack);
     $flightCache = FlightCache::model()->findByAttributes(array('from' => $from, 'to' => $to, 'dateFrom' => $dateFrom, 'dateBack' => $dateBack));
     if (!$flightCache) {
         echo "Creating record {$from} - {$to} - {$from} at {$dateFrom} <-> {$dateBack}. It is {$price}\n";
         $flightCache = new FlightCache();
     } else {
         if ($flightCache->priceBestPrice > $price) {
             echo "Updating record {$from} - {$to} - {$from} at {$dateFrom} <-> {$dateBack}. It was {$flightCache->priceBestPrice} become {$price}\n";
         } else {
             return true;
         }
     }
     $flightCache->from = $from;
     $flightCache->to = $to;
     $flightCache->dateFrom = $dateFrom;
     $flightCache->dateBack = $dateBack;
     $flightCache->priceBestPrice = $price;
     $flightCache->transportBestPrice = $airline;
     $flightCache->validatorBestPrice = $airline;
     return $flightCache->save();
 }
Exemplo n.º 4
0
 public static function createSiblingsDataForRoundTrip(FlightSearchParams $flightSearchParams)
 {
     $siblingDays = self::buildSiblingDays($flightSearchParams);
     $result = array();
     foreach ($siblingDays as $siblingDayDirect) {
         $oneWay = array();
         foreach ($siblingDays as $siblingDayBack) {
             $criteria = new CDbCriteria();
             $criteria->select = "priceBestPrice";
             $criteria->compare('`from`', $flightSearchParams->routes[0]->departureCityId);
             $criteria->compare('`to`', $flightSearchParams->routes[0]->arrivalCityId);
             $criteria->compare('`dateFrom`', $siblingDayDirect->format('Y-m-d'));
             $criteria->compare('`dateBack`', $siblingDayBack->format('Y-m-d'));
             $cacheItem = FlightCache::model()->find($criteria);
             if ($cacheItem) {
                 $value = ceil($cacheItem->priceBestPrice * $flightSearchParams->totalPassengers / 2);
             } else {
                 $value = false;
             }
             $oneWay[] = $value;
         }
         $result[] = $oneWay;
     }
     return $result;
 }
Exemplo n.º 5
0
 /**
  * addCacheFromStack
  * Adding caches into db, flights with best paraments(price,time,pricetime)
  * @param FlightVoyageStack $flightVoyageStack
  */
 public static function addCacheFromStack(FlightVoyageStack $flightVoyageStack)
 {
     $flightCache = new FlightCache();
     if (sizeof($flightVoyageStack->flightVoyages) == 0) {
         return false;
     }
     $firstVoyage = $flightVoyageStack->flightVoyages[0];
     //we aren't saving complex voyage
     if ($firstVoyage->isComplex()) {
         Yii::log("COMPLEX FLIGHT - DOESNOT SAVE IT", 'info');
         return;
     }
     $withReturn = count($firstVoyage->flights) == 2;
     //working on dates
     $flightCache->dateFrom = $firstVoyage->flights[0]->departureDate;
     if ($withReturn) {
         $flightCache->dateBack = $firstVoyage->flights[1]->departureDate;
     }
     //working on from and to cities
     $flightCache->from = $firstVoyage->getDepartureCity()->id;
     $flightCache->to = $firstVoyage->getArrivalCity()->id;
     if ($flightVoyageStack->bestPriceInd !== null) {
         $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd];
         $flightCache->setFromFlightVoyage($voyage, 'BestPrice');
     }
     if ($flightVoyageStack->bestTimeInd !== null) {
         $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd];
         $flightCache->setFromFlightVoyage($voyage, 'BestTime');
     }
     if ($flightVoyageStack->bestPriceTimeInd !== null) {
         $voyage = $flightVoyageStack->flightVoyages[$flightVoyageStack->bestPriceInd];
         $flightCache->setFromFlightVoyage($voyage, 'BestPriceTime');
     }
     if (!$flightCache->validate()) {
         Yii::log("AFTER CALCULATIONS WE GET BAD FLIGHT CACHE: \n" . CVarDumper::dumpAsString($flightCache->errors), 'info');
         throw new CException("Can't save flight cache item." . print_r($flightCache->errors, true) . print_r($flightCache->attributes, true));
     }
     Yii::log("TRYING TO SAVE FLIGHT CACHE", 'info');
     $flightCache->save();
     return $flightCache;
 }
Exemplo n.º 6
0
 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);
 }
Exemplo n.º 7
0
 public function actionRTFlight($countryCode = '', $cityCodeFrom = '', $cityCodeTo = '')
 {
     if (!($country = $this->testCountry($countryCode))) {
         return false;
     }
     $this->morphy = Yii::app()->morphy;
     $countryUp = mb_strtoupper($country->localRu, 'utf-8');
     $countryMorph = array('caseAcc' => $this->getCase($countryUp, 'ВН'));
     if (!$cityCodeFrom) {
         $currentCity = City::getCityByCode('LED');
     } else {
         $currentCity = City::getCityByCode($cityCodeFrom);
     }
     $city = City::getCityByCode($cityCodeTo);
     $citiesFrom = array();
     $this->addCityFrom($citiesFrom, 4466);
     // Moscow
     $this->addCityFrom($citiesFrom, 5185);
     // Spb
     $this->addCityFrom($citiesFrom, $currentCity->id);
     Yii::import('site.common.modules.hotel.models.*');
     $hotelClient = new HotelBookClient();
     /*$criteria = new CDbCriteria();
             $criteria->addCondition('countAirports > 0');
             $criteria->addCondition('countryId = '.$country->id);
             $cities = City::model()->findAll($criteria);
             $cityIds = array();
             foreach($cities as $city){
                 $cityIds[$city->id] = $city->id;
             }
     
             if(isset($cityIds[$currentCity->id])){
                 unset($cityIds[$currentCity->id]);
             }*/
     $criteria = new CDbCriteria();
     $criteria->addCondition('`to` = ' . $city->id);
     $criteria->addCondition('`from` = ' . $currentCity->id);
     //$criteria->group = 'dateBack';
     //$criteria->addCondition('`from` = '.$currentCity->id);
     $criteria->addCondition('`dateFrom` >= ' . date("'Y-m-d'"));
     $criteria->addCondition('`dateFrom` <= ' . date("'Y-m-d'", time() + 3600 * 24 * 18));
     $criteria->addCondition('`dateBack` >= ' . date("'Y-m-d'"));
     $criteria->addCondition('`dateBack` <= ' . date("'Y-m-d'", time() + 3600 * 24 * 18));
     //$criteria->addCondition("`dateBack` <> '0000-00-00'");
     $criteria->order = 'priceBestPrice';
     //$criteria->limit = 18;
     $flightCache = FlightCache::model()->findAll($criteria);
     /*$sortFc = array();
       foreach($flightCache as $fc){
           $k = strtotime($fc->dateFrom);
           $sortFc[$k] = $fc;//array('price'=>$fc->priceBestPrice,'date'=>$fc->dateFrom,'dateBack'=>$fc->dateBack);
       }
       ksort($sortFc);*/
     //$sortFc = array_slice($sortFc,0,18);
     $minPrice = false;
     $maxPrice = false;
     $activeMin = null;
     $activeMax = null;
     foreach ($flightCache as $k => $fc) {
         $k = strtotime($fc->dateFrom);
         if (!$minPrice) {
             $minPrice = $fc->priceBestPrice;
             $maxPrice = $fc->priceBestPrice;
         } else {
             if ($fc->priceBestPrice < $minPrice) {
                 $minPrice = $fc->priceBestPrice;
                 $activeMin = $k;
             }
             if ($fc->priceBestPrice > $maxPrice) {
                 $maxPrice = $fc->priceBestPrice;
                 $activeMax = $k;
             }
         }
     }
     $sortFc = array();
     foreach ($flightCache as $fc) {
         //$k = strtotime($fc->dateFrom);
         $sortFc[] = array('price' => $fc->priceBestPrice, 'date' => $fc->dateFrom, 'dateBack' => $fc->dateBack);
     }
     //print_r($flightCache);die();
     $criteria = new CDbCriteria();
     //$criteria->addInCondition('`cityId`',$cityIds);
     $criteria->addCondition('cityId = ' . $city->id);
     $criteria->limit = 15;
     $hotelCache = HotelDb::model()->findAll($criteria);
     $hotelsInfo = array();
     foreach ($hotelCache as $hc) {
         $hotelInfo = $hotelClient->hotelDetail($hc->id);
         $hotelInfo->price = $hc->minPrice;
         $hotelInfo->hotelName = $hc->name;
         $hotelInfo->hotelId = $hc->id;
         $hotelsInfo[] = $hotelInfo;
     }
     $criteria = new CDbCriteria();
     //$criteria->addInCondition('`to`',$cityIds);
     $criteria->addCondition('`from` = ' . $currentCity->id);
     $criteria->addCondition('`dateFrom` > \'' . date('Y-m-d') . "'");
     $criteria->order = 'priceBestPrice';
     $criteria->limit = 14;
     $criteria->group = '`to`';
     $flightCacheFromCurrent = FlightCache::model()->findAll($criteria);
     $this->layout = 'static';
     //$this->render('landing');
     $this->render('rtflight', array('city' => $city, 'citiesFrom' => $citiesFrom, 'hotelsInfo' => $hotelsInfo, 'currentCity' => $currentCity, 'flightCache' => $sortFc, 'maxPrice' => $maxPrice, 'minPrice' => $minPrice, 'activeMin' => $activeMin, 'flightCacheFromCurrent' => $flightCacheFromCurrent));
 }