Skip to content

Commit 87c02c3

Browse files
committed
[GT-187] Add support for selecting mutiple sites
1 parent 23146b7 commit 87c02c3

File tree

12 files changed

+771
-418
lines changed

12 files changed

+771
-418
lines changed

htdocs/web_portal/controllers/downtime/add_downtime.php

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
* See the License for the specific language governing permissions and
2222
* limitations under the License.
2323
/*======================================================*/
24-
require_once __DIR__.'/../../../../lib/Gocdb_Services/Factory.php';
25-
require_once __DIR__.'/../utils.php';
26-
require_once __DIR__.'/../../../web_portal/components/Get_User_Principle.php';
24+
require_once __DIR__ . '/../../../../lib/Gocdb_Services/Factory.php';
25+
require_once __DIR__ . '/../utils.php';
26+
require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php';
27+
require_once __DIR__ . '/downtime_utils.php';
2728

2829
/**
2930
* Controller for a new_downtime request.
@@ -60,64 +61,60 @@ function add() {
6061
/**
6162
* Retrieves the raw new downtime's data from a portal request and submit it
6263
* to the services layer's downtime functions.
64+
*
6365
* @param \User $user current user
64-
* @return null
6566
*/
66-
function submit(\User $user = null) {
67-
68-
69-
//Check if this is a confirmed submit or intial submit
67+
function submit(\User $user = null)
68+
{
7069
$confirmed = $_POST['CONFIRMED'];
71-
if($confirmed == true){
72-
//Downtime is confirmed, submit it
73-
//$downtimeInfo = unserialize($_REQUEST['newValues']); // didn't cater for UTF-8 chars
74-
$downtimeInfo = json_decode($_POST['newValues'], TRUE);
70+
71+
if ($confirmed == true) {
72+
/**
73+
* If confirmed by an user, submit the details of affected services
74+
* and endpoints along with other details for each individual site.
75+
*/
76+
$downtimeInfo = json_decode($_POST['newValues'], true);
7577
$serv = \Factory::getDowntimeService();
78+
$downtimeInfo = unsetVariables($downtimeInfo, 'add');
79+
$params = [];
7680

77-
$params['dt'] = $serv->addDowntime($downtimeInfo, $user);
78-
show_view("downtime/added_downtime.php", $params);
79-
}else{
80-
//Show user confirmation screen with their input
81-
$downtimeInfo = getDtDataFromWeb();
82-
83-
//Need to sort the impacted_ids into impacted services and impacted endpoints
84-
$impactedids = $downtimeInfo['IMPACTED_IDS'];
85-
86-
$services=array();
87-
$endpoints=array();
88-
89-
//For each impacted id sort between endpoints and services using the prepended letter
90-
foreach($impactedids as $id){
91-
if (strpos($id, 's') !== FALSE){
92-
//This is a service id
93-
$services[] = str_replace('s', '', $id); //trim off the identifying char before storing in array
94-
}else{
95-
//This is an endpoint id
96-
$endpoints[] = str_replace('e', '', $id); //trim off the identifying char before storing in array
97-
}
98-
}
81+
foreach ($downtimeInfo['SITE_LEVEL_DETAILS'] as $siteID) {
82+
$downtimeInfo['Impacted_Services'] = $siteID['services'];
83+
$downtimeInfo['Impacted_Endpoints'] = $siteID['endpoints'];
9984

100-
unset($downtimeInfo['IMPACTED_IDS']); //Delete the unsorted Ids from the downtime info
85+
$params['submittedDowntimes'][$siteID['siteName']] =
86+
$serv->addDowntime($downtimeInfo, $user);
87+
}
10188

102-
$downtimeInfo['Impacted_Endpoints'] = $endpoints;
89+
show_view("downtime/added_downtime.php", $params);
90+
} else {
91+
// Show user confirmation screen with their input
92+
$downtimeInfo = getDowntimeFormData();
10393

94+
list(
95+
$siteLevelDetails,
96+
$serviceWithEndpoints
97+
) = endpointToServiceMapping($downtimeInfo['IMPACTED_IDS']);
10498

105-
$serv = \Factory::getServiceService();
99+
// Delete the unsorted IDs from the downtime info
100+
unset($downtimeInfo['IMPACTED_IDS']);
106101

107-
/** For endpoint put into downtime we want the parent service also. If a user has selected
108-
* endpoints but not the parent service here we will add the service to maintain the link beteween
109-
* a downtime having both the service and the endpoint.
110-
*/
111-
foreach($downtimeInfo['Impacted_Endpoints'] as $endpointIds){
112-
$endpoint = $serv->getEndpoint($endpointIds);
113-
$services[] = $endpoint->getService()->getId();
102+
if (!count($siteLevelDetails) > 1) {
103+
$downtimeInfo['SINGLE_TIMEZONE'] = true;
114104
}
115105

116-
//Remove any duplicate service ids and store the array of ids
117-
$services = array_unique($services);
106+
list(
107+
$siteLevelDetails,
108+
$serviceWithEndpoints
109+
) = addParentServiceForEndpoints(
110+
$serviceWithEndpoints,
111+
$siteLevelDetails,
112+
false,
113+
$downtimeInfo['DOWNTIME']
114+
);
118115

119-
//Assign the impacted services and endpoints to their own arrays for us by the addDowntime method
120-
$downtimeInfo['Impacted_Services'] = $services;
116+
$downtimeInfo['SITE_LEVEL_DETAILS'] = $siteLevelDetails;
117+
$downtimeInfo['SERVICE_WITH_ENDPOINTS'] = $serviceWithEndpoints;
121118

122119
show_view("downtime/confirm_add_downtime.php", $downtimeInfo);
123120
}
@@ -215,5 +212,3 @@ function draw(\User $user = null) {
215212
die();
216213
}
217214
}
218-
219-
?>
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
3+
/*_____________________________________________________________________________
4+
*=============================================================================
5+
* File: downtime_utils.php
6+
* Author: GOCDB DEV TEAM, STFC.
7+
* Description: Helper functions which can be re-used while adding
8+
* or editing a downtime.
9+
*
10+
* License information
11+
*
12+
* Copyright 2013 STFC
13+
* Licensed under the Apache License, Version 2.0 (the "License");
14+
* you may not use this file except in compliance with the License.
15+
* You may obtain a copy of the License at
16+
* http://www.apache.org/licenses/LICENSE-2.0
17+
* Unless required by applicable law or agreed to in writing, software
18+
* distributed under the License is distributed on an "AS IS" BASIS,
19+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20+
* See the License for the specific language governing permissions and
21+
* limitations under the License.
22+
*
23+
/*====================================================== */
24+
require_once __DIR__ . '/../../../../lib/Gocdb_Services/Factory.php';
25+
26+
use DateTime;
27+
use DateTimeZone;
28+
29+
/**
30+
* Sorts the impacted IDs into impacted services and impacted endpoints.
31+
*
32+
* @param array $impactedIDs An array of `impactedIDs` which user has selected.
33+
*
34+
* @return array An array containing
35+
* `$siteLevelDetails` and `serviceWithEndpoints`.
36+
*/
37+
function endpointToServiceMapping($impactedIDs)
38+
{
39+
$siteLevelDetails = [];
40+
$serviceWithEndpoints = [];
41+
42+
/**
43+
* For each impacted ID,
44+
* sort between endpoints and services using the prepended letter.
45+
*/
46+
foreach ($impactedIDs as $id) {
47+
list($siteNumber, $parentService, $idType) = explode(':', $id);
48+
49+
$type = strpos($idType, 's') !== false ? 'services' : 'endpoints';
50+
$id = str_replace(['s', 'e'], '', $idType);
51+
52+
$siteLevelDetails[$siteNumber][$type][] = $id;
53+
$serviceWithEndpoints[$siteNumber][$parentService][$type][] = $id;
54+
}
55+
56+
return [$siteLevelDetails, $serviceWithEndpoints];
57+
}
58+
59+
/**
60+
* If a user has selected endpoints but not the parent service here
61+
* we will add the service to maintain the link between a downtime
62+
* having both the service and the endpoint.
63+
*
64+
* @param array $servWithEndpoints Used for displaying affected service
65+
* with endpoint(s).
66+
* @param array $siteDetails Each site ID will have `services` which
67+
* stores all affected service ID(s) and
68+
* `endpoints` which stores all affected
69+
* endpoint ID(s).
70+
* @param bool $hasMultipleTimezones If the user selects multiple sites in
71+
* web portal along with the option
72+
* "site timezone" it will be true;
73+
* otherwise, it will be false.
74+
* @param mixed $downtimeDetails Downtime information.
75+
*
76+
* @return array An array containing `$siteDetails` and `servWithEndpoints`.
77+
*/
78+
function addParentServiceForEndpoints(
79+
$servWithEndpoints,
80+
$siteDetails,
81+
$hasMultipleTimezones,
82+
$downtimeDetails
83+
) {
84+
foreach ($servWithEndpoints as $siteID => $siteData) {
85+
$siteDetails[$siteID]['services'] = [];
86+
87+
$newSite = \Factory::getSiteService()->getSite($siteID);
88+
$siteDetails[$siteID]['siteName'] = $newSite->getShortName();
89+
90+
if ($hasMultipleTimezones) {
91+
list(
92+
$siteDetails[$siteID]['START_TIMESTAMP'],
93+
$siteDetails[$siteID]['END_TIMESTAMP']
94+
) = setLocalTimeForSites($downtimeDetails, $siteID);
95+
}
96+
97+
foreach (array_keys($siteData) as $serviceID) {
98+
$servWithEndpoints[$siteID][$serviceID]['services'] = [];
99+
$servWithEndpoints[$siteID][$serviceID]['services'][] = $serviceID;
100+
// Ensuring that service IDs are unique for the selected sites.
101+
$siteDetails[$siteID]['services'][] = $serviceID;
102+
}
103+
}
104+
105+
return [$siteDetails, $servWithEndpoints];
106+
}
107+
108+
/**
109+
* Converts UTC start and end timestamps to the local timezone
110+
* of a specific site based on its timezone.
111+
*
112+
* @param mixed $downtimeDetails Downtime information.
113+
* @param integer $siteID Site ID
114+
*/
115+
function setLocalTimeForSites($downtimeDetails, $siteID)
116+
{
117+
$site = \Factory::getSiteService()->getSite($siteID);
118+
119+
$siteTimezone = $site->getTimeZoneId();
120+
121+
$startTimeAsString = $downtimeDetails['START_TIMESTAMP'];
122+
$utcEndTime = $downtimeDetails['END_TIMESTAMP'];
123+
124+
$utcStartDateTime = DateTime::createFromFormat(
125+
'd/m/Y H:i',
126+
$startTimeAsString,
127+
new DateTimeZone('UTC')
128+
);
129+
$utcEndDateTime = DateTime::createFromFormat(
130+
'd/m/Y H:i',
131+
$utcEndTime,
132+
new DateTimeZone('UTC')
133+
);
134+
135+
$targetSiteTimezone = new DateTimeZone($siteTimezone);
136+
$utcOffset = $targetSiteTimezone->getOffset($utcStartDateTime);
137+
138+
// Calculate the equivalent time in the target timezone.
139+
// Ref: https://www.php.net/manual/en/datetime.modify.php
140+
$siteStartDateTime = $utcStartDateTime->modify("-$utcOffset seconds");
141+
$siteEndDateTime = $utcEndDateTime->modify("-$utcOffset seconds");
142+
143+
return [
144+
$siteStartDateTime->format('d/m/Y H:i'),
145+
$siteEndDateTime->format('d/m/Y H:i')
146+
];
147+
}
148+
149+
/**
150+
* Unset a given variable, helper method to destroy the specified variables.
151+
*
152+
* @param mixed $downtimeObj Object to destroy specified variables.
153+
* @param string $fromLocation Location from where the function is being called.
154+
*/
155+
function unsetVariables($downtimeObj, $fromLocation)
156+
{
157+
if ($fromLocation == "add") {
158+
unset($downtimeObj['SERVICE_WITH_ENDPOINTS']);
159+
unset($downtimeObj['SINGLE_TIMEZONE']);
160+
} else {
161+
unset($downtimeObj['DOWNTIME']['EXISTINGID']);
162+
unset($downtimeObj['isEdit']);
163+
unset($downtimeObj['SERVICE_WITH_ENDPOINTS']);
164+
unset($downtimeObj['SINGLE_TIMEZONE']);
165+
}
166+
167+
return $downtimeObj;
168+
}

0 commit comments

Comments
 (0)