Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
Matteo Scandolo | 4b3d872 | 2016-02-24 11:22:48 -0800 | [diff] [blame] | 4 | angular.module('xos.diagnostic') |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 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){ |
Matteo Scandolo | ba2d63d | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 9 | return $resource('/xos/tenants', {id: '@id'}, { |
| 10 | queryVsgInstances: { |
| 11 | method: 'GET', |
| 12 | isArray: true, |
| 13 | interceptor: { |
| 14 | response: (res) => { |
| 15 | |
| 16 | // NOTE |
| 17 | // Note that VCPETenant is now VSGTenant. |
| 18 | |
| 19 | let instances = []; |
| 20 | |
| 21 | angular.forEach(res.data, (tenant) => { |
| 22 | let info = JSON.parse(tenant.service_specific_attribute); |
| 23 | if(info && info.instance_id){ |
| 24 | instances.push(info.instance_id); |
| 25 | } |
| 26 | }); |
| 27 | |
| 28 | return instances; |
| 29 | } |
| 30 | } |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 31 | }, |
| 32 | getSubscriberTag: { |
| 33 | method: 'GET', |
| 34 | isArray: true, |
| 35 | interceptor: { |
| 36 | response: (res) => { |
| 37 | // NOTE we should receive only one vOLT tenant here |
| 38 | return JSON.parse(res.data[0].service_specific_attribute); |
| 39 | } |
| 40 | } |
Matteo Scandolo | ba2d63d | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 41 | } |
| 42 | }); |
| 43 | }) |
| 44 | .service('Ceilometer', function($http, $q, Instances) { |
| 45 | |
| 46 | /** |
| 47 | * Get stats for a single instance |
| 48 | */ |
| 49 | this.getInstanceStats = (instanceUuid) => { |
| 50 | let deferred = $q.defer(); |
| 51 | |
Matteo Scandolo | 653c509 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 52 | $http.get('/xoslib/xos-instance-statistics', {params: {'instance-uuid': instanceUuid}}) |
Matteo Scandolo | ba2d63d | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 53 | .then((res) => { |
| 54 | deferred.resolve(res.data); |
| 55 | }) |
| 56 | .catch((e) => { |
| 57 | deferred.reject(e); |
| 58 | }) |
| 59 | |
| 60 | return deferred.promise; |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Collect stats for an array of instances |
| 65 | */ |
| 66 | this.getInstancesStats = (instances) => { |
| 67 | let deferred = $q.defer(); |
| 68 | let instancePromises = []; |
| 69 | let instanceList = []; |
| 70 | |
| 71 | // retrieve instance details |
| 72 | instances.forEach((instanceId) => { |
| 73 | instancePromises.push(Instances.get({id: instanceId}).$promise); |
| 74 | }); |
| 75 | |
| 76 | // get all instance data |
| 77 | $q.all(instancePromises) |
| 78 | .then((_instanceList) => { |
| 79 | instanceList = _instanceList; |
| 80 | let promises = []; |
| 81 | // foreach instance query stats |
| 82 | instanceList.forEach((instance) => { |
| 83 | promises.push(this.getInstanceStats(instance.instance_uuid)); |
| 84 | }); |
| 85 | return $q.all(promises); |
| 86 | }) |
| 87 | .then(stats => { |
| 88 | // augment instance with stats information |
| 89 | instanceList.map((instance, i) => { |
| 90 | instance.stats = stats[i]; |
| 91 | }); |
| 92 | deferred.resolve(instanceList); |
| 93 | }) |
| 94 | .catch(deferred.reject); |
| 95 | |
| 96 | return deferred.promise; |
| 97 | }; |
Matteo Scandolo | 653c509 | 2016-02-24 11:14:01 -0800 | [diff] [blame] | 98 | |
| 99 | this.getContainerStats = (containerName) => { |
| 100 | const deferred = $q.defer(); |
| 101 | |
| 102 | let res = {}; |
| 103 | |
| 104 | $http.get('/xoslib/meterstatistics', {params: {'resource': containerName}}) |
| 105 | .then((containerStats) => { |
| 106 | res.stats = containerStats.data; |
| 107 | return $http.get('/xoslib/meterstatistics', {params: {'resource': `${containerName}-eth0`}}) |
| 108 | }) |
| 109 | .then((portStats) => { |
| 110 | res.port = { |
| 111 | eth0: portStats.data |
| 112 | }; |
| 113 | return $http.get('/xoslib/meterstatistics', {params: {'resource': `${containerName}-eth1`}}) |
| 114 | }) |
| 115 | .then((portStats) => { |
| 116 | res.port.eth1 = portStats.data; |
| 117 | deferred.resolve(res); |
| 118 | }) |
| 119 | .catch((e) => { |
| 120 | deferred.reject(e); |
| 121 | }) |
| 122 | |
| 123 | return deferred.promise; |
| 124 | } |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 125 | }) |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 126 | .service('Slice', function($resource){ |
| 127 | return $resource('/xos/slices', {id: '@id'}); |
| 128 | }) |
| 129 | .service('Instances', function($resource){ |
Matteo Scandolo | ba2d63d | 2016-02-17 13:54:11 -0800 | [diff] [blame] | 130 | return $resource('/xos/instances/:id', {id: '@id'}); |
Matteo Scandolo | 1418393 | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 131 | }) |
| 132 | .service('Node', function($resource, $q, Instances){ |
| 133 | return $resource('/xos/nodes', {id: '@id'}, { |
| 134 | queryWithInstances: { |
| 135 | method: 'GET', |
| 136 | isArray: true, |
| 137 | interceptor: { |
| 138 | response: function(res){ |
| 139 | |
| 140 | // TODO update the API to include instances in nodes |
| 141 | // http://stackoverflow.com/questions/14573102/how-do-i-include-related-model-fields-using-django-rest-framework |
| 142 | |
| 143 | const deferred = $q.defer(); |
| 144 | |
| 145 | let requests = []; |
| 146 | |
| 147 | angular.forEach(res.data, (node) => { |
| 148 | requests.push(Instances.query({node: node.id}).$promise); |
| 149 | }) |
| 150 | |
| 151 | $q.all(requests) |
| 152 | .then((list) => { |
| 153 | res.data.map((node, i) => { |
| 154 | node.instances = list[i]; |
| 155 | return node; |
| 156 | }); |
Matteo Scandolo | 1418393 | 2016-02-11 08:58:04 -0800 | [diff] [blame] | 157 | deferred.resolve(res.data); |
| 158 | }) |
| 159 | |
| 160 | return deferred.promise; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | }); |
Matteo Scandolo | 6823626 | 2016-01-21 15:38:06 -0800 | [diff] [blame] | 165 | }) |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 166 | .service('Subscribers', function($resource, $q, SubscriberDevice){ |
Matteo Scandolo | a03110c | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 167 | return $resource('/xoslib/cordsubscriber/:id', {id: '@id'}, { |
Matteo Scandolo | 5fc82c3 | 2016-03-02 10:59:46 -0800 | [diff] [blame] | 168 | update: { |
| 169 | method: 'PUT', |
| 170 | isArray: false |
| 171 | }, |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 172 | queryWithDevices: { |
| 173 | method: 'GET', |
| 174 | isArray: true, |
| 175 | interceptor: { |
| 176 | response: function(res){ |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * For each subscriber retrieve devices and append them |
| 180 | */ |
| 181 | |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 182 | let deferred = $q.defer(); |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 183 | |
| 184 | let requests = []; |
| 185 | |
| 186 | angular.forEach(res.data, (subscriber) => { |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 187 | requests.push(SubscriberDevice.query({id: subscriber.id}).$promise); |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 188 | }) |
| 189 | |
| 190 | $q.all(requests) |
| 191 | .then((list) => { |
Matteo Scandolo | 4aae3aa | 2016-02-16 16:33:26 -0800 | [diff] [blame] | 192 | |
| 193 | // adding devices |
| 194 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 195 | res.data.map((subscriber, i) => { |
| 196 | subscriber.devices = list[i]; |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 197 | subscriber.type = 'subscriber'; |
| 198 | |
| 199 | subscriber.devices.map(d => d.type = 'device') |
| 200 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 201 | return subscriber; |
| 202 | }); |
Matteo Scandolo | eeb9c08 | 2016-02-09 11:19:22 -0800 | [diff] [blame] | 203 | |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 204 | // faking to have 2 subscriber |
Matteo Scandolo | c2c6fb0 | 2016-02-16 16:03:43 -0800 | [diff] [blame] | 205 | // res.data.push(angular.copy(res.data[0])); |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 206 | |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 207 | deferred.resolve(res.data); |
| 208 | }) |
| 209 | |
| 210 | return deferred.promise; |
| 211 | } |
| 212 | } |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 213 | }, |
| 214 | getWithDevices: { |
| 215 | method: 'GET', |
| 216 | isArray: false, |
| 217 | interceptor: { |
| 218 | response: (res) => { |
| 219 | let d = $q.defer(); |
| 220 | |
| 221 | SubscriberDevice.query({id: res.data.id}).$promise |
| 222 | .then(devices => { |
| 223 | devices.map(d => d.type = 'device'); |
| 224 | res.data.devices = devices; |
| 225 | res.data.type = 'subscriber'; |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 226 | d.resolve(res.data); |
| 227 | }) |
| 228 | .catch(err => { |
| 229 | d.reject(err); |
| 230 | }); |
| 231 | |
| 232 | return d.promise; |
| 233 | } |
| 234 | } |
Matteo Scandolo | 735606c | 2016-02-09 09:13:30 -0800 | [diff] [blame] | 235 | } |
| 236 | }); |
| 237 | }) |
| 238 | .service('SubscriberDevice', function($resource){ |
| 239 | return $resource('/xoslib/rs/subscriber/:id/users/', {id: '@id'}); |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 240 | }) |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 241 | .service('ServiceRelation', function($q, _, Services, Tenant, Slice, Instances){ |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 242 | |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 243 | // count the mas depth of an object |
| 244 | const depthOf = (obj) => { |
| 245 | var depth = 0; |
| 246 | if (obj.children) { |
| 247 | obj.children.forEach(function (d) { |
| 248 | var tmpDepth = depthOf(d); |
| 249 | if (tmpDepth > depth) { |
| 250 | depth = tmpDepth |
| 251 | } |
| 252 | }) |
| 253 | } |
| 254 | return 1 + depth |
| 255 | }; |
| 256 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 257 | // find all the relation defined for a given root |
| 258 | const findLevelRelation = (tenants, rootId) => { |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 259 | return _.filter(tenants, service => { |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 260 | return service.subscriber_service === rootId; |
| 261 | }); |
| 262 | }; |
| 263 | |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 264 | const findSpecificInformation = (tenants, rootId) => { |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 265 | var tenants = _.filter(tenants, service => { |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 266 | return service.provider_service === rootId && service.subscriber_tenant; |
| 267 | }); |
| 268 | |
| 269 | var info; |
| 270 | |
| 271 | tenants.forEach((tenant) => { |
| 272 | if(tenant.service_specific_attribute){ |
| 273 | info = JSON.parse(tenant.service_specific_attribute); |
| 274 | } |
| 275 | }); |
| 276 | |
| 277 | return info; |
| 278 | }; |
| 279 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 280 | // find all the service defined by a given array of relations |
| 281 | const findLevelServices = (relations, services) => { |
| 282 | const levelServices = []; |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 283 | _.forEach(relations, (tenant) => { |
| 284 | var service = _.find(services, {id: tenant.provider_service}); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 285 | levelServices.push(service); |
| 286 | }); |
| 287 | return levelServices; |
| 288 | }; |
| 289 | |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 290 | const buildLevel = (tenants, services, rootService, rootTenant, parentName = null) => { |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 291 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 292 | // build an array of unlinked services |
| 293 | // these are the services that should still placed in the tree |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 294 | var unlinkedServices = _.difference(services, [rootService]); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 295 | |
| 296 | // find all relations relative to this rootElement |
| 297 | const levelRelation = findLevelRelation(tenants, rootService.id); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 298 | // find all items related to rootElement |
| 299 | const levelServices = findLevelServices(levelRelation, services); |
| 300 | |
| 301 | // remove this item from the list (performance |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 302 | unlinkedServices = _.difference(unlinkedServices, levelServices); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 303 | |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 304 | rootService.service_specific_attribute = findSpecificInformation(tenants, rootService.id); |
| 305 | |
Matteo Scandolo | f0b79ae | 2016-03-07 17:58:34 -0800 | [diff] [blame] | 306 | if(rootService.humanReadableName === 'service_vbng'){ |
| 307 | rootService.humanReadableName = 'service_vrouter' |
| 308 | } |
| 309 | |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 310 | const tree = { |
| 311 | name: rootService.humanReadableName, |
| 312 | parent: parentName, |
| 313 | type: 'service', |
| 314 | service: rootService, |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 315 | tenant: rootTenant, |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 316 | children: [] |
| 317 | }; |
| 318 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 319 | _.forEach(levelServices, (service) => { |
Matteo Scandolo | 0b8b7ab | 2016-03-07 16:41:25 -0800 | [diff] [blame] | 320 | if(service.humanReadableName === 'service_ONOS_vBNG' || service.humanReadableName === 'service_ONOS_vOLT'){ |
| 321 | // remove ONOSes from service chart |
| 322 | return; |
| 323 | } |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 324 | let tenant = _.find(tenants, {subscriber_tenant: rootTenant.id, provider_service: service.id}); |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 325 | tree.children.push(buildLevel(tenants, unlinkedServices, service, tenant, rootService.humanReadableName)); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 326 | }); |
| 327 | |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 328 | // if it is the last element append internet |
| 329 | if(tree.children.length === 0){ |
| 330 | tree.children.push({ |
Matteo Scandolo | 38ba331 | 2016-02-09 16:01:49 -0800 | [diff] [blame] | 331 | name: 'Router', |
| 332 | type: 'router', |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 333 | children: [] |
| 334 | }); |
| 335 | } |
| 336 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 337 | return tree; |
| 338 | }; |
| 339 | |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 340 | const buildSubscriberServiceTree = (services, tenants, subscriber = {id: 1, name: 'fakeSubs'}) => { |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 341 | |
| 342 | // find the root service |
| 343 | // it is the one attached to subsriber_root |
| 344 | // as now we have only one root so this can work |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 345 | const rootTenant = _.find(tenants, {subscriber_root: subscriber.id}); |
| 346 | const rootService = _.find(services, {id: rootTenant.provider_service}); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 347 | |
Matteo Scandolo | 0344ef3 | 2016-02-22 16:53:22 -0800 | [diff] [blame] | 348 | const serviceTree = buildLevel(tenants, services, rootService, rootTenant); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 349 | |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 350 | return { |
Matteo Scandolo | a03110c | 2016-03-01 15:20:29 -0800 | [diff] [blame] | 351 | name: subscriber.name || subscriber.humanReadableName, |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 352 | parent: null, |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 353 | type: 'subscriber', |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 354 | children: [serviceTree] |
| 355 | }; |
| 356 | |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 357 | }; |
| 358 | |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 359 | // applying domain knowledge to build the global service tree |
| 360 | const buildServiceTree = (services, tenants) => { |
| 361 | |
| 362 | // TODO refactor |
| 363 | const buildChild = (services, tenants, currentService) => { |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 364 | |
Matteo Scandolo | f0b79ae | 2016-03-07 17:58:34 -0800 | [diff] [blame] | 365 | if(currentService.humanReadableName === 'service_vbng'){ |
| 366 | currentService.humanReadableName = 'service_vrouter' |
| 367 | } |
| 368 | |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 369 | const response = { |
| 370 | type: 'service', |
| 371 | name: currentService.humanReadableName, |
| 372 | service: currentService |
| 373 | }; |
| 374 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 375 | let tenant = _.find(tenants, {subscriber_service: currentService.id}); |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 376 | if(tenant){ |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 377 | let next = _.find(services, {id: tenant.provider_service}); |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 378 | response.children = [buildChild(services, tenants, next)]; |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 379 | } |
| 380 | else { |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 381 | response.children = [ |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 382 | { |
| 383 | name: 'Router', |
| 384 | type: 'router', |
| 385 | children: [] |
| 386 | } |
| 387 | ] |
| 388 | } |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 389 | delete currentService.id; // conflict with d3 |
Matteo Scandolo | dffc138 | 2016-02-22 14:53:44 -0800 | [diff] [blame] | 390 | return response; |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 391 | } |
Matteo Scandolo | 16763e9 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 392 | |
Matteo Scandolo | 78e9d0e | 2016-05-17 14:29:01 -0700 | [diff] [blame] | 393 | let baseService = _.find(services, {id: 3}); |
Matteo Scandolo | 16763e9 | 2016-02-24 15:40:22 -0800 | [diff] [blame] | 394 | |
| 395 | if(!angular.isDefined(baseService)){ |
| 396 | console.error('Missing Base service!'); |
| 397 | return; |
| 398 | } |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 399 | |
| 400 | const baseData = { |
| 401 | name: 'Subscriber', |
| 402 | type: 'subscriber', |
| 403 | parent: null, |
| 404 | children: [buildChild(services, tenants, baseService)] |
| 405 | }; |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 406 | return baseData; |
| 407 | }; |
| 408 | |
| 409 | const getBySubscriber = (subscriber) => { |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 410 | var deferred = $q.defer(); |
| 411 | var services, tenants; |
| 412 | Services.query().$promise |
| 413 | .then((res) => { |
| 414 | services = res; |
| 415 | return Tenant.query().$promise; |
| 416 | }) |
| 417 | .then((res) => { |
| 418 | tenants = res; |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 419 | deferred.resolve(buildSubscriberServiceTree(services, tenants, subscriber)); |
Matteo Scandolo | 85aad31 | 2016-01-21 14:23:28 -0800 | [diff] [blame] | 420 | }) |
| 421 | .catch((e) => { |
| 422 | throw new Error(e); |
| 423 | }); |
| 424 | |
| 425 | return deferred.promise; |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 426 | }; |
Matteo Scandolo | 8927639 | 2016-01-22 16:36:34 -0800 | [diff] [blame] | 427 | |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 428 | const get = () => { |
| 429 | var deferred = $q.defer(); |
| 430 | var services, tenants; |
| 431 | Services.query().$promise |
| 432 | .then((res) => { |
| 433 | services = res; |
| 434 | return Tenant.query({kind: 'coarse'}).$promise; |
| 435 | }) |
| 436 | .then((res) => { |
| 437 | tenants = res; |
| 438 | deferred.resolve(buildServiceTree(services, tenants)); |
| 439 | }) |
| 440 | .catch((e) => { |
| 441 | throw new Error(e); |
| 442 | }); |
| 443 | |
| 444 | return deferred.promise; |
| 445 | } |
| 446 | |
Matteo Scandolo | fb46f5b | 2016-01-25 10:10:38 -0800 | [diff] [blame] | 447 | // export APIs |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 448 | return { |
| 449 | get: get, |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 450 | buildServiceTree: buildServiceTree, |
Matteo Scandolo | 657d132 | 2016-02-16 17:43:00 -0800 | [diff] [blame] | 451 | getBySubscriber: getBySubscriber, |
| 452 | buildLevel: buildLevel, |
| 453 | buildSubscriberServiceTree: buildSubscriberServiceTree, |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 454 | findLevelRelation: findLevelRelation, |
| 455 | findLevelServices: findLevelServices, |
| 456 | depthOf: depthOf, |
Matteo Scandolo | 998e465 | 2016-01-28 12:02:57 -0800 | [diff] [blame] | 457 | findSpecificInformation: findSpecificInformation |
Matteo Scandolo | c86b4c1 | 2016-01-25 17:36:08 -0800 | [diff] [blame] | 458 | } |
Matteo Scandolo | be9b13d | 2016-01-21 11:21:03 -0800 | [diff] [blame] | 459 | }); |
| 460 | |
| 461 | }()); |