blob: 01b9adce43ebe25b707e5fde759b4c5af5ac7429 [file] [log] [blame]
Matteo Scandoloe3de73d2015-12-04 10:14:40 -08001'use strict';
2
3angular.module('xos.ceilometerDashboard', [
4 'ngResource',
5 'ngCookies',
6 'ngLodash',
7 'ui.router',
8 'xos.helpers',
Matteo Scandoloc2d31102015-12-08 14:35:55 -08009 'ngAnimate',
Matteo Scandolo0f5e1632015-12-09 16:09:59 -080010 'chart.js',
11 'ui.bootstrap.accordion'
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080012])
Matteo Scandoloc2d31102015-12-08 14:35:55 -080013.config(($stateProvider, $urlRouterProvider) => {
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080014 $stateProvider
15 .state('ceilometerDashboard', {
16 url: '/',
17 template: '<ceilometer-dashboard></ceilometer-dashboard>'
Matteo Scandoloec8ad422015-12-04 15:55:20 -080018 })
19 .state('samples', {
20 url: '/:name/:tenant/samples',
21 template: '<ceilometer-samples></ceilometer-samples>'
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080022 });
Matteo Scandoloc2d31102015-12-08 14:35:55 -080023 $urlRouterProvider.otherwise('/');
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080024})
25.config(function($httpProvider){
26 $httpProvider.interceptors.push('NoHyperlinks');
27})
Matteo Scandoloc2d31102015-12-08 14:35:55 -080028.run(function($rootScope){
29 $rootScope.stateName = 'ceilometerDashboard';
30 $rootScope.$on('$stateChangeStart', (event, toState) => {
Matteo Scandoloc2d31102015-12-08 14:35:55 -080031 $rootScope.stateName = toState.name;
32 })
33})
Matteo Scandolo5dd94182015-12-09 17:09:55 -080034.service('Ceilometer', function($http, $q, lodash){
Matteo Scandolod3e696d2015-12-08 15:24:23 -080035
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080036 this.getMeters = () => {
37 let deferred = $q.defer();
38
Matteo Scandolo5dd94182015-12-09 17:09:55 -080039 $http.get('/xoslib/meters/', {cache: true})
40 // $http.get('../meters_mock.json', {cache: true})
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080041 .then((res) => {
Matteo Scandoloec8ad422015-12-04 15:55:20 -080042 deferred.resolve(res.data)
43 })
44 .catch((e) => {
45 deferred.reject(e);
46 });
47
48 return deferred.promise;
49 }
50
51 this.getSamples = (name, tenant) => {
52 let deferred = $q.defer();
53
Matteo Scandolo6c788432015-12-07 17:32:39 -080054 $http.get(`/xoslib/metersamples/`, {params: {meter: name, tenant: tenant}})
Matteo Scandoloec8ad422015-12-04 15:55:20 -080055 .then((res) => {
56 deferred.resolve(res.data)
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080057 })
58 .catch((e) => {
59 deferred.reject(e);
60 });
61
62 return deferred.promise;
63 }
Matteo Scandolo5dd94182015-12-09 17:09:55 -080064
Matteo Scandolo382455c2016-02-02 16:29:40 -080065 this.getStats = (options) => {
Matteo Scandolo5dd94182015-12-09 17:09:55 -080066 let deferred = $q.defer();
67
Matteo Scandolo382455c2016-02-02 16:29:40 -080068 $http.get('/xoslib/meterstatistics/', {cache: true, params: options})
Matteo Scandolo5dd94182015-12-09 17:09:55 -080069 // $http.get('../stats_mock.son', {cache: true})
70 .then((res) => {
Matteo Scandolo382455c2016-02-02 16:29:40 -080071 deferred.resolve(res.data);
Matteo Scandolo5dd94182015-12-09 17:09:55 -080072 })
73 .catch((e) => {
74 deferred.reject(e);
75 });
76
77 return deferred.promise;
78 };
Matteo Scandolod9e1c412015-12-15 14:37:27 -080079
80 // hold dashboard status (opened service, slice, resource)
81 this.selectedService = null;
82 this.selectedSlice = null;
83 this.selectedResource = null;
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080084})
Matteo Scandoloec8ad422015-12-04 15:55:20 -080085.directive('ceilometerDashboard', function(lodash){
Matteo Scandoloe3de73d2015-12-04 10:14:40 -080086 return {
87 restrict: 'E',
88 scope: {},
89 bindToController: true,
90 controllerAs: 'vm',
91 templateUrl: 'templates/ceilometer-dashboard.tpl.html',
92 controller: function(Ceilometer){
93
Matteo Scandolod9e1c412015-12-15 14:37:27 -080094 // this open the accordion
95 this.accordion = {
96 open: {}
97 }
98
99 /**
100 * Open the active panel base on the service stored values
101 */
102 this.openPanels = () => {
103 if(Ceilometer.selectedService){
104 this.accordion.open[Ceilometer.selectedService] = true;
105 if(Ceilometer.selectedSlice){
106 this.selectedSlice = Ceilometer.selectedSlice;
107 this.selectedResources = this.projects[Ceilometer.selectedService][Ceilometer.selectedSlice]
108 if(Ceilometer.selectedResource){
109 this.selectedResource = Ceilometer.selectedResource;
110 this.selectedMeters = this.selectedResources[Ceilometer.selectedResource];
111 }
112 }
113 }
114 }
115
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800116 this.loadMeters = () => {
117 this.loader = true;
118
Matteo Scandolo48d890f2015-12-14 17:36:09 -0800119 // TODO rename projects in meters
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800120 Ceilometer.getMeters()
121 .then(meters => {
Matteo Scandolo30c23c72015-12-09 15:23:51 -0800122 //group project by service
123 this.projects = lodash.groupBy(meters, 'service');
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800124 lodash.forEach(Object.keys(this.projects), (project) => {
Matteo Scandolo30c23c72015-12-09 15:23:51 -0800125 // inside each service group by slice
126 this.projects[project] = lodash.groupBy(this.projects[project], 'slice');
127 lodash.forEach(Object.keys(this.projects[project]), (slice) => {
128 // inside each service => slice group by resource
Matteo Scandoloa918ea82015-12-14 14:26:20 -0800129 this.projects[project][slice] = lodash.groupBy(this.projects[project][slice], 'resource_name');
Matteo Scandolo30c23c72015-12-09 15:23:51 -0800130 });
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800131 });
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800132
133 // open selected panels
134 this.openPanels();
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800135 })
136 .catch(err => {
Matteo Scandoloa918ea82015-12-14 14:26:20 -0800137 this.error = err.data.detail;
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800138 })
139 .finally(() => {
140 this.loader = false;
141 });
142 }
143
144 this.loadMeters();
145
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800146 /**
147 * Select Resources for a slice
148 *
149 * @param Array resources The list of selected resources
150 * @returns void
151 */
152 this.selectedResources = null;
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800153 this.selectResources = (resources, slice, service) => {
Matteo Scandolo382455c2016-02-02 16:29:40 -0800154
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800155 //cleaning
156 this.selectedResources = null;
157 this.selectedResource = null;
158 this.selectedMeters = null;
159
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800160 // hold the resource list for the current slice
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800161 this.selectedResources = resources;
162 this.selectedSlice = slice;
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800163 this.selectedService = service;
164
165 // store the status
166 Ceilometer.selectedSlice = slice;
167 Ceilometer.selectedService = service;
Matteo Scandolo382455c2016-02-02 16:29:40 -0800168
169 // store tenant (slice id for ceilometer)
170 // it is passed to ceilometer-stats directive
171 console.log(resources);
172 this.selectedTenant = resources[Object.keys(resources)[0]][0].project_id;
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800173 }
174
175 /**
176 * Select Meters for a resource
177 *
178 * @param Array meters The list of selected resources
179 * @returns void
180 */
181 this.selectedMeters = null;
182 this.selectMeters = (meters, resource) => {
183 this.selectedMeters = meters;
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800184
185 Ceilometer.selectedResource = resource;
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800186 this.selectedResource = resource;
187 }
188
Matteo Scandoloe3de73d2015-12-04 10:14:40 -0800189 }
190 };
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800191})
192.directive('ceilometerSamples', function(lodash, $stateParams){
193 return {
194 restrict: 'E',
Matteo Scandolo10e61732016-02-02 16:04:31 -0800195 scope: {},
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800196 bindToController: true,
197 controllerAs: 'vm',
198 templateUrl: 'templates/ceilometer-samples.tpl.html',
199 controller: function(Ceilometer) {
200
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800201 // console.log(Ceilometer.selectResource);
202
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800203 this.chartColors = [
Matteo Scandolod3e696d2015-12-08 15:24:23 -0800204 '#286090',
205 '#F7464A',
206 '#46BFBD',
207 '#FDB45C',
208 '#97BBCD',
209 '#4D5360',
210 '#8c4f9f'
211 ];
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800212
213 this.chart = {
214 series: [],
215 labels: [],
216 data: []
217 }
218
219 Chart.defaults.global.colours = this.chartColors;
Matteo Scandolod3e696d2015-12-08 15:24:23 -0800220
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800221 this.chartType = 'line';
222
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800223 if($stateParams.name && $stateParams.tenant){
224 this.name = $stateParams.name;
225 this.tenant = $stateParams.tenant;
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800226 // TODO rename tenant in project_id
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800227 }
Matteo Scandolo48d890f2015-12-14 17:36:09 -0800228 else{
229 throw new Error('Missing Name and Tenant Params!');
230 }
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800231
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800232 /**
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800233 * Goes trough the array and format date to be used as labels
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800234 *
235 * @param Array data
236 * @returns Array a list of labels
237 */
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800238
Matteo Scandolo52193d32015-12-05 18:44:45 -0800239 this.getLabels = (data) => {
240 return data.reduce((list, item) => {
241 let date = new Date(item.timestamp);
242 list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`);
243 return list;
244 }, []);
245 };
246
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800247 /**
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800248 * Goes trough the array and return a flat array of values
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800249 *
250 * @param Array data
251 * @returns Array a list of values
252 */
253
Matteo Scandolo52193d32015-12-05 18:44:45 -0800254 this.getData = (data) => {
255 return data.reduce((list, item) => {
256 list.push(item.volume);
257 return list;
258 }, []);
259 }
260
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800261 /**
262 * Add a samples to the chart
263 *
264 * @param string resource_id
265 */
266 this.chartMeters = [];
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800267 this.addMeterToChart = (project_id) => {
268 this.chart['labels'] = this.getLabels(lodash.sortBy(this.samplesList[project_id], 'timestamp'));
269 this.chart['series'].push(project_id);
270 this.chart['data'].push(this.getData(lodash.sortBy(this.samplesList[project_id], 'timestamp')));
271 this.chartMeters.push(this.samplesList[project_id][0]); //use the 0 as are all samples for the same resource and I need the name
272 lodash.remove(this.sampleLabels, {id: project_id});
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800273 }
274
Matteo Scandoloedea1db2015-12-14 14:42:28 -0800275 this.removeFromChart = (meter) => {
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800276 this.chart.data.splice(this.chart.series.indexOf(meter.project_id), 1);
277 this.chart.series.splice(this.chart.series.indexOf(meter.project_id), 1);
278 this.chartMeters.splice(lodash.findIndex(this.chartMeters, {project_id: meter.project_id}), 1);
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800279 this.sampleLabels.push({
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800280 id: meter.project_id,
281 name: meter.resource_name || meter.project_id
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800282 })
283 };
284
285 /**
286 * Format samples to create a list of labels and ids
287 */
288
289 this.formatSamplesLabels = (samples) => {
290
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800291 return lodash.uniq(samples, 'project_id')
Matteo Scandolo9e1bc292015-12-15 08:16:56 -0800292 .reduce((labels, item) => {
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800293 labels.push({
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800294 id: item.project_id,
295 name: item.resource_name || item.project_id
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800296 });
297 return labels;
Matteo Scandolo9e1bc292015-12-15 08:16:56 -0800298 }, []);
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800299 }
300
301
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800302 /**
303 * Load the samples and format data
304 */
305
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800306 this.showSamples = () => {
307 this.loader = true;
Matteo Scandoloc2d31102015-12-08 14:35:55 -0800308 // Ceilometer.getSamples(this.name, this.tenant) //fetch one
309 Ceilometer.getSamples(this.name) //fetch all
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800310 .then(res => {
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800311
312 // setup data for visualization
Matteo Scandolod9e1c412015-12-15 14:37:27 -0800313 this.samplesList = lodash.groupBy(res, 'project_id');
Matteo Scandoloc6a78982015-12-08 16:39:57 -0800314 this.sampleLabels = this.formatSamplesLabels(res);
315
316 // add current meter to chart
317 this.addMeterToChart(this.tenant);
318
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800319 })
320 .catch(err => {
Matteo Scandolo30c23c72015-12-09 15:23:51 -0800321 this.error = err.data.detail;
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800322 })
323 .finally(() => {
324 this.loader = false;
325 });
326 };
327
328 this.showSamples();
Matteo Scandoloec8ad422015-12-04 15:55:20 -0800329 }
330 }
Matteo Scandolo5dd94182015-12-09 17:09:55 -0800331})
332.directive('ceilometerStats', function(){
333 return {
334 restrict: 'E',
335 scope: {
336 name: '=name',
Matteo Scandolo382455c2016-02-02 16:29:40 -0800337 tenant: '=tenant'
Matteo Scandolo5dd94182015-12-09 17:09:55 -0800338 },
339 bindToController: true,
340 controllerAs: 'vm',
341 templateUrl: 'templates/ceilometer-stats.tpl.html',
342 controller: function($scope, Ceilometer) {
Matteo Scandolo382455c2016-02-02 16:29:40 -0800343
344 this.getStats = (tenant) => {
345 console.log(this.tenant);
Matteo Scandolo5dd94182015-12-09 17:09:55 -0800346 this.loader = true;
Matteo Scandolo382455c2016-02-02 16:29:40 -0800347 Ceilometer.getStats({tenant: tenant})
Matteo Scandolo5dd94182015-12-09 17:09:55 -0800348 .then(res => {
349 this.stats = res;
350 })
351 .catch(err => {
352 this.error = err.data;
353 })
354 .finally(() => {
355 this.loader = false;
356 });
357 };
358
Matteo Scandolo382455c2016-02-02 16:29:40 -0800359 $scope.$watch(() => this.name, (val) => {
360 if(val){
361 this.getStats(this.tenant);
362 }
363 });
Matteo Scandolo5dd94182015-12-09 17:09:55 -0800364 }
365 }
Matteo Scandolo76b4e982015-12-10 10:15:20 -0800366})
367.filter('orderObjectByKey', function(lodash) {
Matteo Scandoloa918ea82015-12-14 14:26:20 -0800368 return function(items) {
Matteo Scandolo76b4e982015-12-10 10:15:20 -0800369
370 if(!items){
371 return;
372 }
373
374 return lodash.reduce(Object.keys(items).reverse(), (list, key) => {
375 list[key] = items[key];
376 return list;
377 }, {});
378
379 };
380});
381;