<commit_before>var classNames = require("classnames");
var lazy = require("lazy.js");
var React = require("react/addons");

var AppListViewTypes = require("../constants/AppListViewTypes");
var AppStatus = require("../constants/AppStatus");
var FilterTypes = require("../constants/FilterTypes");
var Messages = require("../constants/Messages");
var States = require("../constants/States");
var AppListItemComponent = require("./AppListItemComponent");

var AppsActions = require("../actions/AppsActions");
var AppsStore = require("../stores/AppsStore");
var AppsEvents = require("../events/AppsEvents");

var Util = require("../helpers/Util");

function getGroupStatus(status, app) {
  var appStatus = app.status;

  if (appStatus === AppStatus.DEPLOYING ||
      appStatus === AppStatus.DELAYED ||
      appStatus === AppStatus.WAITING) {
    if (appStatus > status) {
      return appStatus;
    }
  }

  return status;
}

function getGroupHealth(health, app) {
  if (health == null) {
    return app.health;
  }

  return health.map(healthState => {
    var appHealthState =
      app.health.find(appHealth => appHealth.state === healthState.state);

    if (appHealthState != null) {
      healthState.quantity += appHealthState.quantity;
    }

    return healthState;
  });
}

function getInitialAppStatusesCount() {
  return Object.values(AppStatus).reduce(function (memo, status) {
    memo[status] = 0;
    return memo;
  }, {});
}

function initGroupNode(groupId, app) {
  return {
    id: groupId,
    instances: app.instances,
    status: getGroupStatus(null, app),
    health: getGroupHealth(null, app),
    tasksRunning: app.tasksRunning,
    totalCpus: app.totalCpus,
    totalMem: app.totalMem,
    isGroup: true
  };
}

function updateGroupNode(group, app) {
  group.instances += app.instances;
  group.tasksRunning += app.tasksRunning;
  group.totalCpus += app.totalCpus;
  group.totalMem += app.totalMem;
  group.health = getGroupHealth(group.health, app);
  group.status = getGroupStatus(group.status, app);
  return group;
}

var AppListComponent = React.createClass({
  displayName: "AppListComponent",

  contextTypes: {
    router: React.PropTypes.func
  },

  propTypes: {
    currentGroup: React.PropTypes.string.isRequired,
    filters: React.PropTypes.object
  },

  getDefaultProps: function () {
    return {
      filters: {}
    };
  },

  getInitialState: function () {
    var fetchState = States.STATE_LOADING;
    var apps = AppsStore.apps;

    if (apps.length > 0) {
      fetchState = States.STATE_SUCCESS;
    }

    return {
      apps: apps,
      fetchState: fetchState,
      sortKey: "id",
      sortDescending: false
    };
  },

  componentWillMount: function () {
    AppsStore.on(AppsEvents.CHANGE, this.onAppsChange);
    AppsStore.on(AppsEvents.REQUEST_APPS_ERROR, this.onAppsRequestError);
  },

  componentWillUnmount: function () {
    AppsStore.removeListener(AppsEvents.CHANGE,
      this.onAppsChange);
    AppsStore.removeListener(AppsEvents.REQUEST_APPS_ERROR,
      this.onAppsRequestError);
  },

  onAppsChange: function () {
    this.setState({
      apps: AppsStore.apps,
      fetchState: States.STATE_SUCCESS
    });
  },

  onAppsRequestError: function (message, statusCode) {
    var fetchState = States.STATE_ERROR;

    switch (statusCode) {
      case 401:
        fetchState = States.STATE_UNAUTHORIZED;
        break;
      case 403:
        fetchState = States.STATE_FORBIDDEN;
        break;
    }

    this.setState({
      fetchState: fetchState
    });
  },

  sortBy: function (sortKey) {
    var state = this.state;

    this.setState({
      sortKey: sortKey,
      sortDescending: state.sortKey === sortKey && !state.sortDescending
    });
  },

  hasFilters: function () {
    return Object.values(this.props.filters).some(filter => {
      return filter != null &&
        (Util.isArray(filter) && filter.length > 0) ||
        (Util.isString(filter) && filter !== "");
    });
  },

  filterNodes: function (nodesSequence, appsStatusesCount) {
    var props = this.props;
    var currentGroup = props.currentGroup;
    var filters = props.filters;

    let filterText = filters[FilterTypes.TEXT];
    if (filterText != null && filterText !== "") {
      nodesSequence = nodesSequence
        .filter(app => app.id.indexOf(filterText) !== -1);
    } else if (currentGroup !== "/") {
      nodesSequence = nodesSequence
          .filter(app => app.id.startsWith(currentGroup));
    }

    nodesSequence.each(app => {
      appsStatusesCount[app.status]++;
    });

    let filterLabels = filters[FilterTypes.LABELS];
    if (filterLabels != null && filterLabels.length > 0) {
      nodesSequence = nodesSequence.filter(app => {
        let labels = app.labels;
        if (labels == null || Object.keys(labels).length === 0) {
          return false;
        }

        return filterLabels.some(label => {
          let [key, value] = label;
          return labels[key] === value;
        });
      });
    }

    let filterStatus = filters[FilterTypes.STATUS];
    if (filterStatus != null && filterStatus.length > 0) {
      nodesSequence = nodesSequence.filter(app => {
        if (app.status == null) {<commit_msg>Hoist cache variables manually<commit_after>var classNames = require("classnames");
var lazy = require("lazy.js");
var React = require("react/addons");

var AppListViewTypes = require("../constants/AppListViewTypes");
var AppStatus = require("../constants/AppStatus");
var FilterTypes = require("../constants/FilterTypes");
var Messages = require("../constants/Messages");
var States = require("../constants/States");
var AppListItemComponent = require("./AppListItemComponent");

var AppsActions = require("../actions/AppsActions");
var AppsStore = require("../stores/AppsStore");
var AppsEvents = require("../events/AppsEvents");

var Util = require("../helpers/Util");

function getGroupStatus(status, app) {
  var appStatus = app.status;

  if (appStatus === AppStatus.DEPLOYING ||
      appStatus === AppStatus.DELAYED ||
      appStatus === AppStatus.WAITING) {
    if (appStatus > status) {
      return appStatus;
    }
  }

  return status;
}

function getGroupHealth(health, app) {
  if (health == null) {
    return app.health;
  }

  return health.map(healthState => {
    var appHealthState =
      app.health.find(appHealth => appHealth.state === healthState.state);

    if (appHealthState != null) {
      healthState.quantity += appHealthState.quantity;
    }

    return healthState;
  });
}

function getInitialAppStatusesCount() {
  return Object.values(AppStatus).reduce(function (memo, status) {
    memo[status] = 0;
    return memo;
  }, {});
}

function initGroupNode(groupId, app) {
  return {
    id: groupId,
    instances: app.instances,
    status: getGroupStatus(null, app),
    health: getGroupHealth(null, app),
    tasksRunning: app.tasksRunning,
    totalCpus: app.totalCpus,
    totalMem: app.totalMem,
    isGroup: true
  };
}

function updateGroupNode(group, app) {
  group.instances += app.instances;
  group.tasksRunning += app.tasksRunning;
  group.totalCpus += app.totalCpus;
  group.totalMem += app.totalMem;
  group.health = getGroupHealth(group.health, app);
  group.status = getGroupStatus(group.status, app);
  return group;
}

var AppListComponent = React.createClass({
  displayName: "AppListComponent",

  contextTypes: {
    router: React.PropTypes.func
  },

  propTypes: {
    currentGroup: React.PropTypes.string.isRequired,
    filters: React.PropTypes.object
  },

  getDefaultProps: function () {
    return {
      filters: {}
    };
  },

  getInitialState: function () {
    var fetchState = States.STATE_LOADING;
    var apps = AppsStore.apps;

    if (apps.length > 0) {
      fetchState = States.STATE_SUCCESS;
    }

    return {
      apps: apps,
      fetchState: fetchState,
      sortKey: "id",
      sortDescending: false
    };
  },

  componentWillMount: function () {
    AppsStore.on(AppsEvents.CHANGE, this.onAppsChange);
    AppsStore.on(AppsEvents.REQUEST_APPS_ERROR, this.onAppsRequestError);
  },

  componentWillUnmount: function () {
    AppsStore.removeListener(AppsEvents.CHANGE,
      this.onAppsChange);
    AppsStore.removeListener(AppsEvents.REQUEST_APPS_ERROR,
      this.onAppsRequestError);
  },

  onAppsChange: function () {
    this.setState({
      apps: AppsStore.apps,
      fetchState: States.STATE_SUCCESS
    });
  },

  onAppsRequestError: function (message, statusCode) {
    var fetchState = States.STATE_ERROR;

    switch (statusCode) {
      case 401:
        fetchState = States.STATE_UNAUTHORIZED;
        break;
      case 403:
        fetchState = States.STATE_FORBIDDEN;
        break;
    }

    this.setState({
      fetchState: fetchState
    });
  },

  sortBy: function (sortKey) {
    var state = this.state;

    this.setState({
      sortKey: sortKey,
      sortDescending: state.sortKey === sortKey && !state.sortDescending
    });
  },

  hasFilters: function () {
    return Object.values(this.props.filters).some(filter => {
      return filter != null &&
        (Util.isArray(filter) && filter.length > 0) ||
        (Util.isString(filter) && filter !== "");
    });
  },

  filterNodes: function (nodesSequence, appsStatusesCount) {
    var props = this.props;
    var currentGroup = props.currentGroup;
    var filters = props.filters;

    var filterText = filters[FilterTypes.TEXT];
    var filterLabels = filters[FilterTypes.LABELS];
    var filterStatus = filters[FilterTypes.STATUS];

    if (filterText != null && filterText !== "") {
      nodesSequence = nodesSequence
        .filter(app => app.id.indexOf(filterText) !== -1);
    } else if (currentGroup !== "/") {
      nodesSequence = nodesSequence
          .filter(app => app.id.startsWith(currentGroup));
    }

    nodesSequence.each(app => {
      appsStatusesCount[app.status]++;
    });

    if (filterLabels != null && filterLabels.length > 0) {
      nodesSequence = nodesSequence.filter(app => {
        let labels = app.labels;
        if (labels == null || Object.keys(labels).length === 0) {
          return false;
        }

        return filterLabels.some(label => {
          let [key, value] = label;
          return labels[key] === value;
        });
      });
    }

    if (filterStatus != null && filterStatus.length > 0) {
      nodesSequence = nodesSequence.filter(app => {
        if (app.status == null) {
{'fraction_non_alphanumeric': 0.13471502590673576, 'fraction_numerical': 0.002494722701976588, 'mean_word_length': 3.073075420085971, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '3384709', 'n_tokens_mistral': 3229, 'n_tokens_neox': 3172, 'n_words': 629}