blob: 76cce2de2d2323ea96292779857e1667808693d1 [file] [log] [blame]
Matteo Scandolobf14f882016-06-02 10:01:34 -07001angular.module('autoscaling')
2.directive('serviceContainer', function(lodash, Autoscaling){
3 return {
4 restrict: 'E',
5 scope: {},
6 bindToController: true,
7 controllerAs: 'vm',
8 templateUrl: 'templates/service-container.tpl.html',
9 controller: function($rootScope) {
10
11 this.loader = true;
12
13 // set to true when a service is manually selected
14 this.manualSelect = false;
15
16 // start polling
17 Autoscaling.getAutoscalingData();
18
19 // list to polling events
20 $rootScope.$on('autoscaling.update', (evt, data) => {
21
22 if (data.length > 0) {
23 this.loader = false;
24 };
25 this.printData(data);
26 });
27
28 // handle errors
29 $rootScope.$on('autoscaling.error', (evt, err) => {
30 this.loader = false;
31 this.error = err.data.message;
32 });
33
34 /**
35 * Group resources by service and slice
36 */
37 this.printData = (data) => {
38 this.services = lodash.groupBy(data, 'service');
39 lodash.forEach(Object.keys(this.services), (service) => {
40 this.services[service] = lodash.groupBy(this.services[service], 'slice');
41 lodash.forEach(Object.keys(this.services[service]), (slice) => {
42 // grouping instance by name
43 this.services[service][slice] = lodash.groupBy(this.services[service][slice], 'instance_name');
44 // instance can't have the same name,
45 // so take them out of an array
46 // and keep only the sample data
47 lodash.forEach(Object.keys(this.services[service][slice]), (instance) => {
48 // TODO maintain the instance order
49 this.services[service][slice][instance] = this.services[service][slice][instance][0].queue;
50 });
51
52 })
53 });
54 // arbitrary set the first service in the list as the selected one
55 if(!this.manualSelect){
56 this.serviceName = Object.keys(this.services)[0];
57 this.selectedService = this.services[Object.keys(this.services)[0]];
58 }
59 else{
60 this.selectedService = this.services[this.serviceName]
61 }
62 };
63
64 /**
65 * Change the current selected service
66 */
67
68 this.selectService = (serviceName) => {
69 this.serviceName = serviceName;
70 this.selectedService = this.services[serviceName];
71 this.manualSelect = true;
72 };
73 }
74 };
75})
76.directive('serviceDetail', function(lodash){
77 return {
78 restrict: 'E',
79 scope: {
80 service: '=service'
81 },
82 bindToController: true,
83 controllerAs: 'vm',
84 templateUrl: 'templates/service-detail.tpl.html',
85 controller: function($scope) {
86
87 }
88 };
89})
90.directive('sliceDetail', function(lodash){
91 return {
92 restrict: 'E',
93 scope: {
94 instances: '=instances'
95 },
96 bindToController: true,
97 controllerAs: 'vm',
98 templateUrl: 'templates/slice-detail.tpl.html',
99 controller: function($scope, $timeout) {
100
101 this.chart = {
102 options: {
103 datasetFill: false,
104 animation: true,
105 // animationEasing: 'easeInBack'
106 }
107 };
108
109 this.chartColors = [
110 '#286090',
111 '#F7464A',
112 '#46BFBD',
113 '#FDB45C',
114 '#97BBCD',
115 '#4D5360',
116 '#8c4f9f'
117 ];
118
119 Chart.defaults.global.colours = this.chartColors;
120
121 /**
122 * Goes trough the array and format date to be used as labels
123 *
124 * @param Array data
125 * @returns Array a list of labels
126 */
127
128 this.getLabels = (data) => {
129 // we should compare the labels and get the last available
130 return this.prependValues(
131 data.reduce((list, item) => {
132 let date = new Date(item.timestamp);
133 list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`);
134 return list;
135 }, [])
136 , '');
137 };
138
139 /**
140 * Prepend value if the array is less than 10 element
141 */
142 this.prependValues = (list, value) => {
143 if(list.length < 10){
144 list.unshift(value);
145 // call itself to check again
146 return this.prependValues(list, value);
147 }
148 return list;
149 }
150
151 /**
152 * Convert an object of array,
153 * in an array of arrays of values
154 */
155 this.getData = (data, instanceNames) => {
156 return lodash.map(instanceNames, (item) => {
157 return this.prependValues(lodash.reduce(data[item], (list, sample) => {
158 // console.log(data[item], sample);
159 list.push(sample.counter_volume);
160 return list;
161 }, []), null);
162 });
163 };
164
165 this.getMostRecentSeries = (instances) => {
166 // console.log(instances);
167 const newestValues = [];
168 instances = lodash.toArray(instances)
169 lodash.forEach(instances, (values) => {
170 newestValues.push(lodash.max(values, item => new Date(item.timestamp)));
171 });
172
173 var highestValue = 0;
174 var newestInstanceIndex = lodash.findIndex(newestValues, (val) => {
175 return new Date(val.timestamp) > highestValue;
176 });
177
178 return instances[newestInstanceIndex]
179 }
180
181 this.drawChart = (data) => {
182
183 const instanceNames = Object.keys(data);
184
185 this.chart.labels = this.getLabels(this.getMostRecentSeries(data));
186 this.chart.series = instanceNames;
187 this.chart.data = this.getData(data, instanceNames);
188 }
189
190 $scope.$watch(() => this.instances, (val) => {
191 $timeout(()=>{this.chart.options.animation = false}, 1000);
192 this.drawChart(val)
193 });
194
195 }
196 };
197});