<commit_before>/**
 * 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');
require('utils/config');

App.Service = DS.Model.extend({

  serviceName: DS.attr('string'),

  workStatus: DS.attr('string'),
  rand: DS.attr('string'),
  alerts: DS.hasMany('App.Alert'),
  quickLinks: DS.hasMany('App.QuickLinks'),
  hostComponents: DS.hasMany('App.HostComponent'),
  serviceConfigsTemplate: App.config.get('preDefinedServiceConfigs'),
  runningHostComponents: null,

  // Instead of making healthStatus a computed property that listens on hostComponents.@each.workStatus,
  // we are creating a separate observer _updateHealthStatus.  This is so that healthStatus is updated
  // only once after the run loop.  This is because Ember invokes the computed property every time
  // a property that it depends on changes.  For example, App.statusMapper's map function would invoke
  // the computed property too many times and freezes the UI without this hack.
  // See http://stackoverflow.com/questions/12467345/ember-js-collapsing-deferring-expensive-observers-or-computed-properties
  healthStatus: '',

  /**
   * Every time when changes workStatus of any component we schedule recalculating values related from them
   */
  _updateHealthStatus: (function() {
    Ember.run.once(this, 'updateStatusDependencies');
  }).observes('hostComponents.@each.workStatus'),

  isStopped: false,
  isStarted: false,

  /**
   * compute service properties which depend on host components status
   * service computed properties:
   *    healthStatus
   *    isStarted
   *    runningHostComponents
   *    unknownHostComponents
   *    isStopped
   */
  updateStatusDependencies: function () {
    var everyStarted = true,
      everyStartedOrMaintenance = true,
      masterComponents = [],
      isStarted = false,
      isUnknown = false,
      isStarting = false,
      isStopped = false,
      isHbaseActive = false,
      serviceName = this.get('serviceName'),
      isRunning = true,
      runningHCs = [],
      unknownHCs = [],
      hdfsHealthStatus;

    //look through all components to find out common statuses
    this.get('hostComponents').forEach(function (_hostComponent) {
      if (_hostComponent.get('isMaster')) {
        everyStartedOrMaintenance = (everyStartedOrMaintenance)
          ? ([App.HostComponentStatus.started, App.HostComponentStatus.maintenance].contains(_hostComponent.get('workStatus')))
          : false;
        everyStarted = (everyStarted)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.started)
          : false;
        isStarted = (!isStarted)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.started)
          : true;
        isUnknown = (!isUnknown)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.unknown)
          : true;
        isStarting = (!isStarting)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.starting)
          : true;
        isStopped = (!isStopped)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.stopped)
          : true;
        isHbaseActive = (!isHbaseActive)
          ? (_hostComponent.get('haStatus') === 'active')
          : true;

        masterComponents.push(_hostComponent);
      }

      if (_hostComponent.get('workStatus') !== App.HostComponentStatus.stopped &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.install_failed &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.unknown &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.maintenance) {
        isRunning = false;
        runningHCs.addObject(_hostComponent);
      } else if (_hostComponent.get('workStatus') == App.HostComponentStatus.unknown) {
        unknownHCs.addObject(_hostComponent);
      }
    }, this);

    //computation of service health status
    var isGreen = serviceName === 'HBASE' && App.supports.multipleHBaseMasters ? isStarted : everyStartedOrMaintenance;
    if (isGreen) {
      this.set('healthStatus', 'green');
    } else if (isUnknown) {
      this.set('healthStatus', 'yellow');
    } else if (isStarting) {
      this.set('healthStatus', 'green-blinking');
    } else if (isStopped) {
      this.set('healthStatus', 'red');
    } else {
      this.set('healthStatus', 'red-blinking');
    }

    if (serviceName === 'HBASE' && App.supports.multipleHBaseMasters) {
      if (!isHbaseActive) {
        this.set('healthStatus', 'red');
      }
    }

    if (isGreen && serviceName === 'HDFS' && masterComponents.length == 5) { // enabled HA
      var activeNN = this.get('activeNameNode');
      var nameNode = this.get('nameNode');

      if (nameNode && !activeNN) { //hdfs model but no active NN
        hdfsHealthStatus = 'red';
      } else if (nameNode && activeNN) {
        hdfsHealthStatus = 'green';
      }
      this.set('healthStatus', hdfsHealthStatus);
    }

    this.set('isStarted', everyStarted);
    this.set('runningHostComponents', runningHCs);
    this.set('unknownHostComponents', unknownHCs);
    this.set('isStopped', isRunning);
  },

  isConfigurable: function () {<commit_msg>Revert "AMBARI-3234. HDFS service status is shown as green when there is no Active NameNode. (xiwang via yusaku)"<commit_after>/**
 * 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');
require('utils/config');

App.Service = DS.Model.extend({

  serviceName: DS.attr('string'),

  workStatus: DS.attr('string'),
  rand: DS.attr('string'),
  alerts: DS.hasMany('App.Alert'),
  quickLinks: DS.hasMany('App.QuickLinks'),
  hostComponents: DS.hasMany('App.HostComponent'),
  serviceConfigsTemplate: App.config.get('preDefinedServiceConfigs'),
  runningHostComponents: null,

  // Instead of making healthStatus a computed property that listens on hostComponents.@each.workStatus,
  // we are creating a separate observer _updateHealthStatus.  This is so that healthStatus is updated
  // only once after the run loop.  This is because Ember invokes the computed property every time
  // a property that it depends on changes.  For example, App.statusMapper's map function would invoke
  // the computed property too many times and freezes the UI without this hack.
  // See http://stackoverflow.com/questions/12467345/ember-js-collapsing-deferring-expensive-observers-or-computed-properties
  healthStatus: '',

  /**
   * Every time when changes workStatus of any component we schedule recalculating values related from them
   */
  _updateHealthStatus: (function() {
    Ember.run.once(this, 'updateStatusDependencies');
  }).observes('hostComponents.@each.workStatus'),

  isStopped: false,
  isStarted: false,

  /**
   * compute service properties which depend on host components status
   * service computed properties:
   *    healthStatus
   *    isStarted
   *    runningHostComponents
   *    unknownHostComponents
   *    isStopped
   */
  updateStatusDependencies: function () {
    var everyStarted = true,
      everyStartedOrMaintenance = true,
      masterComponents = [],
      isStarted = false,
      isUnknown = false,
      isStarting = false,
      isStopped = false,
      isHbaseActive = false,
      serviceName = this.get('serviceName'),
      isRunning = true,
      runningHCs = [],
      unknownHCs = [],
      hdfsHealthStatus;

    //look through all components to find out common statuses
    this.get('hostComponents').forEach(function (_hostComponent) {
      if (_hostComponent.get('isMaster')) {
        everyStartedOrMaintenance = (everyStartedOrMaintenance)
          ? ([App.HostComponentStatus.started, App.HostComponentStatus.maintenance].contains(_hostComponent.get('workStatus')))
          : false;
        everyStarted = (everyStarted)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.started)
          : false;
        isStarted = (!isStarted)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.started)
          : true;
        isUnknown = (!isUnknown)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.unknown)
          : true;
        isStarting = (!isStarting)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.starting)
          : true;
        isStopped = (!isStopped)
          ? (_hostComponent.get('workStatus') === App.HostComponentStatus.stopped)
          : true;
        isHbaseActive = (!isHbaseActive)
          ? (_hostComponent.get('haStatus') === 'active')
          : true;

        masterComponents.push(_hostComponent);
      }

      if (_hostComponent.get('workStatus') !== App.HostComponentStatus.stopped &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.install_failed &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.unknown &&
        _hostComponent.get('workStatus') !== App.HostComponentStatus.maintenance) {
        isRunning = false;
        runningHCs.addObject(_hostComponent);
      } else if (_hostComponent.get('workStatus') == App.HostComponentStatus.unknown) {
        unknownHCs.addObject(_hostComponent);
      }
    }, this);

    //computation of service health status
    var isGreen = serviceName === 'HBASE' && App.supports.multipleHBaseMasters ? isStarted : everyStartedOrMaintenance;
    if (isGreen) {
      this.set('healthStatus', 'green');
    } else if (isUnknown) {
      this.set('healthStatus', 'yellow');
    } else if (isStarting) {
      this.set('healthStatus', 'green-blinking');
    } else if (isStopped) {
      this.set('healthStatus', 'red');
    } else {
      this.set('healthStatus', 'red-blinking');
    }

    if (serviceName === 'HBASE' && App.supports.multipleHBaseMasters) {
      if (!isHbaseActive) {
        this.set('healthStatus', 'red');
      }
    }

    this.set('isStarted', everyStarted);
    this.set('runningHostComponents', runningHCs);
    this.set('unknownHostComponents', unknownHCs);
    this.set('isStopped', isRunning);
  },

  isConfigurable: function () {
{'fraction_non_alphanumeric': 0.11540126395983032, 'fraction_numerical': 0.0025106051424119125, 'mean_word_length': 3.324971920628978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4531464', 'n_tokens_mistral': 3228, 'n_tokens_neox': 3139, 'n_words': 832}