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 | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 20 | .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){ |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 21 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 22 | // count the mas depth of an object |
| 23 | const depthOf = (obj) => { |
| 24 | var depth = 0; |
| 25 | if (obj.children) { |
| 26 | obj.children.forEach(function (d) { |
| 27 | var tmpDepth = depthOf(d); |
| 28 | if (tmpDepth > depth) { |
| 29 | depth = tmpDepth |
| 30 | } |
| 31 | }) |
| 32 | } |
| 33 | return 1 + depth |
| 34 | }; |
| 35 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 36 | // find all the relation defined for a given root |
| 37 | const findLevelRelation = (tenants, rootId) => { |
| 38 | return lodash.filter(tenants, service => { |
| 39 | return service.subscriber_service === rootId; |
| 40 | }); |
| 41 | }; |
| 42 | |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 43 | const findSpecificInformation = (tenants, rootId) => { |
| 44 | var tenants = lodash.filter(tenants, service => { |
| 45 | return service.provider_service === rootId && service.subscriber_tenant; |
| 46 | }); |
| 47 | |
| 48 | var info; |
| 49 | |
| 50 | tenants.forEach((tenant) => { |
| 51 | if(tenant.service_specific_attribute){ |
| 52 | info = JSON.parse(tenant.service_specific_attribute); |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | return info; |
| 57 | }; |
| 58 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 59 | // find all the service defined by a given array of relations |
| 60 | const findLevelServices = (relations, services) => { |
| 61 | const levelServices = []; |
| 62 | lodash.forEach(relations, (tenant) => { |
| 63 | var service = lodash.find(services, {id: tenant.provider_service}); |
| 64 | levelServices.push(service); |
| 65 | }); |
| 66 | return levelServices; |
| 67 | }; |
| 68 | |
| 69 | const buildLevel = (tenants, services, rootService, parentName = null) => { |
| 70 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 71 | // build an array of unlinked services |
| 72 | // these are the services that should still placed in the tree |
| 73 | var unlinkedServices = lodash.difference(services, [rootService]); |
| 74 | |
| 75 | // find all relations relative to this rootElement |
| 76 | const levelRelation = findLevelRelation(tenants, rootService.id); |
| 77 | |
| 78 | // find all items related to rootElement |
| 79 | const levelServices = findLevelServices(levelRelation, services); |
| 80 | |
| 81 | // remove this item from the list (performance |
| 82 | unlinkedServices = lodash.difference(unlinkedServices, levelServices); |
| 83 | |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 84 | rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id); |
| 85 | |
| 86 | const tree = { |
| 87 | name: rootService.humanReadableName, |
| 88 | parent: parentName, |
| 89 | type: 'service', |
| 90 | service: rootService, |
| 91 | children: [] |
| 92 | }; |
| 93 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 94 | lodash.forEach(levelServices, (service) => { |
| 95 | tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName)); |
| 96 | }); |
| 97 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 98 | // if it is the last element append internet |
| 99 | if(tree.children.length === 0){ |
| 100 | tree.children.push({ |
| 101 | name: 'Internet', |
| 102 | type: 'internet', |
| 103 | children: [] |
| 104 | }); |
| 105 | } |
| 106 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 107 | return tree; |
| 108 | }; |
| 109 | |
Matteo Scandolo | cb12a1a | 2016-01-25 14:11:10 -0800 | [diff] [blame] | 110 | const buildServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 111 | |
| 112 | // find the root service |
| 113 | // it is the one attached to subsriber_root |
| 114 | // as now we have only one root so this can work |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 115 | const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service; |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 116 | const rootService = lodash.find(services, {id: rootServiceId}); |
| 117 | |
| 118 | const serviceTree = buildLevel(tenants, services, rootService); |
| 119 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 120 | return { |
| 121 | name: subscriber.name, |
| 122 | parent: null, |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 123 | type: 'subscriber', |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 124 | children: [serviceTree] |
| 125 | }; |
| 126 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 129 | const get = (subscriber) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 130 | var deferred = $q.defer(); |
| 131 | var services, tenants; |
| 132 | Services.query().$promise |
| 133 | .then((res) => { |
| 134 | services = res; |
| 135 | return Tenant.query().$promise; |
| 136 | }) |
| 137 | .then((res) => { |
| 138 | tenants = res; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 139 | deferred.resolve(buildServiceTree(services, tenants, subscriber)); |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 140 | }) |
| 141 | .catch((e) => { |
| 142 | throw new Error(e); |
| 143 | }); |
| 144 | |
| 145 | return deferred.promise; |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 146 | }; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 147 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 148 | // export APIs |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 149 | return { |
| 150 | get: get, |
| 151 | buildLevel: buildLevel, |
| 152 | buildServiceTree: buildServiceTree, |
| 153 | findLevelRelation: findLevelRelation, |
| 154 | findLevelServices: findLevelServices, |
| 155 | depthOf: depthOf, |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 156 | findSpecificInformation: findSpecificInformation |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 157 | } |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 158 | }); |
| 159 | |
| 160 | }()); |