blob: 109e7438782acc6a501210f03fc2a1f9f662d675 [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) {
10 Autoscaling.getAutoscalingData();
Matteo Scandolo8942e042015-12-16 14:41:21 -080011 $rootScope.$on('autoscaling.update', (evt, data) => {
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080012 this.printData(data);
Matteo Scandolo8942e042015-12-16 14:41:21 -080013 });
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080014
15 /**
16 * Group resources by service and slice
17 */
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080018 this.printData = (data) => {
19 this.services = lodash.groupBy(data, 'service');
20 lodash.forEach(Object.keys(this.services), (service) => {
21 this.services[service] = lodash.groupBy(this.services[service], 'slice');
22 lodash.forEach(Object.keys(this.services[service]), (slice) => {
23 // grouping instance by name
24 this.services[service][slice] = lodash.groupBy(this.services[service][slice], 'instance_name');
25 // instance can't have the same name,
26 // so take them out of an array
27 // and keep only the sample data
28 lodash.forEach(Object.keys(this.services[service][slice]), (instance) => {
29 // console.log(this.services[service][slice][instance]);
30 this.services[service][slice][instance] = this.services[service][slice][instance][0].queue;
31 });
32
33 })
34 });
Matteo Scandolo820d8652015-12-16 17:19:57 -080035 // arbitrary set the first service in the list as the selected one
36 this.selectedService = this.services[Object.keys(this.services)[0]];
Matteo Scandolo4d7ee392015-12-16 16:23:18 -080037 };
Matteo Scandolo46b56102015-12-16 14:23:08 -080038 }
39 };
Matteo Scandolo820d8652015-12-16 17:19:57 -080040})
41.directive('serviceDetail', function(lodash){
42 return {
43 restrict: 'E',
44 scope: {
45 service: '=service'
46 },
47 bindToController: true,
48 controllerAs: 'vm',
49 templateUrl: 'templates/service-detail.tpl.html',
50 controller: function($scope) {
51
52 }
53 };
54})
55.directive('sliceDetail', function(lodash){
56 return {
57 restrict: 'E',
58 scope: {
59 instances: '=instances'
60 },
61 bindToController: true,
62 controllerAs: 'vm',
63 templateUrl: 'templates/slice-detail.tpl.html',
64 controller: function($scope) {
65
66 this.chart = {};
67
68 /**
69 * Goes trough the array and format date to be used as labels
70 *
71 * @param Array data
72 * @returns Array a list of labels
73 */
74
75 this.getLabels = (data) => {
76 return data.reduce((list, item) => {
77 let date = new Date(item.timestamp);
78 list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`);
79 return list;
80 }, []);
81 };
82
83 /**
84 * Convert an object of array,
85 * in an array of arrays of values
86 */
87 this.getData = (data, instanceNames) => {
88 return lodash.map(instanceNames, (item) => {
89 return lodash.reduce(data[item], (list, sample) => {
90 // console.log(data[item], sample);
91 list.push(sample.counter_volume);
92 return list;
93 }, []);
94 });
95 };
96
97 this.drawChart = (data) => {
98
99 const instanceNames = Object.keys(data);
100
101 this.chart.labels = this.getLabels(data[instanceNames[0]]);
102 this.chart.series = instanceNames;
103 this.chart.data = this.getData(data, instanceNames);
104
105 console.log(this.getData(data, instanceNames));
106 }
107
108 $scope.$watch(() => this.instances, (val) => {this.drawChart(val)})
109
110 }
111 };
Matteo Scandolo46b56102015-12-16 14:23:08 -0800112});
Matteo Scandolo820d8652015-12-16 17:19:57 -0800113
114// TODO
115// [x] repeat service name in a menu
116// [x] create a directive that receive a service
117// [ ] print a chart for every slice
118// [ ] print a line in the chart for every instance