<commit_before>var express = require('express');
var Promise = require('bluebird');
var router = express.Router();
var _ = require('lodash');
var middleware = require('../core/middleware');
var validator = require('validator');
var accountManager = require('../core/services/account-manager')();
var Deployments = require('../core/services/deployments');
var Collaborators = require('../core/services/collaborators');
var AppManager = require('../core/services/app-manager');
var PackageManager = require('../core/services/package-manager');
var common = require('../core/utils/common');
var config    = require('../core/config');

router.get('/',
  middleware.checkToken, function(req, res, next) {
  var uid = req.users.id;
  var appManager = new AppManager();
  appManager.listApps(uid)
  .then(function (data) {
    res.send({apps: data});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.get('/:appName/deployments',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deployments = new Deployments();
  accountManager.collaboratorCan(uid, appName)
  .then(function (col) {
    return deployments.listDeloyments(col.appid);
  })
  .then(function (data) {
    res.send({deployments: data});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.post('/:appName/deployments',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var name = req.body.name;
  var deployments = new Deployments();
  accountManager.ownerCan(uid, appName)
  .then(function (col) {
    return deployments.addDeloyment(name, col.appid, uid);
  })
  .then(function (data) {
    res.send({deployment: {name: data.name, key: data.deployment_key}});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.get('/:appName/deployments/:deploymentName/metrics',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  var packageManager = new PackageManager();

  accountManager.collaboratorCan(uid, appName)
  .then(function(col) {
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");
      }
      return deploymentInfo;
    })
  })
  .then(function(deploymentInfo) {
    return deployments.getAllPackageIdsByDeploymentsId(deploymentInfo.id);
  })
  .then(function(packagesInfos) {
    return Promise.reduce(packagesInfos, function (result, v) {
      return packageManager.getMetricsbyPackageId(v.get('id'))
      .then(function (metrics) {
        if (metrics) {
          result[v.get('label')] = {
            active: metrics.get('active'),
            downloaded: metrics.get('downloaded'),
            failed: metrics.get('failed'),
            installed: metrics.get('installed'),
          };
        }
        return result;
      });
    }, {});
  })
  .then(function(rs) {
    res.send({"metrics": rs});
  })
  .catch(function(e){
    res.send({"metrics": null});
  });
});

router.get('/:appName/deployments/:deploymentName/history',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  accountManager.collaboratorCan(uid, appName)
  .then(function(col){
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");
      }
      return deploymentInfo;
    });
  })
  .then(function(deploymentInfo) {
    return deployments.getDeploymentHistory(deploymentInfo.id);
  })
  .then(function (rs) {
    res.send({history: rs});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.delete('/:appName/deployments/:deploymentName/history',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  accountManager.collaboratorCan(uid, appName)
  .then(function(col){
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");<commit_msg>own clear<commit_after>var express = require('express');
var Promise = require('bluebird');
var router = express.Router();
var _ = require('lodash');
var middleware = require('../core/middleware');
var validator = require('validator');
var accountManager = require('../core/services/account-manager')();
var Deployments = require('../core/services/deployments');
var Collaborators = require('../core/services/collaborators');
var AppManager = require('../core/services/app-manager');
var PackageManager = require('../core/services/package-manager');
var common = require('../core/utils/common');
var config    = require('../core/config');

router.get('/',
  middleware.checkToken, function(req, res, next) {
  var uid = req.users.id;
  var appManager = new AppManager();
  appManager.listApps(uid)
  .then(function (data) {
    res.send({apps: data});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.get('/:appName/deployments',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deployments = new Deployments();
  accountManager.collaboratorCan(uid, appName)
  .then(function (col) {
    return deployments.listDeloyments(col.appid);
  })
  .then(function (data) {
    res.send({deployments: data});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.post('/:appName/deployments',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var name = req.body.name;
  var deployments = new Deployments();
  accountManager.ownerCan(uid, appName)
  .then(function (col) {
    return deployments.addDeloyment(name, col.appid, uid);
  })
  .then(function (data) {
    res.send({deployment: {name: data.name, key: data.deployment_key}});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.get('/:appName/deployments/:deploymentName/metrics',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  var packageManager = new PackageManager();

  accountManager.collaboratorCan(uid, appName)
  .then(function(col) {
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");
      }
      return deploymentInfo;
    })
  })
  .then(function(deploymentInfo) {
    return deployments.getAllPackageIdsByDeploymentsId(deploymentInfo.id);
  })
  .then(function(packagesInfos) {
    return Promise.reduce(packagesInfos, function (result, v) {
      return packageManager.getMetricsbyPackageId(v.get('id'))
      .then(function (metrics) {
        if (metrics) {
          result[v.get('label')] = {
            active: metrics.get('active'),
            downloaded: metrics.get('downloaded'),
            failed: metrics.get('failed'),
            installed: metrics.get('installed'),
          };
        }
        return result;
      });
    }, {});
  })
  .then(function(rs) {
    res.send({"metrics": rs});
  })
  .catch(function(e){
    res.send({"metrics": null});
  });
});

router.get('/:appName/deployments/:deploymentName/history',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  accountManager.collaboratorCan(uid, appName)
  .then(function(col){
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");
      }
      return deploymentInfo;
    });
  })
  .then(function(deploymentInfo) {
    return deployments.getDeploymentHistory(deploymentInfo.id);
  })
  .then(function (rs) {
    res.send({history: rs});
  })
  .catch(function (e) {
    res.status(406).send(e.message);
  });
});

router.delete('/:appName/deployments/:deploymentName/history',
  middleware.checkToken, function (req, res, next) {
  var uid = req.users.id;
  var appName = _.trim(req.params.appName);
  var deploymentName = _.trim(req.params.deploymentName);
  var deployments = new Deployments();
  accountManager.ownerCan(uid, appName)
  .then(function(col){
    return deployments.findDeloymentByName(deploymentName, col.appid)
    .then(function (deploymentInfo) {
      if (_.isEmpty(deploymentInfo)) {
        throw new Error("does not find the deployment");
{'fraction_non_alphanumeric': 0.15912702616802626, 'fraction_numerical': 0.0025426422290496875, 'mean_word_length': 3.8016276703967447, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '5581233', 'n_tokens_mistral': 3046, 'n_tokens_neox': 3021, 'n_words': 564}