<commit_before>var crypto = require('crypto');
var express = require('express');
var router = express.Router();

router.get('/:workflow/:task/:platform', function(req, res, next) {
    var client = req.app.get('elastic'),
      workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      mf_server = req.app.get('mf_server'),
      expand = req.query.expand,
      json = {};

    client.search({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        searchType: 'count'
    }, function(error, response) {
        if (response.hits !== undefined) {
            size = response.hits.total;
        }

        client.search({
            index: 'deployment_on_' + platform,
            type: workflow + '_' + task,
            size: size,
        }, function(error, response) {
            if (error) {
                res.status(500);
                return next(error);
            }
            var results = response.hits.hits;
            var keys = Object.keys(results);
            keys.forEach(function(key) {
                var next_deployment = {};
                if (!is_defined(expand)) {
                    next_deployment.href = mf_server + '/dreamcloud/mf/deployments/' +
                        workflow + '/' + task + '/' + platform + '/' + results[key]._id;
                } else {
                    next_deployment = results[key]._source;
                }
                json[results[key]._id] = next_deployment;
            });
            res.json(json);
        });
    });
});

router.get('/:workflow/:task/:platform/:experiment', function(req, res, next) {
    var client = req.app.get('elastic'),
      workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      experiment = req.params.experiment, /* we keep the original case */
      deployment = req.params.deployment,
      json = {};

    client.get({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        id: experiment
    }, function(error, response) {
        if (error) {
            res.status(500);
            return next(error);
        }
        if (response.found) {
            json = response._source;
        } else {
            json = "Could not find the deployment plan for the given workflow";
        }
        res.json(json);
    });
});

router.put('/:workflow/:task/:platform/:experiment', function(req, res, next) {
    var workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      experiment = req.params.experiment, /* we keep the original case */
      mf_server = req.app.get('mf_server'),
      client = req.app.get('elastic'),
      hashvalue = '',
      json = {};

    /* generate hash for the request body */
    if (is_defined(req.body)) {
        var hash = crypto.createHash('sha256');
        hash.update(JSON.stringify(req.body));
        hashvalue = hash.digest('hex');
    } else {
        res.json("body is missing");
        return;
    }

    client.exists({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        id: hashvalue
    }, function(error, exists) {
        if (exists === true) {
            client.get({
                index: 'deployment_on_' + platform,
                type: workflow + '_' + task,
                id: hashvalue
            }, function(error, response) {
                /* add experiment to source.experiments array */
                var source = response._source;
                var experiments = source.experiments;
                experiments = ( typeof experiments != 'undefined' &&
                                experiments instanceof Object ) ? experiments : {};
                experiments[experiment] = 1;
                source.experiments = experiments;
                /* update document */
                client.index({
                    index: 'deployment_on_' + platform,
                    type: workflow + '_' + task,
                    id: hashvalue,
                    body: source
                },function(error, response) {
                    if (error) {
                        res.status(500);
                        return next(error);
                    }
                    json.deployment_id = hashvalue;
                    json.predicted_time = source.estimatedTime;
                    json.href = mf_server + '/dreamcloud/mf/deployments/' +
                        workflow + '/' + task + '/' + platform + '/' + hashvalue;
                    res.json(json);
                });
            });
        } else { /* index new deployment plan */
            var source = req.body;
            source.experiments = { experiment: 1 };
            client.index({
                index: 'deployment_on_' + platform,
                type: workflow + '_' + task,
                id: hashvalue,
                body: source
            },function(error, response) {
                if (error) {
                    res.status(500);
                    return next(error);
                }
                json.deployment_id = hashvalue;
                json.predicted_time = source.estimatedTime;
                json.href = mf_server + '/dreamcloud/mf/deployments/' +
                    workflow + '/' + task + '/' + platform + '/' + hashvalue;
                res.json(json);
            });
        }
    });
});

function is_defined(variable) {
    return (typeof variable !== 'undefined');
}

module.exports = router;<commit_msg>fixed key for experiments in deployment plans<commit_after>var crypto = require('crypto');
var express = require('express');
var router = express.Router();

router.get('/:workflow/:task/:platform', function(req, res, next) {
    var client = req.app.get('elastic'),
      workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      mf_server = req.app.get('mf_server'),
      expand = req.query.expand,
      json = {};

    client.search({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        searchType: 'count'
    }, function(error, response) {
        if (response.hits !== undefined) {
            size = response.hits.total;
        }

        client.search({
            index: 'deployment_on_' + platform,
            type: workflow + '_' + task,
            size: size,
        }, function(error, response) {
            if (error) {
                res.status(500);
                return next(error);
            }
            var results = response.hits.hits;
            var keys = Object.keys(results);
            keys.forEach(function(key) {
                var next_deployment = {};
                if (!is_defined(expand)) {
                    next_deployment.href = mf_server + '/dreamcloud/mf/deployments/' +
                        workflow + '/' + task + '/' + platform + '/' + results[key]._id;
                } else {
                    next_deployment = results[key]._source;
                }
                json[results[key]._id] = next_deployment;
            });
            res.json(json);
        });
    });
});

router.get('/:workflow/:task/:platform/:experiment', function(req, res, next) {
    var client = req.app.get('elastic'),
      workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      experiment = req.params.experiment, /* we keep the original case */
      deployment = req.params.deployment,
      json = {};

    client.get({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        id: experiment
    }, function(error, response) {
        if (error) {
            res.status(500);
            return next(error);
        }
        if (response.found) {
            json = response._source;
        } else {
            json = "Could not find the deployment plan for the given workflow";
        }
        res.json(json);
    });
});

router.put('/:workflow/:task/:platform/:experiment', function(req, res, next) {
    var workflow = req.params.workflow.toLowerCase(),
      task = req.params.task.toLowerCase(),
      platform = req.params.platform.toLowerCase(),
      experiment = req.params.experiment, /* we keep the original case */
      mf_server = req.app.get('mf_server'),
      client = req.app.get('elastic'),
      hashvalue = '',
      json = {};

    /* generate hash for the request body */
    if (is_defined(req.body)) {
        var hash = crypto.createHash('sha256');
        hash.update(JSON.stringify(req.body));
        hashvalue = hash.digest('hex');
    } else {
        res.json("body is missing");
        return;
    }

    client.exists({
        index: 'deployment_on_' + platform,
        type: workflow + '_' + task,
        id: hashvalue
    }, function(error, exists) {
        if (exists === true) {
            client.get({
                index: 'deployment_on_' + platform,
                type: workflow + '_' + task,
                id: hashvalue
            }, function(error, response) {
                /* add experiment to source.experiments array */
                var source = response._source;
                var experiments = source.experiments;
                experiments = ( typeof experiments != 'undefined' &&
                                experiments instanceof Object ) ? experiments : {};
                experiments[experiment] = 1;
                source.experiments = experiments;
                /* update document */
                client.index({
                    index: 'deployment_on_' + platform,
                    type: workflow + '_' + task,
                    id: hashvalue,
                    body: source
                },function(error, response) {
                    if (error) {
                        res.status(500);
                        return next(error);
                    }
                    json.deployment_id = hashvalue;
                    json.predicted_time = source.estimatedTime;
                    json.href = mf_server + '/dreamcloud/mf/deployments/' +
                        workflow + '/' + task + '/' + platform + '/' + hashvalue;
                    res.json(json);
                });
            });
        } else { /* index new deployment plan */
            var source = req.body;
            source.experiments = {};
            source.experiments[experiment] = 1;
            client.index({
                index: 'deployment_on_' + platform,
                type: workflow + '_' + task,
                id: hashvalue,
                body: source
            },function(error, response) {
                if (error) {
                    res.status(500);
                    return next(error);
                }
                json.deployment_id = hashvalue;
                json.predicted_time = source.estimatedTime;
                json.href = mf_server + '/dreamcloud/mf/deployments/' +
                    workflow + '/' + task + '/' + platform + '/' + hashvalue;
                res.json(json);
            });
        }
    });
});

function is_defined(variable) {
    return (typeof variable !== 'undefined');
}

module.exports = router;
{'fraction_non_alphanumeric': 0.12877929772588656, 'fraction_numerical': 0.0029624466323952253, 'mean_word_length': 1.54162976085031, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5198515', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2990, 'n_words': 627}