<commit_before>import R from "ramda";
import Promise from "bluebird";
import pm2 from "./pm2";
import prettyBytes from "pretty-bytes";
import { promises } from "./util";
import log from "./log";





export default (apps) => {
  const processNameToAppId = (name) => name.split(":")[0];
  const getApp = (id) => {
    id = processNameToAppId(id);
    return R.find(app => app.id === id, apps);
  };


  const getAppStatus = (app, processItem) => {
      return new Promise((resolve, reject) => {
          if (!app || !processItem) {
            resolve();
            return;
          }

          const status = {
              id: app.id,
              status: processItem.pm2_env.status,
              route: `${ app.route.toString() } ⇨ ${ app.port }`,
              repository: `${ app.repo.name }:${ app.branch }`,
              resources: {
                memory: prettyBytes(processItem.monit.memory),
                cpu: processItem.monit.cpu
              }
          };

          const gettingVersion = app.version().catch(err => reject(err));
          gettingVersion.then(version => {
              status.version = {
                local: version.local,
                remote: version.remote
              };
              if (version.isUpdateRequired) { status.version.isUpdateRequired = true; }
              if (version.isDependenciesChanged) { status.version.isDependenciesChanged = true; }
              if (version.isDownloading) {
                status.status += `, updating to v${ version.remote }`;
                status.version.isDownloading = true;
              }
              resolve(status);
            });
      });
    };



  const getRunningApps = () => {
        return new Promise((resolve, reject) => {
          Promise.coroutine(function*() {
            const processes = yield pm2.apps();
            let { results } = yield promises(processes.map(item => getAppStatus(getApp(item.name), item))).catch(err => reject(err));
            results = R.reject(R.isNil)(results);
            results = R.sortBy(R.prop("id"))(results);
            resolve(results);
          })();
        });
      };



  return {
    getStatuses(req, res) {
      getRunningApps()
        .then(appsStatus => res.send({ apps: appsStatus }))
        .catch(err => {
            log.error(err);
            res.status(500).send({
              error: "Failed while getting the status of running applications",
              message: err.message
            });
        });
    },

    getAppStatus(req, res) {
      Promise.coroutine(function*() {<commit_msg>Process HOSTNAME on API status JSON.<commit_after>import R from "ramda";
import Promise from "bluebird";
import pm2 from "./pm2";
import prettyBytes from "pretty-bytes";
import { promises } from "./util";
import log from "./log";

const HOST_NAME = process.env["HOSTNAME"] || "unknown";



export default (apps) => {
  const processNameToAppId = (name) => name.split(":")[0];
  const getApp = (id) => {
    id = processNameToAppId(id);
    return R.find(app => app.id === id, apps);
  };


  const getAppStatus = (app, processItem) => {
      return new Promise((resolve, reject) => {
          if (!app || !processItem) {
            resolve();
            return;
          }

          const status = {
              id: app.id,
              status: processItem.pm2_env.status,
              route: `${ app.route.toString() } ⇨ ${ app.port }`,
              repository: `${ app.repo.name }:${ app.branch }`,
              resources: {
                memory: prettyBytes(processItem.monit.memory),
                cpu: processItem.monit.cpu
              }
          };

          const gettingVersion = app.version().catch(err => reject(err));
          gettingVersion.then(version => {
              status.version = {
                local: version.local,
                remote: version.remote
              };
              if (version.isUpdateRequired) { status.version.isUpdateRequired = true; }
              if (version.isDependenciesChanged) { status.version.isDependenciesChanged = true; }
              if (version.isDownloading) {
                status.status += `, updating to v${ version.remote }`;
                status.version.isDownloading = true;
              }
              resolve(status);
            });
      });
    };



  const getRunningApps = () => {
        return new Promise((resolve, reject) => {
          Promise.coroutine(function*() {
            const processes = yield pm2.apps();
            let { results } = yield promises(processes.map(item => getAppStatus(getApp(item.name), item))).catch(err => reject(err));
            results = R.reject(R.isNil)(results);
            results = R.sortBy(R.prop("id"))(results);
            resolve(results);
          })();
        });
      };



  return {
    getStatuses(req, res) {
      getRunningApps()
        .then(appsStatus => res.send({ host: HOST_NAME, apps: appsStatus }))
        .catch(err => {
            log.error(err);
            res.status(500).send({
              error: "Failed while getting the status of running applications",
              message: err.message
            });
        });
    },

    getAppStatus(req, res) {
      Promise.coroutine(function*() {
{'fraction_non_alphanumeric': 0.1413105413105413, 'fraction_numerical': 0.0030389363722697054, 'mean_word_length': 1.814537680384821, 'pattern_counts': {'":': 2, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5686984', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1423, 'n_words': 307}