<commit_before>(function() {
  'use strict';

  angular.module('app.states')
    .run(appRun);

  /** @ngInject */
  function appRun(routerHelper) {
    routerHelper.configureStates(getStates());
  }

  function getStates() {
    return {
      'services.list': {
        url: '', // No url, this state is the index of projects
        templateUrl: 'app/states/services/list/list.html',
        controller: StateController,
        controllerAs: 'vm',
        title: N_('Service List'),
        resolve: {
          services: resolveServices,
        },
      },
    };
  }

  /** @ngInject */
  function resolveServices(CollectionsApi) {
    var options = {
      expand: 'resources',
      attributes: ['picture', 'picture.image_href', 'evm_owner.name', 'v_total_vms', 'chargeback_report'],
      filter: ['service_id=nil'],
    };

    return CollectionsApi.query('services', options);
  }

  /** @ngInject */
  function StateController($state, services, ServicesState, $filter, $rootScope, Language, ListView, Chargeback, CollectionsApi, EventNotifications, sprintf) {
    var vm = this;

    vm.services = [];

    if (angular.isDefined($rootScope.notifications) && $rootScope.notifications.data.length > 0) {
      $rootScope.notifications.data.splice(0, $rootScope.notifications.data.length);
    }

    angular.forEach(services.resources, function(item) {
      item.powerState = angular.isDefined(item.options.power_state) ? item.options.power_state : "";
      item.powerStatus = angular.isDefined(item.options.power_status) ? item.options.power_status : "";
    });

    angular.forEach(services.resources, function(item) {
      if (angular.isUndefined(item.service_id)) {
        vm.services.push(item);
      }
    });

    vm.services.forEach(Chargeback.processReports);
    Chargeback.adjustRelativeCost(vm.services);

    vm.servicesList = angular.copy(vm.services);

    vm.listConfig = {
      selectItems: false,
      showSelectBox: false,
      selectionMatchProp: 'service_status',
      onClick: handleClick,
    };

    var serviceFilterConfig = {
      fields: getServiceFilterFields(),
      resultsCount: vm.servicesList.length,
      appliedFilters: ServicesState.filterApplied ? ServicesState.getFilters() : [],
      onFilterChange: filterChange,
    };

    var serviceSortConfig = {
      fields: getServiceSortFields(),
      onSortChange: sortChange,
      isAscending: ServicesState.getSort().isAscending,
      currentField: ServicesState.getSort().currentField,
    };

    vm.toolbarConfig = {
      filterConfig: serviceFilterConfig,
      sortConfig: serviceSortConfig,
    };

    vm.actionButtons = [
      {
        name: __('Start'),
        title: __('Start this service'),
        actionFn: startService,
        isDisabled: false,
      },
    ];

    vm.menuActions = [
      {
        name: __('Stop'),
        title: __('Stop this service'),
        actionFn: stopService,
        isDisabled: false,
      },
      {
        name: __('Suspend'),
        title: __('Suspend this service'),
        actionFn: suspendService,
        isDisabled: false,
      },
    ];

    function getServiceFilterFields() {
      var retires = [__('Current'), __('Soon'), __('Retired')];
      var dollars = ['$', '$$', '$$$', '$$$$'];

      return [
        ListView.createFilterField('name', __('Name'), __('Filter by Name'), 'text'),
        ListView.createFilterField('retirement', __('Retirement Date'), __('Filter by Retirement Date'), 'select', retires),
        ListView.createFilterField('vms', __('Number of VMs'), __('Filter by VMs'), 'text'),
        ListView.createFilterField('owner', __('Owner'), __('Filter by Owner'), 'text'),
        ListView.createFilterField('owner', __('Created'), __('Filter by Created On'), 'text'),
        ListView.createFilterField('chargeback_relative_cost', __('Relative Cost'), __('Filter by Relative Cost'), 'select', dollars),
      ];
    }

    function getServiceSortFields() {
      return [
        ListView.createSortField('name', __('Name'), 'alpha'),
        ListView.createSortField('retires', __('Retirement Date'), 'numeric'),
        ListView.createSortField('vms', __('Number of VMs'), 'numeric'),
        ListView.createSortField('owner', __('Owner'), 'alpha'),
        ListView.createSortField('created', __('Created'), 'numeric'),
        ListView.createSortField('chargeback_relative_cost', __('Relative Cost'), 'alpha'),
      ];
    }

    if (ServicesState.filterApplied) {
      /* Apply the filtering to the data list */
      filterChange(ServicesState.getFilters());
      ServicesState.filterApplied = false;
    } else {
      vm.servicesList = ListView.applyFilters(ServicesState.getFilters(), vm.servicesList, vm.services, ServicesState, matchesFilter);

      /* Make sure sorting direction is maintained */
      sortChange(ServicesState.getSort().currentField, ServicesState.getSort().isAscending);
    }

    vm.enableButtonForItemFn = function (action, item) {
      return item.powerStatus !== "start_complete"
        && item.powerStatus !== "starting"
        && item.powerStatus !== "stopping"
        && item.powerStatus !== "suspending";
    };

    vm.hideMenuForItemFn = function (item) {
      return item.powerStatus === ""
        || (item.powerState === "off" && item.powerStatus === "stop_complete")
        || item.powerStatus === "starting"
        || item.powerStatus === "stopping"
        || item.powerStatus === "suspending";
    };

    vm.updateMenuActionForItemFn = function(action, item) {
      if (item.powerState === "off" && item.powerStatus === "suspend_complete" && action.name === __("Suspend")) {
        action.isDisabled = true;
      }
    };

    function startService(action, item) {
      item.powerStatus = 'starting';
      powerOperation('start', item);
    }

    function stopService(action, item) {
      item.powerStatus = 'stopping';
      powerOperation('stop', item);
    }

    function suspendService(action, item) {
      item.powerStatus = 'suspending';
      powerOperation('suspend', item);
    }

    function powerOperation(powerAction, item) {
      CollectionsApi.post('services', item.id, {}, {action: powerAction}).then(actionSuccess, actionFailure);

      function actionSuccess() {
        if (powerAction === 'start') {
          EventNotifications.success(__(sprintf("%s was started", item.name)));
        } else if (powerAction === 'stop') {<commit_msg>Added actionName variable for actionButtons and menuActions<commit_after>(function() {
  'use strict';

  angular.module('app.states')
    .run(appRun);

  /** @ngInject */
  function appRun(routerHelper) {
    routerHelper.configureStates(getStates());
  }

  function getStates() {
    return {
      'services.list': {
        url: '', // No url, this state is the index of projects
        templateUrl: 'app/states/services/list/list.html',
        controller: StateController,
        controllerAs: 'vm',
        title: N_('Service List'),
        resolve: {
          services: resolveServices,
        },
      },
    };
  }

  /** @ngInject */
  function resolveServices(CollectionsApi) {
    var options = {
      expand: 'resources',
      attributes: ['picture', 'picture.image_href', 'evm_owner.name', 'v_total_vms', 'chargeback_report'],
      filter: ['service_id=nil'],
    };

    return CollectionsApi.query('services', options);
  }

  /** @ngInject */
  function StateController($state, services, ServicesState, $filter, $rootScope, Language, ListView, Chargeback, CollectionsApi, EventNotifications, sprintf) {
    var vm = this;

    vm.services = [];

    if (angular.isDefined($rootScope.notifications) && $rootScope.notifications.data.length > 0) {
      $rootScope.notifications.data.splice(0, $rootScope.notifications.data.length);
    }

    angular.forEach(services.resources, function(item) {
      item.powerState = angular.isDefined(item.options.power_state) ? item.options.power_state : "";
      item.powerStatus = angular.isDefined(item.options.power_status) ? item.options.power_status : "";
    });

    angular.forEach(services.resources, function(item) {
      if (angular.isUndefined(item.service_id)) {
        vm.services.push(item);
      }
    });

    vm.services.forEach(Chargeback.processReports);
    Chargeback.adjustRelativeCost(vm.services);

    vm.servicesList = angular.copy(vm.services);

    vm.listConfig = {
      selectItems: false,
      showSelectBox: false,
      selectionMatchProp: 'service_status',
      onClick: handleClick,
    };

    var serviceFilterConfig = {
      fields: getServiceFilterFields(),
      resultsCount: vm.servicesList.length,
      appliedFilters: ServicesState.filterApplied ? ServicesState.getFilters() : [],
      onFilterChange: filterChange,
    };

    var serviceSortConfig = {
      fields: getServiceSortFields(),
      onSortChange: sortChange,
      isAscending: ServicesState.getSort().isAscending,
      currentField: ServicesState.getSort().currentField,
    };

    vm.toolbarConfig = {
      filterConfig: serviceFilterConfig,
      sortConfig: serviceSortConfig,
    };

    vm.actionButtons = [
      {
        name: __('Start'),
        actionName: 'start',
        title: __('Start this service'),
        actionFn: startService,
        isDisabled: false,
      },
    ];

    vm.menuActions = [
      {
        name: __('Stop'),
        actionName: 'stop',
        title: __('Stop this service'),
        actionFn: stopService,
        isDisabled: false,
      },
      {
        name: __('Suspend'),
        actionName: 'suspend',
        title: __('Suspend this service'),
        actionFn: suspendService,
        isDisabled: false,
      },
    ];

    function getServiceFilterFields() {
      var retires = [__('Current'), __('Soon'), __('Retired')];
      var dollars = ['$', '$$', '$$$', '$$$$'];

      return [
        ListView.createFilterField('name', __('Name'), __('Filter by Name'), 'text'),
        ListView.createFilterField('retirement', __('Retirement Date'), __('Filter by Retirement Date'), 'select', retires),
        ListView.createFilterField('vms', __('Number of VMs'), __('Filter by VMs'), 'text'),
        ListView.createFilterField('owner', __('Owner'), __('Filter by Owner'), 'text'),
        ListView.createFilterField('owner', __('Created'), __('Filter by Created On'), 'text'),
        ListView.createFilterField('chargeback_relative_cost', __('Relative Cost'), __('Filter by Relative Cost'), 'select', dollars),
      ];
    }

    function getServiceSortFields() {
      return [
        ListView.createSortField('name', __('Name'), 'alpha'),
        ListView.createSortField('retires', __('Retirement Date'), 'numeric'),
        ListView.createSortField('vms', __('Number of VMs'), 'numeric'),
        ListView.createSortField('owner', __('Owner'), 'alpha'),
        ListView.createSortField('created', __('Created'), 'numeric'),
        ListView.createSortField('chargeback_relative_cost', __('Relative Cost'), 'alpha'),
      ];
    }

    if (ServicesState.filterApplied) {
      /* Apply the filtering to the data list */
      filterChange(ServicesState.getFilters());
      ServicesState.filterApplied = false;
    } else {
      vm.servicesList = ListView.applyFilters(ServicesState.getFilters(), vm.servicesList, vm.services, ServicesState, matchesFilter);

      /* Make sure sorting direction is maintained */
      sortChange(ServicesState.getSort().currentField, ServicesState.getSort().isAscending);
    }

    vm.enableButtonForItemFn = function (action, item) {
      return item.powerStatus !== "start_complete"
        && item.powerStatus !== "starting"
        && item.powerStatus !== "stopping"
        && item.powerStatus !== "suspending";
    };

    vm.hideMenuForItemFn = function (item) {
      return item.powerStatus === ""
        || (item.powerState === "off" && item.powerStatus === "stop_complete")
        || item.powerStatus === "starting"
        || item.powerStatus === "stopping"
        || item.powerStatus === "suspending";
    };

    vm.updateMenuActionForItemFn = function(action, item) {
      if (item.powerState === "off" && item.powerStatus === "suspend_complete" && action.actionName === "suspend") {
        action.isDisabled = true;
      } else if (item.powerState === "off" && item.powerStatus === "stop_complete" && action.actionName === "stop") {
        action.isDisabled = true;
      } else {
        action.isDisabled = false;
      }
    };

    function startService(action, item) {
      item.powerStatus = 'starting';
      powerOperation('start', item);
    }

    function stopService(action, item) {
      item.powerStatus = 'stopping';
      powerOperation('stop', item);
    }

    function suspendService(action, item) {
      item.powerStatus = 'suspending';
      powerOperation('suspend', item);
    }

    function powerOperation(powerAction, item) {
      CollectionsApi.post('services', item.id, {}, {action: powerAction}).then(actionSuccess, actionFailure);

      function actionSuccess() {
        if (powerAction === 'start') {
          EventNotifications.success(__(sprintf("%s was started", item.name)));
        } else if (powerAction === 'stop') {
{'fraction_non_alphanumeric': 0.14794064203513022, 'fraction_numerical': 0.0003028467595396729, 'mean_word_length': 3.1098319850653393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1681556', 'n_tokens_mistral': 3761, 'n_tokens_neox': 3674, 'n_words': 723}