blob: 76cce2de2d2323ea96292779857e1667808693d1 [file] [log] [blame]
Matteo Scandolo46b56102015-12-16 14:23:08 -08001angular.module('autoscaling')
Matteo Scandolo4d7ee392015-12-16 16:23:18 -08002.directive('serviceContainer', function(lodash, Autoscaling){
Matteo Scandolo46b56102015-12-16 14:23:08 -08003 return {
4 restrict: 'E',
5 scope: {},
6 bindToController: true,
7 controllerAs: 'vm',
8 templateUrl: 'templates/service-container.tpl.html',
Matteo Scandolo4d7ee392015-12-16 16:23:18 -08009 controller: function($rootScope) {
Matteo Scandolo35488182015-12-17 11:16:57 -080010
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080011 this.loader = true;
12
Matteo Scandolo35488182015-12-17 11:16:57 -080013 // set to true when a service is manually selected
14 this.manualSelect = false;
15
16 // start polling
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080017 Autoscaling.getAutoscalingData();
Matteo Scandolo35488182015-12-17 11:16:57 -080018
19 // list to polling events
Matteo Scandolo8942e042015-12-16 14:41:21 -080020 $rootScope.$on('autoscaling.update', (evt, data) => {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080021
22 if (data.length > 0) {
23 this.loader = false;
24 };
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080025 this.printData(data);
Matteo Scandolo8942e042015-12-16 14:41:21 -080026 });
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080027
Matteo Scandolo7564bcb2015-12-17 14:34:20 -080028 // handle errors
29 $rootScope.$on('autoscaling.error', (evt, err) => {
30 this.loader = false;
31 this.error = err.data.message;
32 });
33
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080034 /**
35 * Group resources by service and slice
36 */
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080037 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) => {
Matteo Scandolo169dc162015-12-17 16:12:15 -080048 // TODO maintain the instance order
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080049 this.services[service][slice][instance] = this.services[service][slice][instance][0].queue;
50 });
51
52 })
53 });
Matteo Scandolo820d8652015-12-16 17:19:57 -080054 // arbitrary set the first service in the list as the selected one
Matteo Scandolo35488182015-12-17 11:16:57 -080055 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;
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080072 };
Matteo Scandolo46b56102015-12-16 14:23:08 -080073 }
74 };
Matteo Scandolo820d8652015-12-16 17:19:57 -080075})
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',
Matteo Scandolo35488182015-12-17 11:16:57 -080099 controller: function($scope, $timeout) {
Matteo Scandolo820d8652015-12-16 17:19:57 -0800100
Matteo Scandolo35488182015-12-17 11:16:57 -0800101 this.chart = {
102 options: {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800103 datasetFill: false,
104 animation: true,
105 // animationEasing: 'easeInBack'
Matteo Scandolo35488182015-12-17 11:16:57 -0800106 }
107 };
Matteo Scandolo820d8652015-12-16 17:19:57 -0800108
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800109 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
Matteo Scandolo820d8652015-12-16 17:19:57 -0800121 /**
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) => {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800129 // 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 , '');
Matteo Scandolo820d8652015-12-16 17:19:57 -0800137 };
138
139 /**
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800140 * 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 /**
Matteo Scandolo820d8652015-12-16 17:19:57 -0800152 * 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) => {
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800157 return this.prependValues(lodash.reduce(data[item], (list, sample) => {
Matteo Scandolo820d8652015-12-16 17:19:57 -0800158 // console.log(data[item], sample);
159 list.push(sample.counter_volume);
160 return list;
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800161 }, []), null);
Matteo Scandolo820d8652015-12-16 17:19:57 -0800162 });
163 };
164
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800165 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
Matteo Scandolo820d8652015-12-16 17:19:57 -0800181 this.drawChart = (data) => {
182
183 const instanceNames = Object.keys(data);
184
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800185 this.chart.labels = this.getLabels(this.getMostRecentSeries(data));
Matteo Scandolo820d8652015-12-16 17:19:57 -0800186 this.chart.series = instanceNames;
187 this.chart.data = this.getData(data, instanceNames);
Matteo Scandolo820d8652015-12-16 17:19:57 -0800188 }
189
Matteo Scandolo35488182015-12-17 11:16:57 -0800190 $scope.$watch(() => this.instances, (val) => {
191 $timeout(()=>{this.chart.options.animation = false}, 1000);
192 this.drawChart(val)
193 });
Matteo Scandolo820d8652015-12-16 17:19:57 -0800194
195 }
196 };
Matteo Scandolo7564bcb2015-12-17 14:34:20 -0800197});