dangogh closed pull request #3166: TP: sorts the results of GET /tenants to
represent the hierarchy
URL: https://github.com/apache/trafficcontrol/pull/3166
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/traffic_portal/app/src/common/modules/form/deliveryService/FormDeliveryServiceController.js
b/traffic_portal/app/src/common/modules/form/deliveryService/FormDeliveryServiceController.js
index 38002f658..c41489967 100644
---
a/traffic_portal/app/src/common/modules/form/deliveryService/FormDeliveryServiceController.js
+++
b/traffic_portal/app/src/common/modules/form/deliveryService/FormDeliveryServiceController.js
@@ -17,7 +17,7 @@
* under the License.
*/
-var FormDeliveryServiceController = function(deliveryService, dsCurrent,
origin, type, types, $scope, $location, $uibModal, $window, formUtils,
locationUtils, tenantUtils, deliveryServiceUtils, cdnService, profileService,
tenantService, propertiesModel) {
+var FormDeliveryServiceController = function(deliveryService, dsCurrent,
origin, type, types, $scope, $location, $uibModal, $window, formUtils,
locationUtils, tenantUtils, deliveryServiceUtils, cdnService, profileService,
tenantService, propertiesModel, userModel) {
var getCDNs = function() {
cdnService.getCDNs()
@@ -36,10 +36,13 @@ var FormDeliveryServiceController =
function(deliveryService, dsCurrent, origin,
};
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
+ });
});
};
@@ -299,5 +302,5 @@ var FormDeliveryServiceController =
function(deliveryService, dsCurrent, origin,
};
-FormDeliveryServiceController.$inject = ['deliveryService', 'dsCurrent',
'origin', 'type', 'types', '$scope', '$location', '$uibModal', '$window',
'formUtils', 'locationUtils', 'tenantUtils', 'deliveryServiceUtils',
'cdnService', 'profileService', 'tenantService', 'propertiesModel'];
+FormDeliveryServiceController.$inject = ['deliveryService', 'dsCurrent',
'origin', 'type', 'types', '$scope', '$location', '$uibModal', '$window',
'formUtils', 'locationUtils', 'tenantUtils', 'deliveryServiceUtils',
'cdnService', 'profileService', 'tenantService', 'propertiesModel',
'userModel'];
module.exports = FormDeliveryServiceController;
diff --git
a/traffic_portal/app/src/common/modules/form/origin/FormOriginController.js
b/traffic_portal/app/src/common/modules/form/origin/FormOriginController.js
index 2231a1fc6..f6deaa887 100644
--- a/traffic_portal/app/src/common/modules/form/origin/FormOriginController.js
+++ b/traffic_portal/app/src/common/modules/form/origin/FormOriginController.js
@@ -17,7 +17,7 @@
* under the License.
*/
-var FormOriginController = function(origin, $scope, $window, $location,
formUtils, locationUtils, tenantUtils, deliveryServiceService, profileService,
tenantService, coordinateService, cacheGroupService, originService) {
+var FormOriginController = function(origin, $scope, $window, $location,
formUtils, locationUtils, tenantUtils, deliveryServiceService, profileService,
tenantService, coordinateService, cacheGroupService, userModel) {
var getProfiles = function() {
profileService.getProfiles({ orderby: 'name' })
@@ -29,10 +29,13 @@ var FormOriginController = function(origin, $scope,
$window, $location, formUtil
};
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
+ });
});
};
@@ -97,5 +100,5 @@ var FormOriginController = function(origin, $scope, $window,
$location, formUtil
};
-FormOriginController.$inject = ['origin', '$scope', '$window', '$location',
'formUtils', 'locationUtils', 'tenantUtils', 'deliveryServiceService',
'profileService', 'tenantService', 'coordinateService', 'cacheGroupService',
'originService'];
+FormOriginController.$inject = ['origin', '$scope', '$window', '$location',
'formUtils', 'locationUtils', 'tenantUtils', 'deliveryServiceService',
'profileService', 'tenantService', 'coordinateService', 'cacheGroupService',
'userModel'];
module.exports = FormOriginController;
diff --git
a/traffic_portal/app/src/common/modules/form/tenant/FormTenantController.js
b/traffic_portal/app/src/common/modules/form/tenant/FormTenantController.js
index 551c8fc5b..537d1a315 100644
--- a/traffic_portal/app/src/common/modules/form/tenant/FormTenantController.js
+++ b/traffic_portal/app/src/common/modules/form/tenant/FormTenantController.js
@@ -17,13 +17,16 @@
* under the License.
*/
-var FormTenantController = function(tenant, $scope, $location, formUtils,
tenantUtils, locationUtils, tenantService) {
+var FormTenantController = function(tenant, $scope, $location, formUtils,
tenantUtils, locationUtils, tenantService, userModel) {
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
+ });
});
};
@@ -59,5 +62,5 @@ var FormTenantController = function(tenant, $scope,
$location, formUtils, tenant
};
-FormTenantController.$inject = ['tenant', '$scope', '$location', 'formUtils',
'tenantUtils', 'locationUtils', 'tenantService'];
+FormTenantController.$inject = ['tenant', '$scope', '$location', 'formUtils',
'tenantUtils', 'locationUtils', 'tenantService', 'userModel'];
module.exports = FormTenantController;
diff --git
a/traffic_portal/app/src/common/modules/form/user/FormUserController.js
b/traffic_portal/app/src/common/modules/form/user/FormUserController.js
index 48921985b..a1ebf62ea 100644
--- a/traffic_portal/app/src/common/modules/form/user/FormUserController.js
+++ b/traffic_portal/app/src/common/modules/form/user/FormUserController.js
@@ -17,7 +17,7 @@
* under the License.
*/
-var FormUserController = function(user, $scope, $location, formUtils,
stringUtils, locationUtils, tenantUtils, roleService, tenantService) {
+var FormUserController = function(user, $scope, $location, formUtils,
stringUtils, locationUtils, tenantUtils, roleService, tenantService, userModel)
{
var getRoles = function() {
roleService.getRoles({ orderby: 'priv_level DESC' })
@@ -27,10 +27,13 @@ var FormUserController = function(user, $scope, $location,
formUtils, stringUtil
};
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
+ });
});
};
@@ -64,5 +67,5 @@ var FormUserController = function(user, $scope, $location,
formUtils, stringUtil
};
-FormUserController.$inject = ['user', '$scope', '$location', 'formUtils',
'stringUtils', 'locationUtils', 'tenantUtils', 'roleService', 'tenantService'];
+FormUserController.$inject = ['user', '$scope', '$location', 'formUtils',
'stringUtils', 'locationUtils', 'tenantUtils', 'roleService', 'tenantService',
'userModel'];
module.exports = FormUserController;
diff --git
a/traffic_portal/app/src/common/modules/form/user/register/FormRegisterUserController.js
b/traffic_portal/app/src/common/modules/form/user/register/FormRegisterUserController.js
index 0d68ff8bf..ebe799e81 100644
---
a/traffic_portal/app/src/common/modules/form/user/register/FormRegisterUserController.js
+++
b/traffic_portal/app/src/common/modules/form/user/register/FormRegisterUserController.js
@@ -17,7 +17,7 @@
* under the License.
*/
-var FormRegisterUserController = function($scope, $location, formUtils,
tenantUtils, locationUtils, roleService, tenantService, userService) {
+var FormRegisterUserController = function($scope, $location, formUtils,
tenantUtils, locationUtils, roleService, tenantService, userService, userModel)
{
var getRoles = function() {
roleService.getRoles({ orderby: 'priv_level DESC' })
@@ -27,10 +27,13 @@ var FormRegisterUserController = function($scope,
$location, formUtils, tenantUt
};
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+
tenantUtils.addLevels($scope.tenants);
+ });
});
};
@@ -62,5 +65,5 @@ var FormRegisterUserController = function($scope, $location,
formUtils, tenantUt
};
-FormRegisterUserController.$inject = ['$scope', '$location', 'formUtils',
'tenantUtils', 'locationUtils', 'roleService', 'tenantService', 'userService'];
+FormRegisterUserController.$inject = ['$scope', '$location', 'formUtils',
'tenantUtils', 'locationUtils', 'roleService', 'tenantService', 'userService',
'userModel'];
module.exports = FormRegisterUserController;
diff --git
a/traffic_portal/app/src/common/modules/table/tenants/TableTenantsController.js
b/traffic_portal/app/src/common/modules/table/tenants/TableTenantsController.js
index 110cd3b73..0ea78adcb 100644
---
a/traffic_portal/app/src/common/modules/table/tenants/TableTenantsController.js
+++
b/traffic_portal/app/src/common/modules/table/tenants/TableTenantsController.js
@@ -17,9 +17,11 @@
* under the License.
*/
-var TableTenantsController = function(tenants, $scope, $state, locationUtils) {
+var TableTenantsController = function(currentUserTenant, tenants, $scope,
$state, $timeout, locationUtils, tenantUtils) {
- $scope.tenants = tenants;
+ $scope.isUserTenant = function(tenant) {
+ return tenant.id == currentUserTenant.id;
+ };
$scope.editTenant = function(id) {
locationUtils.navigateToPath('/tenants/' + id);
@@ -29,19 +31,23 @@ var TableTenantsController = function(tenants, $scope,
$state, locationUtils) {
locationUtils.navigateToPath('/tenants/new');
};
- $scope.refresh = function() {
- $state.reload(); // reloads all the resolves for the view
- };
+ var init = function() {
+
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
currentUserTenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
- angular.element(document).ready(function () {
- $('#tenantsTable').dataTable({
- "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
- "iDisplayLength": 25,
- "aaSorting": []
- });
- });
+ $timeout(function () {
+ $('#tenantsTable').dataTable({
+ "aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
+ "iDisplayLength": -1,
+ "bSort": false
+ });
+ }, 100);
+
+ };
+ init();
};
-TableTenantsController.$inject = ['tenants', '$scope', '$state',
'locationUtils'];
+TableTenantsController.$inject = ['currentUserTenant', 'tenants', '$scope',
'$state', '$timeout', 'locationUtils', 'tenantUtils'];
module.exports = TableTenantsController;
diff --git
a/traffic_portal/app/src/common/modules/table/tenants/table.tenants.tpl.html
b/traffic_portal/app/src/common/modules/table/tenants/table.tenants.tpl.html
index aa01a8812..169e8d968 100644
--- a/traffic_portal/app/src/common/modules/table/tenants/table.tenants.tpl.html
+++ b/traffic_portal/app/src/common/modules/table/tenants/table.tenants.tpl.html
@@ -39,7 +39,7 @@
</tr>
</thead>
<tbody>
- <tr ng-click="editTenant(t.id)" ng-repeat="t in ::tenants">
+ <tr ng-click="editTenant(t.id)" ng-repeat="t in ::tenants"
ng-class="::{'active': isUserTenant(t)}">
<td data-search="^{{::t.name}}$">{{::t.name}}</td>
<td data-search="^{{::t.active}}$">{{::t.active}}</td>
<td data-search="^{{::t.parentName}}$">{{::t.parentName}}</td>
diff --git a/traffic_portal/app/src/common/service/utils/TenantUtils.js
b/traffic_portal/app/src/common/service/utils/TenantUtils.js
index 43e4c6b1a..354e43e6e 100644
--- a/traffic_portal/app/src/common/service/utils/TenantUtils.js
+++ b/traffic_portal/app/src/common/service/utils/TenantUtils.js
@@ -17,34 +17,70 @@
* under the License.
*/
-var TenantUtils = function() {
+var TenantUtils = function () {
/*
* recurse thru a hierarchical structure and
* add a 'level' property to each item
*/
- var applyLevels = function(arr, level) {
- _.each(arr, function(item) {
+ var applyLevels = function (arr, level) {
+ _.each(arr, function (item) {
item.level = level;
applyLevels(item.children, level + 1);
});
};
+ var hierarchySortFunc = function (a, b) {
+ return a.name > b.name;
+ };
+
+ /*
+ * sort parent groups into an array
+ * representative of the hierarchy order
+ */
+ this.hierarchySort = function (parentGroups, rootParentId, result) {
+
+ if (parentGroups[rootParentId] == undefined) return;
+ var arr = parentGroups[rootParentId].sort(hierarchySortFunc);
+ for (var i = 0; i < arr.length; i++) {
+ result.push(arr[i]);
+ this.hierarchySort(parentGroups, arr[i].id, result);
+ }
+
+ return result;
+ };
+
+ /*
+ * take a flat tenant array and
+ * group the tenants by parentId
+ */
+ this.groupTenantsByParent = function (tenants) {
+
+ var parentGroups = {};
+
+ for (var i = 0; i < tenants.length; i++) {
+ if (parentGroups[tenants[i].parentId] == undefined)
parentGroups[tenants[i].parentId] = [];
+ parentGroups[tenants[i].parentId].push(tenants[i]);
+ }
+
+ return parentGroups;
+ };
+
/*
* Takes a flat tenant array and turns it into a hierarchical
* representation of tenants based on their parent/child relationships
*/
- this.convertToHierarchy = function(tenantsArr) {
+ this.convertToHierarchy = function (tenantsArr) {
var map = {};
for (var i = 0; i < tenantsArr.length; i++) {
var obj = tenantsArr[i];
- obj.children= [];
+ obj.children = [];
map[obj.id] = obj;
var parent = (i == 0) ? '-' : obj.parentId; // the
first item is always the top-level tenant
- if(!map[parent]){
+ if (!map[parent]) {
map[parent] = {
children: []
};
@@ -55,7 +91,7 @@ var TenantUtils = function() {
return map['-'].children;
};
- this.addLevels = function(tenants) {
+ this.addLevels = function (tenants) {
// convert a flat tenant list into a hierarchy
var tenantHierarchy = this.convertToHierarchy(tenants);
// and then walk down the hierarchy to find out how deeply
nested each tenant is and add a 'level' property to each tenant
diff --git a/traffic_portal/app/src/modules/private/tenants/list/index.js
b/traffic_portal/app/src/modules/private/tenants/list/index.js
index b5c3353d7..566cbcacd 100644
--- a/traffic_portal/app/src/modules/private/tenants/list/index.js
+++ b/traffic_portal/app/src/modules/private/tenants/list/index.js
@@ -27,8 +27,11 @@ module.exports =
angular.module('trafficPortal.private.tenants.list', [])
templateUrl:
'common/modules/table/tenants/table.tenants.tpl.html',
controller: 'TableTenantsController',
resolve: {
+ currentUserTenant: function(tenantService,
userModel) {
+ return
tenantService.getTenant(userModel.user.tenantId);
+ },
tenants: function(tenantService) {
- return tenantService.getTenants();
+ return tenantService.getTenants({ orderby:
'name' });
}
}
}
diff --git a/traffic_portal/app/src/modules/private/user/UserController.js
b/traffic_portal/app/src/modules/private/user/UserController.js
index 618c412fd..ad4932002 100644
--- a/traffic_portal/app/src/modules/private/user/UserController.js
+++ b/traffic_portal/app/src/modules/private/user/UserController.js
@@ -36,10 +36,13 @@ var UserController = function($scope, $state, $location,
$uibModal, formUtils, l
};
var getTenants = function() {
- tenantService.getTenants()
- .then(function(result) {
- $scope.tenants = result;
- tenantUtils.addLevels($scope.tenants);
+ tenantService.getTenant(userModel.user.tenantId)
+ .then(function(tenant) {
+ tenantService.getTenants()
+ .then(function(tenants) {
+ $scope.tenants =
tenantUtils.hierarchySort(tenantUtils.groupTenantsByParent(tenants),
tenant.parentId, []);
+ tenantUtils.addLevels($scope.tenants);
+ });
});
};
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services