<commit_before>var express = require('express');
var routerP = express.Router(),
    routerU = express.Router();
var async = require('async');
var dateFormat = require('dateformat');

routerU.get('/', function(req, res, next) {
    res.redirect('/v2/dreamcloud/mf/workflows?users');
});

routerU.get('/:id', function(req, res, next) {
    res.redirect('/v2/dreamcloud/mf/workflows/' + req.params.id + '?users');
});

/**
 * @api {put} /users/:userID 1. Registers a new user
 * @apiVersion 1.0.0
 * @apiName PutUsers
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix
 *
 * @apiParamExample {json} Request-Example:
 *     {
 *       "name": "<NAME>",
 *       "affiliation": "HLRS",
 *       "applications": [
 *          "avx"
 *       ]
 *     }
 *
 * @apiParam {String} [name] name of the user
 * @apiParam {String} [affiliation] affiliation of the user
 * @apiParam {Array}  [applications] list of applications to be monitored
 *
 * @apiSuccess {String} href link to the data stored for the given user
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *          "href":"http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix"
 *     }
 *
 * @apiError DatabaseError The given user could not be registered at the database
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 500 Internal Server Error
 *     {
 *       "error": "Resource could not be stored."
 *     }
 */
routerP.put('/:id', function(req, res, next) {
    var id = req.params.id.toLowerCase(),
        mf_server = req.app.get('mf_server') + '/mf',
        client = req.app.get('elastic');

    client.index({
        index: 'mf',
        type: 'workflows',
        id: id,
        body: req.body
    }, function(error, response) {
        if (error) {
            var message = 'Resource could not be stored.';
            console.log(error);
            res.status(500);
            return next(message);
        }
        var json = {};
        json.href = mf_server + '/users/' + id;
        res.json(json);
    });
});

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

/**
 * @api {post} /users/:userID/create 2. Create a user and an associated experiment
 * @apiVersion 1.0.0
 * @apiName PostUsers
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 * @apiSuccess {String} experimentID unique identifier generated for the experiment
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix/create
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     
 *          AVX9O-3oz5chEwIt8_M9
 *     
 *
 */
routerP.post('/:id/create', function(req, res, next) {
    var id = req.params.id.toLowerCase(),
        client = req.app.get('elastic');

    var data = req.body;
    if (data.application) {
        data.application = data.application.replace(' ', '_');
    }
    var experiment_id;

    var created_on = {};
    var now = new Date();
    now = dateFormat(now, "yyyy-mm-dd'T'HH:MM:ss");
    created_on.created_on = now;

    if (!is_defined(data['@timestamp'])) {
        data['@timestamp'] = now;
    }

    async.series([
        /* (1) register workflow, if not exists yet */
        function(series_callback) {
            client.index({
                index: 'mf',
                type: 'workflows',
                id: id,
                body: created_on
            }, function(error, response) {
                if (error) {
                    return series_callback(error);
                }

                series_callback(null);
            });
        },
        /* (2) create experiment ID on the fly */
        function(series_callback) {
            client.index({
                index: 'mf',
                type: 'experiments',
                parent: id,
                body: data
            }, function(error, response) {
                if (error) {
                    res.json(error);
                } else {
                    experiment_id = response._id;
                    series_callback(null);
                }
            });
        },
    ], function(error) {
        if (error) {
            res.status(500);
            return next(error);
        }
        res.send(experiment_id);
    });
});

/**
 * @api {post} /users/:userID/:experimentID/create 3. Create a user and an associated experiment with given experiment ID
 * @apiVersion 1.0.0
 * @apiName PostUserExperiment
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 * @apiParam {String} experimentID identifier given for the experiment
 * @apiSuccess {String} experimentID identifier given for the experiment
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix/AVX9O-3oz5chEwIt8_M9/create
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     
 *          AVX9O-3oz5chEwIt8_M9
 *     
 *
 */
routerP.post('/:uid/:eid/create', function(req, res, next) {
    var uid = req.params.uid.toLowerCase(),
        eid = req.params.eid,
        client = req.app.get('elastic');

    var data = req.body;
    if (data.application) {
        data.application = data.application.replace(' ', '_');
    }
    var experiment_id;

    var created_on = {};
    var now = new Date();
    now = dateFormat(now, "yyyy-mm-dd'T'HH:MM:ss");
    created_on.created_on = now;<commit_msg>minor formatting<commit_after>var express = require('express');
var routerP = express.Router(),
    routerU = express.Router();
var async = require('async');
var dateFormat = require('dateformat');

routerU.get('/', function(req, res, next) {
    res.redirect('/v2/dreamcloud/mf/workflows?users');
});

routerU.get('/:id', function(req, res, next) {
    res.redirect('/v2/dreamcloud/mf/workflows/' + req.params.id + '?users');
});

/**
 * @api {put} /users/:userID 1. Registers a new user
 * @apiVersion 1.0.0
 * @apiName PutUsers
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix
 *
 * @apiParamExample {json} Request-Example:
 *     {
 *       "name": "<NAME>",
 *       "affiliation": "HLRS",
 *       "applications": [
 *          "avx"
 *       ]
 *     }
 *
 * @apiParam {String} [name] name of the user
 * @apiParam {String} [affiliation] affiliation of the user
 * @apiParam {Array}  [applications] list of applications to be monitored
 *
 * @apiSuccess {String} href link to the data stored for the given user
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *          "href":"http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix"
 *     }
 *
 * @apiError DatabaseError The given user could not be registered at the database
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 500 Internal Server Error
 *     {
 *       "error": "Resource could not be stored."
 *     }
 */
routerP.put('/:id', function(req, res, next) {
    var id = req.params.id.toLowerCase(),
        mf_server = req.app.get('mf_server') + '/mf',
        client = req.app.get('elastic');

    client.index({
        index: 'mf',
        type: 'workflows',
        id: id,
        body: req.body
    }, function(error, response) {
        if (error) {
            var message = 'Resource could not be stored.';
            console.log(error);
            res.status(500);
            return next(message);
        }
        var json = {};
        json.href = mf_server + '/users/' + id;
        res.json(json);
    });
});

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

/**
 * @api {post} /users/:userID/create 2. Create a user and an associated experiment
 * @apiVersion 1.0.0
 * @apiName PostUsers
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 * @apiSuccess {String} experimentID unique identifier generated for the experiment
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix/create
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     AVX9O-3oz5chEwIt8_M9
 *
 */
routerP.post('/:id/create', function(req, res, next) {
    var id = req.params.id.toLowerCase(),
        client = req.app.get('elastic');

    var data = req.body;
    if (data.application) {
        data.application = data.application.replace(' ', '_');
    }
    var experiment_id;

    var created_on = {};
    var now = new Date();
    now = dateFormat(now, "yyyy-mm-dd'T'HH:MM:ss");
    created_on.created_on = now;

    if (!is_defined(data['@timestamp'])) {
        data['@timestamp'] = now;
    }

    async.series([
        /* (1) register workflow, if not exists yet */
        function(series_callback) {
            client.index({
                index: 'mf',
                type: 'workflows',
                id: id,
                body: created_on
            }, function(error, response) {
                if (error) {
                    return series_callback(error);
                }

                series_callback(null);
            });
        },
        /* (2) create experiment ID on the fly */
        function(series_callback) {
            client.index({
                index: 'mf',
                type: 'experiments',
                parent: id,
                body: data
            }, function(error, response) {
                if (error) {
                    res.json(error);
                } else {
                    experiment_id = response._id;
                    series_callback(null);
                }
            });
        },
    ], function(error) {
        if (error) {
            res.status(500);
            return next(error);
        }
        res.send(experiment_id);
    });
});

/**
 * @api {post} /users/:userID/:experimentID/create 3. Create a user and an associated experiment with given experiment ID
 * @apiVersion 1.0.0
 * @apiName PostUserExperiment
 * @apiGroup Users
 *
 * @apiParam {String} userID identifier for a user, e.g. 'excess'
 * @apiParam {String} experimentID identifier given for the experiment
 * @apiSuccess {String} experimentID identifier given for the experiment
 *
 * @apiExample {curl} Example usage:
 *     curl -i http://mf.excess-project.eu:3030/v2/mf/users/hpcfapix/AVX9O-3oz5chEwIt8_M9/create
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *
 *          AVX9O-3oz5chEwIt8_M9
 *
 *
 */
routerP.post('/:uid/:eid/create', function(req, res, next) {
    var uid = req.params.uid.toLowerCase(),
        eid = req.params.eid,
        client = req.app.get('elastic');

    var data = req.body;
    if (data.application) {
        data.application = data.application.replace(' ', '_');
    }
    var experiment_id;

    var created_on = {};
    var now = new Date();
    now = dateFormat(now, "yyyy-mm-dd'T'HH:MM:ss");
    created_on.created_on = now;
{'fraction_non_alphanumeric': 0.14512063377745768, 'fraction_numerical': 0.01386388188692834, 'mean_word_length': 2.1676646706586826, 'pattern_counts': {'":': 10, '<': 5, '<?xml version=': 0, '>': 5, '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': '6568272', 'n_tokens_mistral': 3610, 'n_tokens_neox': 3488, 'n_words': 818}