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){ |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 15 | return $resource('/xos/instances', {id: '@id'}, {}); |
| 16 | }) |
| 17 | .service('Node', function($resource, $q, Instances){ |
| 18 | return $resource('/xos/nodes', {id: '@id'}, { |
| 19 | queryWithInstances: { |
| 20 | method: 'GET', |
| 21 | isArray: true, |
| 22 | interceptor: { |
| 23 | response: function(res){ |
| 24 | |
| 25 | // TODO update the API to include instances in nodes |
| 26 | // http://stackoverflow.com/questions/14573102/how-do-i-include-related-model-fields-using-django-rest-framework |
| 27 | |
| 28 | const deferred = $q.defer(); |
| 29 | |
| 30 | let requests = []; |
| 31 | |
| 32 | angular.forEach(res.data, (node) => { |
| 33 | requests.push(Instances.query({node: node.id}).$promise); |
| 34 | }) |
| 35 | |
| 36 | $q.all(requests) |
| 37 | .then((list) => { |
| 38 | res.data.map((node, i) => { |
| 39 | node.instances = list[i]; |
| 40 | return node; |
| 41 | }); |
Matteo Scandolo | 170d3be | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 42 | deferred.resolve(res.data); |
| 43 | }) |
| 44 | |
| 45 | return deferred.promise; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | }); |
Matteo Scandolo | 06f45d6 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 50 | }) |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 51 | .service('Subscribers', function($resource, $q, SubscriberDevice){ |
| 52 | return $resource('/xos/subscribers', {id: '@id'}, { |
| 53 | queryWithDevices: { |
| 54 | method: 'GET', |
| 55 | isArray: true, |
| 56 | interceptor: { |
| 57 | response: function(res){ |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * For each subscriber retrieve devices and append them |
| 61 | */ |
| 62 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 63 | const deferred = $q.defer(); |
| 64 | |
| 65 | let requests = []; |
| 66 | |
| 67 | angular.forEach(res.data, (subscriber) => { |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 68 | requests.push(SubscriberDevice.query({id: subscriber.id}).$promise); |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 69 | }) |
| 70 | |
| 71 | $q.all(requests) |
| 72 | .then((list) => { |
Matteo Scandolo | 7e67a9a | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 73 | |
| 74 | // adding devices |
| 75 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 76 | res.data.map((subscriber, i) => { |
| 77 | subscriber.devices = list[i]; |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 78 | subscriber.type = 'subscriber'; |
| 79 | |
| 80 | subscriber.devices.map(d => d.type = 'device') |
| 81 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 82 | return subscriber; |
| 83 | }); |
Matteo Scandolo | 219b1a7 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 84 | |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 85 | // faking to have 2 subscriber |
Matteo Scandolo | fd46858 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 86 | // res.data.push(angular.copy(res.data[0])); |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 87 | |
Matteo Scandolo | 7547f04 | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 88 | deferred.resolve(res.data); |
| 89 | }) |
| 90 | |
| 91 | return deferred.promise; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | }); |
| 96 | }) |
| 97 | .service('SubscriberDevice', function($resource){ |
| 98 | return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'}); |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 99 | }) |
Matteo Scandolo | 071ef46 | 2016-01-25 12:00:42 -0800 | [diff] [blame] | 100 | .service('ServiceRelation', function($q, lodash, Services, Tenant, Slice, Instances){ |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 101 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 102 | // count the mas depth of an object |
| 103 | const depthOf = (obj) => { |
| 104 | var depth = 0; |
| 105 | if (obj.children) { |
| 106 | obj.children.forEach(function (d) { |
| 107 | var tmpDepth = depthOf(d); |
| 108 | if (tmpDepth > depth) { |
| 109 | depth = tmpDepth |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | return 1 + depth |
| 114 | }; |
| 115 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 116 | // find all the relation defined for a given root |
| 117 | const findLevelRelation = (tenants, rootId) => { |
| 118 | return lodash.filter(tenants, service => { |
| 119 | return service.subscriber_service === rootId; |
| 120 | }); |
| 121 | }; |
| 122 | |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 123 | const findSpecificInformation = (tenants, rootId) => { |
| 124 | var tenants = lodash.filter(tenants, service => { |
| 125 | return service.provider_service === rootId && service.subscriber_tenant; |
| 126 | }); |
| 127 | |
| 128 | var info; |
| 129 | |
| 130 | tenants.forEach((tenant) => { |
| 131 | if(tenant.service_specific_attribute){ |
| 132 | info = JSON.parse(tenant.service_specific_attribute); |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | return info; |
| 137 | }; |
| 138 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 139 | // find all the service defined by a given array of relations |
| 140 | const findLevelServices = (relations, services) => { |
| 141 | const levelServices = []; |
| 142 | lodash.forEach(relations, (tenant) => { |
| 143 | var service = lodash.find(services, {id: tenant.provider_service}); |
| 144 | levelServices.push(service); |
| 145 | }); |
| 146 | return levelServices; |
| 147 | }; |
| 148 | |
| 149 | const buildLevel = (tenants, services, rootService, parentName = null) => { |
| 150 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 151 | // build an array of unlinked services |
| 152 | // these are the services that should still placed in the tree |
| 153 | var unlinkedServices = lodash.difference(services, [rootService]); |
| 154 | |
| 155 | // find all relations relative to this rootElement |
| 156 | const levelRelation = findLevelRelation(tenants, rootService.id); |
| 157 | |
| 158 | // find all items related to rootElement |
| 159 | const levelServices = findLevelServices(levelRelation, services); |
| 160 | |
| 161 | // remove this item from the list (performance |
| 162 | unlinkedServices = lodash.difference(unlinkedServices, levelServices); |
| 163 | |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 164 | rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id); |
| 165 | |
| 166 | const tree = { |
| 167 | name: rootService.humanReadableName, |
| 168 | parent: parentName, |
| 169 | type: 'service', |
| 170 | service: rootService, |
| 171 | children: [] |
| 172 | }; |
| 173 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 174 | lodash.forEach(levelServices, (service) => { |
| 175 | tree.children.push(buildLevel(tenants, unlinkedServices, service, rootService.humanReadableName)); |
| 176 | }); |
| 177 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 178 | // if it is the last element append internet |
| 179 | if(tree.children.length === 0){ |
| 180 | tree.children.push({ |
Matteo Scandolo | 9fe01af | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 181 | name: 'Router', |
| 182 | type: 'router', |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 183 | children: [] |
| 184 | }); |
| 185 | } |
| 186 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 187 | return tree; |
| 188 | }; |
| 189 | |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame^] | 190 | const buildSubscriberServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 191 | |
| 192 | // find the root service |
| 193 | // it is the one attached to subsriber_root |
| 194 | // as now we have only one root so this can work |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 195 | const rootServiceId = lodash.find(tenants, {subscriber_root: subscriber.id}).provider_service; |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 196 | const rootService = lodash.find(services, {id: rootServiceId}); |
| 197 | |
| 198 | const serviceTree = buildLevel(tenants, services, rootService); |
| 199 | |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 200 | return { |
| 201 | name: subscriber.name, |
| 202 | parent: null, |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 203 | type: 'subscriber', |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 204 | children: [serviceTree] |
| 205 | }; |
| 206 | |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 207 | }; |
| 208 | |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame^] | 209 | // applying domain knowledge to build the global service tree |
| 210 | const buildServiceTree = (services, tenants) => { |
| 211 | |
| 212 | // TODO refactor |
| 213 | const buildChild = (services, tenants, currentService) => { |
| 214 | let tenant = lodash.find(tenants, {subscriber_service: currentService.id}); |
| 215 | if(tenant){ |
| 216 | let next = lodash.find(services, {id: tenant.provider_service}); |
| 217 | currentService.children = [buildChild(services, tenants, next)]; |
| 218 | } |
| 219 | else { |
| 220 | currentService.children = [ |
| 221 | { |
| 222 | name: 'Router', |
| 223 | type: 'router', |
| 224 | children: [] |
| 225 | } |
| 226 | ] |
| 227 | } |
| 228 | currentService.type = 'service'; |
| 229 | delete currentService.id; // conflict with d3 |
| 230 | return currentService; |
| 231 | } |
| 232 | let baseService = lodash.find(services, {id: 3}); |
| 233 | |
| 234 | const baseData = { |
| 235 | name: 'Subscriber', |
| 236 | type: 'subscriber', |
| 237 | parent: null, |
| 238 | children: [buildChild(services, tenants, baseService)] |
| 239 | }; |
| 240 | |
| 241 | console.log(baseData); |
| 242 | return baseData; |
| 243 | }; |
| 244 | |
| 245 | const getBySubscriber = (subscriber) => { |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 246 | var deferred = $q.defer(); |
| 247 | var services, tenants; |
| 248 | Services.query().$promise |
| 249 | .then((res) => { |
| 250 | services = res; |
| 251 | return Tenant.query().$promise; |
| 252 | }) |
| 253 | .then((res) => { |
| 254 | tenants = res; |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame^] | 255 | deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber)); |
Matteo Scandolo | f16b379 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 256 | }) |
| 257 | .catch((e) => { |
| 258 | throw new Error(e); |
| 259 | }); |
| 260 | |
| 261 | return deferred.promise; |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 262 | }; |
Matteo Scandolo | ff7df76 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 263 | |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame^] | 264 | const get = () => { |
| 265 | var deferred = $q.defer(); |
| 266 | var services, tenants; |
| 267 | Services.query().$promise |
| 268 | .then((res) => { |
| 269 | services = res; |
| 270 | return Tenant.query({kind: 'coarse'}).$promise; |
| 271 | }) |
| 272 | .then((res) => { |
| 273 | tenants = res; |
| 274 | deferred.resolve(buildServiceTree(services, tenants)); |
| 275 | }) |
| 276 | .catch((e) => { |
| 277 | throw new Error(e); |
| 278 | }); |
| 279 | |
| 280 | return deferred.promise; |
| 281 | } |
| 282 | |
Matteo Scandolo | f2c9901 | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 283 | // export APIs |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 284 | return { |
| 285 | get: get, |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 286 | buildServiceTree: buildServiceTree, |
Matteo Scandolo | 77d8fa0 | 2016-02-16 17:43:00 -0800 | [diff] [blame^] | 287 | getBySubscriber: getBySubscriber, |
| 288 | buildLevel: buildLevel, |
| 289 | buildSubscriberServiceTree: buildSubscriberServiceTree, |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 290 | findLevelRelation: findLevelRelation, |
| 291 | findLevelServices: findLevelServices, |
| 292 | depthOf: depthOf, |
Matteo Scandolo | c9ebd92 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 293 | findSpecificInformation: findSpecificInformation |
Matteo Scandolo | 5bf0457 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 294 | } |
Matteo Scandolo | 8a64fa4 | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 295 | }); |
| 296 | |
| 297 | }()); |