Example #1
0
 public function parseAtomFeed()
 {
     $url = Yii::$app->params['AtomFeedParser']['url'];
     $startTime = time();
     $needToUpdateCAP = false;
     try {
         $atomGeneralInformation = Yii::$app->CAPParser->getAtomGeneralContent($url);
         $atomEntriesContent = Yii::$app->CAPParser->getEntriesContent($url);
         if (!isset($atomGeneralInformation['updated']) || !isset($atomGeneralInformation['id'])) {
             throw new Exception("Can't parse AtomFeed file by given URL");
         }
         $processedFeedsModel = new models\ProcessedATOMFeeds();
         $processedFeedsModelSearch = new models\ProcessedATOMFeedsSearch();
         $atomRecordInDb = $processedFeedsModelSearch->findOne(['feedURL' => $atomGeneralInformation['id']]);
         if (!$atomRecordInDb) {
             //We don't have records at all for this file
             $processedFeedsModel->updated = helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated']);
             $processedFeedsModel->feedURL = $atomGeneralInformation['id'];
             $processedFeedsModel->save();
             $this->consoleLog('Added ProcessedATOMFeeds Record, id - ' . $processedFeedsModel->feedURL . ', updated - ' . helpers\DateTimeHelper::createDateFromUnixtime($processedFeedsModel->updated) . '(' . $atomGeneralInformation['updated'] . ')');
             $needToUpdateCAP = true;
         } else {
             if ($atomRecordInDb->updated != helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated'])) {
                 $this->consoleLog('We need to update records in WeatherAlert and WeatherAlertArea tables.Updated time in the database -' . helpers\DateTimeHelper::createDateFromUnixtime($atomRecordInDb->updated) . ', from file - ' . $atomGeneralInformation['updated'] . ' (' . helpers\DateTimeHelper::createDateFromUnixtime(helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated'])) . ')');
                 $atomRecordInDb->updated = helpers\DateTimeHelper::createUnixtimeFromDate($atomGeneralInformation['updated']);
                 $atomRecordInDb->save();
                 $needToUpdateCAP = true;
             }
         }
         if (!$needToUpdateCAP) {
             $this->consoleLog('We DON\'T need to update records in WeatherAlert and WeatherAlertArea tables.');
         }
         if ($needToUpdateCAP) {
             //change 1 to $needToUpdateCAP
             foreach ($atomEntriesContent['entries'] as $entry) {
                 $entryContent = $this->_getCapAreaInformationInDatabse($entry);
                 if ($entryContent) {
                     // Now we try to find this alert and check if we have it already in our database
                     $alertModel = models\WeatherAlert::find()->where(['identifier' => $entryContent['identifier'], 'date' => helpers\DateTimeHelper::createUnixtimeFromDate($entryContent['date'])])->one();
                     if (!$alertModel) {
                         $alertModel = new models\WeatherAlert();
                     } else {
                         $this->skippedAlertsCount++;
                         $this->consoleLog('One alert entry skipped (Information about this alert is actual in our database, ID = ' . $alertModel->id . ')');
                         continue;
                     }
                     $alertModel->attributes = $entryContent;
                     $alertModel->date = helpers\DateTimeHelper::createUnixtimeFromDate($alertModel->date);
                     $alertModel->save();
                     $this->consoleLog('One alert entry added (ID: ' . $alertModel->id . ')');
                     $this->addedAlertsCount++;
                     if ($alertModel->updates) {
                         //WE need to change status of old alert
                         //TODO: this part of code should be checked on real data
                         $alertModel = models\WeatherAlert::find()->where(['identifier' => $alertModel->updates])->one();
                         $alertModel->status = models\WeatherAlert::STATUS_UPDATED;
                         $alertModel->save();
                     }
                     $weatherAlertAreaModel = new models\WeatherAlertArea();
                     $weatherAlertAreaModel->WeatherAlert_id = $alertModel->id;
                     $weatherAlertAreaModel->save();
                     if (isset($entryContent['polygon']['0'])) {
                         $coordinatePairs = explode(' ', $entryContent['polygon']['0']);
                         foreach ($coordinatePairs as $pair) {
                             $areaDefinitionModel = new models\AreaDefinition();
                             $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
                             $latLonFromPair = explode(',', $pair);
                             $areaDefinitionModel->latitude = $latLonFromPair[0];
                             $areaDefinitionModel->longitude = $latLonFromPair[1];
                             $areaDefinitionModel->save();
                         }
                     }
                     if (isset($entryContent['circle']['0'])) {
                         $coordinatePair = explode(' ', $entryContent['polygon']['0']);
                         $areaDefinitionModel = new models\AreaDefinition();
                         $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
                         $latLonFromPair = explode(',', $coordinatePair);
                         $areaDefinitionModel->latitude = $latLonFromPair[0];
                         $areaDefinitionModel->longitude = $latLonFromPair[1];
                         $areaDefinitionModel->radius = $coordinatePair[1];
                         $areaDefinitionModel->save();
                     }
                 }
             }
         }
         $endTime = time();
         $this->consoleLog('Start date - ' . date('Y-m-d\\TH:i:s', $startTime));
         $this->consoleLog('End date - ' . date('Y-m-d\\TH:i:s', $endTime));
         $this->consoleLog('Execution Time - ' . ($endTime - $startTime) . ' seconds.');
         $this->consoleLog('Count of added alerts - ' . $this->addedAlertsCount);
         $this->consoleLog('Count of skipped alerts - ' . $this->skippedAlertsCount);
         $atomGeneralInformation = array_merge(['start' => date('Y-m-d\\TH:i:s', $startTime), 'end' => date('Y-m-d\\TH:i:s', $endTime)], $atomGeneralInformation);
         $atomGeneralInformation['entries'] = isset($atomEntriesContent['entries']) ? $atomEntriesContent['entries'] : [];
         return $atomGeneralInformation;
     } catch (\Exception $e) {
         $this->consoleLog('Process ERROR -  ' . $e->getMessage());
         Yii::$app->end();
     }
 }
Example #2
0
 public function insertNewLSRRecords($records)
 {
     foreach ($records['alerts'] as $record) {
         if (in_array(strtolower($record['event']), $this->filterEventType)) {
             $weatherAlertsModel = new models\WeatherAlert();
             switch (strtolower($record['event'])) {
                 case 'hurricane':
                     $weatherAlertsModel->event = 0;
                     break;
                 case 'tornado':
                     $weatherAlertsModel->event = 1;
                     break;
                 default:
                     $weatherAlertsModel->event = 2;
                     break;
             }
             $weatherAlertsModel->status = models\WeatherAlert::STATUS_ACTUAL;
             $weatherAlertsModel->type = 1;
             $weatherAlertsModel->magnitude = $record['magnitude'];
             $weatherAlertsModel->magnitudeUnit = $record['magnitudeUnit'];
             //We need to display date in needed format
             $newTime = substr_replace($record['time'], ':', 2, 0);
             //                $newDate = str_replace('/', '-', $record['date']);
             $dateforConvertionArray = explode('/', $record['date']);
             $finalDateTime = $dateforConvertionArray[2] . '-' . $dateforConvertionArray[0] . '-' . $dateforConvertionArray[1] . 'T' . date("H:i:s", strtotime($newTime));
             $weatherAlertsModel->date = DateTimeHelper::createUnixtimeFromDate($finalDateTime);
             $weatherAlertsModel->save();
             $weatherAlertAreaModel = new models\WeatherAlertArea();
             $weatherAlertAreaModel->WeatherAlert_id = $weatherAlertsModel->id;
             $weatherAlertAreaModel->save();
             $areaDefinitionModel = new models\AreaDefinition();
             $areaDefinitionModel->WeatherAlertArea_id = $weatherAlertAreaModel->id;
             $areaDefinitionModel->latitude = $record['latitude'];
             $areaDefinitionModel->longitude = $record['longitude'];
             $areaDefinitionModel->save();
             $this->insertedAlerts++;
             $this->consoleLog('One alert entry added (ID: ' . $weatherAlertsModel->id . ')');
         } else {
             $this->ignoredAlerts++;
             $this->consoleLog('One alert entry skipped (Filtered by event type)');
         }
     }
 }
Example #3
0
 public function generatePreStorm()
 {
     if ((int) $this->countPreStorm) {
         for ($i = 0; $i < (int) $this->countPreStorm; $i++) {
             $preStormModel = new WeatherAlert();
             $preStormModel->type = 0;
             //Pre-storm
             $preStormModel->status = WeatherAlert::STATUS_ACTUAL;
             $preStormModel->date = time();
             $preStormModel->event = rand(0, 1);
             $preStormModel->msgType = 1;
             $preStormModel->severity = 'Moderate';
             $preStormModel->save();
             $weatherAlertArea = new WeatherAlertArea();
             $weatherAlertArea->WeatherAlert_id = $preStormModel->id;
             $weatherAlertArea->save();
             $this->_generateCoordinates($weatherAlertArea->id, false, rand(3, 6));
         }
     }
     if ((int) $this->countPostStorm) {
         for ($i = 0; $i < (int) $this->countPostStorm; $i++) {
             $preStormModel = new WeatherAlert();
             $preStormModel->type = 1;
             //Pre-storm
             $preStormModel->status = WeatherAlert::STATUS_ACTUAL;
             $preStormModel->date = time();
             $preStormModel->event = rand(0, 1);
             $preStormModel->msgType = 1;
             $preStormModel->magnitude = rand(1, 10);
             $preStormModel->save();
             $weatherAlertArea = new WeatherAlertArea();
             $weatherAlertArea->WeatherAlert_id = $preStormModel->id;
             $weatherAlertArea->save();
             $this->_generateCoordinates($weatherAlertArea->id, true);
         }
     }
     if ((int) $this->countStormPath) {
         $this->isStormPath = true;
         for ($i = 0; $i < (int) $this->countStormPath; $i++) {
             //generate pre-storm path
             $stormCount = rand(3, 6);
             $identifierOfUpdates = null;
             $event = rand(0, 1);
             for ($j = 0; $j < $stormCount; $j++) {
                 $isLast = $stormCount - 1 - $j ? false : true;
                 $preStormModel = new WeatherAlert();
                 $preStormModel->status = WeatherAlert::STATUS_UPDATED;
                 $preStormModel->type = 0;
                 //Pre-storm
                 if ($isLast) {
                     $preStormModel->status = WeatherAlert::STATUS_ACTUAL;
                 }
                 $preStormModel->date = time();
                 $preStormModel->event = $event;
                 $preStormModel->identifier = 'identifier' . $j;
                 $preStormModel->severity = 'Moderate';
                 $preStormModel->msgType = 1;
                 $preStormModel->updates = $identifierOfUpdates;
                 $preStormModel->save();
                 $identifierOfUpdates = $preStormModel->identifier;
                 $weatherAlertArea = new WeatherAlertArea();
                 $weatherAlertArea->WeatherAlert_id = $preStormModel->id;
                 $weatherAlertArea->save();
                 $this->_generateCoordinates($weatherAlertArea->id, false);
             }
         }
     }
 }