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 | |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 11 | this.loader = true; |
| 12 | |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 13 | // set to true when a service is manually selected |
| 14 | this.manualSelect = false; |
| 15 | |
| 16 | // start polling |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 17 | Autoscaling.getAutoscalingData(); |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 18 | |
| 19 | // list to polling events |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 20 | $rootScope.$on('autoscaling.update', (evt, data) => { |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 21 | |
| 22 | if (data.length > 0) { |
| 23 | this.loader = false; |
| 24 | }; |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 25 | this.printData(data); |
Matteo Scandolo | 69adff8 | 2015-12-16 14:41:21 -0800 | [diff] [blame] | 26 | }); |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 27 | |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 28 | // handle errors |
| 29 | $rootScope.$on('autoscaling.error', (evt, err) => { |
| 30 | this.loader = false; |
| 31 | this.error = err.data.message; |
| 32 | }); |
| 33 | |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Group resources by service and slice |
| 36 | */ |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 37 | 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 Scandolo | f8dabe5 | 2015-12-17 16:12:15 -0800 | [diff] [blame] | 48 | // TODO maintain the instance order |
Matteo Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 49 | this.services[service][slice][instance] = this.services[service][slice][instance][0].queue; |
| 50 | }); |
| 51 | |
| 52 | }) |
| 53 | }); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 54 | // arbitrary set the first service in the list as the selected one |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 55 | 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 Scandolo | eb1e53a | 2015-12-16 16:23:18 -0800 | [diff] [blame] | 72 | }; |
Matteo Scandolo | 7cd88ba | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 73 | } |
| 74 | }; |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 75 | }) |
| 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 Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 99 | controller: function($scope, $timeout) { |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 100 | |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 101 | this.chart = { |
| 102 | options: { |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 103 | datasetFill: false, |
| 104 | animation: true, |
| 105 | // animationEasing: 'easeInBack' |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 106 | } |
| 107 | }; |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 108 | |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 109 | 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 Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 121 | /** |
| 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 Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 129 | // 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 Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /** |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 140 | * 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 Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 152 | * 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 Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 157 | return this.prependValues(lodash.reduce(data[item], (list, sample) => { |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 158 | // console.log(data[item], sample); |
| 159 | list.push(sample.counter_volume); |
| 160 | return list; |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 161 | }, []), null); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 162 | }); |
| 163 | }; |
| 164 | |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 165 | 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 Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 181 | this.drawChart = (data) => { |
| 182 | |
| 183 | const instanceNames = Object.keys(data); |
| 184 | |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 185 | this.chart.labels = this.getLabels(this.getMostRecentSeries(data)); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 186 | this.chart.series = instanceNames; |
| 187 | this.chart.data = this.getData(data, instanceNames); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Matteo Scandolo | f75d0ac | 2015-12-17 11:16:57 -0800 | [diff] [blame] | 190 | $scope.$watch(() => this.instances, (val) => { |
| 191 | $timeout(()=>{this.chart.options.animation = false}, 1000); |
| 192 | this.drawChart(val) |
| 193 | }); |
Matteo Scandolo | 5fdd7c5 | 2015-12-16 17:19:57 -0800 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | }; |
Matteo Scandolo | 43218d2 | 2015-12-17 14:34:20 -0800 | [diff] [blame] | 197 | }); |