<commit_before>module.exports = function(experiments, schema) {
    const parse = require('co-body');
    const status = require('http-status');

    return {
        getAllForProject,
        get,
        getStep,
        createStep,
        updateStep,
        update,
        create
    };

    function* getAllForProject(next) {
        let rv = yield experiments.getAllForProject(this.params.project_id);
        if (rv.error) {
            this.body = rv;
            this.status = status.NOT_FOUND;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* get(next) {
        let isInProject = yield experiments.experimentExistsInProject(this.params.project_id, this.params.experiment_id);
        if (!isInProject) {
            this.status = status.NOT_FOUND;
        } else {
            let rv = yield experiments.get(this.params.experiment_id);
            if (rv.error) {
                this.body = rv;
                this.status = status.NOT_FOUND;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* getStep(next) {
        let isValidStep = yield validateStep(this.params.project_id, this.params.experiment_id, this.params.step_id);
        if (!isValidStep) {
            this.status = status.NOT_FOUND;
            this.body = {error: 'Step not found'};
        }
        let rv = yield experiments.getStep(this.params.step_id);
        if (rv.error) {
            this.body = rv;
            this.status = status.NOT_FOUND;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* validateStep(projectID, experimentID, stepID) {
        let isInProject = yield experiments.experimentExistsInProject(projectID, experimentID);
        if (!isInProject) {
            return false;
        }

        return yield experiments.experimentStepExistsInExperiment(experimentID, stepID);
    }

    function* createStep(next) {
        let stepArgs = yield parse(this);
        schema.prepare(schema.createExperimentStep, stepArgs);
        let errors = yield validateCreateStepArgs(stepArgs, this.params.project_id,
            this.params.experiment_id, this.params.step_id);
        if (errors != null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experiments.createStep(this.params.experiment_id, stepArgs, this.reqctx.user.id);
            if (rv.error) {
                this.status = status.NOT_ACCEPTABLE;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* validateCreateStepArgs(stepArgs, projectID, experimentID, stepID) {
        let errors = yield schema.validate(schema.createExperimentStep, stepArgs);
        if (errors !== null) {
            return errors;
        }

        let isInProject = yield experiments.experimentExistsInProject(projectID, experimentID);
        if (!isInProject) {
            return {error: 'Unknown experiment'};
        }

        if (stepID) {
            let isValidStepId = yield experiments.experimentStepExistsInExperiment(experimentID, stepID);
            if (!isValidStepId) {
                return {error: 'Unknown experiment step'};
            }
            stepArgs.parent_id = stepID;
        }

        return null;
    }

    function* updateStep(next) {
        yield next;
    }

    function* update(next) {
        let updateArgs = yield parse(this);
        let errors = yield validateUpdateExperimentArgs(updateArgs, this.params.project_id, this.params.experiment_id);
        if (errors != null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experiments.update();
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = errors;
            } else {<commit_msg>Pass in the experiment id and the update arguments.<commit_after>module.exports = function(experiments, schema) {
    const parse = require('co-body');
    const status = require('http-status');

    return {
        getAllForProject,
        get,
        getStep,
        createStep,
        updateStep,
        update,
        create
    };

    function* getAllForProject(next) {
        let rv = yield experiments.getAllForProject(this.params.project_id);
        if (rv.error) {
            this.body = rv;
            this.status = status.NOT_FOUND;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* get(next) {
        let isInProject = yield experiments.experimentExistsInProject(this.params.project_id, this.params.experiment_id);
        if (!isInProject) {
            this.status = status.NOT_FOUND;
        } else {
            let rv = yield experiments.get(this.params.experiment_id);
            if (rv.error) {
                this.body = rv;
                this.status = status.NOT_FOUND;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* getStep(next) {
        let isValidStep = yield validateStep(this.params.project_id, this.params.experiment_id, this.params.step_id);
        if (!isValidStep) {
            this.status = status.NOT_FOUND;
            this.body = {error: 'Step not found'};
        }
        let rv = yield experiments.getStep(this.params.step_id);
        if (rv.error) {
            this.body = rv;
            this.status = status.NOT_FOUND;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* validateStep(projectID, experimentID, stepID) {
        let isInProject = yield experiments.experimentExistsInProject(projectID, experimentID);
        if (!isInProject) {
            return false;
        }

        return yield experiments.experimentStepExistsInExperiment(experimentID, stepID);
    }

    function* createStep(next) {
        let stepArgs = yield parse(this);
        schema.prepare(schema.createExperimentStep, stepArgs);
        let errors = yield validateCreateStepArgs(stepArgs, this.params.project_id,
            this.params.experiment_id, this.params.step_id);
        if (errors != null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experiments.createStep(this.params.experiment_id, stepArgs, this.reqctx.user.id);
            if (rv.error) {
                this.status = status.NOT_ACCEPTABLE;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* validateCreateStepArgs(stepArgs, projectID, experimentID, stepID) {
        let errors = yield schema.validate(schema.createExperimentStep, stepArgs);
        if (errors !== null) {
            return errors;
        }

        let isInProject = yield experiments.experimentExistsInProject(projectID, experimentID);
        if (!isInProject) {
            return {error: 'Unknown experiment'};
        }

        if (stepID) {
            let isValidStepId = yield experiments.experimentStepExistsInExperiment(experimentID, stepID);
            if (!isValidStepId) {
                return {error: 'Unknown experiment step'};
            }
            stepArgs.parent_id = stepID;
        }

        return null;
    }

    function* updateStep(next) {
        yield next;
    }

    function* update(next) {
        let updateArgs = yield parse(this);
        let errors = yield validateUpdateExperimentArgs(updateArgs, this.params.project_id, this.params.experiment_id);
        if (errors != null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experiments.update(this.params.experiment_id, updateArgs);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = errors;
            } else {
{'fraction_non_alphanumeric': 0.09788654060066741, 'fraction_numerical': 0.0, 'mean_word_length': 1.8048526863084922, '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': '6526073', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2214, 'n_words': 467}