<commit_before>var create = require('./create');
var common = require('./common');
var download = require('./download');
var util = require('util');

var attributes;

var listMap = {
    app: function(name,cb) {
        if(!common.global.argv.attributes) {
            // special defaults so taskletId is included
            attributes = ['targetType','taskletId','name','lastModifiedAt'];
        }        
        if(name) {
            common.global.session.directive({
                "op": "SELECT",
                "targetType": "CdmTasklet",
                "where": {"taskletId": name},
                "options": {"limit": 1000, "sort":{"name":1}}
            },function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,["taskletId","name","description"]);
                cb();
            });

        } else {
            var query = {
                "op": "SELECT",
                "targetType": "CdmTasklet",
                "where": {},
                "properties": attributes,
                "options": {"limit": 1000, "sort":{"name":1} }
            };
            if(!common.global.argv.includeGlobal) {
                query.where.createdBy = {"$ne":"mms"};
            }
            common.global.session.directive(query,function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,attributes);
                cb();
            });
        }
    },
    externalSystem: function(name,cb) {

        common.global.session.directive({
            "op": "SELECT",
            "targetType": "CdmExternalSystem",
            "properties": attributes,
            "options": {"limit": 1000, "sort":{"name":1} }
        },function(err,res) {
            if(err) common.exit(err.message);
            formatOutput(res.results,attributes);
            cb();
        });
    },
    model: function(name,cb) {
        if(name) {
            var operation = null;
            var s = name.split('.');
            if(s[1]) {
                name = s[0];
                operation = s[1];
            }

            if(operation) {
                common.global.session.directive({
                    "op": "SELECT",
                    "targetType": "CdmOperation",
                    "where":{ "objectType": name, "name": operation },
                    "properties": [],
                    "options": {"limit": 1000, "sort":{"name":1} }
                },function(err,res) {
                    if(err) common.exit(err.message);
                    //console.log(util.inspect(res.results,false,null));
                    formatOutput(res.results,attributes);
                    cb();
                });
            } else {
                common.global.session.directive({
                    "op": "SELECT",
                    "targetType": "CdmProperty",
                    "where":{ "containingType": name },
                    "properties": attributes,
                    "options": {"limit": 1000, "sort":{"name":1} }
                },function(err,res) {
                    if(err) common.exit(err.message);
                    formatOutput(res.results,attributes);

                    console.log(); // blank space between properties and operations

                    common.global.session.directive({
                        "op": "SELECT",
                        "targetType": "CdmOperation",
                        "where":{ "objectType": name, "sort":{"name":1} },
                        "properties": attributes,
                        "options": {"limit": 1000}
                    },function(err,res) {
                        if(err) common.exit(err.message);
                        formatOutput(res.results,attributes);
                        cb();
                    });

                    cb();
                });
            }
        } else {
            var query = {
                "op": "SELECT",
                "targetType": "CdmType",
                "where" : {},
                "properties": attributes,
                "options": {"limit": 1000, "sort":{"name":1} }
            };
            if(!common.global.argv.includeGlobal) {
                query.where.createdBy = {"$ne":"mms"};
            }
            common.global.session.directive(query,function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,attributes);
                cb();
            });

        }
    },
    proxy: function(name,cb) { 
        listResource("^(\\/integrationPackage\\/).+",cb);
    },
    resource: function(name,cb) {
        listResource("^(?!\\/tasklets\\/).+",cb);
    },
    sample: function(name,cb) {
        listResource("^(\\/samples\\/).+",cb);
    },
    template: function(name,cb) {
        console.log("\nname\n----------");
        create.listTemplates(cb); 
    }
};

function listResource(regex,cb) {
    var query = {
        "op": "SELECT",
        "targetType": "CdmResource",
        "properties": attributes,
        "where": {"name": {"$regex": regex}},
        "options": {"limit": 1000, "sort":"name"}
    };
    if(!common.global.argv.includeGlobal) {
        query.where.createdBy = {"$ne":"mms"};
    }
    common.global.session.directive(query,function(err,res) {
        if(err) common.exit(err.message);
        formatOutput(res.results,attributes);
        cb();
    });   
}


var space60 = "                                                            ";<commit_msg>Fix "emote list proxy broken"<commit_after>var create = require('./create');
var common = require('./common');
var download = require('./download');
var util = require('util');

var attributes;

var listMap = {
    app: function(name,cb) {
        if(!common.global.argv.attributes) {
            // special defaults so taskletId is included
            attributes = ['targetType','taskletId','name','lastModifiedAt'];
        }        
        if(name) {
            common.global.session.directive({
                "op": "SELECT",
                "targetType": "CdmTasklet",
                "where": {"taskletId": name},
                "options": {"limit": 1000, "sort":{"name":1}}
            },function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,["taskletId","name","description"]);
                cb();
            });

        } else {
            var query = {
                "op": "SELECT",
                "targetType": "CdmTasklet",
                "where": {},
                "properties": attributes,
                "options": {"limit": 1000, "sort":{"name":1} }
            };
            if(!common.global.argv.includeGlobal) {
                query.where.createdBy = {"$ne":"mms"};
            }
            common.global.session.directive(query,function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,attributes);
                cb();
            });
        }
    },
    externalSystem: function(name,cb) {

        common.global.session.directive({
            "op": "SELECT",
            "targetType": "CdmExternalSystem",
            "properties": attributes,
            "options": {"limit": 1000, "sort":{"name":1} }
        },function(err,res) {
            if(err) common.exit(err.message);
            formatOutput(res.results,attributes);
            cb();
        });
    },
    model: function(name,cb) {
        if(name) {
            var operation = null;
            var s = name.split('.');
            if(s[1]) {
                name = s[0];
                operation = s[1];
            }

            if(operation) {
                common.global.session.directive({
                    "op": "SELECT",
                    "targetType": "CdmOperation",
                    "where":{ "objectType": name, "name": operation },
                    "properties": [],
                    "options": {"limit": 1000, "sort":{"name":1} }
                },function(err,res) {
                    if(err) common.exit(err.message);
                    //console.log(util.inspect(res.results,false,null));
                    formatOutput(res.results,attributes);
                    cb();
                });
            } else {
                common.global.session.directive({
                    "op": "SELECT",
                    "targetType": "CdmProperty",
                    "where":{ "containingType": name },
                    "properties": attributes,
                    "options": {"limit": 1000, "sort":{"name":1} }
                },function(err,res) {
                    if(err) common.exit(err.message);
                    formatOutput(res.results,attributes);

                    console.log(); // blank space between properties and operations

                    common.global.session.directive({
                        "op": "SELECT",
                        "targetType": "CdmOperation",
                        "where":{ "objectType": name, "sort":{"name":1} },
                        "properties": attributes,
                        "options": {"limit": 1000}
                    },function(err,res) {
                        if(err) common.exit(err.message);
                        formatOutput(res.results,attributes);
                        cb();
                    });

                    cb();
                });
            }
        } else {
            var query = {
                "op": "SELECT",
                "targetType": "CdmType",
                "where" : {},
                "properties": attributes,
                "options": {"limit": 1000, "sort":{"name":1} }
            };
            if(!common.global.argv.includeGlobal) {
                query.where.createdBy = {"$ne":"mms"};
            }
            common.global.session.directive(query,function(err,res) {
                if(err) common.exit(err.message);
                formatOutput(res.results,attributes);
                cb();
            });

        }
    },
    proxy: function(name,cb) { 
        listResource("^(\\/integrationPackage\\/).+",cb);
    },
    resource: function(name,cb) {
        listResource("^(?!\\/tasklets\\/).+",cb);
    },
    sample: function(name,cb) {
        listResource("^(\\/samples\\/).+",cb);
    },
    template: function(name,cb) {
        console.log("\nname\n----------");
        create.listTemplates(cb); 
    }
};

function listResource(regex,cb) {
    var query = {
        "op": "SELECT",
        "targetType": "CdmResource",
        "properties": attributes,
        "where": {"name": {"$regex": regex}},
        "options": {"limit": 1000, "sort":{"name":1}}
    };
    if(!common.global.argv.includeGlobal) {
        query.where.createdBy = {"$ne":"mms"};
    }
    common.global.session.directive(query,function(err,res) {
        if(err) common.exit(err.message);
        formatOutput(res.results,attributes);
        cb();
    });   
}


var space60 = "                                                            ";
{'fraction_non_alphanumeric': 0.1613282663771263, 'fraction_numerical': 0.008052841114730366, 'mean_word_length': 1.3805729054490632, 'pattern_counts': {'":': 141, '<': 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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '285493', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2793, 'n_words': 464}