blob: 2762fe76d598e097ce45129e91f2a2c60f20b180 [file] [log] [blame]
Matteo Scandolo8942e042015-12-16 14:41:21 -08001'use strict';
2
3angular.module('autoscaling')
Matteo Scandolo7564bcb2015-12-17 14:34:20 -08004.service('Autoscaling', function($http, $interval, $rootScope, lodash, $q){
Matteo Scandolo8942e042015-12-16 14:41:21 -08005
Matteo Scandolo4d7ee392015-12-16 16:23:18 -08006 const pollingFrequency = 10;
Matteo Scandolo8942e042015-12-16 14:41:21 -08007 var pollinginterval;
8
Matteo Scandolo4d7ee392015-12-16 16:23:18 -08009 /**
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 Scandolo7564bcb2015-12-17 14:34:20 -080034 function requestData(url){
35
36 const deferred = $q.defer();
37
38 $http.get(url)
Matteo Scandolo820d8652015-12-16 17:19:57 -080039 .success((res) => {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080040 deferred.resolve(res);
41 })
42 .error((e) => {
43 deferred.reject(e);
Matteo Scandolo820d8652015-12-16 17:19:57 -080044 });
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080045
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 Scandolo820d8652015-12-16 17:19:57 -080061 pollinginterval = $interval(() => {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080062 requestData('/autoscaledata')
63 .then((res) => {
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080064 $rootScope.$emit('autoscaling.update', this.formatData(res));
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080065 })
66 .catch((e) => {
67 $rootScope.$emit('autoscaling.error', this.formatData(e));
Matteo Scandolo8942e042015-12-16 14:41:21 -080068 });
Matteo Scandolo820d8652015-12-16 17:19:57 -080069 }, pollingFrequency * 1000)
Matteo Scandolo8942e042015-12-16 14:41:21 -080070 };
71});