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