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