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