123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- var App = require('app');
- App.ClusterController = Em.Controller.extend({
- name:'clusterController',
- cluster:null,
- isLoaded:false,
- clusterDataLoadedPercent: 'width:0', // 0 to 1
- /**
- * Whether we need to update statuses automatically or not
- */
- isWorking: false,
- updateLoadStatus:function (item) {
- var loadList = this.get('dataLoadList');
- var loaded = true;
- var numLoaded = 0;
- var loadListLength = 0;
- loadList.set(item, true);
- for (var i in loadList) {
- if (loadList.hasOwnProperty(i)) {
- loadListLength++;
- if(!loadList[i] && loaded){
- loaded = false;
- }
- }
- // calculate the number of true
- if (loadList.hasOwnProperty(i) && loadList[i]){
- numLoaded++;
- }
- }
- this.set('isLoaded', loaded);
- this.set('clusterDataLoadedPercent', 'width:' + (Math.floor(numLoaded / loadListLength * 100)).toString() + '%');
- },
- dataLoadList:Em.Object.create({
- 'hosts':false,
- 'services':false,
- 'cluster':false,
- 'racks':false,
- 'alerts':false,
- 'users':false,
- 'datasets':false,
- 'targetclusters':false
- }),
- /**
- * load cluster name
- */
- loadClusterName:function (reload) {
- if (this.get('clusterName') && !reload) {
- return;
- }
- App.ajax.send({
- name: 'cluster.load_cluster_name',
- sender: this,
- success: 'loadClusterNameSuccessCallback',
- error: 'loadClusterNameErrorCallback'
- });
- if(!App.get('currentStackVersion')){
- App.set('currentStackVersion', App.defaultStackVersion);
- }
- },
- loadClusterNameSuccessCallback: function (data) {
- this.set('cluster', data.items[0]);
- App.set('clusterName', data.items[0].Clusters.cluster_name);
- App.set('currentStackVersion', data.items[0].Clusters.version);
- },
- loadClusterNameErrorCallback: function (request, ajaxOptions, error) {
- console.log('failed on loading cluster name');
- this.set('isLoaded', true);
- },
- getUrl:function (testUrl, url) {
- return (App.testMode) ? testUrl : App.apiPrefix + '/clusters/' + this.get('clusterName') + url;
- },
- /**
- * Provides the URL to use for Ganglia server. This URL
- * is helpful in populating links in UI.
- *
- * If null is returned, it means GANGLIA service is not installed.
- */
- gangliaUrl: function () {
- if (App.testMode) {
- return 'http://gangliaserver/ganglia/?t=yes';
- } else {
- // We want live data here
- var svcs = App.Service.find();
- var gangliaSvc = svcs.findProperty("serviceName", "GANGLIA");
- if (gangliaSvc) {
- var svcComponents = gangliaSvc.get('hostComponents');
- if (svcComponents) {
- var gangliaSvcComponent = svcComponents.findProperty("componentName", "GANGLIA_SERVER");
- if (gangliaSvcComponent) {
- var hostName = gangliaSvcComponent.get('host.hostName');
- if (hostName) {
- var host = App.Host.find(hostName);
- if (host) {
- hostName = host.get('publicHostName');
- }
- return "http://" + hostName + "/ganglia";
- }
- }
- }
- }
- return null;
- }
- }.property('App.router.updateController.isUpdated', 'dataLoadList.hosts'),
- /**
- * Provides the URL to use for NAGIOS server. This URL
- * is helpful in getting alerts data from server and also
- * in populating links in UI.
- *
- * If null is returned, it means NAGIOS service is not installed.
- */
- nagiosUrl:function () {
- if (App.testMode) {
- return 'http://nagiosserver/nagios';
- } else {
- // We want live data here
- var svcs = App.Service.find();
- var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
- if (nagiosSvc) {
- var svcComponents = nagiosSvc.get('hostComponents');
- if (svcComponents) {
- var nagiosSvcComponent = svcComponents.findProperty("componentName", "NAGIOS_SERVER");
- if (nagiosSvcComponent) {
- var hostName = nagiosSvcComponent.get('host.hostName');
- if (hostName) {
- var host = App.Host.find(hostName);
- if (host) {
- hostName = host.get('publicHostName');
- }
- return "http://" + hostName + "/nagios";
- }
- }
- }
- }
- return null;
- }
- }.property('App.router.updateController.isUpdated', 'dataLoadList.services', 'dataLoadList.hosts'),
- isNagiosInstalled:function () {
- if (App.testMode) {
- return true;
- } else {
- var svcs = App.Service.find();
- var nagiosSvc = svcs.findProperty("serviceName", "NAGIOS");
- return nagiosSvc != null;
- }
- }.property('App.router.updateController.isUpdated', 'dataLoadList.services'),
- /**
- * Sorted list of alerts.
- * Changes whenever alerts are loaded.
- */
- alerts:[],
- updateAlerts: function(){
- var alerts = App.Alert.find();
- var alertsArray = alerts.toArray();
- var sortedArray = alertsArray.sort(function (left, right) {
- var statusDiff = right.get('status') - left.get('status');
- if (statusDiff == 0) { // same error severity - sort by time
- var rightTime = right.get('date');
- var leftTime = left.get('date');
- rightTime = rightTime ? rightTime.getTime() : 0;
- leftTime = leftTime ? leftTime.getTime() : 0;
- statusDiff = rightTime - leftTime;
- }
- return statusDiff;
- });
- this.set('alerts', sortedArray);
- },
- /**
- * Load alerts from server
- * @param callback Slave function, should be called to fire delayed update.
- * Look at <code>App.updater.run</code> for more information.
- * Also used to set <code>dataLoadList.alerts</code> status during app loading
- */
- loadAlerts:function (callback) {
- if (this.get('isNagiosInstalled')) {
- var dataUrl = this.getUrl('/data/alerts/alerts.json', '/host_components?fields=HostRoles/nagios_alerts&HostRoles/component_name=NAGIOS_SERVER');
- var self = this;
- var ajaxOptions = {
- dataType:"json",
- complete:function () {
- self.updateAlerts();
- callback();
- },
- error: function(jqXHR, testStatus, error) {
- console.log('Nagios $.ajax() response:', error);
- }
- };
- App.HttpClient.get(dataUrl, App.alertsMapper, ajaxOptions);
- } else {
- console.log("No Nagios URL provided.");
- callback();
- }
- },
- /**
- * Determination of Nagios presence is known only after App.Service is
- * loaded from server. When that is done, no one tells alerts to load,
- * due to which alerts are not loaded & shown till the next polling cycle.
- * This method immediately loads alerts once Nagios presence is known.
- */
- isNagiosInstalledListener: function () {
- var self = this;
- self.loadAlerts(function () {
- self.updateLoadStatus('alerts');
- });
- }.observes('isNagiosInstalled'),
- /**
- * Send request to server to load components updated statuses
- * @param callback Slave function, should be called to fire delayed update.
- * Look at <code>App.updater.run</code> for more information
- * @return {Boolean} Whether we have errors
- */
- loadUpdatedStatus: function(callback){
- if(!this.get('clusterName')){
- callback();
- return false;
- }
-
- var servicesUrl = this.getUrl('/data/dashboard/services.json', '/services?fields=ServiceInfo,components/host_components/HostRoles/desired_state,components/host_components/HostRoles/state');
- App.HttpClient.get(servicesUrl, App.statusMapper, {
- complete: callback
- });
- return true;
- },
- /**
- * Run <code>loadUpdatedStatus</code> with delay
- * @param delay
- */
- loadUpdatedStatusDelayed: function(delay){
- setTimeout(function(){
- App.updater.immediateRun('loadUpdatedStatus');
- }, delay);
- },
- /**
- * Start polling, when <code>isWorking</code> become true
- */
- startPolling: function(){
- if(!this.get('isWorking')){
- return false;
- }
- App.updater.run(this, 'loadUpdatedStatus', 'isWorking'); //update will not run it immediately
- App.updater.run(this, 'loadAlerts', 'isWorking'); //update will not run it immediately
- return true;
- }.observes('isWorking'),
- /**
- *
- * load all data and update load status
- */
- loadClusterData:function () {
- var self = this;
- if (!this.get('clusterName')) {
- return;
- }
- if(this.get('isLoaded')) { // do not load data repeatedly
- return;
- }
- var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
- var hostsUrl = this.getUrl('/data/hosts/hosts.json', '/hosts?fields=Hosts/host_name,Hosts/public_host_name,Hosts/disk_info,Hosts/cpu_count,Hosts/total_mem,Hosts/host_status,Hosts/last_heartbeat_time,Hosts/os_arch,Hosts/os_type,Hosts/ip,host_components,metrics/disk,metrics/load/load_one');
- var usersUrl = App.testMode ? '/data/users/users.json' : App.apiPrefix + '/users/?fields=*';
- var racksUrl = "/data/racks/racks.json";
- var dataSetUrl = "/data/mirroring/all_datasets.json";
- var targetClusterUrl = "/data/mirroring/target_clusters.json";
- App.HttpClient.get(targetClusterUrl, App.targetClusterMapper, {
- complete: function (jqXHR, textStatus) {
- self.updateLoadStatus('targetclusters');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('targetclusters');
- });
- App.HttpClient.get(dataSetUrl, App.dataSetMapper, {
- complete: function (jqXHR, textStatus) {
- self.updateLoadStatus('datasets');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('datasets');
- });
- App.HttpClient.get(racksUrl, App.racksMapper, {
- complete:function (jqXHR, textStatus) {
- self.updateLoadStatus('racks');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('racks');
- });
- App.HttpClient.get(clusterUrl, App.clusterMapper, {
- complete:function (jqXHR, textStatus) {
- self.updateLoadStatus('cluster');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('cluster');
- });
- App.HttpClient.get(hostsUrl, App.hostsMapper, {
- complete:function (jqXHR, textStatus) {
- self.updateLoadStatus('hosts');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('hosts');
- });
- App.HttpClient.get(usersUrl, App.usersMapper, {
- complete:function (jqXHR, textStatus) {
- self.updateLoadStatus('users');
- }
- }, function (jqXHR, textStatus) {
- self.updateLoadStatus('users');
- });
- App.router.get('updateController').updateServiceMetric(function(){
- self.updateLoadStatus('services');
- }, true);
- this.loadAlerts(function(){
- self.updateLoadStatus('alerts');
- });
- },
- clusterName:function () {
- return (this.get('cluster')) ? this.get('cluster').Clusters.cluster_name : null;
- }.property('cluster'),
-
- updateClusterData: function () {
- var clusterUrl = this.getUrl('/data/clusters/cluster.json', '?fields=Clusters');
- App.HttpClient.get(clusterUrl, App.clusterMapper, {
- complete:function(){}
- });
- }
- });
|