Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('autoscaling') |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 4 | .service('Autoscaling', function($http, $interval, $rootScope, lodash, $q){ |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 5 | |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 6 | const pollingFrequency = 10; |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 7 | var pollinginterval; |
| 8 | |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 9 | /** |
| 10 | * Convert data to a flat array of resources |
| 11 | */ |
| 12 | |
| 13 | this.formatData = (data) => { |
| 14 | const list = []; |
| 15 | // cicle trough all slices |
| 16 | lodash.map(data, (item) => { |
| 17 | // cicle trough every resource |
| 18 | item.resources = lodash.forEach( |
| 19 | Object.keys(item.resources), |
| 20 | (resource) => { |
| 21 | const tmp = item.resources[resource]; |
| 22 | tmp.service = item.service; |
| 23 | tmp.slice = item.slice; |
| 24 | tmp.project_id = item.project_id; |
| 25 | tmp.instance_name = tmp.xos_instance_info.instance_name; |
| 26 | delete tmp.xos_instance_info; |
| 27 | list.push(tmp); |
| 28 | } |
| 29 | ) |
| 30 | }); |
| 31 | return list; |
| 32 | }; |
| 33 | |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 34 | function requestData(url){ |
| 35 | |
| 36 | const deferred = $q.defer(); |
| 37 | |
| 38 | $http.get(url) |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 39 | .success((res) => { |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 40 | deferred.resolve(res); |
| 41 | }) |
| 42 | .error((e) => { |
| 43 | deferred.reject(e); |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 44 | }); |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 45 | |
| 46 | return deferred.promise; |
| 47 | }; |
| 48 | |
| 49 | |
| 50 | // TODO Move to Websocket |
| 51 | this.getAutoscalingData = () => { |
| 52 | |
| 53 | requestData('/autoscaledata') |
| 54 | .then((res) => { |
| 55 | $rootScope.$emit('autoscaling.update', this.formatData(res)); |
| 56 | }) |
| 57 | .catch((e) => { |
| 58 | $rootScope.$emit('autoscaling.error', this.formatData(e)); |
| 59 | }); |
| 60 | |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 61 | pollinginterval = $interval(() => { |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 62 | requestData('/autoscaledata') |
| 63 | .then((res) => { |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 64 | $rootScope.$emit('autoscaling.update', this.formatData(res)); |
Matteo Scandolo | 7564bcb | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 65 | }) |
| 66 | .catch((e) => { |
| 67 | $rootScope.$emit('autoscaling.error', this.formatData(e)); |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 68 | }); |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 69 | }, pollingFrequency * 1000) |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 70 | }; |
| 71 | }); |