<commit_before>'use strict';

const globalVariables = require('../../lib/utils/global-variables.js');
const express = require('express');
const consts = require('../../lib/consts/consts.js');
const _ = require('lodash');
const config = require('config');
const validator = require('validator');
const async = require('async');
const mongodb = require('mongodb');
const router = express.Router();

// Get a specific action type
router.get('/action-type/:appid/:_id', validate, handleMoreFileds, function(req, res, next) {
    const appid = req.params.appid;
    const _id = req.params._id;

    const query = {
        _appid: appid,
        _id: new mongodb.ObjectID(_id)
    };

    const projection = {};
    const defaultField = [
        'name',
        '_id',
    ];
    defaultField.concat(req.moreFields).forEach(v => {
        projection[v] = 1;
    });

    const options = {

    };

    const collection = config.mongod.prefix + consts.ACTION_TYPE_COLLECTION;

    globalVariables
        .get('db')
        .collection(collection)
        .findOne(query, projection, options, (err, r) => {
            if(err) {
                return next(err);
            }

            console.log(r);
            res.json({
                ec: consts.CODE.SUCCESS,
                data: r
            });
        });
});

// Middleware for checking data
function validate(req, res, next) {
    console.log(req.params, req.query);

    const _id = req.params._id;

    if(!validator.isMongoId(_id)) {
        res.json({
            ec: consts.CODE.WRONG_PARAM,
            reason: "_id must be an object id"
        });

        return next('route');
    }

    next();
}

function handleMoreFileds(req, res, next) {
    let moreFields = [];
    let extras = req.query.extras;
    if(!_.isUndefined(extras)) {
        let temp = extras.split(',');
        temp.forEach(v => {
            if(validator.isLength(v, 1)) {
                moreFields.push(v);
            }
        });
    }

    req.moreFields = moreFields;
    next();
}

module.exports = function (app) {
    app.use('/', router);
};<commit_msg>add get detail action type<commit_after>'use strict';

const globalVariables = require('../../lib/utils/global-variables.js');
const express = require('express');
const consts = require('../../lib/consts/consts.js');
const _ = require('lodash');
const config = require('config');
const validator = require('validator');
const async = require('async');
const mongodb = require('mongodb');
const router = express.Router();
let util = require('util');

let allFields = [
    '_id',
    'codename',
    'name',
    'desc',
    'deftrendfields',
    'deftrendobjects',
    'fields',
    '_appid',
    '_ctime'
];

let defaultFields = [
    'fields'
];


// Get a specific action type
router.get('/action-type/:_appid/:_typeid',
    validate,
    getConverter,
    handleMoreFileds,
    function(req, res, next) {
        let _appid = req.params._appid;
        let _typeid = req.params._typeid;
        let collection = config.mongod.prefix + consts.ACTION_TYPE_COLLECTION;

        let query = {
            [req.meotrics_converters['_appid']]: _appid,
            [req.meotrics_converters['codename']]: _typeid
        };

        let projection = {};
        req.meotrics_moreFields.forEach(field => {
            projection[req.meotrics_converters[field]] = 1;
        });

        let options = {};

        globalVariables
            .get('db')
            .collection(collection)
            .findOne(query, projection, options, (err, result) => {
                if(err) {
                    return res.json({
                        ec: consts.CODE.ERROR,
                        error: util.inspect(err)
                    });
                }

                res.json({
                    ec: consts.CODE.SUCCESS,
                    data: result
                });
            });
    });

// Middleware for checking data
function validate(req, res, next) {
    return next();
}

function getConverter(req, res, next) {
    globalVariables.get('converter').toIDs(allFields, ids => {
        req.meotrics_converters = ids;
        return next();
    });
}

function handleMoreFileds(req, res, next) {
    let more = req.query.more || '';
    let moreFields = more.split(',');

    let temp = [];
    moreFields.forEach(field => {
        if( (allFields.indexOf(field) !== -1) && (defaultFields.indexOf(field) === -1) ) {
            temp.push(field);
        }
    });

    req.meotrics_moreFields = temp.concat(defaultFields);

    return next();
}

module.exports = function (app) {
    app.use('/', router);
};
{'fraction_non_alphanumeric': 0.15080906148867315, 'fraction_numerical': 0.0010787486515641855, 'mean_word_length': 2.11978465679677, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, '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': '6067719', 'n_tokens_mistral': 1418, 'n_tokens_neox': 1379, 'n_words': 288}