blob: 2762fe76d598e097ce45129e91f2a2c60f20b180 [file] [log] [blame]
Matteo Scandolobf14f882016-06-02 10:01:34 -07001'use strict';
2
3angular.module('autoscaling')
4.service('Autoscaling', function($http, $interval, $rootScope, lodash, $q){
5
6 const pollingFrequency = 10;
7 var pollinginterval;
8
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
34 function requestData(url){
35
36 const deferred = $q.defer();
37
38 $http.get(url)
39 .success((res) => {
40 deferred.resolve(res);
41 })
42 .error((e) => {
43 deferred.reject(e);
44 });
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
61 pollinginterval = $interval(() => {
62 requestData('/autoscaledata')
63 .then((res) => {
64 $rootScope.$emit('autoscaling.update', this.formatData(res));
65 })
66 .catch((e) => {
67 $rootScope.$emit('autoscaling.error', this.formatData(e));
68 });
69 }, pollingFrequency * 1000)
70 };
71});