コード例 #1
0
ファイル: Events.php プロジェクト: rickb838/scalr
 /**
  * Gets formated event list
  *
  * @param $interval              $interval   Interval of the each point on the chart
  * @param \DateTime              $begin      Start date of the period on the chart
  * @param \DateTime              $end        The end date of the period on the chart
  * @param string                 $ccId       optional Cost center id
  * @param string                 $projectId  optional Project id
  * @return array    Returns array of events amount sorted by datetime
  */
 public function count($interval, $begin, $end, $ccId = null, $projectId = null)
 {
     if (!$begin instanceof DateTime || !$end instanceof DateTime) {
         throw new \InvalidArgumentException(sprintf("Both Start end End time should be instance of DateTime."));
     }
     $groupFields = ['1 hour' => "DATE_FORMAT(`dtime`, '%Y-%m-%d %H:00:00')", '1 day' => "DATE(`dtime`)", '1 week' => "YEARWEEK(`dtime`, 0)", '1 month' => "DATE_FORMAT(`dtime`, '%Y-%m')", '1 year' => "YEAR(`dtime`)"];
     $eventEntity = new TimelineEventEntity();
     $joinData = $this->buildJoin($ccId, $projectId);
     $dtimeType = $eventEntity->type('dtime');
     $result = $this->db->Execute("\n            SELECT COUNT(*) as count, " . $groupFields[$interval] . " as dtime\n            FROM " . $eventEntity->table() . " e " . (isset($joinData['join']) ? $joinData['join'] : '') . "\n            WHERE e.dtime BETWEEN ? AND ?\n            GROUP BY " . $groupFields[$interval] . "\n        ", [$dtimeType->toDb($begin), $dtimeType->toDb($end)]);
     $events = [];
     while ($record = $result->FetchRow()) {
         $events[$record['dtime']] = $record['count'];
     }
     //Change cloud pricing events should be included into both projects and cost centers filter
     if (isset($joinData['join'])) {
         $result = $this->db->Execute("\n                SELECT COUNT(*) as count, " . $groupFields[$interval] . " as dtime\n                FROM " . $eventEntity->table() . " e\n                WHERE e.dtime BETWEEN ? AND ?\n                AND e.event_type = ?\n                GROUP BY " . $groupFields[$interval] . "\n            ", [$dtimeType->toDb($begin), $dtimeType->toDb($end), $eventEntity::EVENT_TYPE_CHANGE_CLOUD_PRICING]);
     }
     while ($record = $result->FetchRow()) {
         if (!isset($events[$record['dtime']])) {
             $events[$record['dtime']] = $record['count'];
         } else {
             $events[$record['dtime']] += $record['count'];
         }
     }
     return $events;
 }
コード例 #2
0
ファイル: AbstractEvent.php プロジェクト: scalr/scalr
 /**
  * Fires an event
  *
  * @return boolean Returns true if a new record has been added
  */
 public function fire()
 {
     $this->timelineEvent->uuid = $this->timelineEvent->type('uuid')->toPhp(substr(hash('sha1', $this->messageToHash, true), 0, 16));
     if (!$this->timelineEvent->findPk($this->timelineEvent->uuid)) {
         $this->timelineEvent->description = $this->message;
         $this->timelineEvent->save();
         //Creates timeline event records for events which affect cost centers
         foreach (array_filter($this->ccs, [$this, 'callbackFilter']) as $ccId) {
             $entity = new TimelineEventCostCentreEntity($this->timelineEvent->uuid, $ccId);
             $entity->save();
         }
         //Creates timeline event records for events which affect projects
         foreach (array_filter($this->projects, [$this, 'callbackFilter']) as $projectId) {
             $entity = new TimelineEventProjectEntity($this->timelineEvent->uuid, $projectId);
             $entity->save();
         }
         return true;
     }
     return false;
 }