blob: 42b08e5684660fef062266e347d66d728fd1c12b [file] [log] [blame]
Matteo Scandolo78b80452016-03-21 17:27:42 -07001/**
2 * © OpenCORD
3 *
4 * Visit http://guide.xosproject.org/devguide/addview/ for more information
5 *
6 * Created by teone on 3/21/16.
7 */
8
9(function () {
10 'use strict';
11
12 angular.module('xos.ceilometerDashboard')
13 .directive('ceilometerSamples', function(lodash, $stateParams){
14 return {
15 restrict: 'E',
16 scope: {},
17 bindToController: true,
18 controllerAs: 'vm',
19 templateUrl: 'templates/ceilometer-samples.tpl.html',
20 controller: function(Ceilometer) {
21
22 // console.log(Ceilometer.selectResource);
23
24 this.chartColors = [
25 '#286090',
26 '#F7464A',
27 '#46BFBD',
28 '#FDB45C',
29 '#97BBCD',
30 '#4D5360',
31 '#8c4f9f'
32 ];
33
34 this.chart = {
35 series: [],
36 labels: [],
37 data: []
38 }
39
40 Chart.defaults.global.colours = this.chartColors;
41
42 this.chartType = 'line';
43
44 if($stateParams.name && $stateParams.tenant){
45 this.name = $stateParams.name;
46 this.tenant = $stateParams.tenant;
Matteo Scandolo89b179c2016-03-24 11:38:50 -070047 // TODO rename tenant in resource_id
Matteo Scandolo78b80452016-03-21 17:27:42 -070048 }
49 else{
50 throw new Error('Missing Name and Tenant Params!');
51 }
52
53 /**
54 * Goes trough the array and format date to be used as labels
55 *
56 * @param Array data
57 * @returns Array a list of labels
58 */
59
60 this.getLabels = (data) => {
61 return data.reduce((list, item) => {
62 let date = new Date(item.timestamp);
63 list.push(`${date.getHours()}:${(date.getMinutes()<10?'0':'') + date.getMinutes()}:${date.getSeconds()}`);
64 return list;
65 }, []);
66 };
67
68 /**
69 * Goes trough the array and return a flat array of values
70 *
71 * @param Array data
72 * @returns Array a list of values
73 */
74
75 this.getData = (data) => {
76 return data.reduce((list, item) => {
77 list.push(item.volume);
78 return list;
79 }, []);
80 }
81
82 /**
83 * Add a samples to the chart
84 *
85 * @param string resource_id
86 */
87 this.chartMeters = [];
88 this.addMeterToChart = (resource_id) => {
89 this.chart['labels'] = this.getLabels(lodash.sortBy(this.samplesList[resource_id], 'timestamp'));
90 this.chart['series'].push(resource_id);
91 this.chart['data'].push(this.getData(lodash.sortBy(this.samplesList[resource_id], 'timestamp')));
92 this.chartMeters.push(this.samplesList[resource_id][0]); //use the 0 as are all samples for the same resource and I need the name
93 lodash.remove(this.sampleLabels, {id: resource_id});
94 }
95
96 this.removeFromChart = (meter) => {
97 this.chart.data.splice(this.chart.series.indexOf(meter.resource_id), 1);
98 this.chart.series.splice(this.chart.series.indexOf(meter.resource_id), 1);
99 this.chartMeters.splice(lodash.findIndex(this.chartMeters, {resource_id: meter.resource_id}), 1);
100 this.sampleLabels.push({
101 id: meter.resource_id,
102 name: meter.resource_name || meter.resource_id
103 })
104 };
105
106 /**
107 * Format samples to create a list of labels and ids
108 */
109
110 this.formatSamplesLabels = (samples) => {
111
112 return lodash.uniq(samples, 'resource_id')
113 .reduce((labels, item) => {
114 labels.push({
115 id: item.resource_id,
116 name: item.resource_name || item.resource_id
117 });
118
119 return labels;
120 }, []);
121 }
122
123
124 /**
125 * Load the samples and format data
126 */
127
128 this.showSamples = () => {
129 this.loader = true;
130 // Ceilometer.getSamples(this.name, this.tenant) //fetch one
131 Ceilometer.getSamples(this.name) //fetch all
132 .then(res => {
133
134 // rename things in UI
135 res.map(m => {
136 m.resource_name = m.resource_name.replace('mysite_onos_vbng', 'ONOS_FABRIC');
137 m.resource_name = m.resource_name.replace('mysite_onos_volt', 'ONOS_CORD');
138 m.resource_name = m.resource_name.replace('mysite_vbng', 'mysite_vRouter');
139 return m;
140 });
141 // end rename things in UI
142
143 // setup data for visualization
144 this.samplesList = lodash.groupBy(res, 'resource_id');
145 this.sampleLabels = this.formatSamplesLabels(res);
146
147 // add current meter to chart
148 this.addMeterToChart(this.tenant);
149
150 })
151 .catch(err => {
152 this.error = err.data.detail;
153 })
154 .finally(() => {
155 this.loader = false;
156 });
157 };
158
159 this.showSamples();
160 }
161 }
162 });
163})();
164