/** * Saves/modifies/deletes notifications * * @param int $subjectType Notification subject type * @param array $settings Array of notifications to create/modify * @param string $projectId optional Projects id. * @throws \Scalr\Exception\ModelException */ protected function saveNotifications($subjectType, $settings, $projectId = null) { $uuids = []; foreach ($settings['items'] as $item) { $notification = new NotificationEntity(); if ($item['uuid']) { $notification->findPk($item['uuid']); if (!$notification->hasAccessPermissions($this->getUser())) { continue; } } $notification->subjectType = $subjectType; $notification->subjectId = $item['subjectId'] ? $item['subjectId'] : null; $notification->notificationType = $item['notificationType']; $notification->threshold = $item['threshold']; $notification->recipientType = $item['recipientType']; $notification->emails = $item['emails']; $notification->status = $item['status']; $notification->save(); $uuids[] = $notification->uuid; } $criteria = [['subjectType' => $subjectType], ['accountId' => null]]; if ($projectId) { $criteria[] = ['subjectId' => $projectId]; } foreach (NotificationEntity::find($criteria) as $notification) { /* @var $notification NotificationEntity */ if (!in_array($notification->uuid, $uuids) && $notification->hasAccessPermissions($this->getUser())) { $notification->delete(); } } }
/** * @param JsonData $notifications */ public function xSaveAction(JsonData $notifications) { $data = []; foreach ($notifications as $id => $settings) { if ($id == 'reports') { $this->saveReports($settings); $data[$id] = ReportEntity::find([['accountId' => null]])->getArrayCopy(); } elseif ($id == 'notifications.ccs') { $this->saveNotifications(NotificationEntity::SUBJECT_TYPE_CC, $settings); $data[$id] = NotificationEntity::findBySubjectType(NotificationEntity::SUBJECT_TYPE_CC)->getArrayCopy(); } elseif ($id == 'notifications.projects') { $this->saveNotifications(NotificationEntity::SUBJECT_TYPE_PROJECT, $settings); $data[$id] = NotificationEntity::find([['accountId' => null], ['subjectType' => NotificationEntity::SUBJECT_TYPE_PROJECT]])->getArrayCopy(); } } $this->response->data($data); $this->response->success('Notifications successfully saved'); }
/** * @param JsonData $notifications */ public function xSaveAction(JsonData $notifications) { $data = []; $projects = []; foreach ($this->getContainer()->analytics->projects->getAccountProjects($this->user->getAccountId()) as $project) { /* @var $project ProjectEntity */ if (!$project->archived && $project->shared === ProjectEntity::SHARED_WITHIN_ACCOUNT) { $projects[$project->projectId] = $project->name; } } foreach ($notifications as $id => $settings) { if ($id == 'reports') { $this->saveReports($settings); $data[$id] = ReportEntity::find([['subjectType' => ReportEntity::SUBJECT_TYPE_PROJECT], ['$or' => [['subjectId' => ['$in' => array_keys($projects)]], ['subjectId' => null]]]])->getArrayCopy(); } elseif ($id == 'notifications.projects') { $this->saveNotifications(NotificationEntity::SUBJECT_TYPE_PROJECT, $settings); $data[$id] = NotificationEntity::find([['subjectType' => NotificationEntity::SUBJECT_TYPE_PROJECT], ['$or' => [['subjectId' => ['$in' => array_keys($projects)]], ['subjectId' => null]]]])->getArrayCopy(); } } $this->response->data($data); $this->response->success('Notifications successfully saved'); }
/** * @param string $ccId * @param string $subjectType * @param array $settings * @throws \Scalr\Exception\ModelException */ private function saveNotifications($ccId, $subjectType, $settings) { $uuids = array(); foreach ($settings['items'] as $item) { $notification = new NotificationEntity(); if ($item['uuid']) { $notification->findPk($item['uuid']); } $notification->subjectType = $subjectType; $notification->subjectId = $ccId; $notification->notificationType = $item['notificationType']; $notification->threshold = $item['threshold']; $notification->recipientType = $item['recipientType']; $notification->emails = $item['emails']; $notification->status = $item['status']; $notification->save(); $uuids[] = $notification->uuid; } foreach (NotificationEntity::find([['subjectType' => NotificationEntity::SUBJECT_TYPE_CC], ['subjectId' => $ccId]]) as $notification) { if (!in_array($notification->uuid, $uuids)) { $notification->delete(); } } }
/** * {@inheritdoc} * @see \Scalr\System\Zmq\Cron\TaskInterface::enqueue() */ public function enqueue() { if (!\Scalr::getContainer()->analytics->enabled) { $this->getLogger()->info("Terminating the process as Cost analytics is disabled in the config.\n"); return new ArrayObject(); } $this->getLogger()->info("%s (UTC) Start Analytics Notifications process", gmdate('Y-m-d')); $notifications = NotificationEntity::find(); $this->getLogger()->info('Calculating data for projects and cost centers notifications'); foreach ($notifications as $notification) { /* @var $notification NotificationEntity */ if ($notification->status === NotificationEntity::STATUS_DISABLED) { continue; } if ($notification->subjectType === NotificationEntity::SUBJECT_TYPE_CC) { $subjectEntityName = 'Scalr\\Stats\\CostAnalytics\\Entity\\CostCentre'; } else { if ($notification->subjectType === NotificationEntity::SUBJECT_TYPE_PROJECT) { $subjectEntityName = 'Scalr\\Stats\\CostAnalytics\\Entity\\Project'; } } if (!empty($notification->subjectId)) { $subject = call_user_func($subjectEntityName . 'Entity::findPk', $notification->subjectId); $this->saveNotificationData($subject, $notification); } else { $subjects = call_user_func($subjectEntityName . 'Entity::find'); foreach ($subjects as $subject) { if ($subject->archived) { continue; } $this->saveNotificationData($subject, $notification); } } } $this->getLogger()->info('Calculating data for reports'); $reports = ReportEntity::find(); foreach ($reports as $report) { /* @var $report ReportEntity */ if ($report->status === ReportEntity::STATUS_DISABLED) { continue; } switch ($report->period) { case ReportEntity::PERIOD_DAILY: $period = 'custom'; $start = (new \DateTime('yesterday', new \DateTimeZone('UTC')))->format('Y-m-d'); $end = $start; $startForecast = (new \DateTime('first day of this month', new \DateTimeZone('UTC')))->format('Y-m-d'); $endForecast = (new \DateTime('last day of this month', new \DateTimeZone('UTC')))->format('Y-m-d'); if ($startForecast == (new \DateTime('now', new \DateTimeZone('UTC')))->format('Y-m-d')) { $startForecast = (new \DateTime('first day of last month', new \DateTimeZone('UTC')))->format('Y-m-d'); $endForecast = (new \DateTime('last day of last month', new \DateTimeZone('UTC')))->format('Y-m-d'); } $periodForecast = 'month'; $formatedTitle = (new \DateTime($start, new \DateTimeZone('UTC')))->format('M j'); $formatedForecastDate = (new \DateTime($start, new \DateTimeZone('UTC')))->format('F'); break; case ReportEntity::PERIOD_MONTHLY: $period = 'month'; $start = (new \DateTime('first day of last month', new \DateTimeZone('UTC')))->format('Y-m-d'); $end = (new \DateTime('last day of last month', new \DateTimeZone('UTC')))->format('Y-m-d'); $formatedTitle = (new \DateTime($start, new \DateTimeZone('UTC')))->format('M Y'); break; case ReportEntity::PERIOD_QUARTELY: $period = 'quarter'; $quarters = new Quarters(SettingEntity::getQuarters()); $currentQuarter = $quarters->getQuarterForDate(new \DateTime('yesterday', new \DateTimeZone('UTC'))); $currentYear = (new \DateTime('yesterday', new \DateTimeZone('UTC')))->format('Y'); if ($currentQuarter === 1) { $quarter = 4; $year = $currentYear - 1; } else { $quarter = $currentQuarter - 1; $year = $currentYear; } $date = $quarters->getPeriodForQuarter($quarter, $year); $start = $date->start->format('Y-m-d'); $end = $date->end->format('Y-m-d'); $formatedTitle = 'Q' . $quarter . ' ' . $year; $formatedForecastDate = 'End of ' . $currentYear; $startForecast = $currentYear . '-01-01'; $endForecast = $currentYear . '-12-31'; $periodForecast = 'year'; break; case ReportEntity::PERIOD_WEEKLY: $period = 'week'; $end = (new \DateTime('yesterday', new \DateTimeZone('UTC')))->modify('last saturday')->format('Y-m-d'); $start = (new \DateTime($end, new \DateTimeZone('UTC')))->modify('last sunday')->format('Y-m-d'); $formatedTitle = (new \DateTime($start, new \DateTimeZone('UTC')))->format('M j') . ' - ' . (new \DateTime($end, new \DateTimeZone('UTC')))->format('M j'); break; } if ($report->period !== ReportEntity::PERIOD_DAILY && $report->period !== ReportEntity::PERIOD_QUARTELY) { $quarters = new Quarters(SettingEntity::getQuarters()); $currentQuarter = $quarters->getQuarterForDate(new \DateTime($start, new \DateTimeZone('UTC'))); $currentYear = (new \DateTime($start, new \DateTimeZone('UTC')))->format('Y'); $date = $quarters->getPeriodForQuarter($currentQuarter, $currentYear); $formatedForecastDate = 'End of Q' . $currentQuarter; $startForecast = $date->start->format('Y-m-d'); $endForecast = $date->end->format('Y-m-d'); $periodForecast = 'quarter'; } if ($report->subjectType === ReportEntity::SUBJECT_TYPE_CC) { $getPeriodicSubjectData = 'getCostCenterPeriodData'; $subjectEntityName = 'Scalr\\Stats\\CostAnalytics\\Entity\\CostCentre'; $subjectId = 'ccId'; } else { if ($report->subjectType === ReportEntity::SUBJECT_TYPE_PROJECT) { $getPeriodicSubjectData = 'getProjectPeriodData'; $subjectEntityName = 'Scalr\\Stats\\CostAnalytics\\Entity\\Project'; $subjectId = 'projectId'; } else { $baseUrl = rtrim(\Scalr::getContainer()->config('scalr.endpoint.scheme') . "://" . \Scalr::getContainer()->config('scalr.endpoint.host'), '/'); $periodData = \Scalr::getContainer()->analytics->usage->getDashboardPeriodData($period, $start, $end); $periodDataForecast = \Scalr::getContainer()->analytics->usage->getDashboardPeriodData($periodForecast, $startForecast, $endForecast); $periodData['period'] = $period; $periodData['forecastPeriod'] = $formatedForecastDate; $periodData['totals']['forecastCost'] = $periodDataForecast['totals']['forecastCost']; $periodData['name'] = 'Cloud Cost Report'; $periodData['jsonVersion'] = '1.0.0'; $periodData['detailsUrl'] = $baseUrl . '#/analytics/dashboard'; $periodData['totals']['clouds'] = $this->changeCloudNames($periodData['totals']['clouds']); $periodData['date'] = $formatedTitle; $periodData['totals']['budget']['budget'] = null; if ($period !== 'custom') { $periodData['totals']['prevPeriodDate'] = (new \DateTime($periodData['previousStartDate'], new \DateTimeZone('UTC')))->format('M d') . " - " . (new \DateTime($periodData['previousEndDate'], new \DateTimeZone('UTC')))->format('M d'); } else { $periodData['totals']['prevPeriodDate'] = (new \DateTime($periodData['previousEndDate'], new \DateTimeZone('UTC')))->format('M d'); } if ($period == 'quarter') { $periodData['totals']['budget'] = ['quarter' => $quarter, 'year' => $year, 'quarterStartDate' => $start, 'quarterEndDate' => $end]; } else { if ($period == 'month') { $periodData['totals']['budget'] = ['quarter' => $currentQuarter]; } } unset($periodData['projects'], $periodData['budget']['projects']); if (count($periodData['costcenters'] > 1)) { uasort($periodData['costcenters'], array($this, 'sortItems')); if (count($periodData['costcenters'] > 6)) { array_splice($periodData['costcenters'], 6, count($periodData['costcenters'])); } } if (count($periodData['totals']['clouds'] > 1)) { usort($periodData['totals']['clouds'], array($this, 'sortItems')); } $entity = ReportPayloadEntity::init([$report->subjectType, $report->subjectId, $period], $periodData, $start); if (!ReportPayloadEntity::findPk($entity->uuid)) { $payload = json_decode($entity->payload, true); \Scalr::getContainer()->mailer->setSubject('Summary report.')->setContentType('text/html')->sendTemplate(SCALR_TEMPLATES_PATH . '/emails/report_summary.html.php', $payload, $report->emails); $this->getLogger()->info('Summary report email has been sent'); $payload['date'] = $entity->created->format('Y-m-d'); $entity->payload = json_encode($payload); $entity->save(); } } } unset($currentQuarter, $currentYear); if (!empty($report->subjectType) && !empty($report->subjectId)) { $subject = call_user_func($subjectEntityName . 'Entity::findPk', $report->subjectId); if ($subject->archived) { continue; } $this->saveReportData($getPeriodicSubjectData, $subjectEntityName, ['period' => $period, 'start' => $start, 'end' => $end], ['period' => $periodForecast, 'start' => $startForecast, 'end' => $endForecast], $report->subjectId, $report->subjectType, $report->emails, $formatedTitle, $formatedForecastDate); } else { if (!empty($report->subjectType)) { $subjects = call_user_func($subjectEntityName . 'Entity::find'); foreach ($subjects as $subject) { if ($subject->archived) { continue; } $this->saveReportData($getPeriodicSubjectData, $subjectEntityName, ['period' => $period, 'start' => $start, 'end' => $end], ['period' => $periodForecast, 'start' => $startForecast, 'end' => $endForecast], $subject->{$subjectId}, $report->subjectType, $report->emails, $formatedTitle, $formatedForecastDate); } } } } $this->getLogger()->info('Done'); return new ArrayObject(); }
/** * @param string $projectId * @param JsonData $notifications * @throws Scalr_Exception_InsufficientPermissions */ public function xSaveNotificationsAction($projectId, JsonData $notifications) { $this->request->restrictAccess(Acl::RESOURCE_ANALYTICS_PROJECTS_ACCOUNT); $project = ProjectEntity::findPk($projectId); /* @var $project ProjectEntity */ if (!$project->hasAccessPermissions($this->getUser())) { throw new Scalr_Exception_InsufficientPermissions(); } $data = []; foreach ($notifications as $id => $settings) { if ($id == 'reports') { $this->saveReports($settings, $projectId); $data[$id] = ReportEntity::find([['subjectType' => ReportEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $projectId]])->getArrayCopy(); } elseif ($id == 'notifications.projects') { $this->saveNotifications(NotificationEntity::SUBJECT_TYPE_PROJECT, $settings, $projectId); $data[$id] = NotificationEntity::find([['subjectType' => NotificationEntity::SUBJECT_TYPE_PROJECT], ['subjectId' => $projectId]])->getArrayCopy(); } } $this->response->data($data); $this->response->success('Notifications successfully saved'); }