acd_data_collection.cpp 25.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
// acd_data_collection.cpp: implement AcdDataCollection

#include <json/json.h>
#include "acd_data_collection.h"
#include "acd_tool.h"

#define CHECK_CONF(name, type) \
    if (name.is_null()) { \
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,\
            "not found %s", #name);\
        return -1; \
    } else if (name.get_type() != type) { \
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,\
            "type is equal, need(%d), given(%d)", type, name.get_type());\
        return -1; \
    }

namespace acd {
void AcdCallDataCollection::initialize(const char* path)
{
    reset();
    if (path ==  NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "data cached path is NULL");
        return;
    }
    _cached_file = path;
    _cached_date = bgcc::TimeUtil::get_date();
    _cached_second = bgcc::TimeUtil::get_timestamp_s();
    get_data_from_file();
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "data collection has initialize");
}

void AcdCallDataCollection::unInitialize()
{
    reset();
}

int32_t AcdCallDataCollection::get_data_from_file()
{
    string filename = _cached_file;
    bgcc2::ConfUnit root;
    if(!bgcc2::ConfLoader::load_conf(filename.c_str(), root)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "open cached file failed ,path(%s)", filename.c_str());
        return -1;
    }
    acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
        "begin load =========================================");    

    // load date
    bgcc2::ConfUnit& section = root["Date"];
    if(section.is_null()) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "not find date section");
        return -1;
    }
    bgcc2::ConfUnit& date = section["date"];
    CHECK_CONF(date, bgcc::ConfUnit::UT_STRING);
    string cacheddate = date.to_string();
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|Cached Date  | %s", cacheddate.c_str());

    // check is today?
    string curdate = bgcc::TimeUtil::get_date();
    if (curdate != cacheddate) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "data is not this day, cacheddate(%s), curdate(%s)", cacheddate.c_str(), curdate.c_str());
        return -1;
    }

    // load cc plat info
    bgcc2::ConfUnit& section1 = root["PlatCallData"];
    if(section1.is_null()) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "not find platcalldata section");
        return -1;
    }

    bgcc2::ConfUnit& out_call_num= section1["OUTCALLNUM"];
    bgcc2::ConfUnit& out_ans_num= section1["OUTANSNUM"];
    bgcc2::ConfUnit& out_call_time= section1["OUTCALLTIME"];
    bgcc2::ConfUnit& in_call_time= section1["INCALLTIME"];
    bgcc2::ConfUnit& in_alert_time= section1["INALERTTIME"];
    bgcc2::ConfUnit& in_ans_num= section1["INANWNUM"];
    bgcc2::ConfUnit& in_call_num= section1["INCALLNUM"];
    CHECK_CONF(out_call_num, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(out_ans_num, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(out_call_time, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(in_call_time, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(in_alert_time, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(in_ans_num, bgcc::ConfUnit::UT_STRING);
    CHECK_CONF(in_call_num, bgcc::ConfUnit::UT_STRING);
    if (!bgcc::StringUtil::str2uint32(out_call_num.to_string().c_str(), _cc_call_data.outbound_call_num)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse outcallnum(%s) failed", out_call_num.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(out_ans_num.to_string().c_str(), _cc_call_data.outbound_ans_num)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse outansnum(%s) failed", out_ans_num.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(out_call_time.to_string().c_str(), _cc_call_data.outbound_call_time)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse outcalltime(%s) failed", out_call_time.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(in_call_time.to_string().c_str(), _cc_call_data.inbound_call_time)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse incalltime(%s) failed", in_call_time.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(in_alert_time.to_string().c_str(), _cc_call_data.inbound_alerting_time)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse inalerttime(%s) failed", in_alert_time.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(in_ans_num.to_string().c_str(), _cc_call_data.inbound_ans_num)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse inansnum(%s) failed", in_ans_num.to_string().c_str());
        return -1;
    }
    if (!bgcc::StringUtil::str2uint32(in_call_num.to_string().c_str(), _cc_call_data.inbound_call_num)) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "parse incallnum(%s) failed", out_call_num.to_string().c_str());
        return -1;
    }
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|OUTCALLNUM  | %d", _cc_call_data.outbound_call_num);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|OUTANSNUM  | %d", _cc_call_data.outbound_ans_num);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|OUTCALLTIME  | %d", _cc_call_data.outbound_call_time);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|INCALLTIME  | %d", _cc_call_data.inbound_call_time);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|INALERTTIME  | %d", _cc_call_data.inbound_alerting_time);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|INANSNUM  | %d", _cc_call_data.inbound_ans_num);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "|INCALLNUM  | %d", _cc_call_data.inbound_call_num);

    //begin load skill info
    bgcc2::ConfUnit& section3 = root["SkillCallData"];
    if(section3.is_null()) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "not found SkillCallData section");
        return -1;
    }

    for(bgcc2::ConfUnit::const_iterator itr = section3.begin(); itr != section3.end(); ++itr) {
        bgcc2::ConfUnit& skill = **itr;
        bgcc2::ConfUnit& skillname = skill["SkillName"];
        bgcc2::ConfUnit& calltime = skill["InCallTime"];
        bgcc2::ConfUnit& alerttime = skill["InAlertTime"];
        bgcc2::ConfUnit& ansnum = skill["InAnsNum"];
        bgcc2::ConfUnit& callnum = skill["InCallNum"];
        CHECK_CONF(skillname, bgcc::ConfUnit::UT_STRING);
        CHECK_CONF(calltime, bgcc::ConfUnit::UT_STRING);
        CHECK_CONF(alerttime, bgcc::ConfUnit::UT_STRING);
        CHECK_CONF(ansnum, bgcc::ConfUnit::UT_STRING);
        SkillCallData* p = new (std::nothrow) SkillCallData;
	    if (p == NULL) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                "new skillinfo failed");
            return -1;            
	    }
        if (!bgcc::StringUtil::str2uint32(calltime.to_string().c_str(), p->inbound_call_time)) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                "parse skill calltime(%s) failed", calltime.to_string().c_str());
            return -1;
        }
        if (!bgcc::StringUtil::str2uint32(alerttime.to_string().c_str(), p->inbound_alerting_time)) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                "parse skill alerttime(%s) failed", alerttime.to_string().c_str());
            return -1;
        }
        if (!bgcc::StringUtil::str2uint32(callnum.to_string().c_str(), p->inbound_call_num)) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                "parse skill callnum(%s) failed", callnum.to_string().c_str());
            return -1;
        }
        if (!bgcc::StringUtil::str2uint32(ansnum.to_string().c_str(), p->inbound_answer_num)) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                "parse skill ansnum(%s) failed", ansnum.to_string().c_str());
            return -1;
        }
        _skill_call_info.insert(std::make_pair(skillname.to_string(), p));
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "|SKILLNAME  | %s", skillname.to_string().c_str());
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "        |CALLTIME | %d", p->inbound_call_time);
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "        |ALERTTIME  | %d", p->inbound_alerting_time);
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "        |CALLNUM | %d", p->inbound_call_num);
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "        |ANSNUM | %d", p->inbound_answer_num);
    } 
    acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
        "begin end =========================================");   
    return 0;
}

int32_t AcdCallDataCollection::put_data_to_file()
{
    std::ofstream out(_cached_file.c_str(), std::ios::out);
    if (!out.is_open()){
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "open cached file failed ,path(%s)", _cached_file.c_str());
        return -1;
    }
    // put date
    string curdate = bgcc::TimeUtil::get_date();
    out << "[Date]" << "\n";
    out << "date = " << curdate << "\n";

    // put plat call data
    out << "[PlatCallData]" << "\n";
    out << "OUTCALLNUM = " << _cc_call_data.outbound_call_num << "\n";
    out << "OUTANSNUM = " << _cc_call_data.outbound_ans_num << "\n";
    out << "OUTCALLTIME = " << _cc_call_data.outbound_call_time << "\n";
    out << "INCALLTIME = " << _cc_call_data.inbound_call_time << "\n";
    out << "INALERTTIME = " << _cc_call_data.inbound_call_time << "\n";
    out << "INANWNUM = " << _cc_call_data.inbound_ans_num << "\n";
    out << "INCALLNUM = " << _cc_call_data.inbound_call_num << "\n";


    // load skill call data
    for (iter iter = _skill_call_info.begin(); iter != _skill_call_info.end(); ++iter) {
        out << "[@SkillCallData]" << "\n";
        out << "SkillName = " << iter->first.c_str() << "\n";
        out << "InCallTime = " << iter->second->inbound_call_time << "\n";
        out << "InAlertTime = " << iter->second->inbound_alerting_time << "\n";
        out << "InCallNum = " << iter->second->inbound_call_num << "\n";
        out << "InAnsNum = " << iter->second->inbound_answer_num << "\n";
    }
    out.close();    
    acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
        "commit data to cached file");
    return 0;
}

void AcdCallDataCollection::commit_data()
{
    string cur_date = bgcc::TimeUtil::get_date();
    uint64_t cur_second = bgcc::TimeUtil::get_timestamp_s();
    const static uint64_t time_interval = 30;
    if (cur_date != _cached_date) {
        reset();
        put_data_to_file();
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "not this day, put data to file");
    }
    if (cur_second - _cached_second > time_interval) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__,
            "commit data to file");
        put_data_to_file();
        _cached_second = cur_second;
    }
    _cached_date = cur_date;
}

void AcdCallDataCollection::update_data(calldata_ptr& callinfo)
{
    SingleRWLocker s(&_lock, true);
    if (callinfo.get() == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "callinfo is NULL");
        return;
    }
    uint32_t calltime = callinfo->m_callend - callinfo->m_callbegin;
    if (callinfo->m_callend < callinfo->m_callbegin) {
        calltime = 0;
    }
    uint32_t alerting = callinfo->m_ackend - callinfo->m_ackbegin;
    if (callinfo->m_ackend < callinfo->m_ackbegin) {
        alerting = 0;
    }
    //update cc info
    if (callinfo->m_callDirect.get_value() == CallDirectT::OUTBOUND) {
        ++_cc_call_data.outbound_call_num;
        if (calltime > 0) {
            ++_cc_call_data.outbound_ans_num;
            _cc_call_data.outbound_call_time += calltime;
        }
    }
    else if (callinfo->m_callDirect.get_value() == CallDirectT::INBOUND && calltime > 0) {
        ++_cc_call_data.inbound_call_num;
        _cc_call_data.inbound_alerting_time += alerting;
        if (calltime > 0) {
            ++_cc_call_data.inbound_ans_num;
            _cc_call_data.inbound_call_time += calltime;
        }
    }
    //update skill info
    if (callinfo->m_callDirect.get_value() == CallDirectT::INBOUND && !callinfo->m_skill.empty()) {
        iter iter = _skill_call_info.find(callinfo->m_skill);
        if (iter == _skill_call_info.end()) {
            acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
                "new skill (%s)", callinfo->m_skill.c_str());
            SkillCallData * p = new (std::nothrow) SkillCallData;
            if (p ==  NULL) {
                acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                    "new skill call data info failed, skill(%s)", callinfo->m_skill.c_str());
                return;
            }
            p->inbound_alerting_time = 0;
            p->inbound_answer_num = 0;
            p->inbound_call_time = 0;
            p->inbound_call_num = 0;
            ++p->inbound_call_num;
            p->inbound_alerting_time += alerting;
            if (calltime > 0) {
                ++p->inbound_answer_num;
                p->inbound_call_time += calltime;
            }
            _skill_call_info.insert(std::make_pair(callinfo->m_skill, p));
        }
        else {
            iter->second->inbound_alerting_time += alerting;
            ++iter->second->inbound_call_num;
            if (calltime > 0) {
                ++iter->second->inbound_answer_num;
                iter->second->inbound_call_time += calltime;
            }
            acd_tool::m_logger.WriteLog(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__,
                "update skill info skill(%s), calltime(%d), alerttime(%d)", 
                    callinfo->m_skill.c_str(), calltime, alerting);
        }
    }
    
}

void AcdCallDataCollection::Method()
{
    acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
        "acd data collection thread begin");
    const static int32_t timeout = 30;
    while (this->mIsLoop) {
        calldata_ptr callinfo;
        bool ret = _calldata_queue.UnBlockPop(callinfo, timeout);
        commit_data();
        if (ret) {
            update_data(callinfo);
        }
    }
}

int32_t AcdCallDataCollection::get_call_data_by_plat(string& data)
{
    SingleRWLocker s(&_lock, true);
    string fail_str = "{\"result\":\"-1\",\"data\":\"memory error\"}";
    json_object *root = json_object_new_object();
    if (root == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new root json object failed");
        data = fail_str;
        return -1;
    }
    json_object *calldata = json_object_new_object();
    if (calldata == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new calldata json object failed");
        data = fail_str;
        json_object_put(root);
        return -1;
    }
    json_object_object_add(calldata, "OutCallNum", json_object_new_int(_cc_call_data.outbound_call_num));
    json_object_object_add(calldata, "OutAnsNum", json_object_new_int(_cc_call_data.outbound_ans_num));
    json_object_object_add(calldata, "OutCallTime", json_object_new_int(_cc_call_data.outbound_call_time));
    json_object_object_add(calldata, "InAnsNum", json_object_new_int(_cc_call_data.inbound_ans_num));
    json_object_object_add(calldata, "InCallNum", json_object_new_int(_cc_call_data.inbound_ans_num));
    json_object_object_add(calldata, "InCallTime", json_object_new_int(_cc_call_data.inbound_call_time));
    json_object_object_add(calldata, "InAlertTime", json_object_new_int(_cc_call_data.inbound_alerting_time));
    json_object_object_add(root, "result", json_object_new_string("0"));
    json_object_object_add(root, "data", calldata);
    data = json_object_to_json_string(root);
    //acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
    //    "get cc call data success, data(%s)", data.c_str());
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "get cc call data success");
    json_object_put(root);
    return 0;
}

int32_t AcdCallDataCollection::get_call_data_by_skill(string& data, const string& skill)
{
    SingleRWLocker s(&_lock, true);
    string fail_str = "{\"result\":\"-1\",\"data\":\"memory error\"}";
    json_object *root = json_object_new_object();
    if (root == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new root json object failed");
        data = fail_str;
        return -1;
    }
    json_object *calldata = json_object_new_object();
    if (calldata == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new calldata json object failed");
        data = fail_str;
        json_object_put(root);
        return -1;
    }
    iter iter = _skill_call_info.find(skill);
    if (iter == _skill_call_info.end()) {
        json_object_object_add(root, "result", json_object_new_string("-1"));
        json_object_object_add(root, "data", json_object_new_string("not found skill"));
        data = json_object_to_json_string(root);
        acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
            "get cc call data by skill failed, skill(%s)", skill.c_str());
        json_object_put(calldata);
        json_object_put(calldata);
        return -1;
    }
    json_object_object_add(calldata, "InCallTime", json_object_new_int(iter->second->inbound_call_time));
    json_object_object_add(calldata, "InAlertTime", json_object_new_int(iter->second->inbound_alerting_time));
    json_object_object_add(calldata, "InAnsNum", json_object_new_int(iter->second->inbound_answer_num));
    json_object_object_add(calldata, "InCallNum", json_object_new_int(iter->second->inbound_call_num));
    json_object_object_add(root, "result", json_object_new_string("0"));
    json_object_object_add(root, "data", calldata);
    data = json_object_to_json_string(root);
    //acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
    //    "get call data by skill success, skill(%s), data(%s)", skill.c_str(), data.c_str());
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "get call data by skill success skill(%s)", skill.c_str());
    json_object_put(root);
    return 0;
}

int32_t AcdCallDataCollection::get_call_data_by_all(string& data)
{
    SingleRWLocker s(&_lock, true);
    string fail_str = "{\"result\":\"-1\",\"data\":\"memory error\"}";
    json_object *root = json_object_new_object();
    if (root == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new root json object failed");
        data = fail_str;
        return -1;
    }
    json_object *calldata = json_object_new_object();
    if (calldata == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new calldata json object failed");
        data = fail_str;
        json_object_put(root);
        return -1;
    }
    // add plat call info
    json_object *platdata = json_object_new_object();
    if (calldata == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new platdata json object failed");
        data = fail_str;
        json_object_put(root);
        json_object_put(calldata);
        return -1;
    }
    json_object_object_add(platdata, "OutCallNum", json_object_new_int(_cc_call_data.outbound_call_num));
    json_object_object_add(platdata, "OutAnsNum", json_object_new_int(_cc_call_data.outbound_ans_num));
    json_object_object_add(platdata, "OutCallTime", json_object_new_int(_cc_call_data.outbound_call_time));
    json_object_object_add(platdata, "InAnsNum", json_object_new_int(_cc_call_data.inbound_ans_num));
    json_object_object_add(platdata, "InCallNum", json_object_new_int(_cc_call_data.inbound_call_num));
    json_object_object_add(platdata, "InCallTime", json_object_new_int(_cc_call_data.inbound_call_time));
    json_object_object_add(platdata, "InAlertTime", json_object_new_int(_cc_call_data.inbound_alerting_time));
    json_object_object_add(calldata, "platinfo", platdata);
    json_object *skilldata = json_object_new_array();
    if (skilldata == NULL) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "new skilldata json object failed");
        data = fail_str;
        json_object_put(root);
        json_object_put(calldata);
        return -1;
    }
    for (iter iter = _skill_call_info.begin(); iter != _skill_call_info.end(); ++iter) {
        if (iter->second != NULL) {
            json_object *skillinfo = json_object_new_object();
            if (skillinfo == NULL) {
                acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                    "new skilinfo json object failed, skill(%s)", iter->first.c_str());
                continue;
            }
            json_object_object_add(skillinfo, "skillname", json_object_new_string(iter->first.c_str()));
            json_object_object_add(skillinfo, "InCallTime", json_object_new_int(iter->second->inbound_call_time));
            json_object_object_add(skillinfo, "InAlertTime", json_object_new_int(iter->second->inbound_alerting_time));
            json_object_object_add(skillinfo, "InAnsNum", json_object_new_int(iter->second->inbound_answer_num));
            json_object_object_add(skillinfo, "InCallNum", json_object_new_int(iter->second->inbound_call_num));
            json_object_array_add(skilldata, skillinfo);
        }
    }
    json_object_object_add(calldata, "skillinfo", skilldata);
    json_object_object_add(root, "result", json_object_new_string("0"));
    json_object_object_add(root, "data", calldata);
    data = json_object_to_json_string(root);
    //acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
    //    "get call data by ALL success, data(%s)", data.c_str());
    acd_tool::m_logger.WriteLog(LOG_LEVEL_NOTICE, __FILE__, __LINE__, __FUNCTION__,
        "get call data by ALL success");
    json_object_put(root);
    return 0;    
}

acd::AcdResultT AcdCallDataCollection::get_call_data(int64_t handle, const std::string& agentId,
                                                            int32_t type, const string& input, string& result)
{
    acd::AcdResultT ret = acd::AcdResultT::ArSuccess;
    if (agentId.empty()) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, "empty agentId");
        ret = acd::AcdResultT::ArAgentIdIsEmpty;
        return ret;
    }

    if (!acd_tool::p_m_acd_backup->GetIsMaster()) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
                                    "agentId:%s handle:%"SHOW_LONG"d not master", agentId.c_str(), handle);
        ret = acd::AcdResultT::ArNotMaster;
        return ret;
    }
    if (type == GETBYPLAT) {
        return get_call_data_by_plat(result);
    }
    else if (type == GETBYSKILL) {
        return get_call_data_by_skill(result, input);
    }
    else if (type == GETALL) {
        return get_call_data_by_all(result);
    }
    return -1;
}

void AcdCallDataCollection::reset()
{
    SingleRWLocker s(&_lock, true);
    for (iter iter = _skill_call_info.begin()
            ; iter != _skill_call_info.end()
            ; ++iter) {
         if (iter->second != NULL) {
             delete iter->second;
             iter->second = NULL;
         }
         _skill_call_info.erase(iter->first);
    }

    _cc_call_data.inbound_ans_num = 0;
    _cc_call_data.inbound_call_num = 0;
    _cc_call_data.inbound_call_time = 0;
    _cc_call_data.outbound_ans_num = 0;
    _cc_call_data.outbound_call_num = 0;
    _cc_call_data.outbound_call_time = 0;
}

int32_t AcdCallDataCollection::push_call_data(calldata_ptr& calldata)
{
    if (calldata.get() == NULL ) {
        acd_tool::m_logger.WriteLog(LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__,
            "calldata is NULL");
        return -1;
    }
    _calldata_queue.BlockPush(calldata);
    acd_tool::m_logger.WriteLog(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__,
        "calldata has in queue, agentId(%s),  sessionid(%lu), callid(%s), skill(%s)",
        calldata->m_agentId.c_str(), calldata->m_sessionId, 
        calldata->m_callId.c_str(), calldata->m_skill.c_str());
    return 0;
}
};