<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');
var hostsManagement = require('utils/hosts');
var numberUtils = require('utils/number_utils');

App.ManageConfigGroupsController = Em.Controller.extend({
  name: 'manageConfigGroupsController',

  isLoaded: false,

  isInstaller: false,

  isAddService: false,

  serviceName: null,

  configGroups: [],

  originalConfigGroups: [],

  selectedConfigGroup: null,

  selectedHosts: [],

  clusterHosts: [],

  resortConfigGroup: function() {
    var configGroups = Ember.copy(this.get('configGroups'));
    if(configGroups.length < 2){
      return;
    }
    var defaultConfigGroup = configGroups.findProperty('isDefault');
    configGroups.removeObject(defaultConfigGroup);
    var sorted = [defaultConfigGroup].concat(configGroups.sortProperty('name'));

    this.removeObserver('config<EMAIL>', this, 'resortConfigGroup');
    this.set('configGroups', sorted);
    this.addObserver('configGroups.<EMAIL>', this, 'resortConfigGroup');
  }.observes('configGroups.<EMAIL>'),

  loadHosts: function() {
    this.set('isLoaded', false);
    if (this.get('isInstaller')) {
      var allHosts = this.get('isAddService') ? App.router.get('addServiceController').get('allHosts') : App.router.get('installerController').get('allHosts');
      this.set('clusterHosts', allHosts);
      this.loadConfigGroups(this.get('serviceName'));
    } else {
      this.loadHostsFromServer();
    }
  },

  /**
   * request all hosts directly from server
   */
  loadHostsFromServer: function() {
    App.ajax.send({
      name: 'hosts.config_groups',
      sender: this,
      data: {},
      success: 'loadHostsFromServerSuccessCallback',
      error: 'loadHostsFromServerErrorCallback'
    });
  },

  /**
   * parse hosts response and wrap them into Ember.Object
   * @param data
   */
  loadHostsFromServerSuccessCallback: function (data) {
    var wrappedHosts = [];

    data.items.forEach(function (host) {
      var hostComponents = [];
      var diskInfo = host.Hosts.disk_info.filter(function(item) {
        return /^ext|^ntfs|^fat|^xfs/i.test(item.type);
      });
      if (diskInfo.length) {
        diskInfo = diskInfo.reduce(function(a, b) {
          return {
            available: parseInt(a.available) + parseInt(b.available),
            size: parseInt(a.size) + parseInt(b.size)
          };
        });
      }
      host.host_components.forEach(function (hostComponent) {
        hostComponents.push(Em.Object.create({
          componentName: hostComponent.HostRoles.component_name,
          displayName: App.format.role(hostComponent.HostRoles.component_name)
        }));
      }, this);
      wrappedHosts.pushObject(Em.Object.create({
          id: host.Hosts.host_name,
          ip: host.Hosts.ip,
          osType: host.Hosts.os_type,
          osArch: host.Hosts.os_arch,
          hostName: host.Hosts.host_name,
          publicHostName: host.Hosts.public_host_name,
          cpu: host.Hosts.cpu_count,
          memory: host.Hosts.total_mem,
          diskTotal: numberUtils.bytesToSize(diskInfo.size, 0, undefined, 1024),
          diskFree: numberUtils.bytesToSize(diskInfo.available, 0, undefined, 1024),
          disksMounted: host.Hosts.disk_info.length,
          hostComponents: hostComponents
        }
      ));
    }, this);

    this.set('clusterHosts', wrappedHosts);
    this.loadConfigGroups(this.get('serviceName'));
  },

  loadHostsFromServerErrorCallback: function () {
    console.warn('ERROR: request to fetch all hosts failed');
    this.set('clusterHosts', []);
    this.loadConfigGroups(this.get('serviceName'));
  },

  loadConfigGroups: function (serviceName) {
    if (this.get('isInstaller')) {
      this.set('serviceName', serviceName);
      var configGroups = this.copyConfigGroups(App.router.get('wizardStep7Controller.selectedService.configGroups'));
      var originalConfigGroups = this.copyConfigGroups(configGroups);
      this.set('configGroups', configGroups);
      this.set('originalConfigGroups', originalConfigGroups);
      this.set('isLoaded', true);
    } else {
      this.set('serviceName', serviceName);
      App.ajax.send({
        name: 'service.load_config_groups',
        data: {
          serviceName: serviceName
        },
        sender: this,
        success: 'onLoadConfigGroupsSuccess',
        error: 'onLoadConfigGroupsError'
      });
    }
  },

  onLoadConfigGroupsSuccess: function (data) {
    var usedHosts = [];
    var unusedHosts = [];
    var serviceName = this.get('serviceName');
    var serviceDisplayName =  this.get('displayName');
    var defaultConfigGroup = App.ConfigGroup.create({
      name: serviceDisplayName + " Default",
      description: "Default cluster level " + this.get('serviceName') + " configuration",
      isDefault: true,
      parentConfigGroup: null,
      service: this.get('content'),
      configSiteTags: [],
      serviceName: serviceName
    });

    if (data && data.items) {
      var groupToTypeToTagMap = {};
      var configGroups = [];
      data.items.forEach(function (configGroup) {
        configGroup = configGroup.ConfigGroup;<commit_msg>AMBARI-6491. Default config group name contains 'undefined' instead of service name in Manage Config Groups popup (Max Shepel via alexantonenko)<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');
var hostsManagement = require('utils/hosts');
var numberUtils = require('utils/number_utils');

App.ManageConfigGroupsController = Em.Controller.extend({
  name: 'manageConfigGroupsController',

  isLoaded: false,

  isInstaller: false,

  isAddService: false,

  serviceName: null,

  configGroups: [],

  originalConfigGroups: [],

  selectedConfigGroup: null,

  selectedHosts: [],

  clusterHosts: [],

  resortConfigGroup: function() {
    var configGroups = Ember.copy(this.get('configGroups'));
    if(configGroups.length < 2){
      return;
    }
    var defaultConfigGroup = configGroups.findProperty('isDefault');
    configGroups.removeObject(defaultConfigGroup);
    var sorted = [defaultConfigGroup].concat(configGroups.sortProperty('name'));

    this.removeObserver('configGroups.@<EMAIL>', this, 'resortConfigGroup');
    this.set('configGroups', sorted);
    this.addObserver('configGroups.@<EMAIL>.<EMAIL>', this, 'resortConfigGroup');
  }.observes('configGroups.@<EMAIL>'),

  loadHosts: function() {
    this.set('isLoaded', false);
    if (this.get('isInstaller')) {
      var allHosts = this.get('isAddService') ? App.router.get('addServiceController').get('allHosts') : App.router.get('installerController').get('allHosts');
      this.set('clusterHosts', allHosts);
      this.loadConfigGroups(this.get('serviceName'));
    } else {
      this.loadHostsFromServer();
    }
  },

  /**
   * request all hosts directly from server
   */
  loadHostsFromServer: function() {
    App.ajax.send({
      name: 'hosts.config_groups',
      sender: this,
      data: {},
      success: 'loadHostsFromServerSuccessCallback',
      error: 'loadHostsFromServerErrorCallback'
    });
  },

  /**
   * parse hosts response and wrap them into Ember.Object
   * @param data
   */
  loadHostsFromServerSuccessCallback: function (data) {
    var wrappedHosts = [];

    data.items.forEach(function (host) {
      var hostComponents = [];
      var diskInfo = host.Hosts.disk_info.filter(function(item) {
        return /^ext|^ntfs|^fat|^xfs/i.test(item.type);
      });
      if (diskInfo.length) {
        diskInfo = diskInfo.reduce(function(a, b) {
          return {
            available: parseInt(a.available) + parseInt(b.available),
            size: parseInt(a.size) + parseInt(b.size)
          };
        });
      }
      host.host_components.forEach(function (hostComponent) {
        hostComponents.push(Em.Object.create({
          componentName: hostComponent.HostRoles.component_name,
          displayName: App.format.role(hostComponent.HostRoles.component_name)
        }));
      }, this);
      wrappedHosts.pushObject(Em.Object.create({
          id: host.Hosts.host_name,
          ip: host.Hosts.ip,
          osType: host.Hosts.os_type,
          osArch: host.Hosts.os_arch,
          hostName: host.Hosts.host_name,
          publicHostName: host.Hosts.public_host_name,
          cpu: host.Hosts.cpu_count,
          memory: host.Hosts.total_mem,
          diskTotal: numberUtils.bytesToSize(diskInfo.size, 0, undefined, 1024),
          diskFree: numberUtils.bytesToSize(diskInfo.available, 0, undefined, 1024),
          disksMounted: host.Hosts.disk_info.length,
          hostComponents: hostComponents
        }
      ));
    }, this);

    this.set('clusterHosts', wrappedHosts);
    this.loadConfigGroups(this.get('serviceName'));
  },

  loadHostsFromServerErrorCallback: function () {
    console.warn('ERROR: request to fetch all hosts failed');
    this.set('clusterHosts', []);
    this.loadConfigGroups(this.get('serviceName'));
  },

  loadConfigGroups: function (serviceName) {
    if (this.get('isInstaller')) {
      this.set('serviceName', serviceName);
      var configGroups = this.copyConfigGroups(App.router.get('wizardStep7Controller.selectedService.configGroups'));
      var originalConfigGroups = this.copyConfigGroups(configGroups);
      this.set('configGroups', configGroups);
      this.set('originalConfigGroups', originalConfigGroups);
      this.set('isLoaded', true);
    } else {
      this.set('serviceName', serviceName);
      App.ajax.send({
        name: 'service.load_config_groups',
        data: {
          serviceName: serviceName
        },
        sender: this,
        success: 'onLoadConfigGroupsSuccess',
        error: 'onLoadConfigGroupsError'
      });
    }
  },

  onLoadConfigGroupsSuccess: function (data) {
    var usedHosts = [];
    var unusedHosts = [];
    var serviceName = this.get('serviceName');
    var serviceDisplayName =  App.StackService.find().findProperty('serviceName', this.get('serviceName')).get('displayName');
    var defaultConfigGroup = App.ConfigGroup.create({
      name: serviceDisplayName + " Default",
      description: "Default cluster level " + this.get('serviceName') + " configuration",
      isDefault: true,
      parentConfigGroup: null,
      service: this.get('content'),
      configSiteTags: [],
      serviceName: serviceName
    });

    if (data && data.items) {
      var groupToTypeToTagMap = {};
      var configGroups = [];
      data.items.forEach(function (configGroup) {
        configGroup = configGroup.ConfigGroup;
{'fraction_non_alphanumeric': 0.12302342909181224, 'fraction_numerical': 0.002980379170461131, 'mean_word_length': 3.3035268970431066, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '2771619', 'n_tokens_mistral': 3474, 'n_tokens_neox': 3408, 'n_words': 753}