Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 1 | angular.module('autoscaling') |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 2 | .directive('serviceContainer', function(lodash, Autoscaling){ |
Matteo Scandolo | 7cd88ba | 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 | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 9 | controller: function($rootScope) { |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 10 | |
| 11 | // set to true when a service is manually selected |
| 12 | this.manualSelect = false; |
| 13 | |
| 14 | // start polling |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 15 | Autoscaling.getAutoscalingData(); |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 16 | |
| 17 | // list to polling events |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 18 | $rootScope.$on('autoscaling.update', (evt, data) => { |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 19 | this.printData(data); |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 20 | }); |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Group resources by service and slice |
| 24 | */ |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 25 | 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 Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 42 | // arbitrary set the first service in the list as the selected one |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 43 | 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 Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 60 | }; |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 61 | } |
| 62 | }; |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 63 | }) |
| 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 Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 87 | controller: function($scope, $timeout) { |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 88 | |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 89 | this.chart = { |
| 90 | options: { |
| 91 | animation: true |
| 92 | } |
| 93 | }; |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 94 | |
| 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 Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 132 | // console.log(this.getData(data, instanceNames)); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame^] | 135 | $scope.$watch(() => this.instances, (val) => { |
| 136 | $timeout(()=>{this.chart.options.animation = false}, 1000); |
| 137 | this.drawChart(val) |
| 138 | }); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 139 | |
| 140 | } |
| 141 | }; |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 142 | }); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 143 | |
| 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 |