Matteo Scandolo | 46b5610 | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 1 | angular.module('autoscaling') |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 2 | .directive('serviceContainer', function(lodash, Autoscaling){ |
Matteo Scandolo | 46b5610 | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 3 | return { |
| 4 | restrict: 'E', |
| 5 | scope: {}, |
| 6 | bindToController: true, |
| 7 | controllerAs: 'vm', |
| 8 | templateUrl: 'templates/service-container.tpl.html', |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 9 | controller: function($rootScope) { |
| 10 | Autoscaling.getAutoscalingData(); |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 11 | $rootScope.$on('autoscaling.update', (evt, data) => { |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 12 | this.printData(data); |
Matteo Scandolo | 8942e04 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 13 | }); |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Group resources by service and slice |
| 17 | */ |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 18 | 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 Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 35 | // arbitrary set the first service in the list as the selected one |
| 36 | this.selectedService = this.services[Object.keys(this.services)[0]]; |
Matteo Scandolo | 4d7ee39 | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 37 | }; |
Matteo Scandolo | 46b5610 | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 38 | } |
| 39 | }; |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 40 | }) |
| 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 Scandolo | 46b5610 | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 112 | }); |
Matteo Scandolo | 820d865 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 113 | |
| 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 |