<commit_before>var EventEmitter = require("events").EventEmitter;
var lazy = require("lazy.js");

var AppDispatcher = require("../AppDispatcher");
var AppsEvents = require("../events/AppsEvents");
var appScheme = require("../stores/schemes/appScheme");
var AppTypes = require("../constants/AppTypes");
var ContainerConstants = require("../constants/ContainerConstants");
var AppStatus = require("../constants/AppStatus");
var HealthStatus = require("../constants/HealthStatus");
var TasksEvents = require("../events/TasksEvents");
var TaskStatus = require("../constants/TaskStatus");
var QueueStore = require("./QueueStore");
var QueueEvents = require("../events/QueueEvents");

const healthWeights = Object.freeze({
  [HealthStatus.UNHEALTHY]: 32,
  [HealthStatus.OVERCAPACITY]: 16,
  [HealthStatus.STAGED]: 8,
  [HealthStatus.HEALTHY]: 4,
  [HealthStatus.UNKNOWN]: 2,
  [HealthStatus.UNSCHEDULED]: 1
});

function removeApp(apps, appId) {
  return lazy(apps).reject({
    id: appId
  }).value();
}

function removeTasks(tasks, relatedAppId, taskIds) {
  return lazy(tasks).reject(function (task) {
    return (taskIds.indexOf(task.id) > -1) && task.appId === relatedAppId;
  }).value();
}

function getTaskHealth(task) {
  var healthCheckResults = task.healthCheckResults;

  if (healthCheckResults != null && healthCheckResults.length > 0) {
    let isHealthy = healthCheckResults.every(function (hcr) {
      if (hcr.firstSuccess) {
        return hcr.alive;
      }

      return false;
    });

    return isHealthy ? HealthStatus.HEALTHY : HealthStatus.UNHEALTHY;
  } else {
    return HealthStatus.UNKNOWN;
  }
}

function setTaskStatus(task) {
  if (task.startedAt != null) {
    task.status = TaskStatus.STARTED;
    task.updatedAt = task.startedAt;
  } else if (task.stagedAt != null) {
    task.status = TaskStatus.STAGED;
    task.updatedAt = task.stagedAt;
  }
}

function getAppHealth(app) {
  var tasksWithUnknownHealth = Math.max(
    (app.tasksRunning || 0) -
    (app.tasksHealthy || 0) -
    (app.tasksUnhealthy || 0),
    0
  );

  var healthData = [
    {quantity: app.tasksHealthy || 0, state: HealthStatus.HEALTHY},
    {quantity: app.tasksUnhealthy || 0, state: HealthStatus.UNHEALTHY},
    {quantity: tasksWithUnknownHealth, state: HealthStatus.UNKNOWN},
    {quantity: app.tasksStaged || 0, state: HealthStatus.STAGED}
  ];

  var tasksSum = 0;
  for (let i = 0; i < healthData.length; i++) {
    let capacityLeft = Math.max(0, app.instances - tasksSum);
    tasksSum += healthData[i].quantity;
  }

  var overCapacity = Math.max(0, tasksSum - app.instances);
  healthData.push({quantity: overCapacity, state: HealthStatus.OVERCAPACITY});

  var unscheduled = Math.max(0, (app.instances - tasksSum));
  healthData.push({
    quantity: unscheduled,
    state: HealthStatus.UNSCHEDULED
  });

  return healthData;
}

function getAppHealthWeight(health) {
  return health.reduce(function (totalWeight, entry) {
    if (entry.quantity) {
      return totalWeight + healthWeights[entry.state];
    }
    return totalWeight;
  }, 0);
}

function getAppType(app) {
  if (app.container != null &&
      app.container.type === ContainerConstants.TYPE.DOCKER) {<commit_msg>Refactor `getAppHealth` function in appStore.<commit_after>var EventEmitter = require("events").EventEmitter;
var lazy = require("lazy.js");

var AppDispatcher = require("../AppDispatcher");
var AppsEvents = require("../events/AppsEvents");
var appScheme = require("../stores/schemes/appScheme");
var AppTypes = require("../constants/AppTypes");
var ContainerConstants = require("../constants/ContainerConstants");
var AppStatus = require("../constants/AppStatus");
var HealthStatus = require("../constants/HealthStatus");
var TasksEvents = require("../events/TasksEvents");
var TaskStatus = require("../constants/TaskStatus");
var QueueStore = require("./QueueStore");
var QueueEvents = require("../events/QueueEvents");

const healthWeights = Object.freeze({
  [HealthStatus.UNHEALTHY]: 32,
  [HealthStatus.OVERCAPACITY]: 16,
  [HealthStatus.STAGED]: 8,
  [HealthStatus.HEALTHY]: 4,
  [HealthStatus.UNKNOWN]: 2,
  [HealthStatus.UNSCHEDULED]: 1
});

function removeApp(apps, appId) {
  return lazy(apps).reject({
    id: appId
  }).value();
}

function removeTasks(tasks, relatedAppId, taskIds) {
  return lazy(tasks).reject(function (task) {
    return (taskIds.indexOf(task.id) > -1) && task.appId === relatedAppId;
  }).value();
}

function getTaskHealth(task) {
  var healthCheckResults = task.healthCheckResults;

  if (healthCheckResults != null && healthCheckResults.length > 0) {
    let isHealthy = healthCheckResults.every(function (hcr) {
      if (hcr.firstSuccess) {
        return hcr.alive;
      }

      return false;
    });

    return isHealthy ? HealthStatus.HEALTHY : HealthStatus.UNHEALTHY;
  } else {
    return HealthStatus.UNKNOWN;
  }
}

function setTaskStatus(task) {
  if (task.startedAt != null) {
    task.status = TaskStatus.STARTED;
    task.updatedAt = task.startedAt;
  } else if (task.stagedAt != null) {
    task.status = TaskStatus.STAGED;
    task.updatedAt = task.stagedAt;
  }
}

function getAppHealth(app) {
  var tasksWithUnknownHealth = Math.max(
    (app.tasksRunning || 0) -
    (app.tasksHealthy || 0) -
    (app.tasksUnhealthy || 0),
    0
  );

  var healthData = [
    {quantity: app.tasksHealthy || 0, state: HealthStatus.HEALTHY},
    {quantity: app.tasksUnhealthy || 0, state: HealthStatus.UNHEALTHY},
    {quantity: tasksWithUnknownHealth, state: HealthStatus.UNKNOWN},
    {quantity: app.tasksStaged || 0, state: HealthStatus.STAGED}
  ];

  var tasksSum = healthData.reduce((sum, healthNode) => {
    return sum + healthNode.quantity;
  }, 0);

  healthData.push({
    quantity: Math.max(0, tasksSum - app.instances),
    state: HealthStatus.OVERCAPACITY
  });

  healthData.push({
    quantity: Math.max(0, (app.instances - tasksSum)),
    state: HealthStatus.UNSCHEDULED
  });

  return healthData;
}

function getAppHealthWeight(health) {
  return health.reduce(function (totalWeight, entry) {
    if (entry.quantity) {
      return totalWeight + healthWeights[entry.state];
    }
    return totalWeight;
  }, 0);
}

function getAppType(app) {
  if (app.container != null &&
      app.container.type === ContainerConstants.TYPE.DOCKER) {
{'fraction_non_alphanumeric': 0.1460155877206935, 'fraction_numerical': 0.0069985684746301895, 'mean_word_length': 4.002386634844869, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 8, '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': '785013', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2007, 'n_words': 412}