С версии: 5.0
Автор: Vlad Dobrovolskiy (v.dobrovolskiy@scalr.com)
Наследование: extends Scalr\Model\AbstractEntity
Пример #1
0
 /**
  * View report action
  *
  * @param string $uuid          Report uuid
  * @param string $secretHash    Report secret hash
  * @throws Scalr_UI_Exception_NotFound
  */
 public function reportAction($uuid, $secretHash)
 {
     $data = ReportPayloadEntity::findOne([['uuid' => $uuid], ['secret' => hex2bin($secretHash)]]);
     if (empty($data) || !property_exists($data, 'payload')) {
         throw new Scalr_UI_Exception_NotFound();
     }
     $this->response->page('ui/public/report.js', json_decode($data->payload, true), [], ['ui/analytics/analytics.css', 'ui/admin/analytics/admin.css', 'ui/public/report.css']);
 }
 /**
  * Saves project or cost center notification
  *
  * @param ProjectEntity|CostCentreEntity $subject       Project or cost center entity
  * @param NotificationEntity             $notification  Current notification object
  * @throws InvalidArgumentException
  */
 private function saveNotificationData($subject, NotificationEntity $notification)
 {
     $baseUrl = rtrim(\Scalr::getContainer()->config('scalr.endpoint.scheme') . "://" . \Scalr::getContainer()->config('scalr.endpoint.host'), '/');
     $quarters = new Quarters(SettingEntity::getQuarters());
     $date = $quarters->getPeriodForDate('yesterday');
     $formatedTitle = 'Q' . $quarters->getQuarterForDate('now') . ' budget (' . (new \DateTime('now', new \DateTimeZone('UTC')))->format('M j, Y') . ')';
     if ($subject instanceof ProjectEntity) {
         $getPeriodicSubjectData = 'getProjectPeriodData';
         $subjects = 'projects';
         $childItems = 'farms';
         $subjectIdName = 'projectId';
         $subjectName = 'project';
     } else {
         if ($subject instanceof CostCentreEntity) {
             $getPeriodicSubjectData = 'getCostCenterPeriodData';
             $subjects = 'costcenters';
             $childItems = 'projects';
             $subjectIdName = 'ccId';
             $subjectName = 'cc';
         } else {
             throw new InvalidArgumentException("Invalid subject parameter. It must be either ProjectEntity or CostCentreEntity type.");
         }
     }
     $periodSubjectData = \Scalr::getContainer()->analytics->usage->{$getPeriodicSubjectData}($subject->{$subjectIdName}, 'quarter', $date->start->format('Y-m-d'), $date->end->format('Y-m-d'));
     $subjectAnalytics = ['budget' => $periodSubjectData['totals']['budget'], 'name' => $subject->name, 'trends' => $periodSubjectData['totals']['trends'], 'forecastCost' => $periodSubjectData['totals']['forecastCost'], 'interval' => $periodSubjectData['interval'], 'date' => $formatedTitle, 'detailsUrl' => $baseUrl . '#/analytics/' . $subjects . '?' . $subjectIdName . '=' . $subject->{$subjectIdName}, 'jsonVersion' => '1.0.0', $childItems => []];
     if (!empty($periodSubjectData['totals'][$childItems])) {
         $subjectAnalytics[$childItems] = $this->getSubjectChildItems($subject, $periodSubjectData['totals'][$childItems], $date);
     }
     if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_USAGE) {
         $reportType = $subjectName . 'Usage';
         $budgetThreshold = 'budgetSpentPct';
         $emailSubject = $subjectAnalytics['name'] . ' usage notification.';
     } else {
         if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_PROJECTED_OVERSPEND) {
             $reportType = $subjectName . 'Overspend';
             $budgetThreshold = 'estimateOverspendPct';
             $emailSubject = $subjectAnalytics['name'] . ' overspend notification.';
         }
     }
     if ($subjectAnalytics['budget'][$budgetThreshold] >= $notification->threshold) {
         $subjectAnalytics['reportType'] = $reportType;
         $entity = ReportPayloadEntity::init([$notification->notificationType, $notification->subjectType, $subject->{$subjectIdName}, $notification->threshold], $subjectAnalytics);
         if (!ReportPayloadEntity::findPk($entity->uuid)) {
             $payload = json_decode($entity->payload, true);
             if (!empty($subjectAnalytics['budget']['estimateDate'])) {
                 $subjectAnalytics['budget']['estimateDate'] = (new DateTime($subjectAnalytics['budget']['estimateDate'], new DateTimeZone('UTC')))->format('M j, Y');
                 $subjectAnalytics['reportUrl'] = $payload['reportUrl'];
             }
             \Scalr::getContainer()->mailer->setSubject($emailSubject)->setContentType('text/html')->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/budget_notification_' . $subjectName . '.html.php', $subjectAnalytics, $notification->emails);
             $this->getLogger()->info('Notification email has been sent');
             $payload['date'] = $entity->created->format('Y-m-d');
             $entity->payload = json_encode($payload);
             $entity->save();
         }
     }
 }
 /**
  * Saves notifications
  *
  * @param string             $subject           Subject name (project or cc)
  * @param NotificationEntity $notification      Notification entity
  * @param string             $subjectId         Cost Center or project id
  * @param array              $subjectAnalytics  Array of subjects with data to save
  */
 private function saveNotificationData($subject, $notification, $subjectId, &$subjectAnalytics)
 {
     if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_USAGE) {
         $reportType = $subject . 'Usage';
         $budgetThreshold = 'budgetSpentPct';
         $emailSubject = $subjectAnalytics[$subjectId]['name'] . ' usage notification.';
     } else {
         if ($notification->notificationType === NotificationEntity::NOTIFICATION_TYPE_PROJECTED_OVERSPEND) {
             $reportType = $subject . 'Overspend';
             $budgetThreshold = 'estimateOverspendPct';
             $emailSubject = $subjectAnalytics[$subjectId]['name'] . ' overspend notification.';
         }
     }
     if ($subjectAnalytics[$subjectId]['budget'][$budgetThreshold] >= $notification->threshold) {
         $subjectAnalytics[$subjectId]['reportType'] = $reportType;
         $entity = ReportPayloadEntity::init([$notification->notificationType, $notification->subjectType, $subjectId, $notification->threshold], $subjectAnalytics[$subjectId]);
         if (!ReportPayloadEntity::findPk($entity->uuid)) {
             $payload = json_decode($entity->payload, true);
             if (!empty($subjectAnalytics[$subjectId]['budget']['estimateDate'])) {
                 $subjectAnalytics[$subjectId]['budget']['estimateDate'] = (new DateTime($subjectAnalytics[$subjectId]['budget']['estimateDate'], new DateTimeZone('UTC')))->format('M j, Y');
                 $subjectAnalytics[$subjectId]['reportUrl'] = $payload['reportUrl'];
             }
             \Scalr::getContainer()->mailer->setSubject($emailSubject)->setContentType('text/html')->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/budget_notification_' . $subject . '.html.php', $subjectAnalytics[$subjectId], $notification->emails);
             $this->console->out('Notification email has been sent');
             $payload['date'] = $entity->created->format('Y-m-d');
             $entity->payload = json_encode($payload);
             $entity->save();
         }
     }
 }