ocket8888 commented on a change in pull request #3919: TP: adds a better view 
for comparing/managing params of 2 profiles
URL: https://github.com/apache/trafficcontrol/pull/3919#discussion_r324349161
 
 

 ##########
 File path: 
traffic_portal/app/src/common/modules/table/profilesParamsCompare/TableProfilesParamsCompareController.js
 ##########
 @@ -0,0 +1,213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+var TableProfilesParamsCompareController = function(profile1, profile2, 
profilesParams, $scope, $state, $q, $uibModal, messageModel, locationUtils, 
deliveryServiceService, profileParameterService, serverService) {
+
+       this.profile1Usage;
+       this.profile2Usage;
+
+       let updateProfile1 = false,
+               updateProfile2 = false;
+
+       let getProfileUsage = function(profile, profNum) {
+               if (profile.type === 'DS_PROFILE') { // if this is a ds 
profile, then it is used by delivery service(s) so we'll fetch the ds count...
+                       deliveryServiceService.getDeliveryServices({ profile: 
profile.id }).
+                               then(function(result) {
+                                       this['profile' + profNum + 'Usage'] = 
result.length + ' delivery services';
+                               });
+               } else { // otherwise the profile is used by servers so we'll 
fetch the server count...
+                       serverService.getServers({ profileId: profile.id }).
+                               then(function(result) {
+                                       this['profile' + profNum + 'Usage'] = 
result.length + ' servers';
+                               });
+               }
+       };
+
+       let confirmUpdateProfiles = function() {
+               // ok, this method is fun :)
+               let params = {
+                       title: 'Modify ' + profile1.name + ' parameters',
+                       message: 'The ' + profile1.name + ' profile is used by 
' + this.profile1Usage + '. Are you sure you want to modify the parameters?'
+               };
+               let modalInstance = $uibModal.open({
+                       templateUrl: 
'common/modules/dialog/confirm/dialog.confirm.tpl.html',
+                       controller: 'DialogConfirmController',
+                       size: 'md',
+                       resolve: {
+                               params: function () {
+                                       return params;
+                               }
+                       }
+               });
+               modalInstance.result
+                       .then(
+                               function() { updateProfile1 = true; },
+                               function() { updateProfile1 = false; }
+                       )
+                       .finally(
+                               function() {
+                                       let params = {
+                                               title: 'Modify ' + 
profile2.name + ' parameters',
+                                               message: 'The ' + profile2.name 
+ ' profile is used by ' + this.profile2Usage + '. Are you sure you want to 
modify the parameters?'
+                                       };
+                                       let modalInstance = $uibModal.open({
+                                               templateUrl: 
'common/modules/dialog/confirm/dialog.confirm.tpl.html',
+                                               controller: 
'DialogConfirmController',
+                                               size: 'md',
+                                               resolve: {
+                                                       params: function () {
+                                                               return params;
+                                                       }
+                                               }
+                                       });
+                                       modalInstance.result
+                                               .then(
+                                                       function() { 
updateProfile2 = true; },
+                                                       function() { 
updateProfile2 = false; }
+                                               )
+                                               .finally(
+                                                       function() {
+                                                               let promises = 
[];
+
+                                                               if 
(updateProfile1) {
+                                                                       
promises.push(profileParameterService.linkProfileParameters(profile1, 
_.pluck($scope.selectedProfile1Params, 'id')));
+                                                               }
+
+                                                               if 
(updateProfile2) {
+                                                                       
promises.push(profileParameterService.linkProfileParameters(profile2, 
_.pluck($scope.selectedProfile2Params, 'id')));
+                                                               }
+
+                                                               if 
(promises.length > 0) {
+                                                                       
$q.all(promises)
+                                                                               
.then(
+                                                                               
        function(result) {
+                                                                               
                let messages = [];
+                                                                               
                for (let i = 0; i < result.length; i++) {
+                                                                               
                        messages = _.union(messages, result[i].data.alerts);
 
 Review comment:
   Instead of using `_.union`, consider using the built-in [`Set` 
class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)
 like so:
   
   ```javascript
   const messages = new Set(); // 'messages' isn't being reassigned, so it's 
constant
   for (let r of result) {
        for (let a of r.data.alerts) {
                messages.add(a);
        }
   }
   // You can submit it as an array using Array.from:
   messageModel.setMessages(Array.from(messages), false);
   ```
   For bonus points, you can use 
[`Array.prototype.flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)
 to flatten it into a one-dimensional array prior to iteration:
   ```javascript
   const messages = new Set();
   // This loop directly iterates every alert of every response
   for (let a of result.flatMap(function(x){return x.data.alerts})) {
        messages.add(a);
   }
   messageModel.setMessages(messages, false);
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to