Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | angular.module('xos.serviceTopology') |
| 5 | .service('Services', function($resource){ |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 6 | return $resource('/xos/services/:id', {id: '@id'}); |
| 7 | }) |
| 8 | .service('Tenant', function($resource){ |
| 9 | return $resource('/xos/tenants'); |
| 10 | }) |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 11 | .service('Slice', function($resource){ |
| 12 | return $resource('/xos/slices', {id: '@id'}); |
| 13 | }) |
| 14 | .service('Instances', function($resource){ |
| 15 | return $resource('/xos/instances', {id: '@id'}); |
| 16 | }) |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 17 | .service('ServiceRelation', function($q, _, lodash, Services, Tenant){ |
| 18 | |
| 19 | // find all the relation defined for a given root |
| 20 | const findLevelRelation = (tenants, rootId) => { |
| 21 | return lodash.filter(tenants, service => { |
| 22 | return service.subscriber_service === rootId; |
| 23 | }); |
| 24 | }; |
| 25 | |
| 26 | // find all the service defined by a given array of relations |
| 27 | const findLevelServices = (relations, services) => { |
| 28 | const levelServices = []; |
| 29 | lodash.forEach(relations, (tenant) => { |
| 30 | var service = lodash.find(services, {id: tenant.provider_service}); |
| 31 | levelServices.push(service); |
| 32 | }); |
| 33 | return levelServices; |
| 34 | }; |
| 35 | |
| 36 | const buildLevel = (tenants, services, rootService, parentName = null) => { |
| 37 | |
| 38 | const tree = { |
| 39 | name: rootService.humanReadableName, |
| 40 | parent: parentName, |
| 41 | children: [] |
| 42 | }; |
| 43 | |
| 44 | // build an array of unlinked services |
| 45 | // these are the services that should still placed in the tree |
| 46 | var unlinkedServices = lodash.difference(services, [rootService]); |
| 47 | |
| 48 | // find all relations relative to this rootElement |
| 49 | const levelRelation = findLevelRelation(tenants, rootService.id); |
| 50 | |
| 51 | // find all items related to rootElement |
| 52 | const levelServices = findLevelServices(levelRelation, services); |
| 53 | |
| 54 | // remove this item from the list (performance |
| 55 | unlinkedServices = lodash.difference(unlinkedServices, levelServices); |
| 56 | |
| 57 | lodash.forEach(levelServices, (service) => { |
| 58 | tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName)); |
| 59 | }); |
| 60 | |
| 61 | return tree; |
| 62 | }; |
| 63 | |
| 64 | const buildServiceTree = (services, tenants) => { |
| 65 | |
| 66 | // find the root service |
| 67 | // it is the one attached to subsriber_root |
| 68 | // as now we have only one root so this can work |
| 69 | const rootServiceId = lodash.find(tenants, {subscriber_root: 1}).provider_service; |
| 70 | const rootService = lodash.find(services, {id: rootServiceId}); |
| 71 | |
| 72 | const serviceTree = buildLevel(tenants, services, rootService); |
| 73 | |
| 74 | return serviceTree; |
| 75 | }; |
| 76 | |
| 77 | this.get = () => { |
| 78 | var deferred = $q.defer(); |
| 79 | var services, tenants; |
| 80 | Services.query().$promise |
| 81 | .then((res) => { |
| 82 | services = res; |
| 83 | return Tenant.query().$promise; |
| 84 | }) |
| 85 | .then((res) => { |
| 86 | tenants = res; |
| 87 | deferred.resolve(buildServiceTree(services, tenants)); |
| 88 | }) |
| 89 | .catch((e) => { |
| 90 | throw new Error(e); |
| 91 | }); |
| 92 | |
| 93 | return deferred.promise; |
| 94 | } |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 95 | }); |
| 96 | |
| 97 | }()); |