<commit_before>import lazy from "lazy.js";
import React from "react/addons";

import AppsActions from "../actions/AppsActions";
import PagedNavComponent from "../components/PagedNavComponent";
import TasksActions from "../actions/TasksActions";
import TaskListComponent from "../components/TaskListComponent";

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

  propTypes: {
    appId: React.PropTypes.string.isRequired,
    fetchState: React.PropTypes.number.isRequired,
    getTaskHealthMessage: React.PropTypes.func.isRequired,
    hasHealth: React.PropTypes.bool,
    tasks: React.PropTypes.array.isRequired
  },

  getInitialState: function () {
    return {
      currentPage: 0,
      itemsPerPage: 8,
      selectedTasks: {}
    };
  },

  componentWillReceiveProps: function (nextProps) {
    var state = this.state;
    var tasksLength = nextProps.tasks.length;

    if (state.currentPage * state.itemsPerPage > tasksLength) {
      this.setState({
        currentPage: parseInt(tasksLength / state.itemsPerPage, 10),
        selectedTasks: {}
      });
    }
  },

  handlePageChange: function (pageNum) {
    this.setState({currentPage: pageNum});
  },

  handleRefresh: function () {
    AppsActions.requestApp(this.props.appId);
  },

  handleKillSelectedTasks: function (scaleTask = false, wipeTasks = false) {
    var props = this.props;
    var selectedTaskIds = Object.keys(this.state.selectedTasks);

    var taskIds = lazy(props.tasks).map(function (task) {
      return selectedTaskIds.indexOf(task.id) >= 0
        ? task.id
        : null;
    }).compact().value();

    if (!scaleTask) {
      TasksActions.deleteTasks(props.appId, taskIds, wipeTasks);
    } else {
      TasksActions.deleteTasksAndScale(props.appId, taskIds);
    }
    this.setState({selectedTasks: {}});
  },

  toggleAllTasks: function () {
    var newSelectedTasks = {};
    var modelTasks = this.props.tasks;

    // Note: not an **exact** check for all tasks being selected but a good
    // enough proxy.
    var allTasksSelected = Object.keys(this.state.selectedTasks).length ===
      modelTasks.length;

    if (!allTasksSelected) {
      modelTasks.forEach(function (task) {
        newSelectedTasks[task.id] = true;
      });
    }

    this.setState({selectedTasks: newSelectedTasks});
  },

  onTaskToggle: function (task, value) {
    var selectedTasks = this.state.selectedTasks;

    // If `toggleTask` is used as a callback for an event handler, the second
    // parameter will be an event object. Use it to set the value only if it
    // is a Boolean.
    var localValue = (typeof value === Boolean) ?
      value :
      !selectedTasks[task.id];

    if (localValue === true) {
      selectedTasks[task.id] = true;
    } else {
      delete selectedTasks[task.id];
    }

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

  getButtons: function () {
    var selectedTasksLength = Object.keys(this.state.selectedTasks).length;
    if (selectedTasksLength === 0) {
      return (
        <button
            className="btn btn-sm btn-default"
            onClick={this.handleRefresh}>
          ↻ Refresh
        </button>
      );
    } else {
      return (
        <div className="btn-group">
          <button
              className="btn btn-sm btn-info"
              onClick={this.handleKillSelectedTasks.bind(this, false)}>
            Kill
          </button>
          <button
              className="btn btn-sm btn-info"
              onClick={this.handleKillSelectedTasks.bind(this, true)}>
            Kill &amp; Scale
          </button>
          {this.getKillAndWipeButton()}
        </div>
      );
    }
  },

  getKillAndWipeButton: function () {
    var hasStatefulTasks = Object.keys(this.state.selectedTasks)
      .reduce((memo, taskId) => {
        var task = this.props.tasks.find(task => task.id === taskId);
        if (task != null &&
            task.localVolumes != null
            && task.localVolumes.length > 0) {<commit_msg>Explicitly pass argument<commit_after>import lazy from "lazy.js";
import React from "react/addons";

import AppsActions from "../actions/AppsActions";
import PagedNavComponent from "../components/PagedNavComponent";
import TasksActions from "../actions/TasksActions";
import TaskListComponent from "../components/TaskListComponent";

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

  propTypes: {
    appId: React.PropTypes.string.isRequired,
    fetchState: React.PropTypes.number.isRequired,
    getTaskHealthMessage: React.PropTypes.func.isRequired,
    hasHealth: React.PropTypes.bool,
    tasks: React.PropTypes.array.isRequired
  },

  getInitialState: function () {
    return {
      currentPage: 0,
      itemsPerPage: 8,
      selectedTasks: {}
    };
  },

  componentWillReceiveProps: function (nextProps) {
    var state = this.state;
    var tasksLength = nextProps.tasks.length;

    if (state.currentPage * state.itemsPerPage > tasksLength) {
      this.setState({
        currentPage: parseInt(tasksLength / state.itemsPerPage, 10),
        selectedTasks: {}
      });
    }
  },

  handlePageChange: function (pageNum) {
    this.setState({currentPage: pageNum});
  },

  handleRefresh: function () {
    AppsActions.requestApp(this.props.appId);
  },

  handleKillSelectedTasks: function (scaleTask, wipeTasks) {
    var props = this.props;
    var selectedTaskIds = Object.keys(this.state.selectedTasks);

    var taskIds = lazy(props.tasks).map(function (task) {
      return selectedTaskIds.indexOf(task.id) >= 0
        ? task.id
        : null;
    }).compact().value();

    if (!scaleTask) {
      TasksActions.deleteTasks(props.appId, taskIds, wipeTasks);
    } else {
      TasksActions.deleteTasksAndScale(props.appId, taskIds);
    }
    this.setState({selectedTasks: {}});
  },

  toggleAllTasks: function () {
    var newSelectedTasks = {};
    var modelTasks = this.props.tasks;

    // Note: not an **exact** check for all tasks being selected but a good
    // enough proxy.
    var allTasksSelected = Object.keys(this.state.selectedTasks).length ===
      modelTasks.length;

    if (!allTasksSelected) {
      modelTasks.forEach(function (task) {
        newSelectedTasks[task.id] = true;
      });
    }

    this.setState({selectedTasks: newSelectedTasks});
  },

  onTaskToggle: function (task, value) {
    var selectedTasks = this.state.selectedTasks;

    // If `toggleTask` is used as a callback for an event handler, the second
    // parameter will be an event object. Use it to set the value only if it
    // is a Boolean.
    var localValue = (typeof value === Boolean) ?
      value :
      !selectedTasks[task.id];

    if (localValue === true) {
      selectedTasks[task.id] = true;
    } else {
      delete selectedTasks[task.id];
    }

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

  getButtons: function () {
    var selectedTasksLength = Object.keys(this.state.selectedTasks).length;
    if (selectedTasksLength === 0) {
      return (
        <button
            className="btn btn-sm btn-default"
            onClick={this.handleRefresh}>
          ↻ Refresh
        </button>
      );
    } else {
      return (
        <div className="btn-group">
          <button
              className="btn btn-sm btn-info"
              onClick={this.handleKillSelectedTasks.bind(this, false, false)}>
            Kill
          </button>
          <button
              className="btn btn-sm btn-info"
              onClick={this.handleKillSelectedTasks.bind(this, true, false)}>
            Kill &amp; Scale
          </button>
          {this.getKillAndWipeButton()}
        </div>
      );
    }
  },

  getKillAndWipeButton: function () {
    var hasStatefulTasks = Object.keys(this.state.selectedTasks)
      .reduce((memo, taskId) => {
        var task = this.props.tasks.find(task => task.id === taskId);
        if (task != null &&
            task.localVolumes != null
            && task.localVolumes.length > 0) {
{'fraction_non_alphanumeric': 0.12227617980326236, 'fraction_numerical': 0.0017432449259120906, 'mean_word_length': 2.636034404708013, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, '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': '4925240', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2306, 'n_words': 512}