blob: 5af613d6ea2d986d5d8bb023c27f769d103212a4 [file] [log] [blame]
Matteo Scandolo8942e042015-12-16 14:41:21 -08001'use strict';
2
3angular.module('autoscaling')
Matteo Scandolo4d7ee392015-12-16 16:23:18 -08004.service('Autoscaling', function($http, $interval, $rootScope, lodash){
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 Scandolo8942e042015-12-16 14:41:21 -080034 this.getAutoscalingData = () => {
Matteo Scandolo820d8652015-12-16 17:19:57 -080035 $http.get('/autoscaledata')
36 .success((res) => {
37 $rootScope.$emit('autoscaling.update', this.formatData(res));
38 });
39 pollinginterval = $interval(() => {
40 $http.get('/autoscaledata')
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080041 .success((res) => {
42 $rootScope.$emit('autoscaling.update', this.formatData(res));
Matteo Scandolo8942e042015-12-16 14:41:21 -080043 });
Matteo Scandolo820d8652015-12-16 17:19:57 -080044 }, pollingFrequency * 1000)
Matteo Scandolo8942e042015-12-16 14:41:21 -080045 };
46});