blob: 444100617fe35c8ccb7a7ea1da1cf11c6070b1f4 [file] [log] [blame]
Matteo Scandolo7cd88ba2015-12-16 14:23:08 -08001angular.module('autoscaling')
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -08002.directive('serviceContainer', function(lodash, Autoscaling){
Matteo Scandolo7cd88ba2015-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 Scandoloeb1e53a2015-12-16 16:23:18 -08009 controller: function($rootScope) {
Matteo Scandolof75d0ac2015-12-17 11:16:57 -080010
11 // set to true when a service is manually selected
12 this.manualSelect = false;
13
14 // start polling
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -080015 Autoscaling.getAutoscalingData();
Matteo Scandolof75d0ac2015-12-17 11:16:57 -080016
17 // list to polling events
Matteo Scandolo69adff82015-12-16 14:41:21 -080018 $rootScope.$on('autoscaling.update', (evt, data) => {
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -080019 this.printData(data);
Matteo Scandolo69adff82015-12-16 14:41:21 -080020 });
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -080021
22 /**
23 * Group resources by service and slice
24 */
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -080025 this.printData = (data) => {
26 this.services = lodash.groupBy(data, 'service');
27 lodash.forEach(Object.keys(this.services), (service) => {
28 this.services[service] = lodash.groupBy(this.services[service], 'slice');
29 lodash.forEach(Object.keys(this.services[service]), (slice) => {
30 // grouping instance by name
31 this.services[service][slice] = lodash.groupBy(this.services[service][slice], 'instance_name');
32 // instance can't have the same name,
33 // so take them out of an array
34 // and keep only the sample data
35 lodash.forEach(Object.keys(this.services[service][slice]), (instance) => {
36 // console.log(this.services[service][slice][instance]);
37 this.services[service][slice][instance] = this.services[service][slice][instance][0].queue;
38 });
39
40 })
41 });
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -080042 // arbitrary set the first service in the list as the selected one
Matteo Scandolof75d0ac2015-12-17 11:16:57 -080043 if(!this.manualSelect){
44 this.serviceName = Object.keys(this.services)[0];
45 this.selectedService = this.services[Object.keys(this.services)[0]];
46 }
47 else{
48 this.selectedService = this.services[this.serviceName]
49 }
50 };
51
52 /**
53 * Change the current selected service
54 */
55
56 this.selectService = (serviceName) => {
57 this.serviceName = serviceName;
58 this.selectedService = this.services[serviceName];
59 this.manualSelect = true;
Matteo Scandoloeb1e53a2015-12-16 16:23:18 -080060 };
Matteo Scandolo7cd88ba2015-12-16 14:23:08 -080061 }
62 };
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -080063})
64.directive('serviceDetail', function(lodash){
65 return {
66 restrict: 'E',
67 scope: {
68 service: '=service'
69 },
70 bindToController: true,
71 controllerAs: 'vm',
72 templateUrl: 'templates/service-detail.tpl.html',
73 controller: function($scope) {
74
75 }
76 };
77})
78.directive('sliceDetail', function(lodash){
79 return {
80 restrict: 'E',
81 scope: {
82 instances: '=instances'
83 },
84 bindToController: true,
85 controllerAs: 'vm',
86 templateUrl: 'templates/slice-detail.tpl.html',
Matteo Scandolof75d0ac2015-12-17 11:16:57 -080087 controller: function($scope, $timeout) {
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -080088
Matteo Scandolof75d0ac2015-12-17 11:16:57 -080089 this.chart = {
90 options: {
91 animation: true
92 }
93 };
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -080094
95 /**
96 * Goes trough the array and format date to be used as labels
97 *
98 * @param Array data
99 * @returns Array a list of labels
100 */
101
102 this.getLabels = (data) => {
103 return data.reduce((list, item) => {
104 let date = new Date(item.timestamp);
105 list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`);
106 return list;
107 }, []);
108 };
109
110 /**
111 * Convert an object of array,
112 * in an array of arrays of values
113 */
114 this.getData = (data, instanceNames) => {
115 return lodash.map(instanceNames, (item) => {
116 return lodash.reduce(data[item], (list, sample) => {
117 // console.log(data[item], sample);
118 list.push(sample.counter_volume);
119 return list;
120 }, []);
121 });
122 };
123
124 this.drawChart = (data) => {
125
126 const instanceNames = Object.keys(data);
127
128 this.chart.labels = this.getLabels(data[instanceNames[0]]);
129 this.chart.series = instanceNames;
130 this.chart.data = this.getData(data, instanceNames);
131
Matteo Scandolof75d0ac2015-12-17 11:16:57 -0800132 // console.log(this.getData(data, instanceNames));
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -0800133 }
134
Matteo Scandolof75d0ac2015-12-17 11:16:57 -0800135 $scope.$watch(() => this.instances, (val) => {
136 $timeout(()=>{this.chart.options.animation = false}, 1000);
137 this.drawChart(val)
138 });
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -0800139
140 }
141 };
Matteo Scandolo7cd88ba2015-12-16 14:23:08 -0800142});
Matteo Scandolo5fdd7c52015-12-16 17:19:57 -0800143
144// TODO
145// [x] repeat service name in a menu
146// [x] create a directive that receive a service
147// [ ] print a chart for every slice
148// [ ] print a line in the chart for every instance