Exemple #1
0
 private function saveReports($id, $settings)
 {
     SettingEntity::setValue($id, $settings['enabled']);
     $uuids = array();
     foreach ($settings['items'] as $item) {
         $report = new ReportEntity();
         if ($item['uuid']) {
             $report->findPk($item['uuid']);
         }
         $report->subjectType = $item['subjectType'];
         $subject = null;
         if ($report->subjectType == ReportEntity::SUBJECT_TYPE_CC) {
             $subject = $this->getContainer()->analytics->ccs->get($item['subjectId']);
         } elseif ($report->subjectType == ReportEntity::SUBJECT_TYPE_PROJECT) {
             $subject = $this->getContainer()->analytics->projects->get($item['subjectId']);
         } else {
             $report->subjectType = null;
             $report->subjectId = null;
         }
         if ($report->subjectType) {
             if ($item['subjectId'] && !$subject) {
                 throw new Scalr_UI_Exception_NotFound();
             }
             $report->subjectId = $item['subjectId'] ? $item['subjectId'] : null;
         }
         $report->period = $item['period'];
         $report->emails = $item['emails'];
         $report->save();
         $uuids[] = $report->uuid;
     }
     foreach (ReportEntity::all() as $report) {
         if (!in_array($report->uuid, $uuids)) {
             $report->delete();
         }
     }
 }
Exemple #2
0
 /**
  * xSavePriceAction
  *
  * @param  string   $platform              The cloud platform
  * @param  string   $cloudLocation         The cloud location
  * @param  JsonData $prices                Price list
  * @param  string   $url                   optional The url of the cloud
  * @param  string   $effectiveDate         optional The date when the prices will be applied
  * @param  boolean  $forbidAutomaticUpdate optional
  */
 public function xSavePriceAction($platform, $cloudLocation, JsonData $prices, $url = '', $effectiveDate = null, $forbidAutomaticUpdate = false)
 {
     list($curdate, $effectiveDate) = $this->handleEffectiveDate($effectiveDate);
     $service = $this->getContainer()->analytics->prices;
     $priceHistory = new PriceHistoryEntity();
     $priceHistory->platform = $platform;
     $priceHistory->cloudLocation = $cloudLocation;
     $priceHistory->url = $service->normalizeUrl($url);
     $priceHistory->accountId = $this->user->getAccountId();
     $priceHistory->applied = $effectiveDate;
     $this->getContainer()->analytics->prices->get($priceHistory);
     if ($priceHistory->priceId !== null) {
         //We have found recent prices
         if ($effectiveDate->format('Y-m-d') != $priceHistory->applied->format('Y-m-d')) {
             //We should generate a new version of the price
             $priceHistory->priceId = null;
             $priceHistory->applied = $effectiveDate;
         }
     }
     $details = new ArrayCollection();
     $found = [];
     foreach ($prices as $price) {
         if ($price['type'] && $price['priceLinux']) {
             $item = new PriceEntity();
             $item->instanceType = $price['type'];
             $item->os = PriceEntity::OS_LINUX;
             $item->name = $price['name'];
             $item->cost = floatval($price['priceLinux']);
             $found[$item->instanceType . '-' . $item->os] = $item;
             $details->append($item);
         }
         if ($price['type'] && $price['priceWindows']) {
             $item = new PriceEntity();
             $item->instanceType = $price['type'];
             $item->os = PriceEntity::OS_WINDOWS;
             $item->name = $price['name'];
             $item->cost = floatval($price['priceWindows']);
             $found[$item->instanceType . '-' . $item->os] = $item;
             $details->append($item);
         }
     }
     //Gets actual pricing on date from databse
     $collection = $this->getContainer()->analytics->prices->getActualPrices($platform, $cloudLocation, $priceHistory->url, $priceHistory->applied);
     //Compares if some price has been changed.
     if (!$collection->count()) {
         //There aren't any prices yet. We need to save them.
         $bChanged = true;
     } else {
         $bChanged = false;
         //For each previous price compare difference with new one
         foreach ($collection as $priceEntity) {
             $key = $priceEntity->instanceType . '-' . $priceEntity->os;
             if (isset($found[$key])) {
                 if (abs($found[$key]->cost - $priceEntity->cost) > 9.0E-7) {
                     unset($found[$key]);
                     $bChanged = true;
                     break;
                 }
                 unset($found[$key]);
             } else {
                 $bChanged = true;
                 break;
             }
         }
         //Some new price for new instance type is added while other prices reman untouched
         if (!$bChanged && count($found)) {
             foreach ($found as $priceEntity) {
                 //Zerro prices should not be taken into account if they don't exist before
                 if ($priceEntity->cost >= 1.0E-6) {
                     $bChanged = true;
                     break;
                 }
             }
         }
     }
     if ($bChanged) {
         //Saving actually only when there is some change
         $priceHistory->setDetails($details);
         $service->save($priceHistory);
         $this->getContainer()->analytics->events->fireChangeCloudPricingEvent($platform, $url);
     }
     if ($priceHistory->platform == SERVER_PLATFORMS::EC2) {
         SettingEntity::setValue(SettingEntity::ID_FORBID_AUTOMATIC_UPDATE_AWS_PRICES, $forbidAutomaticUpdate ? '1' : '0');
     }
     $this->response->success('Prices have been updated');
 }