<commit_before>/**
 * @file process/controllers/CommandsController.cpp
 * @author <NAME>
 * @date Created <2010-06-01 10:26:16>
 */
#include "CommandsController.h"
#include "CollectionHandler.h"
#include <common/JobScheduler.h>
#include <process/common/XmlConfigParser.h>
#include <process/common/CollectionManager.h>
#include <bundles/index/IndexTaskService.h>
#include <bundles/index/IndexSearchService.h>
#include <bundles/mining/MiningSearchService.h>
#include <core/mining-manager/MiningManager.h>
#include <bundles/recommend/RecommendTaskService.h>
#include <aggregator-manager/SearchAggregator.h>
#include <mining-manager/MiningQueryLogHandler.h>
#include <node-manager/DistributeRequestHooker.h>
#include <common/Keys.h>

namespace sf1r
{

using driver::Keys;

using namespace izenelib::driver;
using namespace izenelib::osgi;

CommandsController::CommandsController()
    : Sf1Controller(false)
{
}

/**
 * @brief Action \b index. Sends command "index" to SIA to ask it start index
 * SCD files.
 *
 * @section request
 *
 * - @b collection* (@c String): Collection name.
 * - @b document_count (@c Uint = 0): For developer. Index at most \a document_count
 *   documents in each SCD files. 0 indicates no limit.
 *
 * @section response
 *
 * No extra fields.
 *
 * @section example
 *
 * Request
 * @code
 * {
 *   "collection": "ChnWiki",
 *   "document_count": 1000
 * }
 * @endcode
 */
void CommandsController::index()
{
    IZENELIB_DRIVER_BEFORE_HOOK(checkCollectionName());

    indexSearch_();
    indexRecommend_();
}

void CommandsController::indexSearch_()
{
    IndexTaskService* taskService = collectionHandler_->indexTaskService_;
    if (taskService)
    {
        if (request().callType() != Request::FromAPI)
        {
            RecommendTaskService* rec_taskService = collectionHandler_->recommendTaskService_;
            if (rec_taskService)
            {
                if(!DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainBegin))
                {
                    std::cout << "set chain status failed, stopping chain. current status: " <<
                        DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                    return;
                }
            }
        }
        // 0 indicates no limit
        Value::UintType documentCount = asUint(request()[Keys::document_count]);

        taskService->index(documentCount);
    }
}

void CommandsController::indexRecommend_()
{
    RecommendTaskService* taskService = collectionHandler_->recommendTaskService_;
    if (taskService)
    {
        if (request().callType() == Request::FromLog)
        {
            if (!DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainEnd))
            {
                std::cout << "set chain status failed, stopping chain. current status: " <<
                   DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                return;
            }
            taskService->buildCollection();
        }
        else if (request().callType() != Request::FromAPI)
        {
            JobScheduler::get()->waitCurrentFinish(collectionName_);
            if(DistributeRequestHooker::get()->getChainStatus() != DistributeRequestHooker::ChainBegin || 
			    !DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainEnd))
            {
                std::cout << "set chain status failed, stopping chain. current status: " <<
                   DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                return;
            }
        }
        task_type task = boost::bind(&RecommendTaskService::buildCollection, taskService);
        JobScheduler::get()->addTask(task, collectionName_);
    }
}

/**
 * @brief Action \b index_query_log. Sends command "index_query_log" to rebuild all query related features.
 *
 * @section request
 *
 *
 * @section response
 *
 * No extra fields.
 *
 **/
void CommandsController::index_query_log()<commit_msg>tiny fix for redo log<commit_after>/**
 * @file process/controllers/CommandsController.cpp
 * @author <NAME>
 * @date Created <2010-06-01 10:26:16>
 */
#include "CommandsController.h"
#include "CollectionHandler.h"
#include <common/JobScheduler.h>
#include <process/common/XmlConfigParser.h>
#include <process/common/CollectionManager.h>
#include <bundles/index/IndexTaskService.h>
#include <bundles/index/IndexSearchService.h>
#include <bundles/mining/MiningSearchService.h>
#include <core/mining-manager/MiningManager.h>
#include <bundles/recommend/RecommendTaskService.h>
#include <aggregator-manager/SearchAggregator.h>
#include <mining-manager/MiningQueryLogHandler.h>
#include <node-manager/DistributeRequestHooker.h>
#include <common/Keys.h>

namespace sf1r
{

using driver::Keys;

using namespace izenelib::driver;
using namespace izenelib::osgi;

CommandsController::CommandsController()
    : Sf1Controller(false)
{
}

/**
 * @brief Action \b index. Sends command "index" to SIA to ask it start index
 * SCD files.
 *
 * @section request
 *
 * - @b collection* (@c String): Collection name.
 * - @b document_count (@c Uint = 0): For developer. Index at most \a document_count
 *   documents in each SCD files. 0 indicates no limit.
 *
 * @section response
 *
 * No extra fields.
 *
 * @section example
 *
 * Request
 * @code
 * {
 *   "collection": "ChnWiki",
 *   "document_count": 1000
 * }
 * @endcode
 */
void CommandsController::index()
{
    IZENELIB_DRIVER_BEFORE_HOOK(checkCollectionName());

    indexSearch_();
    indexRecommend_();
}

void CommandsController::indexSearch_()
{
    IndexTaskService* taskService = collectionHandler_->indexTaskService_;
    if (taskService)
    {
        if (request().callType() != Request::FromAPI)
        {
            RecommendTaskService* rec_taskService = collectionHandler_->recommendTaskService_;
            if (rec_taskService)
            {
                if(!DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainBegin))
                {
                    std::cout << "set chain status failed, stopping chain. current status: " <<
                        DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                    return;
                }
            }
        }
        // 0 indicates no limit
        Value::UintType documentCount = asUint(request()[Keys::document_count]);

        taskService->index(documentCount);
    }
}

void CommandsController::indexRecommend_()
{
    RecommendTaskService* taskService = collectionHandler_->recommendTaskService_;
    if (taskService)
    {
        if (request().callType() == Request::FromLog)
        {
            if (!DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainEnd))
            {
                std::cout << "set chain status failed, stopping chain. current status: " <<
                   DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                return;
            }
            taskService->buildCollection();
		return;
        }
        else if (request().callType() != Request::FromAPI)
        {
            JobScheduler::get()->waitCurrentFinish(collectionName_);
            if(DistributeRequestHooker::get()->getChainStatus() != DistributeRequestHooker::ChainBegin || 
			    !DistributeRequestHooker::get()->setChainStatus(DistributeRequestHooker::ChainEnd))
            {
                std::cout << "set chain status failed, stopping chain. current status: " <<
                   DistributeRequestHooker::get()->getChainStatus() << std::endl; 
                return;
            }
        }
        task_type task = boost::bind(&RecommendTaskService::buildCollection, taskService);
        JobScheduler::get()->addTask(task, collectionName_);
    }
}

/**
 * @brief Action \b index_query_log. Sends command "index_query_log" to rebuild all query related features.
 *
 * @section request
 *
 *
 * @section response
 *
 * No extra fields.
 *
 **/
void CommandsController::index_query_log()
{'fraction_non_alphanumeric': 0.125, 'fraction_numerical': 0.0057213930348258705, 'mean_word_length': 3.073454913880446, 'pattern_counts': {'":': 4, '<': 67, '<?xml version=': 0, '>': 59, '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': '6882149', 'n_tokens_mistral': 2347, 'n_tokens_neox': 2251, 'n_words': 448}