<commit_before>/*

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      This file is part of the Smart Developer Hub Project:
        http://www.smartdeveloperhub.org/
      Center for Open Middleware
            http://www.centeropenmiddleware.com/
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Copyright (C) 2015 Center for Open Middleware.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
                http://www.apache.org/licenses/LICENSE-2.0
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
     limitations under the License.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
*/
'use strict';

var Promise = require("bluebird");

// Do not silently capture errors
Promise.onPossiblyUnhandledRejection(function(error){
    throw error;
});

module.exports = function(botID, sdhApiUrl, sdhDashboardUrl, log) {

    var core = {};

    var init = function() {

        var validUrl = require('valid-url');

        if (!validUrl.isUri(sdhApiUrl)){
            log.error("Not valid SDH_API_URL: " + sdhApiUrl);
            process.exit(0);
        }

        if (!validUrl.isUri(sdhDashboardUrl)){
            log.error("Not valid SDH_DASHBOARD_URL: " + sdhDashboardUrl);
            process.exit(0);
        }

        var loadStartDate = new Date();

        core.id = botID;
        GLOBAL.botId = botID;
        GLOBAL.sdhProjectsByID = {};
        GLOBAL.sdhProjectsByName = {};
        GLOBAL.sdhReposByID = {};
        GLOBAL.sdhReposByName = {};
        GLOBAL.sdhReposByName = {};
        GLOBAL.sdhProductsByID = {};
        GLOBAL.sdhProductsByName = {};
        GLOBAL.sdhUsersByID = {};
        GLOBAL.sdhOrganizationsByID = {};

        log.info("... Loading SDH BOT CORE ...");

        // Load methods to access SDH data
        core.data = require('./brain/sdhBasic.js')(sdhApiUrl, log);

        // Load core operations into the core object
        core.ops = require('./brain/operations.js')(core, log);

        // Export directive functions
        var directives = require('./brain/directives.js')(log);
        for (var meth in directives) {
            core[meth] = directives[meth];
        }

        return Promise.promisify(preloadEntityIds)();

    };

    // Shut down function
    var gracefullyShuttinDown = function gracefullyShuttinDown() {
        log.warn('Shut down signal Received ');
        log.warn(" ! Shutting Down SDH-API manually.");
        setTimeout(function () {
            process.exit(0);
        }, 500);
    };

    var preloadEntityIds = function preloadEntityIds(cb) {

        var rcounter = 0;
        var endLoad = function endLoad() {

            rcounter++;
            if (rcounter == 5) {
                cb(null, core);
            } else if(rcounter > 5) {
                log.debug('Concurr error sdhbot.preloadEntityIds');
            }
        };
        //Members
        core.data.getSDHMembers(function (err, members) {
            for (var i = 0; i < members.length; i++) {
                sdhUsersByID[members[i].userid] = members[i];
            }
            endLoad()
        });
        //Repositories
        core.data.getSDHRepositories(function (err, rep) {
            for (var i = 0; i < rep.length; i++) {
                sdhReposByID[rep[i].repositoryid] = rep[i];
                sdhReposByName[rep[i].name] = rep[i];
            }
            endLoad()
        });
        //Products
        core.data.getSDHProducts(function (err, prod) {
            for (var i = 0; i < prod.length; i++) {
                sdhProductsByID[prod[i].productid] = prod[i];
            }
            endLoad()
        });
        //Projects
        core.data.getSDHProjects(function (err, proj) {
            for (var i = 0; i < proj.length; i++) {
                sdhProjectsByID[proj[i].projectid] = proj[i];
            }
            endLoad()
        });
        //Organizations
        core.data.getSDHOrganizations(function (err, org) {
            for (var i = 0; i < org.length; i++) {
                // By the moment there are no organizationid in the unique Organization info in SDH
                //sdhOrganizationsByID[org[i].organizationid] = org[i];
                sdhOrganizationsByID[org[i].title] = org[i];
            }
            endLoad()
        });
    };

    var startESindex = function startESindex() {
        //TODO
        var sdhUsersByID_ = {};
        var cleanIndex = [
            elastic.indexExists("users").then(function (exists) {
                if (exists) {<commit_msg>Use Promises to load entities.<commit_after>/*

    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      This file is part of the Smart Developer Hub Project:
        http://www.smartdeveloperhub.org/
      Center for Open Middleware
            http://www.centeropenmiddleware.com/
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Copyright (C) 2015 Center for Open Middleware.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
                http://www.apache.org/licenses/LICENSE-2.0
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
     limitations under the License.
    #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
*/
'use strict';

var Promise = require("bluebird");

// Do not silently capture errors
Promise.onPossiblyUnhandledRejection(function(error){
    throw error;
});

module.exports = function(botID, sdhApiUrl, sdhDashboardUrl, log) {

    var core = {};

    var init = function() {

        var validUrl = require('valid-url');

        if (!validUrl.isUri(sdhApiUrl)){
            log.error("Not valid SDH_API_URL: " + sdhApiUrl);
            process.exit(0);
        }

        if (!validUrl.isUri(sdhDashboardUrl)){
            log.error("Not valid SDH_DASHBOARD_URL: " + sdhDashboardUrl);
            process.exit(0);
        }

        var loadStartDate = new Date();

        core.id = botID;
        GLOBAL.botId = botID;
        GLOBAL.sdhProjectsByID = {};
        GLOBAL.sdhProjectsByName = {};
        GLOBAL.sdhReposByID = {};
        GLOBAL.sdhReposByName = {};
        GLOBAL.sdhReposByName = {};
        GLOBAL.sdhProductsByID = {};
        GLOBAL.sdhProductsByName = {};
        GLOBAL.sdhUsersByID = {};
        GLOBAL.sdhOrganizationsByID = {};

        log.info("... Loading SDH BOT CORE ...");

        // Load methods to access SDH data
        core.data = require('./brain/sdhBasic.js')(sdhApiUrl, log);

        // Load core operations into the core object
        core.ops = require('./brain/operations.js')(core, log);

        // Export directive functions
        var directives = require('./brain/directives.js')(log);
        for (var meth in directives) {
            core[meth] = directives[meth];
        }

        return preloadEntityIds().then(function() {
            return core;
        });

    };

    // Shut down function
    var gracefullyShuttinDown = function gracefullyShuttinDown() {
        log.warn('Shut down signal Received ');
        log.warn(" ! Shutting Down SDH-API manually.");
        setTimeout(function () {
            process.exit(0);
        }, 500);
    };

    var preloadEntityIds = function preloadEntityIds(cb) {

        // COnvert methods to promises
        var sdhData = Promise.promisifyAll(core.data);

        var loadPromises = [
            //Members
            sdhData.getSDHMembersAsync().then(function (members) {
                for (var i = 0; i < members.length; i++) {
                    sdhUsersByID[members[i].userid] = members[i];
                }
            }),
            //Repositories
            sdhData.getSDHRepositoriesAsync().then(function (rep) {
                for (var i = 0; i < rep.length; i++) {
                    sdhReposByID[rep[i].repositoryid] = rep[i];
                    sdhReposByName[rep[i].name] = rep[i];
                }
            }),
            //Products
            sdhData.getSDHProductsAsync().then(function (prod) {
                for (var i = 0; i < prod.length; i++) {
                    sdhProductsByID[prod[i].productid] = prod[i];
                }
            }),
            //Projects
            sdhData.getSDHProjectsAsync().then(function (proj) {
                for (var i = 0; i < proj.length; i++) {
                    sdhProjectsByID[proj[i].projectid] = proj[i];
                }
            }),
            //Organizations
            sdhData.getSDHOrganizationsAsync().then(function (org) {
                for (var i = 0; i < org.length; i++) {
                    // By the moment there are no organizationid in the unique Organization info in SDH
                    //sdhOrganizationsByID[org[i].organizationid] = org[i];
                    sdhOrganizationsByID[org[i].title] = org[i];
                }
            })
        ];

        return Promise.all(loadPromises);

    };

    var startESindex = function startESindex() {
        //TODO
        var sdhUsersByID_ = {};
        var cleanIndex = [
            elastic.indexExists("users").then(function (exists) {
                if (exists) {
{'fraction_non_alphanumeric': 0.16767237115441685, 'fraction_numerical': 0.0040557918686319125, 'mean_word_length': 1.7683461117196058, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2420578', 'n_tokens_mistral': 3126, 'n_tokens_neox': 2843, 'n_words': 670}