Matteo Scandolo | b4fdd0a | 2016-05-09 15:27:47 -0700 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.developer', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
| 6 | 'ui.router', |
| 7 | 'xos.helpers' |
| 8 | ]) |
| 9 | .config(($stateProvider) => { |
| 10 | $stateProvider |
| 11 | .state('developer', { |
| 12 | url: '/', |
| 13 | template: '<developer-dashboard></developer-dashboard>' |
| 14 | }); |
| 15 | }) |
| 16 | .config(function($httpProvider){ |
| 17 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 18 | }) |
| 19 | .directive('developerDashboard', function(){ |
| 20 | return { |
| 21 | restrict: 'E', |
| 22 | scope: {}, |
| 23 | bindToController: true, |
| 24 | controllerAs: 'vm', |
| 25 | templateUrl: 'templates/developer-dashboard.tpl.html', |
| 26 | controller: function($scope, $timeout, SlicesPlus, _){ |
| 27 | |
| 28 | this.instancePerSliceConfig = { |
| 29 | resource: 'Instances', |
| 30 | groupBy: 'slice', |
| 31 | legend: true, |
| 32 | labelFormatter: (labels) => { |
| 33 | return labels.map(l => _.find(this.slices, {id: parseInt(l)}).name); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | this.instancePerSiteConfig = { |
| 38 | data: [], |
| 39 | groupBy: 'site', |
| 40 | legend: true, |
| 41 | }; |
| 42 | |
| 43 | this.networkPerSliceConfig = { |
| 44 | data: [], |
| 45 | groupBy: 'name', |
| 46 | legend: true, |
| 47 | }; |
| 48 | |
| 49 | this.tableConfig = { |
| 50 | columns: [ |
| 51 | { |
| 52 | label: 'Name', |
| 53 | prop: 'name' |
| 54 | }, |
| 55 | { |
| 56 | label: 'Privileges', |
| 57 | prop: 'current_user_roles', |
| 58 | type: 'array' |
| 59 | }, |
| 60 | { |
| 61 | label: 'Number of Instances (active / total)', |
| 62 | type: 'custom', |
| 63 | formatter: item => `${item.instance_total_ready} / ${item.instance_total}` |
| 64 | }, |
| 65 | { |
| 66 | label: 'Sites', |
| 67 | type: 'object', |
| 68 | prop: 'instance_distribution' |
| 69 | // formatter: item => item.instance_distribution |
| 70 | }, |
| 71 | { |
| 72 | label: 'Networks', |
| 73 | type: 'custom', |
| 74 | formatter: item => item.networks.length |
| 75 | } |
| 76 | ] |
| 77 | }; |
| 78 | |
| 79 | // retrieving user list |
| 80 | SlicesPlus.query().$promise |
| 81 | .then(slices => { |
| 82 | |
| 83 | // retrieving network per slices |
| 84 | let networkPerSlice = _.reduce(slices, (list, s) => { |
| 85 | if( s.networks.length > 1){ |
| 86 | // push s.neworks.length values in array |
| 87 | for(let i = 0; i < s.networks.length; i++){ |
| 88 | list.push({id: s.id, name: s.name, network: s.networks[i]}) |
| 89 | } |
| 90 | } |
| 91 | else if ( s.networks.length === 1){ |
| 92 | list.push({id: s.id, name: s.name, network: s.networks[0]}); |
| 93 | } |
| 94 | return list; |
| 95 | }, []); |
| 96 | |
| 97 | // retrieving instance distribution across sites |
| 98 | let instancePerSite = _.reduce(slices, (list, s) => { |
| 99 | _.forEach(Object.keys(s.instance_distribution), k => { |
| 100 | for(let i = 0; i < s.instance_distribution[k]; i++){ |
| 101 | list.push({site: k, instance: i}) |
| 102 | } |
| 103 | }) |
| 104 | return list; |
| 105 | }, []); |
| 106 | |
| 107 | $timeout(() => { |
| 108 | console.log(instancePerSite, networkPerSlice); |
| 109 | this.instancePerSiteConfig.data = instancePerSite; |
| 110 | this.networkPerSliceConfig.data = networkPerSlice; |
| 111 | $scope.$apply(); |
| 112 | }, 1); |
| 113 | |
| 114 | this.slices = slices; |
| 115 | }) |
| 116 | .catch((e) => { |
| 117 | throw new Error(e); |
| 118 | }); |
| 119 | } |
| 120 | }; |
| 121 | }); |