Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.ceilometerDashboard', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
| 6 | 'ngLodash', |
| 7 | 'ui.router', |
| 8 | 'xos.helpers', |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 9 | 'ngAnimate', |
Matteo Scandolo | 0f5e163 | 2015-12-09 16:09:59 -0800 | [diff] [blame] | 10 | 'chart.js', |
| 11 | 'ui.bootstrap.accordion' |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 12 | ]) |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 13 | .config(($stateProvider, $urlRouterProvider) => { |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 14 | $stateProvider |
| 15 | .state('ceilometerDashboard', { |
| 16 | url: '/', |
| 17 | template: '<ceilometer-dashboard></ceilometer-dashboard>' |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 18 | }) |
| 19 | .state('samples', { |
| 20 | url: '/:name/:tenant/samples', |
| 21 | template: '<ceilometer-samples></ceilometer-samples>' |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 22 | }); |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 23 | $urlRouterProvider.otherwise('/'); |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 24 | }) |
| 25 | .config(function($httpProvider){ |
| 26 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 27 | }) |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 28 | .run(function($rootScope){ |
| 29 | $rootScope.stateName = 'ceilometerDashboard'; |
| 30 | $rootScope.$on('$stateChangeStart', (event, toState) => { |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 31 | $rootScope.stateName = toState.name; |
| 32 | }) |
| 33 | }) |
Matteo Scandolo | 32f8392 | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 34 | .service('Ceilometer', function($http, $q){ |
Matteo Scandolo | d3e696d | 2015-12-08 15:24:23 -0800 | [diff] [blame] | 35 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 36 | this.getMappings = () => { |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 37 | let deferred = $q.defer(); |
| 38 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 39 | $http.get('/xoslib/xos-slice-service-mapping/') |
| 40 | .then((res) => { |
| 41 | deferred.resolve(res.data) |
| 42 | }) |
| 43 | .catch((e) => { |
| 44 | deferred.reject(e); |
| 45 | }); |
| 46 | |
| 47 | return deferred.promise; |
| 48 | } |
| 49 | |
| 50 | this.getMeters = (params) => { |
| 51 | let deferred = $q.defer(); |
| 52 | |
| 53 | $http.get('/xoslib/meters/', {cache: true, params: params}) |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 54 | // $http.get('../meters_mock.json', {cache: true}) |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 55 | .then((res) => { |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 56 | deferred.resolve(res.data) |
| 57 | }) |
| 58 | .catch((e) => { |
| 59 | deferred.reject(e); |
| 60 | }); |
| 61 | |
| 62 | return deferred.promise; |
| 63 | } |
| 64 | |
| 65 | this.getSamples = (name, tenant) => { |
| 66 | let deferred = $q.defer(); |
| 67 | |
Matteo Scandolo | 6c78843 | 2015-12-07 17:32:39 -0800 | [diff] [blame] | 68 | $http.get(`/xoslib/metersamples/`, {params: {meter: name, tenant: tenant}}) |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 69 | .then((res) => { |
| 70 | deferred.resolve(res.data) |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 71 | }) |
| 72 | .catch((e) => { |
| 73 | deferred.reject(e); |
| 74 | }); |
| 75 | |
| 76 | return deferred.promise; |
| 77 | } |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 78 | |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 79 | this.getStats = (options) => { |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 80 | let deferred = $q.defer(); |
| 81 | |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 82 | $http.get('/xoslib/meterstatistics/', {cache: true, params: options}) |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 83 | // $http.get('../stats_mock.son', {cache: true}) |
| 84 | .then((res) => { |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 85 | deferred.resolve(res.data); |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 86 | }) |
| 87 | .catch((e) => { |
| 88 | deferred.reject(e); |
| 89 | }); |
| 90 | |
| 91 | return deferred.promise; |
| 92 | }; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 93 | |
| 94 | // hold dashboard status (opened service, slice, resource) |
| 95 | this.selectedService = null; |
| 96 | this.selectedSlice = null; |
| 97 | this.selectedResource = null; |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 98 | }) |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 99 | .directive('ceilometerDashboard', function(lodash){ |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 100 | return { |
| 101 | restrict: 'E', |
| 102 | scope: {}, |
| 103 | bindToController: true, |
| 104 | controllerAs: 'vm', |
| 105 | templateUrl: 'templates/ceilometer-dashboard.tpl.html', |
| 106 | controller: function(Ceilometer){ |
| 107 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 108 | this.showStats = false; |
| 109 | |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 110 | // this open the accordion |
| 111 | this.accordion = { |
| 112 | open: {} |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Open the active panel base on the service stored values |
| 117 | */ |
| 118 | this.openPanels = () => { |
| 119 | if(Ceilometer.selectedService){ |
| 120 | this.accordion.open[Ceilometer.selectedService] = true; |
| 121 | if(Ceilometer.selectedSlice){ |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 122 | this.loadSliceMeter(Ceilometer.selectedSlice, Ceilometer.selectedService); |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 123 | this.selectedSlice = Ceilometer.selectedSlice; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 124 | if(Ceilometer.selectedResource){ |
| 125 | this.selectedResource = Ceilometer.selectedResource; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 131 | /** |
| 132 | * Load the list of service and slices |
| 133 | */ |
| 134 | |
| 135 | this.loadMappings = () => { |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 136 | this.loader = true; |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 137 | Ceilometer.getMappings() |
| 138 | .then((services) => { |
| 139 | this.services = services; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 140 | this.openPanels(); |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 141 | }) |
| 142 | .catch(err => { |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 143 | this.error = (err.data && err.data.detail) ? err.data.detail : 'An Error occurred. Please try again later.'; |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 144 | }) |
| 145 | .finally(() => { |
| 146 | this.loader = false; |
| 147 | }); |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 148 | }; |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 149 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 150 | this.loadMappings(); |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 151 | |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 152 | /** |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 153 | * Load the list of a single slice |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 154 | */ |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 155 | |
| 156 | this.loadSliceMeter = (slice, service_name) => { |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 157 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 158 | Ceilometer.selectedSlice = null; |
| 159 | Ceilometer.selectedService = null; |
| 160 | Ceilometer.selectedResources = null; |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 161 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 162 | // visualization info |
| 163 | this.loader = true; |
Matteo Scandolo | 32f8392 | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 164 | this.error = null; |
| 165 | this.ceilometerError = null; |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 166 | |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 167 | Ceilometer.getMeters({tenant: slice.project_id}) |
| 168 | .then((sliceMeters) => { |
Matteo Scandolo | 32f8392 | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 169 | this.selectedSlice = slice.slice; |
| 170 | this.selectedTenant = slice.project_id; |
| 171 | |
| 172 | // store the status |
| 173 | Ceilometer.selectedSlice = slice; |
| 174 | Ceilometer.selectedService = service_name; |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 175 | this.selectedResources = lodash.groupBy(sliceMeters, 'resource_name'); |
| 176 | |
| 177 | // hacky |
| 178 | if(Ceilometer.selectedResource){ |
| 179 | this.selectedMeters = this.selectedResources[Ceilometer.selectedResource]; |
| 180 | } |
| 181 | }) |
| 182 | .catch(err => { |
Matteo Scandolo | 32f8392 | 2016-02-11 09:35:29 -0800 | [diff] [blame] | 183 | |
| 184 | // this means that ceilometer is not yet ready |
| 185 | if(err.status === 503){ |
| 186 | return this.ceilometerError = err.data.detail.specific_error; |
| 187 | } |
| 188 | |
Matteo Scandolo | 4cd6629 | 2016-02-11 09:49:34 -0800 | [diff] [blame] | 189 | this.error = (err.data && err.data.detail.specific_error) ? err.data.detail.specific_error : 'An Error occurred. Please try again later.'; |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 190 | }) |
| 191 | .finally(() => { |
| 192 | this.loader = false; |
| 193 | }); |
| 194 | }; |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 195 | |
| 196 | /** |
| 197 | * Select Meters for a resource |
| 198 | * |
| 199 | * @param Array meters The list of selected resources |
| 200 | * @returns void |
| 201 | */ |
| 202 | this.selectedMeters = null; |
| 203 | this.selectMeters = (meters, resource) => { |
| 204 | this.selectedMeters = meters; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 205 | |
| 206 | Ceilometer.selectedResource = resource; |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 207 | this.selectedResource = resource; |
| 208 | } |
| 209 | |
Matteo Scandolo | e3de73d | 2015-12-04 10:14:40 -0800 | [diff] [blame] | 210 | } |
| 211 | }; |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 212 | }) |
| 213 | .directive('ceilometerSamples', function(lodash, $stateParams){ |
| 214 | return { |
| 215 | restrict: 'E', |
Matteo Scandolo | 10e6173 | 2016-02-02 16:04:31 -0800 | [diff] [blame] | 216 | scope: {}, |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 217 | bindToController: true, |
| 218 | controllerAs: 'vm', |
| 219 | templateUrl: 'templates/ceilometer-samples.tpl.html', |
| 220 | controller: function(Ceilometer) { |
| 221 | |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 222 | // console.log(Ceilometer.selectResource); |
| 223 | |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 224 | this.chartColors = [ |
Matteo Scandolo | d3e696d | 2015-12-08 15:24:23 -0800 | [diff] [blame] | 225 | '#286090', |
| 226 | '#F7464A', |
| 227 | '#46BFBD', |
| 228 | '#FDB45C', |
| 229 | '#97BBCD', |
| 230 | '#4D5360', |
| 231 | '#8c4f9f' |
| 232 | ]; |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 233 | |
| 234 | this.chart = { |
| 235 | series: [], |
| 236 | labels: [], |
| 237 | data: [] |
| 238 | } |
| 239 | |
| 240 | Chart.defaults.global.colours = this.chartColors; |
Matteo Scandolo | d3e696d | 2015-12-08 15:24:23 -0800 | [diff] [blame] | 241 | |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 242 | this.chartType = 'line'; |
| 243 | |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 244 | if($stateParams.name && $stateParams.tenant){ |
| 245 | this.name = $stateParams.name; |
| 246 | this.tenant = $stateParams.tenant; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 247 | // TODO rename tenant in project_id |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 248 | } |
Matteo Scandolo | 48d890f | 2015-12-14 17:36:09 -0800 | [diff] [blame] | 249 | else{ |
| 250 | throw new Error('Missing Name and Tenant Params!'); |
| 251 | } |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 252 | |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 253 | /** |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 254 | * Goes trough the array and format date to be used as labels |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 255 | * |
| 256 | * @param Array data |
| 257 | * @returns Array a list of labels |
| 258 | */ |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 259 | |
Matteo Scandolo | 52193d3 | 2015-12-05 18:44:45 -0800 | [diff] [blame] | 260 | this.getLabels = (data) => { |
| 261 | return data.reduce((list, item) => { |
| 262 | let date = new Date(item.timestamp); |
| 263 | list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`); |
| 264 | return list; |
| 265 | }, []); |
| 266 | }; |
| 267 | |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 268 | /** |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 269 | * Goes trough the array and return a flat array of values |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 270 | * |
| 271 | * @param Array data |
| 272 | * @returns Array a list of values |
| 273 | */ |
| 274 | |
Matteo Scandolo | 52193d3 | 2015-12-05 18:44:45 -0800 | [diff] [blame] | 275 | this.getData = (data) => { |
| 276 | return data.reduce((list, item) => { |
| 277 | list.push(item.volume); |
| 278 | return list; |
| 279 | }, []); |
| 280 | } |
| 281 | |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 282 | /** |
| 283 | * Add a samples to the chart |
| 284 | * |
| 285 | * @param string resource_id |
| 286 | */ |
| 287 | this.chartMeters = []; |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 288 | this.addMeterToChart = (project_id) => { |
| 289 | this.chart['labels'] = this.getLabels(lodash.sortBy(this.samplesList[project_id], 'timestamp')); |
| 290 | this.chart['series'].push(project_id); |
| 291 | this.chart['data'].push(this.getData(lodash.sortBy(this.samplesList[project_id], 'timestamp'))); |
| 292 | this.chartMeters.push(this.samplesList[project_id][0]); //use the 0 as are all samples for the same resource and I need the name |
| 293 | lodash.remove(this.sampleLabels, {id: project_id}); |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Matteo Scandolo | edea1db | 2015-12-14 14:42:28 -0800 | [diff] [blame] | 296 | this.removeFromChart = (meter) => { |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 297 | this.chart.data.splice(this.chart.series.indexOf(meter.project_id), 1); |
| 298 | this.chart.series.splice(this.chart.series.indexOf(meter.project_id), 1); |
| 299 | this.chartMeters.splice(lodash.findIndex(this.chartMeters, {project_id: meter.project_id}), 1); |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 300 | this.sampleLabels.push({ |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 301 | id: meter.project_id, |
| 302 | name: meter.resource_name || meter.project_id |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 303 | }) |
| 304 | }; |
| 305 | |
| 306 | /** |
| 307 | * Format samples to create a list of labels and ids |
| 308 | */ |
| 309 | |
| 310 | this.formatSamplesLabels = (samples) => { |
| 311 | |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 312 | return lodash.uniq(samples, 'project_id') |
Matteo Scandolo | 9e1bc29 | 2015-12-15 08:16:56 -0800 | [diff] [blame] | 313 | .reduce((labels, item) => { |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 314 | labels.push({ |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 315 | id: item.project_id, |
| 316 | name: item.resource_name || item.project_id |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 317 | }); |
| 318 | return labels; |
Matteo Scandolo | 9e1bc29 | 2015-12-15 08:16:56 -0800 | [diff] [blame] | 319 | }, []); |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 323 | /** |
| 324 | * Load the samples and format data |
| 325 | */ |
| 326 | |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 327 | this.showSamples = () => { |
| 328 | this.loader = true; |
Matteo Scandolo | c2d3110 | 2015-12-08 14:35:55 -0800 | [diff] [blame] | 329 | // Ceilometer.getSamples(this.name, this.tenant) //fetch one |
| 330 | Ceilometer.getSamples(this.name) //fetch all |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 331 | .then(res => { |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 332 | |
| 333 | // setup data for visualization |
Matteo Scandolo | d9e1c41 | 2015-12-15 14:37:27 -0800 | [diff] [blame] | 334 | this.samplesList = lodash.groupBy(res, 'project_id'); |
Matteo Scandolo | c6a7898 | 2015-12-08 16:39:57 -0800 | [diff] [blame] | 335 | this.sampleLabels = this.formatSamplesLabels(res); |
| 336 | |
| 337 | // add current meter to chart |
| 338 | this.addMeterToChart(this.tenant); |
| 339 | |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 340 | }) |
| 341 | .catch(err => { |
Matteo Scandolo | 30c23c7 | 2015-12-09 15:23:51 -0800 | [diff] [blame] | 342 | this.error = err.data.detail; |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 343 | }) |
| 344 | .finally(() => { |
| 345 | this.loader = false; |
| 346 | }); |
| 347 | }; |
| 348 | |
| 349 | this.showSamples(); |
Matteo Scandolo | ec8ad42 | 2015-12-04 15:55:20 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 352 | }) |
| 353 | .directive('ceilometerStats', function(){ |
| 354 | return { |
| 355 | restrict: 'E', |
| 356 | scope: { |
| 357 | name: '=name', |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 358 | tenant: '=tenant' |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 359 | }, |
| 360 | bindToController: true, |
| 361 | controllerAs: 'vm', |
| 362 | templateUrl: 'templates/ceilometer-stats.tpl.html', |
| 363 | controller: function($scope, Ceilometer) { |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 364 | |
| 365 | this.getStats = (tenant) => { |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 366 | this.loader = true; |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 367 | Ceilometer.getStats({tenant: tenant}) |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 368 | .then(res => { |
| 369 | this.stats = res; |
| 370 | }) |
| 371 | .catch(err => { |
| 372 | this.error = err.data; |
| 373 | }) |
| 374 | .finally(() => { |
| 375 | this.loader = false; |
| 376 | }); |
| 377 | }; |
| 378 | |
Matteo Scandolo | 382455c | 2016-02-02 16:29:40 -0800 | [diff] [blame] | 379 | $scope.$watch(() => this.name, (val) => { |
| 380 | if(val){ |
| 381 | this.getStats(this.tenant); |
| 382 | } |
| 383 | }); |
Matteo Scandolo | 5dd9418 | 2015-12-09 17:09:55 -0800 | [diff] [blame] | 384 | } |
| 385 | } |
Matteo Scandolo | c012856 | 2016-02-08 14:17:42 -0800 | [diff] [blame] | 386 | }); |