function newDowntime($downtimeXml) { $downtime = new Downtime(); foreach ($downtimeXml->attributes() as $key => $value) { switch ($key) { case "ID": $promId = (int) $value; break; case "PRIMARY_KEY": $primaryKey = (string) $value; break; case "CLASSIFICATION": $classification = (string) $value; break; } } // Get the largest v4 downtime PK which is an integer appended by the string 'G0' // slice off the 'G0' and get the integer value. $v4pk = (int) substr($primaryKey, 0, strlen($primaryKey) - 2); if ($v4pk > $GLOBALS['largestV4DowntimePK']) { $GLOBALS['largestV4DowntimePK'] = $v4pk; } $downtime->setClassification($classification); $downtime->setDescription((string) $downtimeXml->DESCRIPTION); $downtime->setSeverity((string) $downtimeXml->SEVERITY); $startDate = new DateTime("@" . (string) $downtimeXml->START_DATE); $downtime->setStartDate($startDate); $endDate = new DateTime("@" . (string) $downtimeXml->END_DATE); $downtime->setEndDate($endDate); $insertDate = new DateTime("@" . (string) $downtimeXml->INSERT_DATE); $downtime->setInsertDate($insertDate); $downtime->setPrimaryKey($primaryKey); return $downtime; }
/** * Array * ( * [DOWNTIME] => Array * ( * [SEVERITY] => OUTAGE * [DESCRIPTION] => Test * [START_TIMESTAMP] => 20/03/2013 00:00 * [END_TIMESTAMP] => 23/03/2013 00:00 * ) * [Impacted_Services] => Array * ( * [0] => 824 * [1] => 825 * [2] => 2146 * ) * [Impacted_Endpoints] => Array * ( * [0] => 54 * [1] => 15 * [2] => 26 * ) * Adds a downtime * @param Array $values Downtime values, shown above * @param \User $user User making the request * @return \Downtime $dt The new downtime */ public function addDowntime($values, \User $user = null) { //Check the portal is not in read only mode, throws exception if it is $this->checkPortalIsNotReadOnlyOrUserIsAdmin($user); require_once __DIR__ . '/ServiceService.php'; $serviceService = new \org\gocdb\services\ServiceService(); $serviceService->setEntityManager($this->em); // Get the affected services $services = array(); foreach ($values['Impacted_Services'] as $service) { $services[] = $serviceService->getService($service); } // Get the affected endpoints $endpoints = array(); foreach ($values['Impacted_Endpoints'] as $endpoint) { $endpoints[] = $serviceService->getEndpoint($endpoint); } if (count($services) == 0) { throw new \Exception("A downtime must affect at least one service"); } // Check that each endpoint belongs to one of the services. // It is an error if an endpoint does not belong to one of the services. foreach ($endpoints as $checkEndpoint) { $endpointBelongsToService = false; foreach ($services as $checkService) { if (in_array($checkEndpoint, $checkService->getEndpointLocations()->toArray())) { $endpointBelongsToService = true; } } if (!$endpointBelongsToService) { throw new \Exception('Error, affected endpoint is not owned by an affected service'); } } // get the affected sites $sites = array(); foreach ($services as $se) { $site = $se->getParentSite(); if (!in_array($site, $sites)) { $sites[] = $site; } } if (count($sites) != 1) { // if there are multiple affected services from multiple sites, // the gui must either enforce single site selection // or prevent selecting 'define DT in site timezone' if selecting // multiple sites (i.e. utc only when selecting services from multiple sites). throw new \Exception("Downtime creation for multiple sites not supported yet"); } // Check the user has a role covering the passed SEs $this->authorization($services, $user); $this->validate($values['DOWNTIME']); $startStr = $values['DOWNTIME']['START_TIMESTAMP']; $endStr = $values['DOWNTIME']['END_TIMESTAMP']; //echo($startStr. ' '.$endStr); // 14/05/2015 16:16 14/05/2015 16:17 d/m/Y H:i // did the user specify the downtime info in utc (default) or in site's local timezone? /*$requestUtcOrSiteTimezone = $values['DOWNTIME']['DEFINE_TZ_BY_UTC_OR_SITE']; $tzStr = 'UTC'; // the timezone label specified by the user (e.g. 'UTC' or 'Europe/London') if($requestUtcOrSiteTimezone == 'site'){ // if there are multiple affected services from multiple sites, // we assume utc - the gui must therefore either enforce single site selection // or prevent selecting 'define DT in site timezone' if selecting // multiple sites (i.e. utc only when selecting services from multiple sites). // get the site timezone label if(count($sites) > 1){ $tzStr = 'UTC'; // if many sites are affected (not implemented yet), assume UTC } else { $siteTzOrNull = $sites[0]->getTimezoneId(); if( !empty($siteTzOrNull)){ $tzStr = $siteTzOrNull; } else { $tzStr = 'UTC'; } } } // convert start and end into UTC $UTC = new \DateTimeZone("UTC"); if($tzStr != 'UTC'){ // specify dateTime in source TZ $sourceTZ = new \DateTimeZone($tzStr); $start = \DateTime::createFromFormat($this::FORMAT, $startStr, $sourceTZ); $end = \DateTime::createFromFormat($this::FORMAT, $endStr, $sourceTZ); // reset the TZ to UTC $start->setTimezone($UTC); $end->setTimezone($UTC); } else { $start = \DateTime::createFromFormat($this::FORMAT, $startStr, $UTC); $end = \DateTime::createFromFormat($this::FORMAT, $endStr, $UTC); }*/ $start = \DateTime::createFromFormat($this::FORMAT, $startStr, new \DateTimeZone("UTC")); $end = \DateTime::createFromFormat($this::FORMAT, $endStr, new \DateTimeZone("UTC")); $this->validateDates($start, $end); // calculate classification $nowPlus1Day = new \DateTime(null, new \DateTimeZone('UTC')); $oneDay = \DateInterval::createFromDateString('1 days'); if ($start > $nowPlus1Day->add($oneDay)) { $class = "SCHEDULED"; } else { $class = "UNSCHEDULED"; } $this->em->getConnection()->beginTransaction(); try { $dt = new \Downtime(); $dt->setClassification($class); $dt->setDescription($values['DOWNTIME']['DESCRIPTION']); $dt->setSeverity($values['DOWNTIME']['SEVERITY']); $dt->setStartDate($start); $dt->setEndDate($end); $dt->setInsertDate(new \DateTime(null, new \DateTimeZone('UTC'))); // Create a new pk and persist/flush to sync in-mem object state // with DB - is needed so that the call to v4DowntimePK->getId() actually // returns a value (we can still rollback no probs if an issue occurs // to remove the Downtime) $v4DowntimePk = new \PrimaryKey(); $this->em->persist($v4DowntimePk); $this->em->flush(); $dt->setPrimaryKey($v4DowntimePk->getId() . 'G0'); //Create a link to the services foreach ($services as $service) { $dt->addService($service); } //Create a link to the affected endpoints (if any endpoints were selected) foreach ($endpoints as $endpoint) { $dt->addEndpointLocation($endpoint); } $this->em->persist($dt); $this->em->flush(); $this->em->getConnection()->commit(); } catch (\Exception $e) { $this->em->getConnection()->rollback(); $this->em->close(); throw $e; } return $dt; }