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