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_r324353425
 
 

 ##########
 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);
+                                                                               
                };
+                                                                               
                messageModel.setMessages(messages, false);
+                                                                               
        })
+                                                                               
.finally(
+                                                                               
        function() {
+                                                                               
                $scope.refresh();
+                                                                               
        }
+                                                                               
);
+                                                               }
+                                                       }
+                                               );
+                               }
+                       );
+       };
+
+       $scope.dirty = false;
+
+       $scope.profile1 = profile1;
+       $scope.profile2 = profile2;
+       $scope.profilesParams = profilesParams;
+
+       $scope.selectedProfile1Params = [];
+       $scope.selectedProfile2Params = [];
+
+       $scope.selectedParams = _.map(profilesParams, function(param) {
+               let isAssignedToProfile1 = _.find(profile1.params, 
function(profile1param) { return profile1param.id == param.id }),
+                       isAssignedToProfile2 = _.find(profile2.params, 
function(profile2param) { return profile2param.id == param.id });
 
 Review comment:
   I don't think `_.find` does what you're looking for. It returns the first 
value in the array that yields a truthy value from the predicate function. I 
think what you want to know is whether or not such a value *exists* - you don't 
care about the value. Instead, consider using [the built-in 
`Array.prototype.some` 
method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)
 like so:
   ```javascript
   // also note the use of triple-equals (===)
   const isAssignedToProfile1 = profile1.params.some(function(p){return p.id 
=== param.id});
   //...
   ```

----------------------------------------------------------------
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